From 473997e41882cb62918fc6634e444f7d8ef1e9e5 Mon Sep 17 00:00:00 2001 From: Martin Bertschler Date: Tue, 3 Oct 2017 23:17:35 +0200 Subject: [PATCH 1/2] add test that shows a bug with FileMode not being set for MemMapFs directories, clean up tests --- .travis.yml | 3 ++- appveyor.yml | 2 +- composite_test.go | 2 +- mem/file_test.go | 10 +++++----- memmap_test.go | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 40d0789..618159a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,5 +16,6 @@ matrix: fast_finish: true script: - - go test -race -v ./... - go build + - go test -race -v ./... + diff --git a/appveyor.yml b/appveyor.yml index 98ac35c..a633ad5 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -12,4 +12,4 @@ build_script: go build github.com/spf13/afero test_script: -- cmd: go test --race -v github.com/spf13/afero +- cmd: go test -race -v github.com/spf13/afero/... diff --git a/composite_test.go b/composite_test.go index c2e3c9c..8e44611 100644 --- a/composite_test.go +++ b/composite_test.go @@ -368,7 +368,7 @@ func TestUnionCacheExpire(t *testing.T) { } } -func TestCacheOnReadFs_Open_NotInLayer(t *testing.T) { +func TestCacheOnReadFsNotInLayer(t *testing.T) { base := NewMemMapFs() layer := NewMemMapFs() fs := NewCacheOnReadFs(base, layer, 0) diff --git a/mem/file_test.go b/mem/file_test.go index 1aeea23..5769067 100644 --- a/mem/file_test.go +++ b/mem/file_test.go @@ -5,7 +5,7 @@ import ( "time" ) -func Test_FileData_Name_withConcurrence(t *testing.T) { +func TestFileDataNameRace(t *testing.T) { t.Parallel() const someName = "someName" const someOtherName = "someOtherName" @@ -31,7 +31,7 @@ func Test_FileData_Name_withConcurrence(t *testing.T) { } } -func Test_FileData_ModTime_withConcurrence(t *testing.T) { +func TestFileDataModTimeRace(t *testing.T) { t.Parallel() someTime := time.Now() someOtherTime := someTime.Add(1 * time.Minute) @@ -62,7 +62,7 @@ func Test_FileData_ModTime_withConcurrence(t *testing.T) { } } -func Test_FileData_Mode_withConcurrence(t *testing.T) { +func TestFileDataModeRace(t *testing.T) { t.Parallel() const someMode = 0777 const someOtherMode = 0660 @@ -93,7 +93,7 @@ func Test_FileData_Mode_withConcurrence(t *testing.T) { } } -func Test_FileData_IsDir_withConcurrence(t *testing.T) { +func TestFileDataIsDirRace(t *testing.T) { t.Parallel() d := FileData{ @@ -118,7 +118,7 @@ func Test_FileData_IsDir_withConcurrence(t *testing.T) { t.Logf("Value is %v", s.IsDir()) } -func Test_FileData_Size_withConcurrence(t *testing.T) { +func TestFileDataSizeRace(t *testing.T) { t.Parallel() const someData = "Hello" diff --git a/memmap_test.go b/memmap_test.go index d28e912..23a8881 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -384,3 +384,35 @@ loop: } } } + +func TestMemFsDirMode(t *testing.T) { + fs := NewMemMapFs() + err := fs.Mkdir("/testDir1", 0644) + if err != nil { + t.Error(err) + } + err = fs.MkdirAll("/sub/testDir2", 0644) + if err != nil { + t.Error(err) + } + info, err := fs.Stat("/testDir1") + if err != nil { + t.Error(err) + } + if !info.IsDir() { + t.Error("should be a directory") + } + if info.Mode()&os.ModeDir == 0 { + t.Error("FileMode is not directory") + } + info, err = fs.Stat("/sub/testDir2") + if err != nil { + t.Error(err) + } + if !info.IsDir() { + t.Error("should be a directory") + } + if info.Mode()&os.ModeDir == 0 { + t.Error("FileMode is not directory") + } +} From 84cf6dc70734caf7df4d35b2a9ae34fbcd2a2e6d Mon Sep 17 00:00:00 2001 From: Martin Bertschler Date: Tue, 3 Oct 2017 23:46:18 +0200 Subject: [PATCH 2/2] set ModeDir on MkDir calls on MemMapFs closes #118 --- memmap.go | 5 ++--- memmap_test.go | 19 +++++++++++-------- memradix.go | 14 -------------- 3 files changed, 13 insertions(+), 25 deletions(-) delete mode 100644 memradix.go diff --git a/memmap.go b/memmap.go index 14cd438..09498e7 100644 --- a/memmap.go +++ b/memmap.go @@ -141,7 +141,7 @@ func (m *MemMapFs) Mkdir(name string, perm os.FileMode) error { m.registerWithParent(item) m.mu.Unlock() - m.Chmod(name, perm) + m.Chmod(name, perm|os.ModeDir) return nil } @@ -151,9 +151,8 @@ func (m *MemMapFs) MkdirAll(path string, perm os.FileMode) error { if err != nil { if err.(*os.PathError).Err == ErrFileExists { return nil - } else { - return err } + return err } return nil } diff --git a/memmap_test.go b/memmap_test.go index 23a8881..09d8680 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -110,6 +110,8 @@ func TestPermSet(t *testing.T) { const dirPathAll = "/my/path/to/dir" const fileMode = os.FileMode(0765) + // directories will also have the directory bit set + const dirMode = fileMode | os.ModeDir fs := NewMemMapFs() @@ -132,7 +134,7 @@ func TestPermSet(t *testing.T) { } // Test Mkdir - err = fs.Mkdir(dirPath, fileMode) + err = fs.Mkdir(dirPath, dirMode) if err != nil { t.Errorf("MkDir Create failed: %s", err) return @@ -142,13 +144,14 @@ func TestPermSet(t *testing.T) { t.Errorf("Stat failed: %s", err) return } - if s.Mode().String() != fileMode.String() { - t.Errorf("Permissions Incorrect: %s != %s", s.Mode().String(), fileMode.String()) + // sets File + if s.Mode().String() != dirMode.String() { + t.Errorf("Permissions Incorrect: %s != %s", s.Mode().String(), dirMode.String()) return } // Test MkdirAll - err = fs.MkdirAll(dirPathAll, fileMode) + err = fs.MkdirAll(dirPathAll, dirMode) if err != nil { t.Errorf("MkDir Create failed: %s", err) return @@ -158,8 +161,8 @@ func TestPermSet(t *testing.T) { t.Errorf("Stat failed: %s", err) return } - if s.Mode().String() != fileMode.String() { - t.Errorf("Permissions Incorrect: %s != %s", s.Mode().String(), fileMode.String()) + if s.Mode().String() != dirMode.String() { + t.Errorf("Permissions Incorrect: %s != %s", s.Mode().String(), dirMode.String()) return } } @@ -402,7 +405,7 @@ func TestMemFsDirMode(t *testing.T) { if !info.IsDir() { t.Error("should be a directory") } - if info.Mode()&os.ModeDir == 0 { + if !info.Mode().IsDir() { t.Error("FileMode is not directory") } info, err = fs.Stat("/sub/testDir2") @@ -412,7 +415,7 @@ func TestMemFsDirMode(t *testing.T) { if !info.IsDir() { t.Error("should be a directory") } - if info.Mode()&os.ModeDir == 0 { + if !info.Mode().IsDir() { t.Error("FileMode is not directory") } } diff --git a/memradix.go b/memradix.go deleted file mode 100644 index 87527f3..0000000 --- a/memradix.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright © 2014 Steve Francia . -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package afero