| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestApprovalSubjectBodyShowsClippedCommandInFull(t *testing.T) { |
| 9 | full := "bash -lc 'cd /very/long/path/to/project && go test ./... -run TestSomethingWithAVeryLongName -count=1 -race'" |
| 10 | preview := truncateSubject(full, 80) |
| 11 | if preview == full { |
| 12 | t.Fatal("fixture must be long enough to clip at this width") |
| 13 | } |
| 14 | |
| 15 | body := approvalSubjectBody(full, preview, 80) |
| 16 | if body == "" { |
| 17 | t.Fatal("a clipped command must be expanded below the banner") |
| 18 | } |
| 19 | flattened := strings.ReplaceAll(body, "\n", "") |
| 20 | if !strings.Contains(flattened, "-race") { |
| 21 | t.Errorf("the tail of the command must survive wrapping, got %q", body) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestApprovalSubjectBodyStaysEmptyForShortCommands(t *testing.T) { |
| 26 | full := "git status" |
| 27 | if body := approvalSubjectBody(full, full, 80); body != "" { |
| 28 | t.Errorf("a command the banner already shows must not be repeated, got %q", body) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestApprovalSubjectBodyIsBounded(t *testing.T) { |
| 33 | full := strings.Repeat("echo aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && ", 200) |
| 34 | body := approvalSubjectBody(full, truncateSubject(full, 60), 60) |
| 35 | lines := strings.Split(body, "\n") |
| 36 | if len(lines) > maxApprovalSubjectLines { |
| 37 | t.Fatalf("expanded command must stay bounded, got %d lines", len(lines)) |
| 38 | } |
| 39 | if !strings.HasSuffix(lines[len(lines)-1], "…") { |
| 40 | t.Errorf("a command longer than the bound must end with an ellipsis, got %q", lines[len(lines)-1]) |
| 41 | } |
| 42 | } |
| 43 |