(t *testing.T)
| 202 | } |
| 203 | |
| 204 | func TestDelegateDiffExamples(t *testing.T) { |
| 205 | examples := delegateExamples(t) |
| 206 | r := newDelegateRepo(t) |
| 207 | modes := map[string]bool{} |
| 208 | for _, example := range examples { |
| 209 | t.Run(example, func(t *testing.T) { |
| 210 | if strings.HasPrefix(example, "cat ") { |
| 211 | modes["untracked"] = true |
| 212 | if example != "cat \"<path>\"" { |
| 213 | t.Fatal("untracked file path must be quoted") |
| 214 | } |
| 215 | bash, err := exec.LookPath("bash") |
| 216 | if err != nil { |
| 217 | t.Skip("shell execution requires Bash; path quotation was checked") |
| 218 | } |
| 219 | name := "untracked sample file.txt" |
| 220 | if err := os.WriteFile(filepath.Join(r.dir, name), []byte("untracked\n"), 0o644); err != nil { |
| 221 | t.Fatal(err) |
| 222 | } |
| 223 | ctx, cancel := context.WithTimeout(t.Context(), 15*time.Second) |
| 224 | defer cancel() |
| 225 | cmd := exec.CommandContext(ctx, bash, "-c", strings.ReplaceAll(example, "<path>", "$1"), "ocr-read", name) |
| 226 | cmd.Dir, cmd.Env = r.dir, r.env |
| 227 | if out, err := cmd.CombinedOutput(); err != nil || string(out) != "untracked\n" { |
| 228 | t.Fatalf("read untracked file: %v\n%s", err, out) |
| 229 | } |
| 230 | return |
| 231 | } |
| 232 | if !strings.Contains(example, " -- \"<path>\"") && !strings.Contains(example, "\"<ref>:<path>\"") { |
| 233 | t.Fatal("file path placeholder must be quoted") |
| 234 | } |
| 235 | patchPath := filepath.Join(t.TempDir(), "review patch.diff") |
| 236 | args := r.args(example, patchPath) |
| 237 | if len(args) < 2 || args[0] != "--no-pager" { |
| 238 | t.Fatal("every Git command must disable the pager before the subcommand") |
| 239 | } |
| 240 | if args[1] == "log" || args[1] == "blame" || strings.Contains(example, "<ref>:<path>") { |
| 241 | modes[args[1]] = true |
| 242 | want := "workspace" |
| 243 | if args[1] == "log" { |
| 244 | want = "change" |
| 245 | } else if args[1] == "show" { |
| 246 | want = "committed" |
| 247 | } |
| 248 | if out := r.must(t, args...); !strings.Contains(out, want) { |
| 249 | t.Fatalf("context output missing %q: %s", want, out) |
| 250 | } |
| 251 | return |
| 252 | } |
| 253 | for _, flag := range []string{"--no-ext-diff", "--no-textconv", "--no-color"} { |
| 254 | if !slices.Contains(args, flag) { |
| 255 | t.Errorf("missing %s", flag) |
| 256 | } |
| 257 | } |
| 258 | mode, want := "workspace", "+workspace\n" |
| 259 | if strings.Contains(example, "<merge_base>") { |
| 260 | mode, want = "range", "+committed\n" |
| 261 | } else if strings.Contains(example, "<commit>") { |
nothing calls this directly
no test coverage detected