RemoveRecursive used Stat on the root, so deleting a remote symlink to a directory walked into the target and deleted its contents. The root is now examined with Lstat (links are removed with Remove), and Walk also Lstats its root. Ports the analysts' TestRemoveRecursiveSymlinkToDir onto the sshtest server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package remote
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// B1 / N-P0-3: deleting a remote symlink that points to a directory wiped
|
|
// the target directory.
|
|
func TestRemoveRecursiveSymlinkToDir(t *testing.T) {
|
|
_, c, dir := setup(t)
|
|
target := filepath.Join(dir, "precious")
|
|
writeFile(t, filepath.Join(target, "a.txt"), 10, 0o644)
|
|
writeFile(t, filepath.Join(target, "sub", "b.txt"), 10, 0o644)
|
|
link := filepath.Join(dir, "link")
|
|
if err := os.Symlink(target, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := c.RemoveRecursive(link); err != nil {
|
|
t.Fatalf("RemoveRecursive(link): %v", err)
|
|
}
|
|
for _, p := range []string{"a.txt", "sub/b.txt"} {
|
|
if _, err := os.Stat(filepath.Join(target, p)); err != nil {
|
|
t.Errorf("deleting the link removed target content %s: %v", p, err)
|
|
}
|
|
}
|
|
if _, err := os.Lstat(link); !os.IsNotExist(err) {
|
|
t.Errorf("the link itself must be gone, Lstat err=%v", err)
|
|
}
|
|
}
|
|
|
|
func TestRemoveRecursiveDirKeepsLinkedTarget(t *testing.T) {
|
|
_, c, dir := setup(t)
|
|
outside := filepath.Join(dir, "outside")
|
|
writeFile(t, filepath.Join(outside, "keep.txt"), 10, 0o644)
|
|
tree := filepath.Join(dir, "tree")
|
|
writeFile(t, filepath.Join(tree, "f.txt"), 10, 0o644)
|
|
if err := os.Symlink(outside, filepath.Join(tree, "to-outside")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := c.RemoveRecursive(tree); err != nil {
|
|
t.Fatalf("RemoveRecursive(tree): %v", err)
|
|
}
|
|
if _, err := os.Lstat(tree); !os.IsNotExist(err) {
|
|
t.Errorf("tree should be gone: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(outside, "keep.txt")); err != nil {
|
|
t.Errorf("symlink inside the tree was followed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRemoveRecursiveFileAndDir(t *testing.T) {
|
|
_, c, dir := setup(t)
|
|
f := filepath.Join(dir, "f.bin")
|
|
writeFile(t, f, 100, 0o644)
|
|
if err := c.RemoveRecursive(f); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d := filepath.Join(dir, "d")
|
|
writeFile(t, filepath.Join(d, "x", "y", "z.bin"), 100, 0o644)
|
|
if err := c.RemoveRecursive(d); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, p := range []string{f, d} {
|
|
if _, err := os.Lstat(p); !os.IsNotExist(err) {
|
|
t.Errorf("%s still exists", p)
|
|
}
|
|
}
|
|
}
|