2016-04-19 02:25:52 +03:00
|
|
|
package afero
|
|
|
|
|
2018-12-16 13:57:17 +03:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
2016-04-19 02:25:52 +03:00
|
|
|
|
|
|
|
func TestCopyOnWrite(t *testing.T) {
|
2018-12-16 13:57:17 +03:00
|
|
|
osFs := NewOsFs()
|
|
|
|
writeDir, err := TempDir(osFs, "", "copy-on-write-test")
|
2016-04-19 02:25:52 +03:00
|
|
|
if err != nil {
|
2018-12-16 13:57:17 +03:00
|
|
|
t.Fatal("error creating tempDir", err)
|
2016-04-19 02:25:52 +03:00
|
|
|
}
|
2018-12-16 13:57:17 +03:00
|
|
|
defer osFs.RemoveAll(writeDir)
|
|
|
|
|
|
|
|
compositeFs := NewCopyOnWriteFs(NewReadOnlyFs(NewOsFs()), osFs)
|
|
|
|
|
2022-12-12 18:01:24 +03:00
|
|
|
dir := filepath.Join(writeDir, "some/path")
|
2018-12-16 13:57:17 +03:00
|
|
|
|
2022-12-12 18:01:24 +03:00
|
|
|
err = compositeFs.MkdirAll(dir, 0o744)
|
2018-12-16 13:57:17 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = compositeFs.Create(filepath.Join(dir, "newfile"))
|
2016-04-19 02:25:52 +03:00
|
|
|
if err != nil {
|
2018-12-16 13:57:17 +03:00
|
|
|
t.Fatal(err)
|
2016-04-19 02:25:52 +03:00
|
|
|
}
|
|
|
|
|
2022-12-12 18:01:24 +03:00
|
|
|
// https://git.internal/re/afero/issues/189
|
2018-12-16 13:57:17 +03:00
|
|
|
// We want the composite file system to behave like the OS file system
|
|
|
|
// on Mkdir and MkdirAll
|
|
|
|
for _, fs := range []Fs{osFs, compositeFs} {
|
2022-12-12 18:01:24 +03:00
|
|
|
err = fs.Mkdir(dir, 0o744)
|
2018-12-16 13:57:17 +03:00
|
|
|
if err == nil || !os.IsExist(err) {
|
|
|
|
t.Errorf("Mkdir: Got %q for %T", err, fs)
|
|
|
|
}
|
|
|
|
|
2018-12-16 14:45:33 +03:00
|
|
|
// MkdirAll does not return an error when the directory already exists
|
2022-12-12 18:01:24 +03:00
|
|
|
err = fs.MkdirAll(dir, 0o744)
|
2018-12-16 14:45:33 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("MkdirAll: Got %q for %T", err, fs)
|
2018-12-16 13:57:17 +03:00
|
|
|
}
|
2018-12-16 14:45:33 +03:00
|
|
|
|
2018-12-16 13:57:17 +03:00
|
|
|
}
|
2016-04-19 02:25:52 +03:00
|
|
|
}
|
2017-11-12 19:05:09 +03:00
|
|
|
|
|
|
|
func TestCopyOnWriteFileInMemMapBase(t *testing.T) {
|
|
|
|
base := &MemMapFs{}
|
|
|
|
layer := &MemMapFs{}
|
|
|
|
|
2022-12-12 18:01:24 +03:00
|
|
|
if err := WriteFile(base, "base.txt", []byte("base"), 0o755); err != nil {
|
2017-11-12 19:05:09 +03:00
|
|
|
t.Fatalf("Failed to write file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ufs := NewCopyOnWriteFs(base, layer)
|
|
|
|
|
|
|
|
_, err := ufs.Stat("base.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|