go-webdav/fs_local_test.go
Jay Hemnani 2b106fb99b webdav: fix LocalFileSystem.Copy for directories
The walk ignored the path of each entry: it created dstPath for every directory
and copied srcPath to dstPath for every file, so copying a non-empty directory
failed with "open: is a directory". With Overwrite the destination had already
been removed, so its files were lost. Copy each entry to its own path under the
destination.
2026-11-18 21:18:25 +01:00

37 lines
1000 B
Go

package webdav
import (
"context"
"os"
"path/filepath"
"testing"
)
func TestLocalFileSystemCopyDirectory(t *testing.T) {
dir := t.TempDir()
for name, data := range map[string]string{"src/a.txt": "a", "src/sub/b.txt": "b", "dst/old.txt": "old"} {
p := filepath.Join(dir, name)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(data), 0o644); err != nil {
t.Fatal(err)
}
}
fs := LocalFileSystem(dir)
if _, err := fs.Copy(context.Background(), "/src", "/dst", &CopyOptions{}); err != nil {
t.Fatalf("Copy() = %v", err)
}
for name, want := range map[string]string{"dst/a.txt": "a", "dst/sub/b.txt": "b"} {
got, err := os.ReadFile(filepath.Join(dir, name))
if err != nil || string(got) != want {
t.Errorf("%s = %q, %v; want %q", name, got, err, want)
}
}
if _, err := os.Stat(filepath.Join(dir, "dst/old.txt")); !os.IsNotExist(err) {
t.Errorf("dst/old.txt still exists after overwrite: %v", err)
}
}