| 1 | package tinygit |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestPRDiffUsesLocalCommitsAndReportsMetadata(t *testing.T) { |
| 12 | g, r, base := browseFixture(t) |
| 13 | tip := r.Refs["refs/heads/main" ] |
| 14 | got := g.PRDiff(context.Background(), r, base, tip) |
| 15 | if got.Status != "ok" || got.Base != base || got.Tip != tip || got.MergeBase != base { |
| 16 | t.Fatalf("result = %+v" , got) |
| 17 | } |
| 18 | if !strings.Contains(got.Diff, "+// second" ) || got.Truncated { |
| 19 | t.Fatalf("diff = %+v" , got) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestPRDiffDoesNotTurnMissingOrUnrelatedObjectsIntoEmptyDiff(t *testing.T) { |
| 24 | g, r, base := browseFixture(t) |
| 25 | tip := r.Refs["refs/heads/main" ] |
| 26 | missing := strings.Repeat("f" , 40) |
| 27 | if got := g.PRDiff(context.Background(), r, base, missing); got.Status != "diff_unavailable" || got.Reason == "" { |
| 28 | t.Fatalf("missing tip result = %+v" , got) |
| 29 | } |
| 30 | if got := g.PRDiff(context.Background(), r, missing, r.Refs["refs/heads/main" ]); got.Status != "diff_unavailable" || got.Reason == "" { |
| 31 | t.Fatalf("missing base result = %+v" , got) |
| 32 | } |
| 33 | // The first commit in the fixture is not related to an independently |
| 34 | // created root. The backend must disclose that it cannot establish a PR |
| 35 | // base instead of presenting a whole-tree comparison as a normal diff. |
| 36 | tree, err := exec.Command("git" , "--git-dir" , g.repoPath(r), "mktree" ).Output() |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | cmd := exec.Command("git" , "--git-dir" , g.repoPath(r), "commit-tree" , strings.TrimSpace(string(tree)), "-m" , "unrelated" ) |
| 41 | cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=Test" , "GIT_AUTHOR_EMAIL=test@example.test" , "GIT_COMMITTER_NAME=Test" , "GIT_COMMITTER_EMAIL=test@example.test" ) |
| 42 | otherBytes, err := cmd.Output() |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | other := strings.TrimSpace(string(otherBytes)) |
| 47 | if got := g.PRDiff(context.Background(), r, other, base); got.Status != "diff_unavailable" { |
| 48 | t.Fatalf("unrelated result = %+v" , got) |
| 49 | } |
| 50 | // This base is an ancestor of the PR tip but belongs only to that PR; |
| 51 | // it is not an ancestor of the repository target. |
| 52 | treeBytes, err := exec.Command("git" , "--git-dir" , g.repoPath(r), "rev-parse" , base+"^{tree}" ).Output() |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | commit := func(parents ...string) string { |
| 57 | args := []string{"--git-dir" , g.repoPath(r), "commit-tree" , strings.TrimSpace(string(treeBytes))} |
| 58 | for _, parent := range parents { |
| 59 | args = append(args, "-p" , parent) |
| 60 | } |
| 61 | args = append(args, "-m" , "PR-only" ) |
| 62 | cmd := exec.Command("git" , args...) |
| 63 | cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=Test" , "GIT_AUTHOR_EMAIL=test@example.test" , "GIT_COMMITTER_NAME=Test" , "GIT_COMMITTER_EMAIL=test@example.test" ) |
| 64 | out, err := cmd.Output() |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | return strings.TrimSpace(string(out)) |
| 69 | } |
| 70 | prBase := commit(base) |
| 71 | prTip := commit(prBase) |
| 72 | if got := g.PRDiff(context.Background(), r, prBase, prTip); got.Status != "diff_unavailable" { |
| 73 | t.Fatalf("PR-only ancestor result = %+v (main tip %s)" , got, tip) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestPRDiffAcceptsBaseWhenTargetBranchDiverged(t *testing.T) { |
| 78 | g, r, base := browseFixture(t) |
| 79 | tip := r.Refs["refs/heads/main" ] |
| 80 | treeBytes, err := exec.Command("git" , "--git-dir" , g.repoPath(r), "rev-parse" , base+"^{tree}" ).Output() |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | commit := func(message string) string { |
| 85 | cmd := exec.Command("git" , "--git-dir" , g.repoPath(r), "commit-tree" , strings.TrimSpace(string(treeBytes)), "-p" , base, "-m" , message) |
| 86 | cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=Test" , "GIT_AUTHOR_EMAIL=test@example.test" , "GIT_COMMITTER_NAME=Test" , "GIT_COMMITTER_EMAIL=test@example.test" ) |
| 87 | out, err := cmd.Output() |
| 88 | if err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | return strings.TrimSpace(string(out)) |
| 92 | } |
| 93 | target := commit("target branch" ) |
| 94 | if err := exec.Command("git" , "--git-dir" , g.repoPath(r), "update-ref" , "refs/heads/target" , target).Run(); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | r.Head = "refs/heads/target" |
| 98 | got := g.PRDiff(context.Background(), r, base, tip) |
| 99 | if got.Status != "ok" || got.MergeBase != base { |
| 100 | t.Fatalf("diverged target result = %+v" , got) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestPRDiffFindsMergeBaseFromRepositoryHead(t *testing.T) { |
| 105 | g, r, base := browseFixture(t) |
| 106 | tip := r.Refs["refs/heads/main" ] |
| 107 | r.Head = "refs/heads/main" |
| 108 | got := g.PRDiff(context.Background(), r, "" , tip) |
| 109 | if got.Status != "ok" || got.MergeBase != tip || got.Base != tip { |
| 110 | t.Fatalf("result = %+v (base fixture commit %s)" , got, base) |
| 111 | } |
| 112 | } |