| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 11 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | var maskTestPaths = []string{ |
| 16 | ".env", ".env.local", ".env.development", ".env.development.local", |
| 17 | ".env.production", ".env.production.local", ".env.test", ".env.test.local", |
| 18 | ".gitmodules", ".npmrc", ".yarnrc", ".yarnrc.yml", "bunfig.toml", |
| 19 | "package-lock.json", "package.json", "pnpm-lock.yaml", "yarn.lock", |
| 20 | } |
| 21 | |
| 22 | func writeMaskTest(t *testing.T, dir, path, body string) { |
| 23 | t.Helper() |
| 24 | if err := os.WriteFile(filepath.Join(dir, path), []byte(body), 0600); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestSandboxMasksPublish(t *testing.T) { |
| 30 | for _, kind := range []string{"placeholders", "real_package", "existing_empty", "existing_emptied", "symlink", "ordinary_empty"} { |
| 31 | t.Run(kind, func(t *testing.T) { |
| 32 | ctx := context.Background() |
| 33 | e, f, r, _ := hubRig(t) |
| 34 | seed := filepath.Join(filepath.Dir(f.dir), "seed") |
| 35 | if kind == "existing_empty" || kind == "existing_emptied" { |
| 36 | body := "" |
| 37 | if kind == "existing_emptied" { |
| 38 | body = "old contents\n" |
| 39 | } |
| 40 | writeMaskTest(t, seed, ".gitmodules", body) |
| 41 | gitOut(t, seed, "add", ".gitmodules") |
| 42 | gitOut(t, seed, "commit", "-qm", "existing file") |
| 43 | gitOut(t, seed, "push", "-q", f.dir, "main") |
| 44 | } |
| 45 | if err := e.fetch(ctx, r.Repo); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | base, err := e.defaultTip(ctx, r.Repo) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | r.Specialist, r.Branch, r.Base = wire.SpecialistEngineering, "equipo/tk1-masks", base |
| 53 | loc := e.tree(r, base) |
| 54 | tree, err := workspace.PrepareTaskTree(ctx, loc.Git, e.Clones.Path(r.Repo), base, r.Branch, loc.Dir, loc.GitDir) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | writeMaskTest(t, tree.Dir, "NEWS", "intentional change\n") |
| 59 | keep := "" |
| 60 | switch kind { |
| 61 | case "placeholders": |
| 62 | for _, p := range maskTestPaths { |
| 63 | writeMaskTest(t, tree.Dir, p, "") |
| 64 | } |
| 65 | case "real_package": |
| 66 | keep = "package.json" |
| 67 | writeMaskTest(t, tree.Dir, keep, "{\"name\":\"real\"}\n") |
| 68 | case "existing_empty", "existing_emptied": |
| 69 | keep = ".gitmodules" |
| 70 | writeMaskTest(t, tree.Dir, keep, "") |
| 71 | case "symlink": |
| 72 | keep = ".npmrc" |
| 73 | if err := os.Symlink("NEWS", filepath.Join(tree.Dir, keep)); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | case "ordinary_empty": |
| 77 | keep = "ordinary.txt" |
| 78 | writeMaskTest(t, tree.Dir, keep, "") |
| 79 | } |
| 80 | sha, err := tree.Commit(ctx, "step", commitName, commitEmail) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | if kind == "placeholders" { |
| 85 | for _, p := range maskTestPaths { |
| 86 | if _, err := os.Lstat(filepath.Join(tree.Dir, p)); !os.IsNotExist(err) { |
| 87 | t.Errorf("placeholder %s remains before publish: %v", p, err) |
| 88 | } |
| 89 | } |
| 90 | } else if _, err := os.Lstat(filepath.Join(tree.Dir, keep)); err != nil { |
| 91 | t.Fatalf("real file removed: %v", err) |
| 92 | } |
| 93 | r.Candidate = sha |
| 94 | r.Specialist = wire.SpecialistEngineering |
| 95 | rep := e.publish(ctx, r, wire.StepReport{}) |
| 96 | if rep.State != wire.StepDone || !rep.Pushed { |
| 97 | t.Fatalf("publish: %+v", rep) |
| 98 | } |
| 99 | if rep.ChangeFiles == nil || rep.ChangeFiles.Base != r.Base || rep.ChangeFiles.SHA != sha || !wire.ValidChangeFiles(rep.ChangeFiles) { |
| 100 | t.Fatalf("published candidate has no display facts: %+v", rep.ChangeFiles) |
| 101 | } |
| 102 | paths := "\n" + gitOut(t, f.dir, "ls-tree", "-r", "--name-only", r.Branch) + "\n" |
| 103 | if kind == "placeholders" { |
| 104 | for _, p := range maskTestPaths { |
| 105 | if strings.Contains(paths, "\n"+p+"\n") { |
| 106 | t.Errorf("published placeholder %s", p) |
| 107 | } |
| 108 | } |
| 109 | } else if !strings.Contains(paths, "\n"+keep+"\n") { |
| 110 | t.Fatalf("real file missing from published tree: %s", paths) |
| 111 | } |
| 112 | }) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func TestMergeStepSandboxMasks(t *testing.T) { |
| 117 | for _, p := range append(append([]string{}, maskTestPaths...), "ordinary.txt", "nested/package.json", "nonempty", "existing", ".npmrc/empty") { |
| 118 | t.Run(p, func(t *testing.T) { |
| 119 | e, f, r, _ := hubRig(t) |
| 120 | seed := filepath.Join(filepath.Dir(f.dir), "seed") |
| 121 | name, body, refused := p, "", true |
| 122 | if p == "existing" { |
| 123 | name, refused = ".gitmodules", false |
| 124 | writeMaskTest(t, seed, name, "") |
| 125 | gitOut(t, seed, "add", name) |
| 126 | gitOut(t, seed, "commit", "-qm", "existing empty") |
| 127 | gitOut(t, seed, "push", "-q", f.dir, "main") |
| 128 | } |
| 129 | gitOut(t, seed, "checkout", "-q", "-B", r.Branch, "main") |
| 130 | if p == "ordinary.txt" || p == "nested/package.json" || p == ".npmrc/empty" { |
| 131 | refused = false |
| 132 | } |
| 133 | if p == "nonempty" { |
| 134 | name, body, refused = "package.json", "{}\n", false |
| 135 | } |
| 136 | if err := os.MkdirAll(filepath.Dir(filepath.Join(seed, name)), 0700); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | writeMaskTest(t, seed, name, body) |
| 140 | writeMaskTest(t, seed, "NEWS", "merge change\n") |
| 141 | gitOut(t, seed, "add", "-A") |
| 142 | gitOut(t, seed, "commit", "-qm", "candidate") |
| 143 | r.CandidateSHA = gitOut(t, seed, "rev-parse", "HEAD") |
| 144 | gitOut(t, seed, "push", "-q", "--force", f.dir, r.Branch) |
| 145 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 146 | if refused { |
| 147 | if rep.State != wire.StepFailed || f.requests != 0 || !strings.Contains(rep.LogTail, "sandbox placeholder") || !strings.Contains(rep.LogTail, name) { |
| 148 | t.Fatalf("placeholder merge: %+v, requests %d", rep, f.requests) |
| 149 | } |
| 150 | } else if rep.State != wire.StepDone || f.requests != 1 { |
| 151 | t.Fatalf("real file merge: %+v, requests %d", rep, f.requests) |
| 152 | } |
| 153 | }) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func TestMergeStepSandboxMasksConfirmsLast(t *testing.T) { |
| 158 | e, f, r, _ := hubRig(t) |
| 159 | f.answer = func(_ int, merge func() (forge.MergeResult, error)) (forge.MergeResult, error) { |
| 160 | e.Clones.Forge = f |
| 161 | return merge() |
| 162 | } |
| 163 | e.ConfirmMerge = func(context.Context, string, int64) error { |
| 164 | e.Clones.Forge = brokenRemote{f} |
| 165 | return nil |
| 166 | } |
| 167 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 168 | if rep.State != wire.StepDone || f.requests != 1 { |
| 169 | t.Fatalf("read the repository after final authorization: %+v", rep) |
| 170 | } |
| 171 | } |
| 172 | |