An existing file is a conflict for the policy instead of a silent overwrite (U-P1-11); a symlink in the target where the archive has a directory is a type mismatch that is never written through (KeepBoth puts the directory beside it); unpacking into and from a remote panel uses the same engine (N-P1-15). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
123 lines
4.4 KiB
Go
123 lines
4.4 KiB
Go
package archive
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"vcom/internal/testutil/fstree"
|
|
"vcom/internal/transfer"
|
|
)
|
|
|
|
// U-P1-11: an existing file is a conflict, not a silent overwrite.
|
|
func TestUnpackConflictAsks(t *testing.T) {
|
|
for _, name := range []string{"a.zip", "a.tar.gz"} {
|
|
arch := writeArchive(t, name, file("top/keep.txt", "from archive"), file("top/new.txt", "n"))
|
|
idx, err := IndexFile(bg, local, arch, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dst := t.TempDir()
|
|
fstree.Build(t, dst, fstree.Spec{fstree.File("top/keep.txt", "user data")})
|
|
var asked []string
|
|
rep := Unpack(bg, local, arch, idx, nil, local, dst, transfer.DefaultOptions(), transfer.Hooks{
|
|
Resolve: func(_ context.Context, c transfer.Conflict) (transfer.Resolution, error) {
|
|
asked = append(asked, c.Item.Rel)
|
|
return transfer.Resolution{Action: transfer.ResolveSkip}, nil
|
|
},
|
|
})
|
|
if len(asked) != 1 || asked[0] != "top/keep.txt" {
|
|
t.Fatalf("%s: asked %v", name, asked)
|
|
}
|
|
if rep.Done != 1 || rep.Skipped != 1 {
|
|
t.Fatalf("%s: %+v", name, rep)
|
|
}
|
|
if b, _ := os.ReadFile(filepath.Join(dst, "top", "keep.txt")); string(b) != "user data" {
|
|
t.Fatalf("%s: existing file overwritten", name)
|
|
}
|
|
// Overwrite replaces atomically.
|
|
rep = Unpack(bg, local, arch, idx, []string{"top/keep.txt"}, local, filepath.Join(dst, "top"),
|
|
transfer.Options{Conflict: transfer.Overwrite}, transfer.Hooks{})
|
|
if b, _ := os.ReadFile(filepath.Join(dst, "top", "keep.txt")); rep.Done != 1 || string(b) != "from archive" {
|
|
t.Fatalf("%s: overwrite %+v %q", name, rep, b)
|
|
}
|
|
noTemps(t, filepath.Join(dst, "top"))
|
|
}
|
|
}
|
|
|
|
func TestUnpackDoesNotWriteThroughSymlink(t *testing.T) {
|
|
elsewhere := t.TempDir()
|
|
arch := writeArchive(t, "a.tar", file("foo/x", "payload"), file("bar", "b"))
|
|
idx, err := IndexFile(bg, local, arch, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dst := t.TempDir()
|
|
if err := os.Symlink(elsewhere, filepath.Join(dst, "foo")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, pol := range []transfer.ConflictPolicy{transfer.Overwrite, transfer.Ask, transfer.Skip} {
|
|
rep := Unpack(bg, local, arch, idx, nil, local, dst, transfer.Options{Conflict: pol}, transfer.Hooks{
|
|
Resolve: func(context.Context, transfer.Conflict) (transfer.Resolution, error) {
|
|
return transfer.Resolution{Action: transfer.ResolveOverwrite}, nil
|
|
},
|
|
})
|
|
if rep.Err != nil {
|
|
t.Fatal(rep.Err)
|
|
}
|
|
if ents, _ := os.ReadDir(elsewhere); len(ents) != 0 {
|
|
t.Fatalf("policy %d: wrote through the symlink: %v", pol, ents)
|
|
}
|
|
if fi, err := os.Lstat(filepath.Join(dst, "foo")); err != nil || fi.Mode()&os.ModeSymlink == 0 {
|
|
t.Fatal("the link itself was replaced")
|
|
}
|
|
}
|
|
// KeepBoth puts the directory beside the link.
|
|
rep := Unpack(bg, local, arch, idx, nil, local, dst, transfer.Options{Conflict: transfer.KeepBoth}, transfer.Hooks{})
|
|
if b, _ := os.ReadFile(filepath.Join(dst, "foo (1)", "x")); rep.Err != nil || string(b) != "payload" {
|
|
t.Fatalf("keep both: %+v", rep)
|
|
}
|
|
}
|
|
|
|
// N-P1-15: unpacking into a remote panel goes through the same engine.
|
|
func TestUnpackToRemote(t *testing.T) {
|
|
remote := remoteFS(t)
|
|
arch := writeArchive(t, "a.zip", file("top/a.txt", "alpha"), symlink("top/ln", "a.txt"), dir("top/empty/"))
|
|
idx, err := IndexFile(bg, local, arch, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dst := t.TempDir()
|
|
fstree.Build(t, dst, fstree.Spec{fstree.File("top/a.txt", "remote data")})
|
|
asked := 0
|
|
rep := Unpack(bg, local, arch, idx, nil, remote, dst, transfer.DefaultOptions(), transfer.Hooks{
|
|
Resolve: func(context.Context, transfer.Conflict) (transfer.Resolution, error) {
|
|
asked++
|
|
return transfer.Resolution{Action: transfer.ResolveOverwrite}, nil
|
|
},
|
|
})
|
|
if rep.Err != nil || rep.Done != 2 || asked != 1 {
|
|
t.Fatalf("%+v asked=%d", rep, asked)
|
|
}
|
|
if b, _ := os.ReadFile(filepath.Join(dst, "top", "a.txt")); string(b) != "alpha" {
|
|
t.Fatalf("content %q", b)
|
|
}
|
|
if l, _ := os.Readlink(filepath.Join(dst, "top", "ln")); l != "a.txt" {
|
|
t.Fatalf("link %q", l)
|
|
}
|
|
noTemps(t, filepath.Join(dst, "top"))
|
|
|
|
// And from a remote archive into a local directory.
|
|
arch2 := filepath.Join(dst, "copy.zip")
|
|
b, _ := os.ReadFile(arch)
|
|
_ = os.WriteFile(arch2, b, 0o644)
|
|
idx2, err := IndexFile(bg, remote, arch2, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dst2 := t.TempDir()
|
|
if rep := Unpack(bg, remote, arch2, idx2, nil, local, dst2, transfer.DefaultOptions(), transfer.Hooks{}); rep.Err != nil || rep.Done != 2 {
|
|
t.Fatalf("remote archive: %+v", rep)
|
|
}
|
|
}
|