Fix TestWalk to use the new setup functions and make it Windows ready

I had to wrap the setupTestFiles() function for this test, one for returning the root of the test dir and the other one to reuse the path that was returned from the first function. If this is not done setupTestDir writes into two different folders that are returned from os.TempDir() and the output of the two walk functions can't be compared.
This commit is contained in:
Martin Bertschler 2015-12-09 23:34:42 +01:00
parent 943d877c43
commit 90b5a9bd18
2 changed files with 28 additions and 18 deletions

View File

@ -345,8 +345,23 @@ func TestWriteAt(t *testing.T) {
}
func setupTestDir(t *testing.T, fs Fs) string {
x := testDir(fs)
testSubDir := filepath.Join(x, "more", "subdirectories", "for", "testing", "we")
path := testDir(fs)
return setupTestFiles(t, fs, path)
}
func setupTestDirRoot(t *testing.T, fs Fs) string {
path := testDir(fs)
setupTestFiles(t, fs, path)
return path
}
func setupTestDirReusePath(t *testing.T, fs Fs, path string) string {
testRegistry[fs] = append(testRegistry[fs], path)
return setupTestFiles(t, fs, path)
}
func setupTestFiles(t *testing.T, fs Fs, path string) string {
testSubDir := filepath.Join(path, "more", "subdirectories", "for", "testing", "we")
err := fs.MkdirAll(testSubDir, 0700)
if err != nil && !os.IsExist(err) {
t.Fatal(err)

View File

@ -21,26 +21,22 @@ import (
)
func TestWalk(t *testing.T) {
var Fss = []Fs{&MemMapFs{}, &OsFs{}}
var testDir = "/tmp/afero"
var testName = "test.txt"
for _, fs := range Fss {
path := testDir + "/" + testName
if err := fs.MkdirAll(testDir, 0777); err != nil {
t.Fatal(fs.Name(), "unable to create dir", err)
defer removeAllTestFiles()
var testDir string
for i, fs := range Fss {
if i == 0 {
testDir = setupTestDirRoot(t, fs)
} else {
setupTestDirReusePath(t, fs, testDir)
}
f, err := fs.Create(path)
if err != nil {
t.Fatal(fs.Name(), "create failed:", err)
}
defer f.Close()
f.WriteString("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
}
outputs := make([]string, len(Fss))
for i, fs := range Fss {
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
t.Error("walkFn err:", err)
}
var size int64
if !info.IsDir() {
size = info.Size()
@ -66,8 +62,7 @@ func TestWalk(t *testing.T) {
if fail {
t.Log("Walk outputs not equal!")
for i, o := range outputs {
t.Log(Fss[i].Name())
t.Log(o)
t.Log(Fss[i].Name() + "\n" + o)
}
t.Fail()
}