Merge pull request #24 from stephen679/leading_whitespace_comments

parse: ignore leading whitespace for comments
This commit is contained in:
Vincent Batts 2016-07-19 09:00:56 +09:00 committed by GitHub
commit 738b0fed45
2 changed files with 72 additions and 3 deletions

View File

@ -108,7 +108,7 @@ func TestTimeComparison(t *testing.T) {
// This is the format of time from FreeBSD
spec := `
/set type=file time=5.000000000
. type=dir
. type=dir
file time=5.000000000
..
`
@ -139,3 +139,69 @@ func TestTimeComparison(t *testing.T) {
t.Fatal(res.Failures)
}
}
func TestIgnoreComments(t *testing.T) {
dir, err := ioutil.TempDir("", "test-comments.")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
// This is the format of time from FreeBSD
spec := `
/set type=file time=5.000000000
. type=dir
file1 time=5.000000000
..
`
fh, err := os.Create(filepath.Join(dir, "file1"))
if err != nil {
t.Fatal(err)
}
// This is what mode we're checking for. Round integer of epoch seconds
epoch := time.Unix(5, 0)
if err := os.Chtimes(fh.Name(), epoch, epoch); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(dir, epoch, epoch); err != nil {
t.Fatal(err)
}
if err := fh.Close(); err != nil {
t.Error(err)
}
dh, err := ParseSpec(bytes.NewBufferString(spec))
if err != nil {
t.Fatal(err)
}
res, err := Check(dir, dh, nil)
if err != nil {
t.Error(err)
}
if len(res.Failures) > 0 {
t.Fatal(res.Failures)
}
// now change the spec to a comment that looks like an actual Entry but has
// whitespace in front of it
spec = `
/set type=file time=5.000000000
. type=dir
file1 time=5.000000000
#file2 time=5.000000000
..
`
dh, err = ParseSpec(bytes.NewBufferString(spec))
res, err = Check(dir, dh, nil)
if err != nil {
t.Error(err)
}
if len(res.Failures) > 0 {
t.Fatal(res.Failures)
}
}

View File

@ -16,11 +16,14 @@ func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) {
}
for s.Scan() {
str := s.Text()
trimmedStr := strings.TrimLeftFunc(str, func(c rune) bool {
return c == ' ' || c == '\t'
})
e := Entry{Pos: i}
switch {
case strings.HasPrefix(str, "#"):
case strings.HasPrefix(trimmedStr, "#"):
e.Raw = str
if strings.HasPrefix(str, "#mtree") {
if strings.HasPrefix(trimmedStr, "#mtree") {
e.Type = SignatureType
} else {
e.Type = CommentType