| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // factsRepo is a scratch repository with one commit; git runs in it. |
| 15 | func factsRepo(t *testing.T) (dir string, git func(...string) string) { |
| 16 | t.Helper() |
| 17 | dir = t.TempDir() |
| 18 | git = func(args ...string) string { |
| 19 | t.Helper() |
| 20 | cmd := exec.Command("/usr/bin/git", append([]string{"-C", dir, "-c", "user.name=t", "-c", "user.email=t@example.com", "-c", "commit.gpgsign=false"}, args...)...) |
| 21 | cmd.Env = []string{"PATH=/usr/bin:/bin", "HOME=/nonexistent", "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null"} |
| 22 | out, err := cmd.CombinedOutput() |
| 23 | if err != nil { |
| 24 | t.Fatalf("git %v: %v\n%s", args, err, out) |
| 25 | } |
| 26 | return strings.TrimSpace(string(out)) |
| 27 | } |
| 28 | git("init", "-q", "-b", "main") |
| 29 | writeFile(t, dir, ".equipo/gates.json", `{"version":1,"files":[]}`) |
| 30 | writeFile(t, dir, "README.md", "hello\n") |
| 31 | writeFile(t, dir, "Sources/Pane.swift", "struct Pane {}\n") |
| 32 | writeFile(t, dir, "Woodstar.xcodeproj/project.pbxproj", "objects = {\n};\n") |
| 33 | writeFile(t, dir, "assets/icon.png", "\x89PNG\x00\x01") |
| 34 | git("add", "-A") |
| 35 | git("commit", "-q", "-m", "base") |
| 36 | return dir, git |
| 37 | } |
| 38 | |
| 39 | func writeFile(t *testing.T, dir, name, text string) { |
| 40 | t.Helper() |
| 41 | p := filepath.Join(dir, name) |
| 42 | if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if err := os.WriteFile(p, []byte(text), 0o600); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func measure(t *testing.T, dir, base, sha, revert string) *wire.ChangeFacts { |
| 51 | t.Helper() |
| 52 | r := stepRow{DispatchCommand: wire.DispatchCommand{StepID: "s_r", Attempt: 2, Repo: "amadan.net/oficina/sample", BaseSHA: base, CandidateSHA: sha, RevertSHA: revert}} |
| 53 | return changeFacts(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), r) |
| 54 | } |
| 55 | |
| 56 | // TestReviewReportsChangeFacts: the runner measures exactly the reviewed |
| 57 | // pair: lines and files, both paths of a rename, binary files, symlinks, |
| 58 | // submodules, mode changes, new executables and signing lines, and says |
| 59 | // incomplete when it cannot measure the whole. |
| 60 | func TestReviewReportsChangeFacts(t *testing.T) { |
| 61 | dir, git := factsRepo(t) |
| 62 | base := git("rev-parse", "HEAD") |
| 63 | |
| 64 | // An ordinary change: one edit, one rename, one new file. |
| 65 | writeFile(t, dir, "Sources/Pane.swift", "struct Pane {\n var edge = true\n}\n") |
| 66 | os.MkdirAll(filepath.Join(dir, "docs"), 0o700) |
| 67 | git("mv", "README.md", "docs/README.md") |
| 68 | writeFile(t, dir, "Sources/Edge.swift", "let edge = 1\n") |
| 69 | writeFile(t, dir, "Woodstar.xcodeproj/project.pbxproj", "objects = {\n F1 = {isa = PBXFileReference; path = Edge.swift; };\n};\n") |
| 70 | git("add", "-A") |
| 71 | git("commit", "-q", "-m", "change") |
| 72 | cand := git("rev-parse", "HEAD") |
| 73 | f := measure(t, dir, base, cand, "") |
| 74 | if !f.Complete || f.Files != 4 || f.Binary != 0 || f.Special || f.Signing || f.ReviewStep != "s_r" || f.Attempt != 2 || f.SHA != cand || f.Base != base { |
| 75 | t.Fatalf("ordinary %+v", f) |
| 76 | } |
| 77 | if f.Lines != 1+3+1+1 { |
| 78 | t.Fatalf("lines %d (%+v)", f.Lines, f.Entries) |
| 79 | } |
| 80 | renamed := false |
| 81 | for _, e := range f.Entries { |
| 82 | if strings.HasPrefix(e.Status, "R") && e.Old == "README.md" && e.New == "docs/README.md" { |
| 83 | renamed = true |
| 84 | } |
| 85 | } |
| 86 | if !renamed { |
| 87 | t.Fatalf("rename paths %+v", f.Entries) |
| 88 | } |
| 89 | |
| 90 | // Signing lines in the project file. |
| 91 | writeFile(t, dir, "Woodstar.xcodeproj/project.pbxproj", "objects = {\n DEVELOPMENT_TEAM = ABC123;\n};\n") |
| 92 | git("commit", "-q", "-am", "team") |
| 93 | if f := measure(t, dir, cand, git("rev-parse", "HEAD"), ""); !f.Complete || !f.Signing { |
| 94 | t.Fatalf("signing %+v", f) |
| 95 | } |
| 96 | prev := git("rev-parse", "HEAD") |
| 97 | |
| 98 | // A binary file, a mode change, a symlink and a new executable. |
| 99 | writeFile(t, dir, "assets/icon.png", "\x89PNG\x00\x02\x03") |
| 100 | git("commit", "-q", "-am", "icon") |
| 101 | if f := measure(t, dir, prev, git("rev-parse", "HEAD"), ""); f.Binary != 1 || f.Lines != 0 || f.Special { |
| 102 | t.Fatalf("binary %+v", f) |
| 103 | } |
| 104 | for name, change := range map[string]func(){ |
| 105 | "mode": func() { git("update-index", "--chmod=+x", "Sources/Pane.swift") }, |
| 106 | "symlink": func() { os.Symlink("Sources/Pane.swift", filepath.Join(dir, "link")); git("add", "link") }, |
| 107 | "executable": func() { |
| 108 | writeFile(t, dir, "run.sh", "#!/bin/sh\n") |
| 109 | git("add", "run.sh") |
| 110 | git("update-index", "--chmod=+x", "run.sh") |
| 111 | }, |
| 112 | "submodule": func() { git("update-index", "--add", "--cacheinfo", "160000,"+base+",vendored") }, |
| 113 | } { |
| 114 | before := git("rev-parse", "HEAD") |
| 115 | change() |
| 116 | git("commit", "-q", "-m", name) |
| 117 | if f := measure(t, dir, before, git("rev-parse", "HEAD"), ""); !f.Complete || !f.Special { |
| 118 | t.Errorf("%s not special: %+v", name, f) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Not a descendant of the base, or a base that is not a SHA: |
| 123 | // incomplete. |
| 124 | git("checkout", "-q", "-f", "-b", "other", base) |
| 125 | writeFile(t, dir, "x.txt", "x\n") |
| 126 | git("add", "x.txt") |
| 127 | git("commit", "-q", "-m", "other") |
| 128 | if f := measure(t, dir, cand, git("rev-parse", "HEAD"), ""); f.Complete { |
| 129 | t.Fatalf("unrelated pair complete %+v", f) |
| 130 | } |
| 131 | if f := measure(t, dir, "main", cand, ""); f.Complete { |
| 132 | t.Fatalf("a ref measured %+v", f) |
| 133 | } |
| 134 | |
| 135 | // More entries than the bound: incomplete. |
| 136 | git("checkout", "-q", "-f", "main") |
| 137 | before := git("rev-parse", "HEAD") |
| 138 | for i := 0; i <= wire.MaxChangeEntries; i++ { |
| 139 | writeFile(t, dir, filepath.Join("many", "f"+itoa(int64(i))+".txt"), "x\n") |
| 140 | } |
| 141 | git("add", "-A") |
| 142 | git("commit", "-q", "-m", "many") |
| 143 | if f := measure(t, dir, before, git("rev-parse", "HEAD"), ""); f.Complete { |
| 144 | t.Fatalf("over the bound complete: %d", f.Files) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // TestRevertCheck: a revert task's candidate is exact only when it sits |
| 149 | // right on the merge commit and has that commit's first parent's tree. |
| 150 | func TestRevertCheck(t *testing.T) { |
| 151 | dir, git := factsRepo(t) |
| 152 | base := git("rev-parse", "HEAD") |
| 153 | git("checkout", "-q", "-b", "task") |
| 154 | writeFile(t, dir, "Sources/Pane.swift", "struct Pane { var edge = true }\n") |
| 155 | git("commit", "-q", "-am", "task") |
| 156 | git("checkout", "-q", "main") |
| 157 | git("merge", "-q", "--no-ff", "-m", "merge task", "task") |
| 158 | merge := git("rev-parse", "HEAD") |
| 159 | |
| 160 | git("revert", "--no-edit", "-m", "1", merge) |
| 161 | exact := git("rev-parse", "HEAD") |
| 162 | if f := measure(t, dir, merge, exact, merge); f.Revert == nil || !f.Revert.OnTop || !f.Revert.Exact || f.Revert.SHA != merge { |
| 163 | t.Fatalf("exact revert %+v", f.Revert) |
| 164 | } |
| 165 | // The revert plus another edit is not exact. |
| 166 | writeFile(t, dir, "README.md", "hello again\n") |
| 167 | git("commit", "-q", "-a", "--amend", "--no-edit") |
| 168 | more := git("rev-parse", "HEAD") |
| 169 | if f := measure(t, dir, merge, more, merge); f.Revert == nil || !f.Revert.OnTop || f.Revert.Exact { |
| 170 | t.Fatalf("revert and more %+v", f.Revert) |
| 171 | } |
| 172 | // Main moved on before the revert: not on top. |
| 173 | git("reset", "-q", "--hard", merge) |
| 174 | writeFile(t, dir, "NEWS", "news\n") |
| 175 | git("add", "NEWS") |
| 176 | git("commit", "-q", "-m", "news") |
| 177 | git("revert", "--no-edit", "-m", "1", merge) |
| 178 | late := git("rev-parse", "HEAD") |
| 179 | if f := measure(t, dir, merge, late, merge); f.Revert == nil || f.Revert.OnTop || f.Revert.Exact { |
| 180 | t.Fatalf("not on top %+v", f.Revert) |
| 181 | } |
| 182 | if f := measure(t, dir, base, exact, ""); f.Revert != nil { |
| 183 | t.Fatalf("a revert check without a target %+v", f.Revert) |
| 184 | } |
| 185 | } |
| 186 | |