|
| 1 | +package check |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestGitHooksCheck_Node_PassAndWarn(t *testing.T) { |
| 11 | + dir := t.TempDir() |
| 12 | + |
| 13 | + // ensure we are treated as a git repo |
| 14 | + if err := os.Mkdir(filepath.Join(dir, ".git"), 0o755); err != nil { |
| 15 | + t.Fatalf("failed to create .git directory: %v", err) |
| 16 | + } |
| 17 | + |
| 18 | + check := &GitHooksCheck{Dir: dir, Stack: "node"} |
| 19 | + |
| 20 | + // Pass when .husky exists |
| 21 | + if err := os.Mkdir(filepath.Join(dir, ".husky"), 0o755); err != nil { |
| 22 | + t.Fatalf("failed to create .husky directory: %v", err) |
| 23 | + } |
| 24 | + result := check.Run(context.Background()) |
| 25 | + if result.Status != StatusPass { |
| 26 | + t.Errorf("expected pass when .husky exists, got %v: %s", result.Status, result.Message) |
| 27 | + } |
| 28 | + |
| 29 | + // Warn when .husky is missing |
| 30 | + if err := os.RemoveAll(filepath.Join(dir, ".husky")); err != nil { |
| 31 | + t.Fatalf("failed to remove .husky directory: %v", err) |
| 32 | + } |
| 33 | + result = check.Run(context.Background()) |
| 34 | + if result.Status != StatusWarn { |
| 35 | + t.Errorf("expected warn when .husky missing, got %v: %s", result.Status, result.Message) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestGitHooksCheck_Python_PassAndWarn(t *testing.T) { |
| 40 | + dir := t.TempDir() |
| 41 | + |
| 42 | + // ensure we are treated as a git repo |
| 43 | + if err := os.Mkdir(filepath.Join(dir, ".git"), 0o755); err != nil { |
| 44 | + t.Fatalf("failed to create .git directory: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + // override lookPath so tests don't depend on environment |
| 48 | + origLookPath := lookPath |
| 49 | + defer func() { lookPath = origLookPath }() |
| 50 | + |
| 51 | + check := &GitHooksCheck{Dir: dir, Stack: "python"} |
| 52 | + |
| 53 | + // Pass when config exists and pre-commit is "installed" |
| 54 | + if err := os.WriteFile(filepath.Join(dir, ".pre-commit-config.yaml"), []byte("repos: []\n"), 0o644); err != nil { |
| 55 | + t.Fatalf("failed to write .pre-commit-config.yaml: %v", err) |
| 56 | + } |
| 57 | + lookPath = func(string) (string, error) { |
| 58 | + return "/usr/bin/pre-commit", nil |
| 59 | + } |
| 60 | + |
| 61 | + result := check.Run(context.Background()) |
| 62 | + if result.Status != StatusPass { |
| 63 | + t.Errorf("expected pass when pre-commit and config present, got %v: %s", result.Status, result.Message) |
| 64 | + } |
| 65 | + |
| 66 | + // Warn when either piece is missing (simulate missing pre-commit) |
| 67 | + lookPath = func(string) (string, error) { |
| 68 | + return "", os.ErrNotExist |
| 69 | + } |
| 70 | + |
| 71 | + result = check.Run(context.Background()) |
| 72 | + if result.Status != StatusWarn { |
| 73 | + t.Errorf("expected warn when pre-commit missing, got %v: %s", result.Status, result.Message) |
| 74 | + } |
| 75 | +} |
| 76 | + |
0 commit comments