Commit cc3f8bdd authored by Steven Allen's avatar Steven Allen

test: test file size when ignoring

Also, fix the file ignore tests.
parent 241cb611
...@@ -71,23 +71,32 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) { ...@@ -71,23 +71,32 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
t.Fatal(err) t.Fatal(err)
} }
} }
expectedHiddenPaths := make([]string, 0, 4) expectedPaths := make([]string, 0, 4)
expectedRegularPaths := make([]string, 0, 6) expectedSize := int64(0)
testInputs:
for p := range testInputs { for p := range testInputs {
path := filepath.Join(tmppath, p) components := strings.Split(p, "/")
stat, err := os.Stat(path) var stat os.FileInfo
if err != nil { for i := range components {
t.Fatal(err) stat, err = os.Stat(filepath.Join(
} append([]string{tmppath}, components[:i+1]...)...,
if !fileFilter.ShouldExclude(stat) { ))
if isFullPathHidden(path) { if err != nil {
expectedHiddenPaths = append(expectedHiddenPaths, p) t.Fatal(err)
} else {
expectedRegularPaths = append(expectedRegularPaths, p)
} }
if fileFilter.ShouldExclude(stat) {
continue testInputs
}
}
expectedPaths = append(expectedPaths, p)
if stat.Mode().IsRegular() {
expectedSize += stat.Size()
} }
} }
sort.Strings(expectedPaths)
stat, err := os.Stat(tmppath) stat, err := os.Stat(tmppath)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -102,9 +111,14 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) { ...@@ -102,9 +111,14 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
} }
defer sf.Close() defer sf.Close()
if size, err := sf.Size(); err != nil {
t.Fatalf("failed to determine size: %s", err)
} else if size != expectedSize {
t.Fatalf("expected size %d, got size %d", expectedSize, size)
}
rootFound := false rootFound := false
actualRegularPaths := make([]string, 0, len(expectedRegularPaths)) actualPaths := make([]string, 0, len(expectedPaths))
actualHiddenPaths := make([]string, 0, len(expectedHiddenPaths))
err = Walk(sf, func(path string, nd Node) error { err = Walk(sf, func(path string, nd Node) error {
defer nd.Close() defer nd.Close()
...@@ -119,16 +133,15 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) { ...@@ -119,16 +133,15 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
rootFound = true rootFound = true
return nil return nil
} }
if isFullPathHidden(path) { actualPaths = append(actualPaths, path)
actualHiddenPaths = append(actualHiddenPaths, path)
} else {
actualRegularPaths = append(actualRegularPaths, path)
}
if !hidden && isFullPathHidden(path) { if !hidden && isFullPathHidden(path) {
return fmt.Errorf("found a hidden file") return fmt.Errorf("found a hidden file")
} }
if fileFilter.Rules.MatchesPath(path) { components := filepath.SplitList(path)
return fmt.Errorf("found a file that should be excluded") for i := range components {
if fileFilter.Rules.MatchesPath(filepath.Join(components[:i+1]...)) {
return fmt.Errorf("found a file that should be excluded")
}
} }
data, ok := testInputs[path] data, ok := testInputs[path]
...@@ -155,19 +168,27 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) { ...@@ -155,19 +168,27 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
} }
return nil return nil
}) })
if err != nil {
t.Fatal(err)
}
if !rootFound { if !rootFound {
t.Fatal("didn't find the root") t.Fatal("didn't find the root")
} }
for _, regular := range expectedRegularPaths {
if idx := sort.SearchStrings(actualRegularPaths, regular); idx < 0 { if len(expectedPaths) != len(actualPaths) {
t.Errorf("missed regular path %q", regular) t.Fatalf("expected %d paths, found %d",
} len(expectedPaths),
len(actualPaths),
)
} }
if hidden && len(actualHiddenPaths) != len(expectedHiddenPaths) {
for _, missing := range expectedHiddenPaths { for i := range expectedPaths {
if idx := sort.SearchStrings(actualHiddenPaths, missing); idx < 0 { if expectedPaths[i] != actualPaths[i] {
t.Errorf("missed hidden path %q", missing) t.Errorf(
} "expected path %q does not match actual %q",
expectedPaths[i],
actualPaths[i],
)
} }
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment