| 1 | package prose |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // TestSettle: an edit is applied when the worktree, or the head commit's |
| 11 | // message, has its new text and not its old; skipped edits get no outcome. |
| 12 | func TestSettle(t *testing.T) { |
| 13 | dir := t.TempDir() |
| 14 | if err := os.WriteFile(filepath.Join(dir, "README.md"), []byte("Install it first.\nRun it from main only.\n"), 0o644); err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | for _, args := range [][]string{{"init", "-q"}, {"add", "-A"}, {"-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "Say where it runs"}} { |
| 18 | if out, err := exec.Command("git", append([]string{"-C", dir}, args...)...).CombinedOutput(); err != nil { |
| 19 | t.Fatalf("git %v: %v\n%s", args, err, out) |
| 20 | } |
| 21 | } |
| 22 | r := &Result{Edits: []Edit{ |
| 23 | {File: "README.md", Old: "In order to install it, do so first.", New: "Install it first."}, |
| 24 | {File: "README.md", Old: "Run it from main.", New: "Run it anywhere."}, |
| 25 | {File: CommitFile, Old: "Say where the tool runs", New: "Say where it runs"}, |
| 26 | {File: "README.md", Old: "x", New: "Run it from main only.", Skipped: "no change"}, |
| 27 | {File: "README.md", Old: "Install it first.", New: "Run it from main only."}, |
| 28 | }} |
| 29 | Settle(dir, r) |
| 30 | for i, want := range []string{"applied", "rejected", "applied", "", "rejected"} { |
| 31 | if got := r.Edits[i].Outcome; got != want { |
| 32 | t.Errorf("edit %d outcome = %q, want %q", i+1, got, want) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // TestBuildSplicesFromTheOriginal: each edit replaces its own text in the |
| 38 | // original, not text an earlier edit wrote, and an overlapping edit is left |
| 39 | // out. |
| 40 | func TestBuildSplicesFromTheOriginal(t *testing.T) { |
| 41 | in := &input{message: "one two three four"} |
| 42 | r := &Result{Edits: []Edit{ |
| 43 | {File: CommitFile, Old: "one", New: "four"}, |
| 44 | {File: CommitFile, Old: "four", New: "five"}, |
| 45 | {File: CommitFile, Old: "two three", New: "2 3"}, |
| 46 | {File: CommitFile, Old: "three", New: "3"}, |
| 47 | }} |
| 48 | if _, msg := build("", in, r); msg != "four 2 3 five\n" { |
| 49 | t.Errorf("message = %q, want %q", msg, "four 2 3 five\n") |
| 50 | } |
| 51 | if r.Edits[3].Skipped == "" { |
| 52 | t.Error("the overlapping edit wasn't skipped") |
| 53 | } |
| 54 | } |