forked from mirror/afero
Return os.ErrExist from CopyOnWriteFs.Mkdir/MkdirAll
Before this commit, `CopyOnWriteFs` would return `syscall.EEXIST` in `Mkdir` and `MkdirAll` when a directory already exists. The main problem with this is that `os.IsExist` returns `false` for that error on Windows. These methods now return `os.ErrExist`, which is in line with how the other file systems behave. Fixes #189
This commit is contained in:
parent
d40851caa0
commit
5e9f8ecaa4
|
@ -267,7 +267,7 @@ func (u *CopyOnWriteFs) Mkdir(name string, perm os.FileMode) error {
|
|||
return u.layer.MkdirAll(name, perm)
|
||||
}
|
||||
if dir {
|
||||
return syscall.EEXIST
|
||||
return ErrFileExists
|
||||
}
|
||||
return u.layer.MkdirAll(name, perm)
|
||||
}
|
||||
|
@ -282,7 +282,7 @@ func (u *CopyOnWriteFs) MkdirAll(name string, perm os.FileMode) error {
|
|||
return u.layer.MkdirAll(name, perm)
|
||||
}
|
||||
if dir {
|
||||
return syscall.EEXIST
|
||||
return ErrFileExists
|
||||
}
|
||||
return u.layer.MkdirAll(name, perm)
|
||||
}
|
||||
|
|
|
@ -1,26 +1,49 @@
|
|||
package afero
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCopyOnWrite(t *testing.T) {
|
||||
var fs Fs
|
||||
var err error
|
||||
base := NewOsFs()
|
||||
roBase := NewReadOnlyFs(base)
|
||||
ufs := NewCopyOnWriteFs(roBase, NewMemMapFs())
|
||||
|
||||
fs = ufs
|
||||
err = fs.MkdirAll("nonexistent/directory/", 0744)
|
||||
osFs := NewOsFs()
|
||||
writeDir, err := TempDir(osFs, "", "copy-on-write-test")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
t.Fatal("error creating tempDir", err)
|
||||
}
|
||||
_, err = fs.Create("nonexistent/directory/newfile")
|
||||
defer osFs.RemoveAll(writeDir)
|
||||
|
||||
compositeFs := NewCopyOnWriteFs(NewReadOnlyFs(NewOsFs()), osFs)
|
||||
|
||||
var dir = filepath.Join(writeDir, "some/path")
|
||||
|
||||
err = compositeFs.MkdirAll(dir, 0744)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = compositeFs.Create(filepath.Join(dir, "newfile"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// https://github.com/spf13/afero/issues/189
|
||||
// We want the composite file system to behave like the OS file system
|
||||
// on Mkdir and MkdirAll
|
||||
for _, fs := range []Fs{osFs, compositeFs} {
|
||||
err = fs.Mkdir(dir, 0744)
|
||||
if err == nil || !os.IsExist(err) {
|
||||
t.Errorf("Mkdir: Got %q for %T", err, fs)
|
||||
}
|
||||
|
||||
// https://github.com/spf13/afero/issues/191
|
||||
if _, ok := fs.(*OsFs); !ok {
|
||||
err = fs.MkdirAll(dir, 0744)
|
||||
if err == nil || !os.IsExist(err) {
|
||||
t.Errorf("MkdirAll: Got %q for %T", err, fs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyOnWriteFileInMemMapBase(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue