| 1 | package gitguard |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "sort" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "github.com/sylvinus/agent-vm/internal/bashref" |
| 15 | "github.com/sylvinus/agent-vm/internal/mounts" |
| 16 | "github.com/sylvinus/agent-vm/internal/paths" |
| 17 | ) |
| 18 | |
| 19 | type box struct { |
| 20 | t *testing.T |
| 21 | root, home, proj, sb string |
| 22 | state, volumes, xdg string |
| 23 | } |
| 24 | |
| 25 | func newBox(t *testing.T) *box { |
| 26 | t.Helper() |
| 27 | if _, err := exec.LookPath("git"); err != nil { |
| 28 | t.Skip("no git") |
| 29 | } |
| 30 | root, _ := filepath.EvalSymlinks(t.TempDir()) |
| 31 | |
| 32 | |
| 33 | root = paths.Host(root) |
| 34 | b := &box{t: t, root: root, home: root + "/home", proj: root + "/home/proj", sb: root + "/sb"} |
| 35 | b.state = b.home + "/.agent-vm" |
| 36 | b.volumes = b.state + "/volumes" |
| 37 | for _, d := range []string{b.proj, b.state, b.sb} { |
| 38 | os.MkdirAll(d, 0o755) |
| 39 | } |
| 40 | b.git(b.proj, "init", "-q") |
| 41 | return b |
| 42 | } |
| 43 | |
| 44 | func (b *box) env() []string { |
| 45 | e := []string{"HOME=" + b.home, "GIT_CONFIG_NOSYSTEM=1", "AGENT_VM_STATE_DIR=" + b.state} |
| 46 | if b.xdg != "" { |
| 47 | e = append(e, "XDG_CONFIG_HOME="+b.xdg) |
| 48 | } |
| 49 | return e |
| 50 | } |
| 51 | |
| 52 | func (b *box) git(dir string, args ...string) { |
| 53 | b.t.Helper() |
| 54 | cmd := exec.Command("git", append([]string{"-C", dir}, args...)...) |
| 55 | cmd.Env = append([]string{"PATH=" + os.Getenv("PATH")}, b.env()...) |
| 56 | if out, err := cmd.CombinedOutput(); err != nil { |
| 57 | b.t.Fatalf("git %q: %v %s", args, err, out) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | func (b *box) setRaw(section, key, value string) { |
| 65 | b.t.Helper() |
| 66 | f, err := os.OpenFile(b.proj+"/.git/config", os.O_APPEND|os.O_WRONLY, 0o644) |
| 67 | if err != nil { |
| 68 | b.t.Fatal(err) |
| 69 | } |
| 70 | defer f.Close() |
| 71 | if _, err := f.WriteString("[" + section + "]\n\t" + key + " = \"" + value + "\"\n"); err != nil { |
| 72 | b.t.Fatal(err) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func (b *box) write(p, content string) { |
| 77 | os.MkdirAll(filepath.Dir(p), 0o755) |
| 78 | os.WriteFile(p, []byte(content), 0o755) |
| 79 | } |
| 80 | |
| 81 | func (b *box) vol(lines ...string) { b.write(b.volumes, strings.Join(lines, "\n")+"\n") } |
| 82 | |
| 83 | |
| 84 | func (b *box) useEnv() { |
| 85 | b.t.Helper() |
| 86 | for _, kv := range b.env() { |
| 87 | k, v, _ := strings.Cut(kv, "=") |
| 88 | b.t.Setenv(k, v) |
| 89 | } |
| 90 | |
| 91 | b.t.Setenv("GIT_CONFIG_GLOBAL", "") |
| 92 | os.Unsetenv("GIT_CONFIG_GLOBAL") |
| 93 | if b.xdg == "" { |
| 94 | b.t.Setenv("XDG_CONFIG_HOME", "") |
| 95 | os.Unsetenv("XDG_CONFIG_HOME") |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | func (b *box) compare(name string) []string { |
| 101 | b.t.Helper() |
| 102 | b.useEnv() |
| 103 | refs := mounts.Refs(b.home, bashref.Root(), b.state, b.home+"/.lima") |
| 104 | shares := Shares(b.proj, mounts.RWDirs(mounts.Entries(b.volumes, b.proj, b.home, refs, io.Discard))) |
| 105 | got := Scan(context.Background(), shares, b.home).Lines() |
| 106 | r := bashref.Run(b.t, "", b.env(), "_agent_vm_git_scan", b.proj) |
| 107 | |
| 108 | r.Stdout = strings.ReplaceAll(r.Stdout, "repositories in .:", "repositories in the project:") |
| 109 | want := strings.Split(strings.TrimSuffix(r.Stdout, "\n"), "\n") |
| 110 | if r.Stdout == "" { |
| 111 | want = nil |
| 112 | } |
| 113 | g := append([]string(nil), got...) |
| 114 | sort.Strings(g) |
| 115 | sort.Strings(want) |
| 116 | if name == "other capitals" && paths.NoCase() { |
| 117 | |
| 118 | |
| 119 | |
| 120 | return got |
| 121 | } |
| 122 | if strings.Join(g, "\n") != strings.Join(want, "\n") { |
| 123 | b.t.Errorf("%s:\n go:\n %s\n bash:\n %s", name, strings.Join(g, "\n "), strings.Join(want, "\n ")) |
| 124 | } |
| 125 | return got |
| 126 | } |
| 127 | |
| 128 | |
| 129 | func TestRisksUnderDeclinedName(t *testing.T) { |
| 130 | b := newBox(t) |
| 131 | b.git(b.proj, "config", "core.hooksPath", "tools/hooks") |
| 132 | b.git(b.proj, "config", "core.fsmonitor", "tools/fsmon.sh") |
| 133 | |
| 134 | |
| 135 | os.MkdirAll(b.proj+"/tools", 0o755) |
| 136 | os.MkdirAll(b.proj+"/scripts", 0o755) |
| 137 | os.Symlink("../scripts/real.sh", b.proj+"/tools/relay.sh") |
| 138 | b.git(b.proj, "config", "core.pager", "tools/relay.sh") |
| 139 | b.compare("declined") |
| 140 | r := Scan(context.Background(), Shares(b.proj, nil), b.home) |
| 141 | accepted := r.Risks([]string{".git", ".hg", "tools"}) |
| 142 | if strings.Join(accepted, "\n") != "core.pager = tools/relay.sh" { |
| 143 | t.Errorf("tools accepted: %q", accepted) |
| 144 | } |
| 145 | declined := strings.Join(r.Risks(mounts.BaseNames), "\n") |
| 146 | if !strings.Contains(declined, "core.fsmonitor = tools/fsmon.sh") || !strings.Contains(declined, "core.pager = tools/relay.sh") { |
| 147 | t.Errorf("tools declined: %q", declined) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | func TestScanMiddleLink(t *testing.T) { |
| 155 | b := newBox(t) |
| 156 | os.MkdirAll(b.sb+"/outside", 0o755) |
| 157 | os.MkdirAll(b.sb+"/other/b", 0o755) |
| 158 | os.Symlink(b.proj+"/lnk", b.sb+"/outside/a") |
| 159 | os.Symlink(b.sb+"/other", b.proj+"/lnk") |
| 160 | b.git(b.proj, "config", "core.hooksPath", b.sb+"/outside/a/b") |
| 161 | b.git(b.proj, "config", "core.fsmonitor", b.sb+"/outside/a/mon.sh") |
| 162 | r := Scan(context.Background(), Shares(b.proj, nil), b.home) |
| 163 | if strings.Join(r.Hooks, "\n") != "lnk\tlnk" { |
| 164 | t.Errorf("hooks: %q", r.Hooks) |
| 165 | } |
| 166 | if risks := r.Risks(mounts.BaseNames); len(risks) != 1 || risks[0] != "core.fsmonitor = "+b.sb+"/outside/a/mon.sh" { |
| 167 | t.Errorf("risks: %q", risks) |
| 168 | } |
| 169 | |
| 170 | if risks := r.Risks(r.Names()); len(risks) != 0 { |
| 171 | t.Errorf("risks with the names: %q", risks) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | func TestScanHooksInVolume(t *testing.T) { |
| 180 | b := newBox(t) |
| 181 | os.MkdirAll(b.proj+"/.husky/_", 0o755) |
| 182 | b.git(b.proj, "config", "core.hooksPath", ".husky/_") |
| 183 | r := Scan(context.Background(), Shares(b.proj, []string{b.proj + "/.husky"}), b.home) |
| 184 | if strings.Join(r.Hooks, "\n") != ".husky/_\t." { |
| 185 | t.Errorf("hooks: %q", r.Hooks) |
| 186 | } |
| 187 | |
| 188 | r = Scan(context.Background(), Shares(b.proj, nil), b.home) |
| 189 | if strings.Join(r.Hooks, "\n") != ".husky/_\t.husky/_" { |
| 190 | t.Errorf("hooks without the volume: %q", r.Hooks) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
| 196 | func TestScanCommandKeys(t *testing.T) { |
| 197 | b := newBox(t) |
| 198 | keys := []string{ |
| 199 | "mergetool.vimdiff.path", "difftool.vimdiff.path", "browser.firefox.path", "man.konqueror.path", |
| 200 | "guitool.lint.cmd", "trailer.sign.cmd", "trailer.x.command", "submodule.s.update", |
| 201 | "remote.o.uploadpack", "remote.o.receivepack", "tar.tgz.command", "instaweb.httpd", |
| 202 | "sendemail.smtpServer", "sendemail.sendmailCmd", "sendemail.toCmd", "sendemail.ccCmd", |
| 203 | "sendemail.headerCmd", "sendemail.work.toCmd", "gpg.ssh.defaultKeyCommand", |
| 204 | "hook.lint.command", "imap.tunnel", "lfs.customtransfer.agent.path", "lfs.extension.ext.clean", |
| 205 | "lfs.extension.ext.smudge", "init.templateDir", |
| 206 | } |
| 207 | for _, k := range keys { |
| 208 | v := "tools/run.sh" |
| 209 | if k == "submodule.s.update" { |
| 210 | v = "!" + v |
| 211 | } |
| 212 | b.git(b.proj, "config", k, v) |
| 213 | } |
| 214 | |
| 215 | b.git(b.proj, "config", "sendemail.work.smtpServer", "smtp.example.com") |
| 216 | risks := Scan(context.Background(), Shares(b.proj, nil), b.home).Risks(mounts.BaseNames) |
| 217 | if len(risks) != len(keys) { |
| 218 | t.Errorf("%d risks for %d keys:\n%s", len(risks), len(keys), strings.Join(risks, "\n")) |
| 219 | } |
| 220 | |
| 221 | all := strings.ToLower(strings.Join(risks, "\n")) |
| 222 | for _, k := range keys { |
| 223 | if !strings.Contains(all, strings.ToLower(k)+" = ") { |
| 224 | t.Errorf("missing %s", k) |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | func TestScanBareArgument(t *testing.T) { |
| 233 | b := newBox(t) |
| 234 | b.write(b.proj+"/build.sh", "#!/bin/sh\n") |
| 235 | b.git(b.proj, "config", "alias.b", "!bash build.sh") |
| 236 | b.git(b.proj, "config", "alias.lg", "!git log --graph") |
| 237 | b.git(b.proj, "config", "core.fsmonitor", "fsmon.sh") |
| 238 | risks := strings.Join(Scan(context.Background(), Shares(b.proj, nil), b.home).Risks(mounts.BaseNames), "\n") |
| 239 | if risks != "alias.b = !bash build.sh" { |
| 240 | t.Errorf("risks:\n%s", risks) |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | func TestScanNonASCII(t *testing.T) { |
| 247 | b := newBox(t) |
| 248 | dir := b.proj + "/été/cfg" |
| 249 | os.MkdirAll(dir, 0o755) |
| 250 | b.write(dir+"/first.gitconfig", "[include]\n\tpath = second.gitconfig\n") |
| 251 | b.write(dir+"/second.gitconfig", "[core]\n\teditor = vi\n") |
| 252 | b.git(b.proj, "config", "include.path", "../été/cfg/first.gitconfig") |
| 253 | risks := strings.Join(Scan(context.Background(), Shares(b.proj, nil), b.home).Risks(mounts.BaseNames), "\n") |
| 254 | for _, want := range []string{"config file été/cfg/first.gitconfig", "config file été/cfg/second.gitconfig"} { |
| 255 | if !strings.Contains(risks, want) { |
| 256 | t.Errorf("missing %q in:\n%s", want, risks) |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | func TestScanBash(t *testing.T) { |
| 262 | cases := map[string]func(b *box){ |
| 263 | "nothing": func(b *box) {}, |
| 264 | "husky": func(b *box) { b.git(b.proj, "config", "core.hooksPath", ".husky/_") }, |
| 265 | "absolute": func(b *box) { b.git(b.proj, "config", "core.hooksPath", b.proj+"/.githooks") }, |
| 266 | "other capitals": func(b *box) { |
| 267 | b.git(b.proj, "config", "core.hooksPath", strings.ToUpper(b.proj)+"/.husky/_") |
| 268 | }, |
| 269 | "nested": func(b *box) { |
| 270 | os.MkdirAll(b.proj+"/lib/inner", 0o755) |
| 271 | b.git(b.proj+"/lib/inner", "init", "-q") |
| 272 | b.git(b.proj+"/lib/inner", "config", "core.hooksPath", ".githooks") |
| 273 | }, |
| 274 | "project itself": func(b *box) { b.git(b.proj, "config", "core.hooksPath", ".") }, |
| 275 | "dot-dot": func(b *box) { b.git(b.proj, "config", "core.hooksPath", "tools/../.githooks") }, |
| 276 | "under .Git": func(b *box) { b.git(b.proj, "config", "core.hooksPath", ".Git/hooks") }, |
| 277 | "outside": func(b *box) { b.git(b.proj, "config", "core.hooksPath", b.sb+"/hooks") }, |
| 278 | "project in a repository": func(b *box) { |
| 279 | os.RemoveAll(b.proj + "/.git") |
| 280 | b.git(b.home, "init", "-q") |
| 281 | b.git(b.home, "config", "core.hooksPath", "proj/hooks") |
| 282 | }, |
| 283 | "hooks above the project": func(b *box) { |
| 284 | os.RemoveAll(b.proj + "/.git") |
| 285 | b.git(b.home, "init", "-q") |
| 286 | b.git(b.home, "config", "core.hooksPath", ".githooks") |
| 287 | }, |
| 288 | "bad name": func(b *box) { b.git(b.proj, "config", "core.hooksPath", `a"b/x`) }, |
| 289 | "hooks dir linked": func(b *box) { |
| 290 | os.MkdirAll(b.proj+"/scripts/hooks", 0o755) |
| 291 | os.RemoveAll(b.proj + "/.git/hooks") |
| 292 | os.Symlink("../scripts/hooks", b.proj+"/.git/hooks") |
| 293 | }, |
| 294 | "hooksPath linked": func(b *box) { |
| 295 | os.MkdirAll(b.proj+"/scripts/hooks", 0o755) |
| 296 | os.Symlink("scripts/hooks", b.proj+"/.githooks") |
| 297 | b.git(b.proj, "config", "core.hooksPath", ".githooks") |
| 298 | }, |
| 299 | "hook links": func(b *box) { |
| 300 | b.write(b.proj+"/scripts/pre-commit", "#!/bin/sh\n") |
| 301 | b.write(b.sb+"/elsewhere-hook", "#!/bin/sh\n") |
| 302 | os.Symlink("../../scripts/pre-commit", b.proj+"/.git/hooks/pre-commit") |
| 303 | os.Symlink(b.sb+"/elsewhere-hook", b.proj+"/.git/hooks/post-commit") |
| 304 | os.Symlink("../../later/pre-push", b.proj+"/.git/hooks/pre-push") |
| 305 | os.Symlink(b.sb+"/elsewhere-hook", b.proj+"/scripts/relay") |
| 306 | os.Symlink("../../scripts/relay", b.proj+"/.git/hooks/post-merge") |
| 307 | os.Symlink(b.proj+"/scripts/pre-commit", b.sb+"/back-link") |
| 308 | os.Symlink(b.sb+"/back-link", b.proj+"/.git/hooks/pre-rebase") |
| 309 | os.Symlink("../../scripts/pre-commit", b.proj+"/.git/hooks/.hidden") |
| 310 | }, |
| 311 | "hooks dir not there yet": func(b *box) { |
| 312 | os.RemoveAll(b.proj + "/.git/hooks") |
| 313 | os.Symlink("../tools/hooks", b.proj+"/.git/hooks") |
| 314 | }, |
| 315 | "hooks dir through a relay": func(b *box) { |
| 316 | os.MkdirAll(b.sb+"/shared-hooks", 0o755) |
| 317 | os.MkdirAll(b.proj+"/scripts", 0o755) |
| 318 | os.Symlink(b.sb+"/shared-hooks", b.proj+"/scripts/hooks-relay") |
| 319 | os.RemoveAll(b.proj + "/.git/hooks") |
| 320 | os.Symlink("../scripts/hooks-relay", b.proj+"/.git/hooks") |
| 321 | }, |
| 322 | "protected folder link and control characters": func(b *box) { |
| 323 | b.write(b.proj+"/.githooks/post-merge", "#!/bin/sh\n") |
| 324 | os.Symlink("post-merge", b.proj+"/.githooks/post-rewrite") |
| 325 | b.write(b.proj+"/scripts/pre-commit", "#!/bin/sh\n") |
| 326 | os.Symlink("../scripts/pre-commit", b.proj+"/.githooks/a\033]0;x\007b") |
| 327 | b.git(b.proj, "config", "core.hooksPath", ".githooks") |
| 328 | }, |
| 329 | "volume repo": func(b *box) { |
| 330 | os.MkdirAll(b.sb+"/vrepo", 0o755) |
| 331 | b.git(b.sb+"/vrepo", "init", "-q") |
| 332 | b.git(b.sb+"/vrepo", "config", "core.hooksPath", "tools/hooks") |
| 333 | b.git(b.sb+"/vrepo", "config", "core.fsmonitor", "./fsmon.sh") |
| 334 | b.vol(b.sb + "/vrepo:/mnt/vrepo:rw") |
| 335 | }, |
| 336 | "read-only volume repo": func(b *box) { |
| 337 | os.MkdirAll(b.sb+"/vrepo", 0o755) |
| 338 | b.git(b.sb+"/vrepo", "init", "-q") |
| 339 | b.git(b.sb+"/vrepo", "config", "core.hooksPath", "tools/hooks") |
| 340 | b.vol(b.sb + "/vrepo:/mnt/vrepo:ro") |
| 341 | }, |
| 342 | "across shares": func(b *box) { |
| 343 | os.MkdirAll(b.sb+"/teamtools/hooks", 0o755) |
| 344 | b.vol(b.sb + "/teamtools:/mnt/teamtools:rw") |
| 345 | b.git(b.proj, "config", "core.fsmonitor", b.sb+"/teamtools/fsmon.sh") |
| 346 | b.git(b.proj, "config", "include.path", b.sb+"/teamtools/gitconfig") |
| 347 | }, |
| 348 | "hooks in a volume": func(b *box) { |
| 349 | os.MkdirAll(b.sb+"/teamtools/hooks", 0o755) |
| 350 | b.vol(b.sb + "/teamtools:/mnt/teamtools:rw") |
| 351 | b.git(b.proj, "config", "core.hooksPath", b.sb+"/teamtools/hooks") |
| 352 | }, |
| 353 | "git's own config in a volume": func(b *box) { |
| 354 | b.write(b.sb+"/teamtools/git/config", "[core]\n\tfsmonitor = "+b.sb+"/teamtools/fsmon.sh\n") |
| 355 | b.vol(b.sb + "/teamtools:/mnt/teamtools:rw") |
| 356 | b.xdg = b.sb + "/teamtools" |
| 357 | }, |
| 358 | "volume through a symlink": func(b *box) { |
| 359 | os.MkdirAll(b.sb+"/data/code/app", 0o755) |
| 360 | os.Symlink(b.sb+"/data/code", b.sb+"/code-link") |
| 361 | b.git(b.sb+"/data/code/app", "init", "-q") |
| 362 | b.git(b.sb+"/data/code/app", "config", "core.hooksPath", ".husky/_") |
| 363 | b.vol(b.sb + "/code-link:/mnt/code:rw") |
| 364 | }, |
| 365 | "bare repository in a volume": func(b *box) { |
| 366 | os.MkdirAll(b.sb+"/remotes", 0o755) |
| 367 | exec.Command("git", "init", "-q", "--bare", b.sb+"/remotes/proj.git").Run() |
| 368 | b.vol(b.sb + "/remotes:/mnt/remotes:rw") |
| 369 | }, |
| 370 | "config": func(b *box) { |
| 371 | b.git(b.proj, "config", "core.pager", "less") |
| 372 | b.git(b.proj, "config", "core.sshCommand", "ssh -o ProxyCommand=/usr/bin/nc") |
| 373 | b.git(b.proj, "config", "alias.st", "status") |
| 374 | b.write(b.proj+"/.gitconfig", "[core]\n\tfsmonitor = ./watch.sh\n") |
| 375 | b.git(b.proj, "config", "include.path", "../.gitconfig") |
| 376 | b.git(b.proj, "config", "filter.x.clean", "scripts/clean.sh %f") |
| 377 | b.git(b.proj, "config", "alias.t", "!./t.sh") |
| 378 | b.git(b.proj, "config", "alias.q", `!"./q.sh" x`) |
| 379 | b.git(b.proj, "config", "credential.helper", "'tools/cred' --x=y") |
| 380 | }, |
| 381 | "includes": func(b *box) { |
| 382 | b.git(b.proj, "config", "includeIf.gitdir:/.path", "../later.gitconfig") |
| 383 | b.git(b.proj, "config", "--add", "include.path", "../.git/x.gitconfig") |
| 384 | b.git(b.proj, "config", "--add", "include.path", b.sb+"/outside.gitconfig") |
| 385 | b.git(b.proj, "config", "--add", "include.path", "~/proj/home.gitconfig") |
| 386 | }, |
| 387 | "other scopes": func(b *box) { |
| 388 | b.write(b.home+"/.gitconfig", "[alias]\n\tg = !./g.sh\n") |
| 389 | b.git(b.proj, "config", "extensions.worktreeConfig", "true") |
| 390 | b.git(b.proj, "config", "--worktree", "core.editor", "./ed.sh") |
| 391 | }, |
| 392 | "global hooksPath into the project": func(b *box) { |
| 393 | b.write(b.home+"/.gitconfig", "[core]\n\thooksPath = ~/proj/hooks\n") |
| 394 | }, |
| 395 | "many repositories": func(b *box) { |
| 396 | for i := 0; i < 52; i++ { |
| 397 | d := b.proj + "/r/" + strconv.Itoa(i) |
| 398 | os.MkdirAll(d+"/.git", 0o755) |
| 399 | } |
| 400 | }, |
| 401 | "node_modules": func(b *box) { |
| 402 | os.MkdirAll(b.proj+"/node_modules/pkg/.git", 0o755) |
| 403 | os.MkdirAll(b.proj+"/a/node_modules/.git", 0o755) |
| 404 | os.MkdirAll(b.proj+"/a/b/.git", 0o755) |
| 405 | os.MkdirAll(b.proj+"/a/b/c/.git", 0o755) |
| 406 | }, |
| 407 | } |
| 408 | |
| 409 | expect := map[string]string{ |
| 410 | "nothing": "", |
| 411 | "husky": "H\t.husky/_\t.husky/_", |
| 412 | "absolute": "H\t.githooks\t.githooks", |
| 413 | "other capitals": "", |
| 414 | "nested": "H\tlib/inner/.githooks\t.githooks", |
| 415 | "project itself": "H\t.\t.", |
| 416 | "dot-dot": "H\t.githooks\t.githooks", |
| 417 | "under .Git": "", |
| 418 | "outside": "", |
| 419 | "project in a repository": "H\thooks\thooks", |
| 420 | "hooks above the project": "", |
| 421 | "bad name": "H\ta\"b/x\ta\"b/x", |
| 422 | "hooks dir linked": "H\tscripts/hooks\tscripts/hooks", |
| 423 | "hooksPath linked": "H\tscripts/hooks\tscripts/hooks", |
| 424 | "hook links": "R\thook pre-rebase runs scripts/pre-commit", |
| 425 | "hooks dir not there yet": "H\ttools/hooks\ttools/hooks", |
| 426 | "hooks dir through a relay": "H\tscripts/hooks-relay\tscripts/hooks-relay", |
| 427 | "protected folder link and control characters": "R\thook a?]0;x?b runs scripts/pre-commit", |
| 428 | "volume repo": "R\tcore.fsmonitor = ./fsmon.sh", |
| 429 | "read-only volume repo": "", |
| 430 | "across shares": "/teamtools/gitconfig", |
| 431 | "hooks in a volume": "/teamtools/hooks\thooks", |
| 432 | "git's own config in a volume": "/teamtools/git/config", |
| 433 | "volume through a symlink": "/data/code/app/.husky/_\t.husky/_", |
| 434 | "bare repository in a volume": "/remotes/proj.git/hooks\tproj.git/hooks", |
| 435 | "config": "R\tfilter.x.clean = scripts/clean.sh %f", |
| 436 | "includes": "R\tconfig file later.gitconfig", |
| 437 | "other scopes": "R\tcore.editor = ./ed.sh", |
| 438 | "global hooksPath into the project": "H\thooks\thooks", |
| 439 | "many repositories": "R\tmore than 50 repositories in the project:", |
| 440 | "node_modules": "", |
| 441 | } |
| 442 | |
| 443 | if paths.NoCase() { |
| 444 | expect["other capitals"] = "H\t.husky/_\t.husky/_" |
| 445 | } |
| 446 | for name, setup := range cases { |
| 447 | t.Run(name, func(t *testing.T) { |
| 448 | b := newBox(t) |
| 449 | setup(b) |
| 450 | got := strings.Join(b.compare(name), "\n") |
| 451 | want, ok := expect[name] |
| 452 | switch { |
| 453 | case !ok: |
| 454 | t.Errorf("no expectation for %q", name) |
| 455 | case want == "" && got != "": |
| 456 | t.Errorf("expected nothing, got:\n%s", got) |
| 457 | case want != "": |
| 458 | |
| 459 | found := false |
| 460 | for _, l := range strings.Split(got, "\n") { |
| 461 | found = found || strings.Contains(l, want) |
| 462 | } |
| 463 | if !found { |
| 464 | t.Errorf("expected %q in a line of:\n%s", want, got) |
| 465 | } |
| 466 | } |
| 467 | }) |
| 468 | } |
| 469 | } |