mirror of
https://github.com/vbatts/go-mtree.git
synced 2024-11-22 08:25:38 +00:00
parse: ignore leading whitespace for comments
Comments were only ignored if the string parsed started with "#". This commit trims leading whitespace and tab characters to make sure all lines with "#" being the first non-whitespace character are ignored. Signed-off-by: Stephen Chung <schung@redhat.com>
This commit is contained in:
parent
a29236e678
commit
f3fc3d06d6
2 changed files with 72 additions and 3 deletions
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
7
parse.go
7
parse.go
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue