|
| 1 | +package installer |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "runtime" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// copyNode backs the cross-filesystem move fallback. A symlink must survive as a |
| 11 | +// symlink (target not dereferenced), matching the rename path it stands in for. |
| 12 | +func TestCopyNodePreservesSymlink(t *testing.T) { |
| 13 | + if runtime.GOOS == "windows" { |
| 14 | + t.Skip("symlinks need privileges on Windows") |
| 15 | + } |
| 16 | + dir := t.TempDir() |
| 17 | + realTarget := filepath.Join(dir, "real-php") |
| 18 | + if err := os.WriteFile(realTarget, []byte("BINARY"), 0o755); err != nil { |
| 19 | + t.Fatal(err) |
| 20 | + } |
| 21 | + src := filepath.Join(dir, "php") // symlink -> real-php |
| 22 | + if err := os.Symlink(realTarget, src); err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + |
| 26 | + dst := filepath.Join(dir, "backup", "php") |
| 27 | + if err := copyNode(src, dst, 0o755); err != nil { |
| 28 | + t.Fatalf("copyNode: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + fi, err := os.Lstat(dst) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("lstat dst: %v", err) |
| 34 | + } |
| 35 | + if fi.Mode()&os.ModeSymlink == 0 { |
| 36 | + t.Fatal("dst should be a symlink, not a dereferenced regular file") |
| 37 | + } |
| 38 | + got, err := os.Readlink(dst) |
| 39 | + if err != nil || got != realTarget { |
| 40 | + t.Errorf("symlink target = %q (err %v), want %q", got, err, realTarget) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestCopyNodeCopiesRegularFile(t *testing.T) { |
| 45 | + dir := t.TempDir() |
| 46 | + src := filepath.Join(dir, "bin") |
| 47 | + if err := os.WriteFile(src, []byte("CONTENTS"), 0o755); err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + dst := filepath.Join(dir, "sub", "bin") |
| 51 | + if err := copyNode(src, dst, 0o755); err != nil { |
| 52 | + t.Fatalf("copyNode: %v", err) |
| 53 | + } |
| 54 | + if isLink, _ := isSymlinkNode(dst); isLink { |
| 55 | + t.Error("regular file should not become a symlink") |
| 56 | + } |
| 57 | + if b, err := os.ReadFile(dst); err != nil || string(b) != "CONTENTS" { |
| 58 | + t.Errorf("dst contents = %q (err %v), want CONTENTS", b, err) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Two files backed up under the same key in one install (e.g. a Windows php.exe |
| 63 | +// and php.cmd) must get distinct backup paths — even sharing a basename — so one |
| 64 | +// never overwrites the other. |
| 65 | +func TestBackupExistingUniquePaths(t *testing.T) { |
| 66 | + dir := t.TempDir() |
| 67 | + backups := filepath.Join(dir, "backups") |
| 68 | + |
| 69 | + // Two distinct sources sharing a basename ("php"), backed up under one key. |
| 70 | + srcA := filepath.Join(dir, "a", "php") |
| 71 | + srcB := filepath.Join(dir, "b", "php") |
| 72 | + if err := os.MkdirAll(filepath.Dir(srcA), 0o755); err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + if err := os.MkdirAll(filepath.Dir(srcB), 0o755); err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + if err := os.WriteFile(srcA, []byte("AAA"), 0o755); err != nil { |
| 79 | + t.Fatal(err) |
| 80 | + } |
| 81 | + if err := os.WriteFile(srcB, []byte("BBB"), 0o755); err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + |
| 85 | + pa, err := backupExisting(srcA, backups, "8.3") |
| 86 | + if err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + pb, err := backupExisting(srcB, backups, "8.3") |
| 90 | + if err != nil { |
| 91 | + t.Fatal(err) |
| 92 | + } |
| 93 | + |
| 94 | + if pa == pb { |
| 95 | + t.Fatalf("backup paths collided: %q", pa) |
| 96 | + } |
| 97 | + if data, _ := os.ReadFile(pa); string(data) != "AAA" { |
| 98 | + t.Errorf("backup A content = %q, want AAA", data) |
| 99 | + } |
| 100 | + if data, _ := os.ReadFile(pb); string(data) != "BBB" { |
| 101 | + t.Errorf("backup B content = %q, want BBB", data) |
| 102 | + } |
| 103 | + // Both sources were moved into their backups. |
| 104 | + if _, err := os.Stat(srcA); !os.IsNotExist(err) { |
| 105 | + t.Error("srcA should have been moved") |
| 106 | + } |
| 107 | + if _, err := os.Stat(srcB); !os.IsNotExist(err) { |
| 108 | + t.Error("srcB should have been moved") |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func isSymlinkNode(path string) (bool, error) { |
| 113 | + fi, err := os.Lstat(path) |
| 114 | + if err != nil { |
| 115 | + return false, err |
| 116 | + } |
| 117 | + return fi.Mode()&os.ModeSymlink != 0, nil |
| 118 | +} |
0 commit comments