1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-11-22 08:25:38 +00:00

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 // This is the format of time from FreeBSD
spec := ` spec := `
/set type=file time=5.000000000 /set type=file time=5.000000000
. type=dir . type=dir
file time=5.000000000 file time=5.000000000
.. ..
` `
@ -139,3 +139,69 @@ func TestTimeComparison(t *testing.T) {
t.Fatal(res.Failures) 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() { for s.Scan() {
str := s.Text() str := s.Text()
trimmedStr := strings.TrimLeftFunc(str, func(c rune) bool {
return c == ' ' || c == '\t'
})
e := Entry{Pos: i} e := Entry{Pos: i}
switch { switch {
case strings.HasPrefix(str, "#"): case strings.HasPrefix(trimmedStr, "#"):
e.Raw = str e.Raw = str
if strings.HasPrefix(str, "#mtree") { if strings.HasPrefix(trimmedStr, "#mtree") {
e.Type = SignatureType e.Type = SignatureType
} else { } else {
e.Type = CommentType e.Type = CommentType