From 90b5a9bd18a72dbf3e27160fc47acfaac6c08389 Mon Sep 17 00:00:00 2001 From: Martin Bertschler Date: Wed, 9 Dec 2015 23:34:42 +0100 Subject: [PATCH] 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. --- afero_test.go | 19 +++++++++++++++++-- path_test.go | 27 +++++++++++---------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/afero_test.go b/afero_test.go index 4c627a1..acca837 100644 --- a/afero_test.go +++ b/afero_test.go @@ -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) diff --git a/path_test.go b/path_test.go index 6a584a4..b3c0417 100644 --- a/path_test.go +++ b/path_test.go @@ -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() }