Skip to content

Commit 5affa3a

Browse files
squaremodarkowlzz
authored andcommitted
Correct directory diffing test and algorithm
Two steps: 1. TestDiffDirectories did not check if the expected only return value was correct; the intention was there to do so (judging by the comment "change in order"), but my eye for detail failed me. 2. Reversing the directory comparison in the test revealed bugs in the comparison code -- in general, it should skip any directory that is not a directory in the comparator. To make this easier, the code now keeps track of the expected files it saw. That means the check for whether an actual file has an expected counterpart only has two cases, yes or no (rather that trying to account for whether it's a directory and so on). If a directory was skipped while scanning the expected files, it won't be in the actual files anyway. Signed-off-by: Michael Bridgen <[email protected]>
1 parent ffbc825 commit 5affa3a

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

pkg/test/files.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,21 @@ func (d dirfile) FailedExpectation(g *WithT) {
8282
// `foo.yaml~`). It panics if it encounters any error apart from a
8383
// file not found.
8484
func DiffDirectories(actual, expected string) (actualonly []string, expectedonly []string, different []Diff) {
85+
seen := make(map[string]struct{})
86+
8587
filepath.Walk(expected, func(expectedPath string, expectedInfo os.FileInfo, err error) error {
8688
if err != nil {
8789
panic(err)
8890
}
91+
92+
relPath := expectedPath[len(expected):]
93+
seen[relPath] = struct{}{}
94+
8995
// ignore emacs backups
9096
if strings.HasSuffix(expectedPath, "~") {
9197
return nil
9298
}
93-
relPath := expectedPath[len(expected):]
94-
actualPath := filepath.Join(actual, relPath)
99+
95100
// ignore dotfiles
96101
if strings.HasPrefix(filepath.Base(expectedPath), ".") {
97102
if expectedInfo.IsDir() {
@@ -100,24 +105,30 @@ func DiffDirectories(actual, expected string) (actualonly []string, expectedonly
100105
return nil
101106
}
102107

108+
actualPath := filepath.Join(actual, relPath)
103109
actualInfo, err := os.Stat(actualPath)
104110
switch {
105111
case err == nil:
106112
break
107113
case os.IsNotExist(err):
108114
expectedonly = append(expectedonly, relPath)
115+
if expectedInfo.IsDir() {
116+
return filepath.SkipDir
117+
}
109118
return nil
110119
default:
111120
panic(err)
112121
}
113122

114123
// file exists in both places
115-
116124
switch {
117125
case actualInfo.IsDir() && expectedInfo.IsDir():
118126
return nil // i.e., keep recursing
119127
case actualInfo.IsDir() || expectedInfo.IsDir():
120128
different = append(different, dirfile{path: relPath, abspath: actualPath, expectedRegularFile: actualInfo.IsDir()})
129+
if expectedInfo.IsDir() {
130+
return filepath.SkipDir
131+
}
121132
return nil
122133
}
123134

@@ -151,18 +162,12 @@ func DiffDirectories(actual, expected string) (actualonly []string, expectedonly
151162
if actualInfo.IsDir() && strings.HasPrefix(filepath.Base(actualPath), ".") {
152163
return filepath.SkipDir
153164
}
154-
// since I've already compared any file that exists in
155-
// expected or both, I'm only concerned with files that appear
156-
// in actual but not in expected.
157-
expectedPath := filepath.Join(expected, relPath)
158-
_, err = os.Stat(expectedPath)
159-
switch {
160-
case err == nil:
161-
break
162-
case os.IsNotExist(err):
165+
166+
if _, ok := seen[relPath]; !ok {
163167
actualonly = append(actualonly, relPath)
164-
default:
165-
panic(err)
168+
if actualInfo.IsDir() {
169+
return filepath.SkipDir
170+
}
166171
}
167172
return nil
168173
})

pkg/test/files_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ func TestExpectMatchingDirectories(t *testing.T) {
5151
func TestDiffDirectories(t *testing.T) {
5252
g := NewWithT(t)
5353

54-
// Finds files in expected from a/ but not in actual b/.
55-
aonly, _, _ := DiffDirectories("testdata/diff/a", "testdata/diff/b")
56-
g.Expect(aonly).To(Equal([]string{"/only", "/only/here.yaml", "/onlyhere.yaml"}))
57-
5854
// Finds files in actual a/ that weren't expected from b/.
59-
bonly, _, _ := DiffDirectories("testdata/diff/a", "testdata/diff/b") // change in order
60-
g.Expect(bonly).To(Equal([]string{"/only", "/only/here.yaml", "/onlyhere.yaml"}))
55+
actualonly, _, _ := DiffDirectories("testdata/diff/a", "testdata/diff/b")
56+
g.Expect(actualonly).To(Equal([]string{"/only", "/onlyhere.yaml"}))
57+
58+
// Finds files in expected from a/ but not in actual b/.
59+
_, expectedonly, _ := DiffDirectories("testdata/diff/b", "testdata/diff/a") // NB change in order
60+
g.Expect(expectedonly).To(Equal([]string{"/only", "/onlyhere.yaml"}))
6161

6262
// Finds files that are different in a and b.
6363
_, _, diffs := DiffDirectories("testdata/diff/a", "testdata/diff/b")

0 commit comments

Comments
 (0)