|
4 | 4 | "context" |
5 | 5 | "os" |
6 | 6 | "path/filepath" |
| 7 | + "runtime" |
7 | 8 | "strings" |
8 | 9 | "testing" |
9 | 10 | ) |
@@ -67,6 +68,82 @@ func TestEnsureRepoUpdatesExistingOrigin(t *testing.T) { |
67 | 68 | } |
68 | 69 | } |
69 | 70 |
|
| 71 | +func TestEnsureRepoAppliesPrivateDirectoryMode(t *testing.T) { |
| 72 | + ctx := context.Background() |
| 73 | + dir := t.TempDir() |
| 74 | + remote := filepath.Join(dir, "remote.git") |
| 75 | + if err := run(ctx, "", "git", "init", "--bare", remote); err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + repo := filepath.Join(dir, "share") |
| 79 | + if err := EnsureRemote(ctx, Options{RepoPath: repo, Remote: remote, Branch: "main", DirMode: 0o750}); err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + if runtime.GOOS == "windows" { |
| 83 | + return |
| 84 | + } |
| 85 | + info, err := os.Stat(repo) |
| 86 | + if err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + if mode := info.Mode().Perm(); mode != 0o750 { |
| 90 | + t.Fatalf("repo mode = %o, want 750", mode) |
| 91 | + } |
| 92 | + localRepo := filepath.Join(dir, "local-share") |
| 93 | + if err := os.Mkdir(localRepo, 0o700); err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + if err := EnsureRepo(ctx, Options{RepoPath: localRepo, Branch: "main", DirMode: 0o750}); err != nil { |
| 97 | + t.Fatal(err) |
| 98 | + } |
| 99 | + info, err = os.Stat(localRepo) |
| 100 | + if err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + if mode := info.Mode().Perm(); mode != 0o750 { |
| 104 | + t.Fatalf("local repo mode = %o, want 750", mode) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestEnsureRepoClonesRequestedRemoteBranch(t *testing.T) { |
| 109 | + ctx := context.Background() |
| 110 | + dir := t.TempDir() |
| 111 | + seed := filepath.Join(dir, "seed") |
| 112 | + remote := filepath.Join(dir, "remote.git") |
| 113 | + repo := filepath.Join(dir, "share") |
| 114 | + if err := run(ctx, "", "git", "init", "-b", "main", seed); err != nil { |
| 115 | + t.Fatal(err) |
| 116 | + } |
| 117 | + if err := os.WriteFile(filepath.Join(seed, "manifest.json"), []byte("release\n"), 0o600); err != nil { |
| 118 | + t.Fatal(err) |
| 119 | + } |
| 120 | + if committed, err := Commit(ctx, Options{RepoPath: seed, Branch: "main"}, "release"); err != nil || !committed { |
| 121 | + t.Fatalf("commit = %v, %v", committed, err) |
| 122 | + } |
| 123 | + if err := run(ctx, seed, "git", "branch", "release"); err != nil { |
| 124 | + t.Fatal(err) |
| 125 | + } |
| 126 | + if err := os.WriteFile(filepath.Join(seed, "manifest.json"), []byte("main\n"), 0o600); err != nil { |
| 127 | + t.Fatal(err) |
| 128 | + } |
| 129 | + if committed, err := Commit(ctx, Options{RepoPath: seed, Branch: "main"}, "main"); err != nil || !committed { |
| 130 | + t.Fatalf("commit = %v, %v", committed, err) |
| 131 | + } |
| 132 | + if err := run(ctx, "", "git", "clone", "--bare", seed, remote); err != nil { |
| 133 | + t.Fatal(err) |
| 134 | + } |
| 135 | + if err := EnsureRepo(ctx, Options{RepoPath: repo, Remote: remote, Branch: "release"}); err != nil { |
| 136 | + t.Fatal(err) |
| 137 | + } |
| 138 | + body, err := os.ReadFile(filepath.Join(repo, "manifest.json")) |
| 139 | + if err != nil { |
| 140 | + t.Fatal(err) |
| 141 | + } |
| 142 | + if strings.ReplaceAll(string(body), "\r\n", "\n") != "release\n" { |
| 143 | + t.Fatalf("manifest = %q, want release", body) |
| 144 | + } |
| 145 | +} |
| 146 | + |
70 | 147 | func TestPushWritesCurrentBranchToOrigin(t *testing.T) { |
71 | 148 | ctx := context.Background() |
72 | 149 | dir := t.TempDir() |
@@ -227,6 +304,80 @@ func TestPullCurrentUsesExistingOrigin(t *testing.T) { |
227 | 304 | } |
228 | 305 | } |
229 | 306 |
|
| 307 | +func TestPullCurrentPreservesUnpublishedLocalBranch(t *testing.T) { |
| 308 | + ctx := context.Background() |
| 309 | + dir := t.TempDir() |
| 310 | + remote := filepath.Join(dir, "remote.git") |
| 311 | + repo := filepath.Join(dir, "share") |
| 312 | + if err := run(ctx, "", "git", "init", "--bare", remote); err != nil { |
| 313 | + t.Fatal(err) |
| 314 | + } |
| 315 | + opts := Options{RepoPath: repo, Remote: remote, Branch: "private"} |
| 316 | + if err := EnsureRemote(ctx, opts); err != nil { |
| 317 | + t.Fatal(err) |
| 318 | + } |
| 319 | + if err := os.WriteFile(filepath.Join(repo, "manifest.json"), []byte("local\n"), 0o600); err != nil { |
| 320 | + t.Fatal(err) |
| 321 | + } |
| 322 | + if committed, err := Commit(ctx, opts, "local snapshot"); err != nil || !committed { |
| 323 | + t.Fatalf("commit = %v, %v", committed, err) |
| 324 | + } |
| 325 | + headBefore, err := output(ctx, repo, "git", "rev-parse", "HEAD") |
| 326 | + if err != nil { |
| 327 | + t.Fatal(err) |
| 328 | + } |
| 329 | + if err := PullCurrent(ctx, Options{RepoPath: repo, Branch: "private"}); err != nil { |
| 330 | + t.Fatal(err) |
| 331 | + } |
| 332 | + headAfter, err := output(ctx, repo, "git", "rev-parse", "HEAD") |
| 333 | + if err != nil { |
| 334 | + t.Fatal(err) |
| 335 | + } |
| 336 | + if strings.TrimSpace(headAfter) != strings.TrimSpace(headBefore) { |
| 337 | + t.Fatalf("HEAD moved from %s to %s", strings.TrimSpace(headBefore), strings.TrimSpace(headAfter)) |
| 338 | + } |
| 339 | +} |
| 340 | + |
| 341 | +func TestPullCurrentRejectsDeletedTrackedBranch(t *testing.T) { |
| 342 | + ctx := context.Background() |
| 343 | + dir := t.TempDir() |
| 344 | + remote := filepath.Join(dir, "remote.git") |
| 345 | + seed := filepath.Join(dir, "seed") |
| 346 | + repo := filepath.Join(dir, "share") |
| 347 | + if err := run(ctx, "", "git", "init", "--bare", remote); err != nil { |
| 348 | + t.Fatal(err) |
| 349 | + } |
| 350 | + if err := run(ctx, "", "git", "clone", remote, seed); err != nil { |
| 351 | + t.Fatal(err) |
| 352 | + } |
| 353 | + if err := run(ctx, seed, "git", "checkout", "-B", "main"); err != nil { |
| 354 | + t.Fatal(err) |
| 355 | + } |
| 356 | + if err := os.WriteFile(filepath.Join(seed, "manifest.json"), []byte("remote\n"), 0o600); err != nil { |
| 357 | + t.Fatal(err) |
| 358 | + } |
| 359 | + seedOpts := Options{RepoPath: seed, Remote: remote, Branch: "main"} |
| 360 | + if committed, err := Commit(ctx, seedOpts, "remote snapshot"); err != nil || !committed { |
| 361 | + t.Fatalf("commit = %v, %v", committed, err) |
| 362 | + } |
| 363 | + if err := Push(ctx, seedOpts); err != nil { |
| 364 | + t.Fatal(err) |
| 365 | + } |
| 366 | + if err := Pull(ctx, Options{RepoPath: repo, Remote: remote, Branch: "main"}); err != nil { |
| 367 | + t.Fatal(err) |
| 368 | + } |
| 369 | + if err := run(ctx, repo, "git", "branch", "--set-upstream-to", "origin/main", "main"); err != nil { |
| 370 | + t.Fatal(err) |
| 371 | + } |
| 372 | + if err := run(ctx, remote, "git", "update-ref", "-d", "refs/heads/main"); err != nil { |
| 373 | + t.Fatal(err) |
| 374 | + } |
| 375 | + err := PullCurrent(ctx, Options{RepoPath: repo, Branch: "main"}) |
| 376 | + if err == nil || !strings.Contains(err.Error(), "tracked remote branch origin/main is missing") { |
| 377 | + t.Fatalf("PullCurrent error = %v", err) |
| 378 | + } |
| 379 | +} |
| 380 | + |
230 | 381 | func TestSyncForWriteRebasesUnpushedCommit(t *testing.T) { |
231 | 382 | ctx := context.Background() |
232 | 383 | dir := t.TempDir() |
|
0 commit comments