| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // forbidTerms registers the sandbox repo as project example-project and |
| 14 | // writes a host config whose forbidden_terms has an entry under key. |
| 15 | func (h *Harness) forbidTerms(key string) { |
| 16 | h.t.Helper() |
| 17 | h.RegisterProject("acme/example-project", h.RepoDir) |
| 18 | cfg := map[string]any{"forbidden_terms": map[string]any{ |
| 19 | key: map[string]any{"terms": []string{"example-host", "10.1.2."}, "allow_paths": []string{"docs/hosts"}}, |
| 20 | }} |
| 21 | data, _ := json.Marshal(cfg) |
| 22 | if err := os.MkdirAll(filepath.Join(h.Home, ".klaus"), 0o755); err != nil { |
| 23 | h.t.Fatal(err) |
| 24 | } |
| 25 | if err := os.WriteFile(filepath.Join(h.Home, ".klaus", "config.json"), data, 0o644); err != nil { |
| 26 | h.t.Fatal(err) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // stubReviewer makes _pre-review's peer review a claude stub that finds |
| 31 | // nothing, and returns the file the stub creates when it runs. It turns the |
| 32 | // prose pass off, which would otherwise ask the real agy or codex on PATH. |
| 33 | func (h *Harness) stubReviewer() string { |
| 34 | h.t.Helper() |
| 35 | marker := filepath.Join(h.E2EDir, "reviewed") |
| 36 | h.WriteStub("claude", "#!/usr/bin/env bash\ncat >/dev/null\ntouch "+marker+"\necho '{\"findings\": [], \"summary\": \"No issues found.\"}'\n") |
| 37 | h.AmendRepoConfig(map[string]any{ |
| 38 | "backends": map[string]any{"claude": map[string]any{"review_backend": "claude"}}, |
| 39 | "pre_review": map[string]any{"prose": map[string]any{"enabled": false}}, |
| 40 | }) |
| 41 | return marker |
| 42 | } |
| 43 | |
| 44 | // commitReadme rewrites README.md and commits it with message. |
| 45 | func (h *Harness) commitReadme(content, message string) string { |
| 46 | h.t.Helper() |
| 47 | if err := os.WriteFile(filepath.Join(h.RepoDir, "README.md"), []byte(content), 0o644); err != nil { |
| 48 | h.t.Fatal(err) |
| 49 | } |
| 50 | h.git(h.RepoDir, "commit", "-qam", message) |
| 51 | return h.GitOutput(h.RepoDir, "rev-parse", "HEAD") |
| 52 | } |
| 53 | |
| 54 | // TestPreReviewRefusesForbiddenTerms: with an entry for the repo in the host |
| 55 | // config, _pre-review fails a branch that adds a forbidden term, naming the |
| 56 | // file, line and term but not the rest of the list, before the peer |
| 57 | // reviewer sees the diff. Once the term is replaced it runs the review. |
| 58 | func TestPreReviewRefusesForbiddenTerms(t *testing.T) { |
| 59 | t.Parallel() |
| 60 | h := NewHarness(t) |
| 61 | h.forbidTerms("example-project") |
| 62 | reviewed := h.stubReviewer() |
| 63 | h.git(h.RepoDir, "checkout", "-qb", "work") |
| 64 | h.commitReadme("# widget\nDeploy to Example-Host first.\n", "add deploy note") |
| 65 | |
| 66 | res := h.RunKlaus("_pre-review") |
| 67 | if res.ExitCode != 1 { |
| 68 | t.Fatalf("_pre-review exit = %d, want 1\n%s%s", res.ExitCode, res.Stdout, res.Stderr) |
| 69 | } |
| 70 | for _, want := range []string{"README.md:2: example-host", "replace each with a neutral name", "PR creation should be blocked"} { |
| 71 | if !strings.Contains(res.Stdout, want) { |
| 72 | t.Errorf("_pre-review output lacks %q:\n%s", want, res.Stdout) |
| 73 | } |
| 74 | } |
| 75 | if strings.Contains(res.Stdout, "10.1.2.") { |
| 76 | t.Errorf("_pre-review printed a term that didn't hit:\n%s", res.Stdout) |
| 77 | } |
| 78 | if _, err := os.Stat(reviewed); err == nil { |
| 79 | t.Error("the peer reviewer ran on a diff with a forbidden term") |
| 80 | } |
| 81 | |
| 82 | h.commitReadme("# widget\nDeploy to the build host first.\n", "use a neutral name") |
| 83 | res = h.RunKlaus("_pre-review") |
| 84 | if res.ExitCode != 0 { |
| 85 | t.Fatalf("_pre-review after the fix: exit %d\n%s%s", res.ExitCode, res.Stdout, res.Stderr) |
| 86 | } |
| 87 | for _, want := range []string{"✓ no forbidden terms added", "All checks passed."} { |
| 88 | if !strings.Contains(res.Stdout, want) { |
| 89 | t.Errorf("_pre-review output lacks %q:\n%s", want, res.Stdout) |
| 90 | } |
| 91 | } |
| 92 | if _, err := os.Stat(reviewed); err != nil { |
| 93 | t.Error("the peer reviewer didn't run once the term was gone") |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // TestPreReviewIgnoresOtherReposTerms: an entry for another repo leaves |
| 98 | // _pre-review as it was: no forbidden-terms check, the review runs. |
| 99 | func TestPreReviewIgnoresOtherReposTerms(t *testing.T) { |
| 100 | t.Parallel() |
| 101 | h := NewHarness(t) |
| 102 | h.forbidTerms("acme/gadget") |
| 103 | reviewed := h.stubReviewer() |
| 104 | h.git(h.RepoDir, "checkout", "-qb", "work") |
| 105 | h.commitReadme("# widget\nDeploy to example-host first.\n", "add deploy note") |
| 106 | |
| 107 | res := h.RunKlaus("_pre-review") |
| 108 | if res.ExitCode != 0 || !strings.Contains(res.Stdout, "All checks passed.") { |
| 109 | t.Fatalf("_pre-review exit = %d, want 0 and all checks passed\n%s%s", res.ExitCode, res.Stdout, res.Stderr) |
| 110 | } |
| 111 | if strings.Contains(res.Stdout, "orbidden") { |
| 112 | t.Errorf("_pre-review checked forbidden terms for a repo without an entry:\n%s", res.Stdout) |
| 113 | } |
| 114 | if _, err := os.Stat(reviewed); err != nil { |
| 115 | t.Error("the peer reviewer didn't run") |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // TestForbiddenTermsCommand: klaus _forbidden-terms scans the range it is |
| 120 | // given, defaults to the change since origin's default branch, catches a |
| 121 | // term in a commit message, and passes a repo --repo says has no entry. |
| 122 | func TestForbiddenTermsCommand(t *testing.T) { |
| 123 | t.Parallel() |
| 124 | h := NewHarness(t) |
| 125 | h.forbidTerms("example-project") |
| 126 | c0 := h.GitOutput(h.RepoDir, "rev-parse", "HEAD") |
| 127 | c1 := h.commitReadme("# widget\nDeploy to example-host first.\n", "add deploy note") |
| 128 | c2 := h.commitReadme("# widget\nDeploy to example-host first.\nThen check it.\n", "add a check") |
| 129 | h.git(h.RepoDir, "commit", "-q", "--allow-empty", "-m", "Move EXAMPLE-HOST behind the proxy") |
| 130 | c3 := h.GitOutput(h.RepoDir, "rev-parse", "HEAD") |
| 131 | |
| 132 | for _, tc := range []struct { |
| 133 | name string |
| 134 | args []string |
| 135 | exit int |
| 136 | want string |
| 137 | }{ |
| 138 | {"range that adds a term", []string{c0, c1}, 1, "README.md:2: example-host"}, |
| 139 | {"range after it", []string{c1, c2}, 0, "✓ no forbidden terms added"}, |
| 140 | {"commit message", []string{c2, c3}, 1, "commit " + c3[:7]}, |
| 141 | {"default range", nil, 1, "README.md:2: example-host"}, |
| 142 | {"repo without an entry", []string{"--repo", "acme/gadget", c0, c1}, 0, "No forbidden terms are configured for this repo."}, |
| 143 | } { |
| 144 | t.Run(tc.name, func(t *testing.T) { |
| 145 | res := h.RunKlaus(append([]string{"_forbidden-terms"}, tc.args...)...) |
| 146 | if res.ExitCode != tc.exit || !strings.Contains(res.Stdout, tc.want) { |
| 147 | t.Errorf("_forbidden-terms %v: exit %d, want %d with %q\n%s%s", tc.args, res.ExitCode, tc.exit, tc.want, res.Stdout, res.Stderr) |
| 148 | } |
| 149 | }) |
| 150 | } |
| 151 | } |