| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "the-valley/change" |
| 14 | ) |
| 15 | |
| 16 | // A withdrawal given the head it was read at writes only while the |
| 17 | // change's active version is still at that head, as review's [r]eject |
| 18 | // withdraws the version whose branch it deleted: a version readied since |
| 19 | // is left as it is. |
| 20 | func TestSetStateWithdrawsOnlyAtTheHeadRead(t *testing.T) { |
| 21 | dir := t.TempDir() |
| 22 | t.Setenv("HOME", dir) |
| 23 | t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config")) |
| 24 | t.Setenv("GIT_CONFIG_NOSYSTEM", "1") |
| 25 | origin, work := filepath.Join(dir, "origin.git"), filepath.Join(dir, "work") |
| 26 | sh := func(args ...string) string { |
| 27 | t.Helper() |
| 28 | cmd := exec.Command("git", args...) |
| 29 | cmd.Dir = work |
| 30 | out, err := cmd.Output() |
| 31 | if err != nil { |
| 32 | t.Fatalf("git %s: %v", strings.Join(args, " "), err) |
| 33 | } |
| 34 | return strings.TrimSpace(string(out)) |
| 35 | } |
| 36 | valley := func(args ...string) { |
| 37 | t.Helper() |
| 38 | var stdout, stderr bytes.Buffer |
| 39 | if code := run(args, &stdout, &stderr, os.Exit); code != 0 { |
| 40 | t.Fatalf("valley %s: %s", strings.Join(args, " "), stderr.String()) |
| 41 | } |
| 42 | } |
| 43 | if out, err := exec.Command("git", "init", "--quiet", "--bare", "--initial-branch=main", origin).CombinedOutput(); err != nil { |
| 44 | t.Fatalf("git init: %v\n%s", err, out) |
| 45 | } |
| 46 | if out, err := exec.Command("git", "clone", "--quiet", origin, work).CombinedOutput(); err != nil { |
| 47 | t.Fatalf("git clone: %v\n%s", err, out) |
| 48 | } |
| 49 | sh("config", "user.name", "valley-test") |
| 50 | sh("config", "user.email", "valley-test@localhost") |
| 51 | sh("config", "valley.principal", "alice") |
| 52 | sh("checkout", "--quiet", "-b", "main") |
| 53 | sh("commit", "--quiet", "--allow-empty", "-m", "base") |
| 54 | sh("push", "--quiet", "origin", "main") |
| 55 | t.Chdir(work) |
| 56 | |
| 57 | id := strings.Repeat("k", 32) |
| 58 | sh("checkout", "--quiet", "-b", "topic") |
| 59 | sh("commit", "--quiet", "--allow-empty", "-m", "one") |
| 60 | first := sh("rev-parse", "HEAD") |
| 61 | sh("push", "--quiet", "origin", "topic") |
| 62 | valley("ready", "--change", id, "topic") |
| 63 | sh("commit", "--quiet", "--allow-empty", "-m", "two") |
| 64 | second := sh("rev-parse", "HEAD") |
| 65 | sh("push", "--quiet", "origin", "topic") |
| 66 | valley("ready", "--change", id, "topic") |
| 67 | |
| 68 | state := func() string { |
| 69 | t.Helper() |
| 70 | blob, err := exec.Command("git", "-C", origin, "cat-file", "blob", change.Ref(recordTarget, id)+":record.json").Output() |
| 71 | if err != nil { |
| 72 | t.Fatalf("origin holds no record of %s: %v", id, err) |
| 73 | } |
| 74 | var r change.Record |
| 75 | if err := json.Unmarshal(blob, &r); err != nil { |
| 76 | t.Fatalf("the record of %s: %v", id, err) |
| 77 | } |
| 78 | return r.State |
| 79 | } |
| 80 | g := git{env: gitEnvironment(os.Environ()), stderr: io.Discard} |
| 81 | err := setState(g, []string{id}, io.Discard, io.Discard, "withdraw", change.Withdrawn, first) |
| 82 | if err == nil || !strings.Contains(err.Error(), "is at v2 now") { |
| 83 | t.Errorf("a withdrawal at v1's head, with v2 active, was not refused: %v", err) |
| 84 | } |
| 85 | if s := state(); s != change.Ready { |
| 86 | t.Errorf("a refused withdrawal left the change %s", s) |
| 87 | } |
| 88 | if err := setState(g, []string{id}, io.Discard, io.Discard, "withdraw", change.Withdrawn, second); err != nil { |
| 89 | t.Fatalf("a withdrawal at the active head: %v", err) |
| 90 | } |
| 91 | if s := state(); s != change.Withdrawn { |
| 92 | t.Errorf("a withdrawal at the active head left the change %s", s) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // readyFixture sets up a bare origin and a working clone configured for |
| 97 | // alice, with an initial commit on main containing the files named. |
| 98 | type readyFixture struct { |
| 99 | t *testing.T |
| 100 | origin string |
| 101 | work string |
| 102 | } |
| 103 | |
| 104 | func newReadyFixture(t *testing.T, files map[string]string) *readyFixture { |
| 105 | t.Helper() |
| 106 | dir := t.TempDir() |
| 107 | t.Setenv("HOME", dir) |
| 108 | t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config")) |
| 109 | t.Setenv("GIT_CONFIG_NOSYSTEM", "1") |
| 110 | f := &readyFixture{ |
| 111 | t: t, |
| 112 | origin: filepath.Join(dir, "origin.git"), |
| 113 | work: filepath.Join(dir, "work"), |
| 114 | } |
| 115 | if out, err := exec.Command("git", "init", "--quiet", "--bare", "--initial-branch=main", f.origin).CombinedOutput(); err != nil { |
| 116 | t.Fatalf("git init: %v\n%s", err, out) |
| 117 | } |
| 118 | if out, err := exec.Command("git", "clone", "--quiet", f.origin, f.work).CombinedOutput(); err != nil { |
| 119 | t.Fatalf("git clone: %v\n%s", err, out) |
| 120 | } |
| 121 | f.git("config", "user.name", "valley-test") |
| 122 | f.git("config", "user.email", "valley-test@localhost") |
| 123 | f.git("config", "valley.principal", "alice") |
| 124 | f.git("checkout", "--quiet", "-b", "main") |
| 125 | for name, body := range files { |
| 126 | f.write(name, body) |
| 127 | } |
| 128 | if len(files) == 0 { |
| 129 | f.git("commit", "--quiet", "--allow-empty", "-m", "base") |
| 130 | } else { |
| 131 | f.git("add", "-A") |
| 132 | f.git("commit", "--quiet", "-m", "base") |
| 133 | } |
| 134 | f.git("push", "--quiet", "origin", "main") |
| 135 | t.Chdir(f.work) |
| 136 | return f |
| 137 | } |
| 138 | |
| 139 | func (f *readyFixture) git(args ...string) string { |
| 140 | f.t.Helper() |
| 141 | cmd := exec.Command("git", args...) |
| 142 | cmd.Dir = f.work |
| 143 | out, err := cmd.CombinedOutput() |
| 144 | if err != nil { |
| 145 | f.t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) |
| 146 | } |
| 147 | return strings.TrimSpace(string(out)) |
| 148 | } |
| 149 | |
| 150 | func (f *readyFixture) write(name, body string) { |
| 151 | f.t.Helper() |
| 152 | path := filepath.Join(f.work, name) |
| 153 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 154 | f.t.Fatalf("mkdir %s: %v", path, err) |
| 155 | } |
| 156 | if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| 157 | f.t.Fatalf("write %s: %v", path, err) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func (f *readyFixture) valley(args ...string) (int, string, string) { |
| 162 | f.t.Helper() |
| 163 | var stdout, stderr bytes.Buffer |
| 164 | code := run(args, &stdout, &stderr, os.Exit) |
| 165 | return code, stdout.String(), stderr.String() |
| 166 | } |
| 167 | |
| 168 | func (f *readyFixture) mustValley(args ...string) (string, string) { |
| 169 | f.t.Helper() |
| 170 | code, stdout, stderr := f.valley(args...) |
| 171 | if code != 0 { |
| 172 | f.t.Fatalf("valley %s exited %d:\nstdout: %s\nstderr: %s", strings.Join(args, " "), code, stdout, stderr) |
| 173 | } |
| 174 | return stdout, stderr |
| 175 | } |
| 176 | |
| 177 | func (f *readyFixture) records() map[string]*change.Record { |
| 178 | f.t.Helper() |
| 179 | out, err := exec.Command("git", "-C", f.origin, "for-each-ref", "--format=%(refname)", change.RefPrefix+recordTarget+"/").Output() |
| 180 | if err != nil { |
| 181 | f.t.Fatalf("for-each-ref: %v", err) |
| 182 | } |
| 183 | recs := map[string]*change.Record{} |
| 184 | for _, ref := range strings.Fields(string(out)) { |
| 185 | _, id, ok := change.ParseRef(ref) |
| 186 | if !ok { |
| 187 | continue |
| 188 | } |
| 189 | blob, err := exec.Command("git", "-C", f.origin, "cat-file", "blob", ref+":record.json").Output() |
| 190 | if err != nil { |
| 191 | f.t.Fatalf("cat-file %s: %v", ref, err) |
| 192 | } |
| 193 | var r change.Record |
| 194 | if err := json.Unmarshal(blob, &r); err != nil { |
| 195 | f.t.Fatalf("unmarshal %s: %v", ref, err) |
| 196 | } |
| 197 | recs[id] = &r |
| 198 | } |
| 199 | return recs |
| 200 | } |
| 201 | |
| 202 | // A commit made with plain git (no header, no trailer), readied on a branch, |
| 203 | // then rebased onto a moved main and readied again produces one change with |
| 204 | // two versions. |
| 205 | func TestReadyPlainGitRebaseKeepsChange(t *testing.T) { |
| 206 | f := newReadyFixture(t, map[string]string{ |
| 207 | "a.txt": "line1\n", |
| 208 | "b.txt": "line1\n", |
| 209 | }) |
| 210 | f.git("checkout", "--quiet", "-b", "topic") |
| 211 | f.write("a.txt", "line1\nfrom topic\n") |
| 212 | f.git("commit", "--quiet", "-am", "topic work") |
| 213 | first := f.git("rev-parse", "HEAD") |
| 214 | f.git("push", "--quiet", "origin", "topic") |
| 215 | |
| 216 | out1, err1 := f.mustValley("ready") |
| 217 | if err1 != "" { |
| 218 | t.Errorf("unexpected warning on current base: %q", err1) |
| 219 | } |
| 220 | recs := f.records() |
| 221 | if len(recs) != 1 { |
| 222 | t.Fatalf("expected 1 record after first ready, got %d", len(recs)) |
| 223 | } |
| 224 | var id string |
| 225 | for k := range recs { |
| 226 | id = k |
| 227 | } |
| 228 | handle := id[:4] |
| 229 | if !strings.Contains(out1, "v1 ready") || !strings.Contains(out1, "The change ID "+id+" is new.") { |
| 230 | t.Errorf("unexpected first ready output: %q", out1) |
| 231 | } |
| 232 | |
| 233 | // Advance main on origin, then rebase topic with plain git and ready again. |
| 234 | f.git("checkout", "--quiet", "main") |
| 235 | f.write("b.txt", "line1\nfrom main\n") |
| 236 | f.git("commit", "--quiet", "-am", "main advances") |
| 237 | f.git("push", "--quiet", "origin", "main") |
| 238 | |
| 239 | f.git("checkout", "--quiet", "topic") |
| 240 | f.git("rebase", "--quiet", "origin/main") |
| 241 | second := f.git("rev-parse", "HEAD") |
| 242 | f.git("push", "--quiet", "--force", "origin", "topic") |
| 243 | |
| 244 | out2, err2 := f.mustValley("ready") |
| 245 | if err2 != "" { |
| 246 | t.Errorf("unexpected warning after rebase onto current main: %q", err2) |
| 247 | } |
| 248 | recs = f.records() |
| 249 | if len(recs) != 1 { |
| 250 | t.Fatalf("expected 1 change after plain-git rebase and ready, got %d changes", len(recs)) |
| 251 | } |
| 252 | r := recs[id] |
| 253 | if r == nil || r.Active != 2 || len(r.Versions) != 2 { |
| 254 | t.Fatalf("expected change %s to have 2 versions with active=2, got %+v", id, r) |
| 255 | } |
| 256 | if r.Versions[0].Head != change.HeadPrefix+first || r.Versions[1].Head != change.HeadPrefix+second { |
| 257 | t.Errorf("unexpected version heads: %+v", r.Versions) |
| 258 | } |
| 259 | if !strings.Contains(out2, handle+" v2 ready") { |
| 260 | t.Errorf("expected %s v2 ready in output, got %q", handle, out2) |
| 261 | } |
| 262 | if !strings.Contains(out2, "Using change "+handle+": branch topic already has it.") { |
| 263 | t.Errorf("expected branch-match line naming %s, got %q", handle, out2) |
| 264 | } |
| 265 | if strings.Contains(out2, "is new") { |
| 266 | t.Errorf("second ready claimed change ID is new: %q", out2) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // Branch-matched change ID rules: drafts stay live, explicit IDs and trailers |
| 271 | // win, two live changes on one branch are refused, landed and withdrawn |
| 272 | // changes do not capture the branch, and --stamp and --if-active still guard. |
| 273 | func TestReadyBranchMatchRules(t *testing.T) { |
| 274 | t.Run("draft stays live and revives or advances", func(t *testing.T) { |
| 275 | f := newReadyFixture(t, map[string]string{"f.txt": "0\n"}) |
| 276 | f.git("checkout", "--quiet", "-b", "draft-branch") |
| 277 | f.write("f.txt", "1\n") |
| 278 | f.git("commit", "--quiet", "-am", "v1") |
| 279 | f.git("push", "--quiet", "origin", "draft-branch") |
| 280 | f.mustValley("ready") |
| 281 | var id string |
| 282 | for k := range f.records() { |
| 283 | id = k |
| 284 | } |
| 285 | handle := id[:4] |
| 286 | f.mustValley("unready", handle) |
| 287 | |
| 288 | // Readying the same head revives the draft at v1. |
| 289 | out, _ := f.mustValley("ready") |
| 290 | r := f.records()[id] |
| 291 | if len(f.records()) != 1 || r.State != change.Ready || r.Active != 1 || len(r.Versions) != 1 { |
| 292 | t.Fatalf("expected revived draft v1, got recs=%d r=%+v", len(f.records()), r) |
| 293 | } |
| 294 | if !strings.Contains(out, "Using change "+handle+": branch draft-branch already has it.") { |
| 295 | t.Errorf("expected branch-match line, got %q", out) |
| 296 | } |
| 297 | |
| 298 | // Unready again, push a new head, ready without --change -> v2. |
| 299 | f.mustValley("unready", handle) |
| 300 | f.write("f.txt", "2\n") |
| 301 | f.git("commit", "--quiet", "-am", "v2") |
| 302 | f.git("push", "--quiet", "origin", "draft-branch") |
| 303 | f.mustValley("ready") |
| 304 | r = f.records()[id] |
| 305 | if len(f.records()) != 1 || r.State != change.Ready || r.Active != 2 || len(r.Versions) != 2 { |
| 306 | t.Fatalf("expected draft advanced to v2, got recs=%d r=%+v", len(f.records()), r) |
| 307 | } |
| 308 | }) |
| 309 | |
| 310 | t.Run("two live changes on the same branch are refused and both named", func(t *testing.T) { |
| 311 | f := newReadyFixture(t, map[string]string{"f.txt": "0\n"}) |
| 312 | id1 := strings.Repeat("k", 32) |
| 313 | id2 := strings.Repeat("m", 32) |
| 314 | f.git("checkout", "--quiet", "-b", "shared") |
| 315 | f.write("f.txt", "1\n") |
| 316 | f.git("commit", "--quiet", "-am", "one") |
| 317 | f.git("push", "--quiet", "origin", "shared") |
| 318 | f.mustValley("ready", "--change", id1, "shared") |
| 319 | f.mustValley("ready", "--change", id2, "shared") |
| 320 | |
| 321 | f.write("f.txt", "2\n") |
| 322 | f.git("commit", "--quiet", "-am", "two") |
| 323 | f.git("push", "--quiet", "origin", "shared") |
| 324 | |
| 325 | code, stdout, stderr := f.valley("ready", "shared") |
| 326 | if code == 0 { |
| 327 | t.Fatalf("expected refusal with two live changes on branch, got exit 0:\nstdout: %s\nstderr: %s", stdout, stderr) |
| 328 | } |
| 329 | for _, want := range []string{"kkkk", id1, "mmmm", id2, "--change"} { |
| 330 | if !strings.Contains(stderr, want) { |
| 331 | t.Errorf("refusal stderr missing %q:\n%s", want, stderr) |
| 332 | } |
| 333 | } |
| 334 | if len(f.records()) != 2 { |
| 335 | t.Errorf("refused ready created a record: got %d records", len(f.records())) |
| 336 | } |
| 337 | }) |
| 338 | |
| 339 | t.Run("withdrawn change does not capture branch, explicit id still revives", func(t *testing.T) { |
| 340 | f := newReadyFixture(t, map[string]string{"f.txt": "0\n"}) |
| 341 | f.git("checkout", "--quiet", "-b", "reused-w") |
| 342 | f.write("f.txt", "1\n") |
| 343 | f.git("commit", "--quiet", "-am", "first") |
| 344 | f.git("push", "--quiet", "origin", "reused-w") |
| 345 | f.mustValley("ready") |
| 346 | var firstID string |
| 347 | for k := range f.records() { |
| 348 | firstID = k |
| 349 | } |
| 350 | f.mustValley("withdraw", firstID) |
| 351 | |
| 352 | // A new plain-git commit readied without --change starts a new change. |
| 353 | f.write("f.txt", "2\n") |
| 354 | f.git("commit", "--quiet", "-am", "second") |
| 355 | f.git("push", "--quiet", "origin", "reused-w") |
| 356 | out, _ := f.mustValley("ready") |
| 357 | recs := f.records() |
| 358 | if len(recs) != 2 { |
| 359 | t.Fatalf("expected 2 changes (withdrawn + new), got %d", len(recs)) |
| 360 | } |
| 361 | if !strings.Contains(out, "is new.") { |
| 362 | t.Errorf("expected new change ID message, got %q", out) |
| 363 | } |
| 364 | if recs[firstID].State != change.Withdrawn { |
| 365 | t.Errorf("withdrawn change was altered: %+v", recs[firstID]) |
| 366 | } |
| 367 | |
| 368 | // Explicit --change still revives the withdrawn change. |
| 369 | f.write("f.txt", "3\n") |
| 370 | f.git("commit", "--quiet", "-am", "third") |
| 371 | f.git("push", "--quiet", "origin", "reused-w") |
| 372 | f.mustValley("ready", "--change", firstID) |
| 373 | recs = f.records() |
| 374 | if recs[firstID].State != change.Ready || recs[firstID].Active != 2 { |
| 375 | t.Errorf("explicit --change did not revive withdrawn change: %+v", recs[firstID]) |
| 376 | } |
| 377 | }) |
| 378 | |
| 379 | t.Run("landed change does not capture branch", func(t *testing.T) { |
| 380 | f := newReadyFixture(t, map[string]string{"f.txt": "0\n"}) |
| 381 | f.git("checkout", "--quiet", "-b", "reused-l") |
| 382 | f.write("f.txt", "1\n") |
| 383 | f.git("commit", "--quiet", "-am", "lands") |
| 384 | landedHead := f.git("rev-parse", "HEAD") |
| 385 | f.git("push", "--quiet", "origin", "reused-l") |
| 386 | f.mustValley("ready") |
| 387 | var firstID string |
| 388 | for k := range f.records() { |
| 389 | firstID = k |
| 390 | } |
| 391 | |
| 392 | // Land the commit onto origin/main (first-parent history). |
| 393 | f.git("push", "--quiet", "origin", landedHead+":refs/heads/main") |
| 394 | f.git("checkout", "--quiet", "main") |
| 395 | f.git("reset", "--quiet", "--hard", landedHead) |
| 396 | |
| 397 | // Reuse the same branch name for new work. |
| 398 | f.git("checkout", "--quiet", "reused-l") |
| 399 | f.write("f.txt", "2\n") |
| 400 | f.git("commit", "--quiet", "-am", "after landing") |
| 401 | f.git("push", "--quiet", "origin", "reused-l") |
| 402 | out, _ := f.mustValley("ready") |
| 403 | recs := f.records() |
| 404 | if len(recs) != 2 { |
| 405 | t.Fatalf("expected 2 changes (landed + new), got %d", len(recs)) |
| 406 | } |
| 407 | if !strings.Contains(out, "is new.") { |
| 408 | t.Errorf("expected new change ID message after landing, got %q", out) |
| 409 | } |
| 410 | if len(recs[firstID].Versions) != 1 { |
| 411 | t.Errorf("landed change gained a version: %+v", recs[firstID]) |
| 412 | } |
| 413 | }) |
| 414 | |
| 415 | t.Run("trailer and --change win over live branch change", func(t *testing.T) { |
| 416 | f := newReadyFixture(t, map[string]string{"f.txt": "0\n"}) |
| 417 | liveID := strings.Repeat("k", 32) |
| 418 | trailerID := strings.Repeat("n", 32) |
| 419 | flagID := strings.Repeat("p", 32) |
| 420 | |
| 421 | f.git("checkout", "--quiet", "-b", "topic") |
| 422 | f.write("f.txt", "1\n") |
| 423 | f.git("commit", "--quiet", "-am", "live") |
| 424 | f.git("push", "--quiet", "origin", "topic") |
| 425 | f.mustValley("ready", "--change", liveID) |
| 426 | |
| 427 | // A commit carrying a Change-Id trailer uses the trailer ID, not liveID. |
| 428 | f.write("f.txt", "2\n") |
| 429 | f.git("commit", "--quiet", "-a", "-m", "with trailer", "-m", "Change-Id: "+trailerID) |
| 430 | f.git("push", "--quiet", "origin", "topic") |
| 431 | out, _ := f.mustValley("ready") |
| 432 | if strings.Contains(out, "Using change") { |
| 433 | t.Errorf("trailer commit should not report branch match: %q", out) |
| 434 | } |
| 435 | if recs := f.records(); recs[trailerID] == nil || len(recs[liveID].Versions) != 1 { |
| 436 | t.Fatalf("trailer did not win over live branch change: %+v", recs) |
| 437 | } |
| 438 | |
| 439 | // --change wins over both trailer and live branch change. |
| 440 | f.mustValley("ready", "--change", flagID) |
| 441 | if recs := f.records(); recs[flagID] == nil { |
| 442 | t.Fatalf("--change did not win: %+v", recs) |
| 443 | } |
| 444 | }) |
| 445 | |
| 446 | t.Run("--stamp and --if-active still guard branch-matched change", func(t *testing.T) { |
| 447 | f := newReadyFixture(t, map[string]string{"f.txt": "0\n"}) |
| 448 | f.git("checkout", "--quiet", "-b", "guarded") |
| 449 | f.write("f.txt", "1\n") |
| 450 | f.git("commit", "--quiet", "-am", "v1") |
| 451 | h1 := f.git("rev-parse", "HEAD") |
| 452 | f.git("push", "--quiet", "origin", "guarded") |
| 453 | f.mustValley("ready") |
| 454 | var id string |
| 455 | for k := range f.records() { |
| 456 | id = k |
| 457 | } |
| 458 | |
| 459 | // Local unpushed commit with --stamp must be refused because branch already has a change. |
| 460 | f.write("f.txt", "2\n") |
| 461 | f.git("commit", "--quiet", "-am", "v2") |
| 462 | h2 := f.git("rev-parse", "HEAD") |
| 463 | code, _, stderr := f.valley("ready", "--stamp") |
| 464 | if code == 0 || !strings.Contains(stderr, "has a record on origin already") { |
| 465 | t.Errorf("expected --stamp refusal on branch with live change, got code=%d stderr=%q", code, stderr) |
| 466 | } |
| 467 | if got := f.git("rev-parse", "HEAD"); got != h2 { |
| 468 | t.Errorf("--stamp rewrote local HEAD on refusal: %s != %s", got, h2) |
| 469 | } |
| 470 | |
| 471 | // Push h2 and check --if-active guard without --change. |
| 472 | f.git("push", "--quiet", "origin", "guarded") |
| 473 | wrong := strings.Repeat("a", 40) |
| 474 | code, _, stderr = f.valley("ready", "--if-active", wrong) |
| 475 | if code == 0 || !strings.Contains(stderr, "no version is added") { |
| 476 | t.Errorf("expected --if-active mismatch refusal, got code=%d stderr=%q", code, stderr) |
| 477 | } |
| 478 | f.mustValley("ready", "--if-active", h1) |
| 479 | if r := f.records()[id]; r.Active != 2 || r.Versions[1].Head != change.HeadPrefix+h2 { |
| 480 | t.Errorf("--if-active with active head did not advance branch-matched change: %+v", r) |
| 481 | } |
| 482 | }) |
| 483 | } |
| 484 | |
| 485 | // Stale-base warning: printed on stderr when base is behind origin/main and |
| 486 | // paths overlap, absent when paths are disjoint or base is current, and |
| 487 | // skipped without failing ready if computing the warning fails. |
| 488 | func TestReadyStaleBaseWarning(t *testing.T) { |
| 489 | baseFiles := map[string]string{ |
| 490 | "a.txt": "0\n", |
| 491 | "b.txt": "0\n", |
| 492 | "c.txt": "0\n", |
| 493 | "d.txt": "0\n", |
| 494 | "e.txt": "0\n", |
| 495 | "other.txt": "0\n", |
| 496 | } |
| 497 | |
| 498 | t.Run("no warning when base is current", func(t *testing.T) { |
| 499 | f := newReadyFixture(t, baseFiles) |
| 500 | f.git("checkout", "--quiet", "-b", "current") |
| 501 | f.write("a.txt", "1\n") |
| 502 | f.git("commit", "--quiet", "-am", "edit a") |
| 503 | f.git("push", "--quiet", "origin", "current") |
| 504 | _, stderr := f.mustValley("ready") |
| 505 | if stderr != "" { |
| 506 | t.Errorf("expected no warning on current base, got %q", stderr) |
| 507 | } |
| 508 | if len(f.records()) != 1 { |
| 509 | t.Fatalf("expected 1 record, got %d", len(f.records())) |
| 510 | } |
| 511 | }) |
| 512 | |
| 513 | t.Run("no warning when base is behind but paths do not overlap", func(t *testing.T) { |
| 514 | f := newReadyFixture(t, baseFiles) |
| 515 | oldMain := f.git("rev-parse", "main") |
| 516 | f.write("other.txt", "main\n") |
| 517 | f.git("commit", "--quiet", "-am", "main edits other") |
| 518 | f.git("push", "--quiet", "origin", "main") |
| 519 | |
| 520 | f.git("checkout", "--quiet", "-b", "disjoint", oldMain) |
| 521 | f.write("a.txt", "branch\n") |
| 522 | f.git("commit", "--quiet", "-am", "branch edits a") |
| 523 | f.git("push", "--quiet", "origin", "disjoint") |
| 524 | |
| 525 | _, stderr := f.mustValley("ready") |
| 526 | if stderr != "" { |
| 527 | t.Errorf("expected no warning for disjoint paths, got %q", stderr) |
| 528 | } |
| 529 | if len(f.records()) != 1 { |
| 530 | t.Fatalf("expected 1 record, got %d", len(f.records())) |
| 531 | } |
| 532 | }) |
| 533 | |
| 534 | t.Run("warns when base is behind and one path overlaps", func(t *testing.T) { |
| 535 | f := newReadyFixture(t, baseFiles) |
| 536 | oldMain := f.git("rev-parse", "main") |
| 537 | f.write("a.txt", "main\n") |
| 538 | f.git("commit", "--quiet", "-am", "main edits a") |
| 539 | f.git("push", "--quiet", "origin", "main") |
| 540 | |
| 541 | f.git("checkout", "--quiet", "-b", "overlap-one", oldMain) |
| 542 | f.write("a.txt", "branch\n") |
| 543 | f.git("commit", "--quiet", "-am", "branch edits a") |
| 544 | f.git("push", "--quiet", "origin", "overlap-one") |
| 545 | |
| 546 | stdout, stderr := f.mustValley("ready") |
| 547 | recs := f.records() |
| 548 | if len(recs) != 1 { |
| 549 | t.Fatalf("expected ready to write record despite warning, got %d", len(recs)) |
| 550 | } |
| 551 | var id string |
| 552 | for k := range recs { |
| 553 | id = k |
| 554 | } |
| 555 | handle := id[:4] |
| 556 | if !strings.Contains(stdout, handle+" v1 ready") { |
| 557 | t.Errorf("unexpected stdout: %q", stdout) |
| 558 | } |
| 559 | for _, want := range []string{ |
| 560 | "valley: warning:", |
| 561 | "a.txt", |
| 562 | "restack is likely to conflict", |
| 563 | "valley rebase " + handle, |
| 564 | "git rebase origin/main", |
| 565 | } { |
| 566 | if !strings.Contains(stderr, want) { |
| 567 | t.Errorf("stale-base warning missing %q:\n%s", want, stderr) |
| 568 | } |
| 569 | } |
| 570 | }) |
| 571 | |
| 572 | t.Run("warns with first three paths and a count when many overlap", func(t *testing.T) { |
| 573 | f := newReadyFixture(t, baseFiles) |
| 574 | oldMain := f.git("rev-parse", "main") |
| 575 | for _, name := range []string{"a.txt", "b.txt", "c.txt", "d.txt", "e.txt"} { |
| 576 | f.write(name, "main\n") |
| 577 | } |
| 578 | f.git("commit", "--quiet", "-am", "main edits five") |
| 579 | f.git("push", "--quiet", "origin", "main") |
| 580 | |
| 581 | f.git("checkout", "--quiet", "-b", "overlap-many", oldMain) |
| 582 | for _, name := range []string{"a.txt", "b.txt", "c.txt", "d.txt", "e.txt"} { |
| 583 | f.write(name, "branch\n") |
| 584 | } |
| 585 | f.git("commit", "--quiet", "-am", "branch edits five") |
| 586 | f.git("push", "--quiet", "origin", "overlap-many") |
| 587 | |
| 588 | _, stderr := f.mustValley("ready") |
| 589 | if len(f.records()) != 1 { |
| 590 | t.Fatalf("expected 1 record, got %d", len(f.records())) |
| 591 | } |
| 592 | if !strings.Contains(stderr, "a.txt, b.txt, c.txt and 2 more") { |
| 593 | t.Errorf("expected 3 paths + count in warning, got:\n%s", stderr) |
| 594 | } |
| 595 | if strings.Contains(stderr, "d.txt") || strings.Contains(stderr, "e.txt") { |
| 596 | t.Errorf("warning should not list paths past the preview limit:\n%s", stderr) |
| 597 | } |
| 598 | }) |
| 599 | |
| 600 | t.Run("failure to compute warning does not fail ready", func(t *testing.T) { |
| 601 | f := newReadyFixture(t, baseFiles) |
| 602 | oldMain := f.git("rev-parse", "main") |
| 603 | f.write("a.txt", "main\n") |
| 604 | f.git("commit", "--quiet", "-am", "main edits a") |
| 605 | f.git("push", "--quiet", "origin", "main") |
| 606 | |
| 607 | f.git("checkout", "--quiet", "-b", "diff-fails", oldMain) |
| 608 | f.write("a.txt", "branch\n") |
| 609 | f.git("commit", "--quiet", "-am", "branch edits a") |
| 610 | f.git("push", "--quiet", "origin", "diff-fails") |
| 611 | |
| 612 | realGit, err := exec.LookPath("git") |
| 613 | if err != nil { |
| 614 | t.Fatal(err) |
| 615 | } |
| 616 | bin := t.TempDir() |
| 617 | shim := "#!/bin/sh\nfor a in \"$@\"; do\n case \"$a\" in\n diff) exit 1 ;;\n -*) ;;\n *) break ;;\n esac\ndone\nexec '" + realGit + "' \"$@\"\n" |
| 618 | if err := os.WriteFile(filepath.Join(bin, "git"), []byte(shim), 0o755); err != nil { |
| 619 | t.Fatal(err) |
| 620 | } |
| 621 | t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 622 | |
| 623 | _, stderr := f.mustValley("ready") |
| 624 | if stderr != "" { |
| 625 | t.Errorf("expected warning skipped when diff fails, got %q", stderr) |
| 626 | } |
| 627 | if len(f.records()) != 1 { |
| 628 | t.Fatalf("expected ready to succeed when diff fails, got %d records", len(f.records())) |
| 629 | } |
| 630 | }) |
| 631 | } |