| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "reflect" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "the-valley/flow/internal/config" |
| 12 | ) |
| 13 | |
| 14 | // forbiddenScanRepo is a real git repo whose main already holds a term (in |
| 15 | // hosts.md) and a dotted address (in net.md), checked out on branch work. |
| 16 | func forbiddenScanRepo(t *testing.T) string { |
| 17 | t.Helper() |
| 18 | dir := t.TempDir() |
| 19 | runGitT(t, dir, "init", "-q", "-b", "main") |
| 20 | runGitT(t, dir, "config", "user.email", "t@example.com") |
| 21 | runGitT(t, dir, "config", "user.name", "T") |
| 22 | writeScanFile(t, dir, "hosts.md", "first\nexample-host is old\nlast\n") |
| 23 | writeScanFile(t, dir, "net.md", "one\n") |
| 24 | runGitT(t, dir, "add", "-A") |
| 25 | runGitT(t, dir, "commit", "-qm", "init") |
| 26 | runGitT(t, dir, "checkout", "-qb", "work") |
| 27 | return dir |
| 28 | } |
| 29 | |
| 30 | func writeScanFile(t *testing.T, dir, name, content string) { |
| 31 | t.Helper() |
| 32 | p := filepath.Join(dir, name) |
| 33 | if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | if err := os.WriteFile(p, []byte(content), 0o644); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func headShort(t *testing.T, dir string) string { |
| 42 | t.Helper() |
| 43 | out, err := exec.Command("git", "-C", dir, "log", "-1", "--format=%h").Output() |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | return strings.TrimSpace(string(out)) |
| 48 | } |
| 49 | |
| 50 | func TestScanForbiddenTerms(t *testing.T) { |
| 51 | entry := config.ForbiddenTermsConfig{ |
| 52 | Terms: []string{"example-host", "example-project", "10.1.2."}, |
| 53 | AllowPaths: []string{"docs/allowed", "exempt.md", "docs/host notes.md"}, |
| 54 | } |
| 55 | tests := []struct { |
| 56 | name string |
| 57 | change func(t *testing.T, dir string) // commits on work |
| 58 | want func(dir string) []string |
| 59 | }{ |
| 60 | { |
| 61 | name: "added line", |
| 62 | change: func(t *testing.T, dir string) { |
| 63 | writeScanFile(t, dir, "net.md", "one\nreach example-host here\n") |
| 64 | runGitT(t, dir, "commit", "-qam", "add a line") |
| 65 | }, |
| 66 | want: func(string) []string { return []string{"net.md:2: example-host"} }, |
| 67 | }, |
| 68 | { |
| 69 | name: "deleted line", |
| 70 | change: func(t *testing.T, dir string) { |
| 71 | writeScanFile(t, dir, "hosts.md", "first\nlast\n") |
| 72 | runGitT(t, dir, "commit", "-qam", "drop the old line") |
| 73 | }, |
| 74 | }, |
| 75 | { |
| 76 | name: "allow-listed paths", |
| 77 | change: func(t *testing.T, dir string) { |
| 78 | writeScanFile(t, dir, "exempt.md", "example-host\n") |
| 79 | writeScanFile(t, dir, "docs/allowed/a.md", "example-host\n") |
| 80 | writeScanFile(t, dir, "docs/allowed-not/a.md", "x\nexample-host\n") |
| 81 | runGitT(t, dir, "add", "-A") |
| 82 | runGitT(t, dir, "commit", "-qm", "exempt files") |
| 83 | }, |
| 84 | // A directory prefix covers what is under it, not its namesakes. |
| 85 | want: func(string) []string { return []string{"docs/allowed-not/a.md:2: example-host"} }, |
| 86 | }, |
| 87 | { |
| 88 | name: "file moved out of allow_paths", |
| 89 | change: func(t *testing.T, dir string) { |
| 90 | runGitT(t, dir, "checkout", "-q", "main") |
| 91 | writeScanFile(t, dir, "exempt.md", "example-host\n") |
| 92 | runGitT(t, dir, "add", "-A") |
| 93 | runGitT(t, dir, "commit", "-qm", "exempt file") |
| 94 | runGitT(t, dir, "checkout", "-q", "work") |
| 95 | runGitT(t, dir, "merge", "-q", "--ff-only", "main") |
| 96 | runGitT(t, dir, "mv", "exempt.md", "moved.md") |
| 97 | runGitT(t, dir, "commit", "-qm", "move it") |
| 98 | }, |
| 99 | want: func(string) []string { return []string{"moved.md:1: example-host"} }, |
| 100 | }, |
| 101 | { |
| 102 | name: "paths with spaces", |
| 103 | change: func(t *testing.T, dir string) { |
| 104 | writeScanFile(t, dir, "docs/host notes.md", "example-host\n") |
| 105 | writeScanFile(t, dir, "my notes.md", "example-host\n") |
| 106 | runGitT(t, dir, "add", "-A") |
| 107 | runGitT(t, dir, "commit", "-qm", "notes") |
| 108 | }, |
| 109 | want: func(string) []string { return []string{"my notes.md:1: example-host"} }, |
| 110 | }, |
| 111 | { |
| 112 | name: "file git won't diff", |
| 113 | change: func(t *testing.T, dir string) { |
| 114 | writeScanFile(t, dir, ".gitattributes", "*.dat -diff\n") |
| 115 | writeScanFile(t, dir, "hosts.dat", "a\x00b\nexample-host\n") |
| 116 | runGitT(t, dir, "add", "-A") |
| 117 | runGitT(t, dir, "commit", "-qm", "data") |
| 118 | }, |
| 119 | want: func(string) []string { return []string{"hosts.dat:2: example-host"} }, |
| 120 | }, |
| 121 | { |
| 122 | name: "commit message", |
| 123 | change: func(t *testing.T, dir string) { |
| 124 | writeScanFile(t, dir, "net.md", "one\ntwo\n") |
| 125 | runGitT(t, dir, "commit", "-qam", "tidy\n\nFor the example-project rollout.") |
| 126 | }, |
| 127 | want: func(dir string) []string { return []string{"commit " + headShort(t, dir) + ": example-project"} }, |
| 128 | }, |
| 129 | { |
| 130 | name: "case-insensitive", |
| 131 | change: func(t *testing.T, dir string) { |
| 132 | writeScanFile(t, dir, "net.md", "one\nThe EXAMPLE-Host box\n") |
| 133 | runGitT(t, dir, "commit", "-qam", "shout") |
| 134 | }, |
| 135 | want: func(string) []string { return []string{"net.md:2: example-host"} }, |
| 136 | }, |
| 137 | { |
| 138 | name: "dots are literal", |
| 139 | change: func(t *testing.T, dir string) { |
| 140 | writeScanFile(t, dir, "net.md", "one\nroute 10.1.20.0/24\nvia 10x1x2x3\ngateway 10.1.2.7\n") |
| 141 | runGitT(t, dir, "commit", "-qam", "routes") |
| 142 | }, |
| 143 | want: func(string) []string { return []string{"net.md:4: 10.1.2."} }, |
| 144 | }, |
| 145 | { |
| 146 | name: "added file name", |
| 147 | change: func(t *testing.T, dir string) { |
| 148 | writeScanFile(t, dir, "notes/example-host.md", "clean\n") |
| 149 | runGitT(t, dir, "add", "-A") |
| 150 | runGitT(t, dir, "commit", "-qm", "notes") |
| 151 | }, |
| 152 | want: func(string) []string { return []string{"notes/example-host.md: example-host (file name)"} }, |
| 153 | }, |
| 154 | { |
| 155 | name: "added line that looks like a file header", |
| 156 | change: func(t *testing.T, dir string) { |
| 157 | writeScanFile(t, dir, "net.md", "one\n++ b/example-host\n") |
| 158 | runGitT(t, dir, "commit", "-qam", "odd line") |
| 159 | }, |
| 160 | want: func(string) []string { return []string{"net.md:2: example-host"} }, |
| 161 | }, |
| 162 | } |
| 163 | for _, tt := range tests { |
| 164 | t.Run(tt.name, func(t *testing.T) { |
| 165 | dir := forbiddenScanRepo(t) |
| 166 | tt.change(t, dir) |
| 167 | hits, err := scanForbiddenTerms(dir, "main", "HEAD", []config.ForbiddenTermsConfig{entry}) |
| 168 | if err != nil { |
| 169 | t.Fatalf("scanForbiddenTerms: %v", err) |
| 170 | } |
| 171 | var got []string |
| 172 | for _, h := range hits { |
| 173 | got = append(got, h.String()) |
| 174 | } |
| 175 | var want []string |
| 176 | if tt.want != nil { |
| 177 | want = tt.want(dir) |
| 178 | } |
| 179 | if !reflect.DeepEqual(got, want) { |
| 180 | t.Errorf("hits = %q, want %q", got, want) |
| 181 | } |
| 182 | }) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // An entry applies to a repo by its owner/repo origin, or by its registered |
| 187 | // project name from any worktree of that project's checkout. |
| 188 | func TestForbiddenTermsForMatchesRepo(t *testing.T) { |
| 189 | root := t.TempDir() |
| 190 | widget := initRepo(t, filepath.Join(root, "widget"), "git@github.com:acme/widget.git") |
| 191 | runGitT(t, widget, "-c", "user.email=t@example.com", "-c", "user.name=T", "commit", "-q", "--allow-empty", "-m", "init") |
| 192 | wt := filepath.Join(root, "wt") |
| 193 | runGitT(t, widget, "worktree", "add", "-q", "-b", "work", wt) |
| 194 | local := initRepo(t, filepath.Join(root, "gizmo"), filepath.Join(root, "gizmo-origin.git")) |
| 195 | writeRegistry(t, withTempHome(t), map[string]string{"example-project": local, "widget": widget}) |
| 196 | |
| 197 | terms := func(ts ...string) config.ForbiddenTermsConfig { return config.ForbiddenTermsConfig{Terms: ts} } |
| 198 | tests := []struct { |
| 199 | name string |
| 200 | entries map[string]config.ForbiddenTermsConfig |
| 201 | dir string |
| 202 | repoRef string |
| 203 | want []config.ForbiddenTermsConfig |
| 204 | }{ |
| 205 | {"owner/repo origin", map[string]config.ForbiddenTermsConfig{"Acme/Widget": terms("a")}, wt, "", []config.ForbiddenTermsConfig{terms("a")}}, |
| 206 | {"project of the worktree", map[string]config.ForbiddenTermsConfig{"widget": terms("a")}, wt, "", []config.ForbiddenTermsConfig{terms("a")}}, |
| 207 | {"both keys apply", map[string]config.ForbiddenTermsConfig{"acme/widget": terms("a"), "widget": terms("b")}, wt, "", []config.ForbiddenTermsConfig{terms("a"), terms("b")}}, |
| 208 | {"project without a GitHub origin", map[string]config.ForbiddenTermsConfig{"example-project": terms("a")}, local, "", []config.ForbiddenTermsConfig{terms("a")}}, |
| 209 | {"other repos' entries", map[string]config.ForbiddenTermsConfig{"acme/gadget": terms("a"), "example-project": terms("b")}, wt, "", nil}, |
| 210 | {"--repo by project name", map[string]config.ForbiddenTermsConfig{"acme/widget": terms("a")}, local, "widget", []config.ForbiddenTermsConfig{terms("a")}}, |
| 211 | {"--repo by owner/repo", map[string]config.ForbiddenTermsConfig{"widget": terms("a")}, local, "acme/widget", []config.ForbiddenTermsConfig{terms("a")}}, |
| 212 | {"--repo unregistered", map[string]config.ForbiddenTermsConfig{"elsewhere": terms("a")}, local, "elsewhere", []config.ForbiddenTermsConfig{terms("a")}}, |
| 213 | } |
| 214 | for _, tt := range tests { |
| 215 | t.Run(tt.name, func(t *testing.T) { |
| 216 | got := forbiddenTermsFor(config.Config{ForbiddenTerms: tt.entries}, tt.dir, tt.repoRef) |
| 217 | if !reflect.DeepEqual(got, tt.want) { |
| 218 | t.Errorf("forbiddenTermsFor = %+v, want %+v", got, tt.want) |
| 219 | } |
| 220 | }) |
| 221 | } |
| 222 | } |