(t *testing.T)
| 279 | } |
| 280 | |
| 281 | func TestExtractFilePathAndEditHooks(t *testing.T) { |
| 282 | root := t.TempDir() |
| 283 | target := filepath.Join(root, "pkg", "types.go") |
| 284 | if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil { |
| 285 | t.Fatal(err) |
| 286 | } |
| 287 | if err := os.WriteFile(target, []byte("package pkg\n"), 0o644); err != nil { |
| 288 | t.Fatal(err) |
| 289 | } |
| 290 | |
| 291 | writeWatchState(t, root, watch.State{ |
| 292 | UpdatedAt: time.Now(), |
| 293 | FileCount: 3, |
| 294 | Importers: map[string][]string{ |
| 295 | "pkg/types.go": {"cmd/a.go", "cmd/b.go", "cmd/c.go"}, |
| 296 | }, |
| 297 | Imports: map[string][]string{ |
| 298 | "pkg/types.go": {"shared/hub.go"}, |
| 299 | "cmd/a.go": {"pkg/types.go"}, |
| 300 | "cmd/b.go": {"pkg/types.go"}, |
| 301 | "cmd/c.go": {"pkg/types.go"}, |
| 302 | "shared/hub.go": {"x.go", "y.go", "z.go"}, |
| 303 | }, |
| 304 | }) |
| 305 | |
| 306 | withStdinInput(t, mustJSONInput(t, map[string]string{"file_path": target}), func() { |
| 307 | got, err := extractFilePathFromStdin() |
| 308 | if err != nil { |
| 309 | t.Fatalf("extractFilePathFromStdin() error: %v", err) |
| 310 | } |
| 311 | if got != target { |
| 312 | t.Fatalf("extractFilePathFromStdin() = %q, want %q", got, target) |
| 313 | } |
| 314 | }) |
| 315 | |
| 316 | withStdinInput(t, `garbage "file_path": "`+target+`"`, func() { |
| 317 | got, err := extractFilePathFromStdin() |
| 318 | if err != nil { |
| 319 | t.Fatalf("expected regex fallback, got error %v", err) |
| 320 | } |
| 321 | if got != target { |
| 322 | t.Fatalf("fallback extracted %q, want %q", got, target) |
| 323 | } |
| 324 | }) |
| 325 | |
| 326 | checkOutput := func(fn func(string) error) { |
| 327 | withStdinInput(t, mustJSONInput(t, map[string]string{"file_path": target}), func() { |
| 328 | var hookErr error |
| 329 | out := captureOutput(func() { hookErr = fn(root) }) |
| 330 | if hookErr != nil { |
| 331 | t.Fatalf("hook returned error: %v", hookErr) |
| 332 | } |
| 333 | if !strings.Contains(out, "HUB FILE: pkg/types.go") { |
| 334 | t.Fatalf("expected hub warning, got:\n%s", out) |
| 335 | } |
| 336 | }) |
| 337 | } |
| 338 | checkOutput(hookPreEdit) |
nothing calls this directly
no test coverage detected