90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestMultiUsesUniqueVirtualRootNames(t *testing.T) {
|
|
a := t.TempDir()
|
|
b := t.TempDir()
|
|
m := NewMulti([]string{a, b})
|
|
if len(m.Names) != 2 || m.Names[0] == m.Names[1] {
|
|
t.Fatalf("names=%v", m.Names)
|
|
}
|
|
if _, _, ok := m.FindRoot(m.Names[0]); !ok {
|
|
t.Fatal("first root not addressable")
|
|
}
|
|
if _, _, ok := m.FindRoot(m.Names[1]); !ok {
|
|
t.Fatal("second root not addressable")
|
|
}
|
|
}
|
|
|
|
func TestMultiRenameCannotCrossRoots(t *testing.T) {
|
|
a := t.TempDir()
|
|
b := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(a, "src.txt"), []byte("src"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m := NewMulti([]string{a, b})
|
|
err := m.Rename(m.Names[0]+"/src.txt", m.Names[1]+"/dst.txt")
|
|
if err == nil {
|
|
t.Fatal("expected cross-root rename to fail")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(a, "src.txt")); err != nil {
|
|
t.Fatalf("source disappeared: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(b, "dst.txt")); !os.IsNotExist(err) {
|
|
t.Fatalf("destination unexpectedly created: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestMultiRemoveCannotRemoveVirtualRoot(t *testing.T) {
|
|
m := NewMulti([]string{t.TempDir()})
|
|
if err := m.Remove(""); err == nil {
|
|
t.Fatal("expected virtual root removal to fail")
|
|
}
|
|
}
|
|
|
|
func TestLocalCreateExclusiveRejectsExistingFile(t *testing.T) {
|
|
root := t.TempDir()
|
|
st := Local{Root: root}
|
|
if err := os.WriteFile(filepath.Join(root, "x.txt"), []byte("old"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f, err := st.CreateExclusive("x.txt")
|
|
if !os.IsExist(err) || f != nil {
|
|
t.Fatalf("expected EEXIST, file=%v err=%v", f, err)
|
|
}
|
|
got, err := os.ReadFile(filepath.Join(root, "x.txt"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(got) != "old" {
|
|
t.Fatalf("existing file changed: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestLocalCreateExclusiveCreatesMissingFile(t *testing.T) {
|
|
st := Local{Root: t.TempDir()}
|
|
f, err := st.CreateExclusive("nested/x.txt")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := f.WriteString("new"); err != nil {
|
|
f.Close()
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := os.ReadFile(filepath.Join(st.Root, "nested", "x.txt"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(got) != "new" {
|
|
t.Fatalf("unexpected content: %q", got)
|
|
}
|
|
}
|