| 1 | package review |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "runtime" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestRunLinters_empty(t *testing.T) { |
| 10 | results, err := RunLinters(t.TempDir(), nil) |
| 11 | if err != nil { |
| 12 | t.Fatalf("unexpected error: %v", err) |
| 13 | } |
| 14 | if results != nil { |
| 15 | t.Fatalf("expected nil results, got %v", results) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | func TestRunLinters_passingCommand(t *testing.T) { |
| 20 | if runtime.GOOS == "windows" { |
| 21 | t.Skip("skipping on windows") |
| 22 | } |
| 23 | results, err := RunLinters(t.TempDir(), []string{"true"}) |
| 24 | if err != nil { |
| 25 | t.Fatalf("unexpected error: %v", err) |
| 26 | } |
| 27 | if len(results) != 1 { |
| 28 | t.Fatalf("expected 1 result, got %d", len(results)) |
| 29 | } |
| 30 | if !results[0].Passed { |
| 31 | t.Error("expected command to pass") |
| 32 | } |
| 33 | if results[0].Command != "true" { |
| 34 | t.Errorf("command = %q, want %q", results[0].Command, "true") |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestRunLinters_failingCommand(t *testing.T) { |
| 39 | if runtime.GOOS == "windows" { |
| 40 | t.Skip("skipping on windows") |
| 41 | } |
| 42 | results, err := RunLinters(t.TempDir(), []string{"false"}) |
| 43 | if err != nil { |
| 44 | t.Fatalf("unexpected error: %v", err) |
| 45 | } |
| 46 | if len(results) != 1 { |
| 47 | t.Fatalf("expected 1 result, got %d", len(results)) |
| 48 | } |
| 49 | if results[0].Passed { |
| 50 | t.Error("expected command to fail") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestRunLinters_capturesOutput(t *testing.T) { |
| 55 | if runtime.GOOS == "windows" { |
| 56 | t.Skip("skipping on windows") |
| 57 | } |
| 58 | results, err := RunLinters(t.TempDir(), []string{"echo hello"}) |
| 59 | if err != nil { |
| 60 | t.Fatalf("unexpected error: %v", err) |
| 61 | } |
| 62 | if len(results) != 1 { |
| 63 | t.Fatalf("expected 1 result, got %d", len(results)) |
| 64 | } |
| 65 | if results[0].Output != "hello" { |
| 66 | t.Errorf("output = %q, want %q", results[0].Output, "hello") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestRunLinters_multipleCommands(t *testing.T) { |
| 71 | if runtime.GOOS == "windows" { |
| 72 | t.Skip("skipping on windows") |
| 73 | } |
| 74 | results, err := RunLinters(t.TempDir(), []string{"true", "false", "echo ok"}) |
| 75 | if err != nil { |
| 76 | t.Fatalf("unexpected error: %v", err) |
| 77 | } |
| 78 | if len(results) != 3 { |
| 79 | t.Fatalf("expected 3 results, got %d", len(results)) |
| 80 | } |
| 81 | if !results[0].Passed { |
| 82 | t.Error("expected first command to pass") |
| 83 | } |
| 84 | if results[1].Passed { |
| 85 | t.Error("expected second command to fail") |
| 86 | } |
| 87 | if !results[2].Passed { |
| 88 | t.Error("expected third command to pass") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestRunLinters_usesDir(t *testing.T) { |
| 93 | if runtime.GOOS == "windows" { |
| 94 | t.Skip("skipping on windows") |
| 95 | } |
| 96 | dir := t.TempDir() |
| 97 | if err := os.WriteFile(dir+"/marker.txt", []byte("found"), 0644); err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | results, err := RunLinters(dir, []string{"ls marker.txt"}) |
| 101 | if err != nil { |
| 102 | t.Fatalf("unexpected error: %v", err) |
| 103 | } |
| 104 | if !results[0].Passed { |
| 105 | t.Errorf("expected ls to find marker.txt in dir, output: %s", results[0].Output) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestRunLinters_commandNotFound(t *testing.T) { |
| 110 | if runtime.GOOS == "windows" { |
| 111 | t.Skip("skipping on windows") |
| 112 | } |
| 113 | results, err := RunLinters(t.TempDir(), []string{"nonexistent-command-xyz"}) |
| 114 | if err != nil { |
| 115 | t.Fatalf("unexpected error: %v", err) |
| 116 | } |
| 117 | if len(results) != 1 { |
| 118 | t.Fatalf("expected 1 result, got %d", len(results)) |
| 119 | } |
| 120 | if results[0].Passed { |
| 121 | t.Error("expected command to fail") |
| 122 | } |
| 123 | } |