|
| 1 | +package check |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDepsCheck_Node_PassAndFail(t *testing.T) { |
| 12 | + dir := t.TempDir() |
| 13 | + check := &DepsCheck{Dir: dir, Stack: "node"} |
| 14 | + |
| 15 | + // Pass when node_modules exists |
| 16 | + if err := os.Mkdir(filepath.Join(dir, "node_modules"), 0o755); err != nil { |
| 17 | + t.Fatalf("failed to create node_modules: %v", err) |
| 18 | + } |
| 19 | + result := check.Run(context.Background()) |
| 20 | + if result.Status != StatusPass { |
| 21 | + t.Errorf("expected pass when node_modules exists, got %v: %s", result.Status, result.Message) |
| 22 | + } |
| 23 | + |
| 24 | + // Fail when node_modules is missing |
| 25 | + if err := os.RemoveAll(filepath.Join(dir, "node_modules")); err != nil { |
| 26 | + t.Fatalf("failed to remove node_modules: %v", err) |
| 27 | + } |
| 28 | + result = check.Run(context.Background()) |
| 29 | + if result.Status != StatusFail { |
| 30 | + t.Errorf("expected fail when node_modules missing, got %v: %s", result.Status, result.Message) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestDepsCheck_Python_PassAndFail(t *testing.T) { |
| 35 | + dir := t.TempDir() |
| 36 | + check := &DepsCheck{Dir: dir, Stack: "python"} |
| 37 | + |
| 38 | + // Pass when .venv exists |
| 39 | + if err := os.Mkdir(filepath.Join(dir, ".venv"), 0o755); err != nil { |
| 40 | + t.Fatalf("failed to create .venv: %v", err) |
| 41 | + } |
| 42 | + result := check.Run(context.Background()) |
| 43 | + if result.Status != StatusPass { |
| 44 | + t.Errorf("expected pass when .venv exists, got %v: %s", result.Status, result.Message) |
| 45 | + } |
| 46 | + |
| 47 | + // Fail when no venv directories exist |
| 48 | + if err := os.RemoveAll(filepath.Join(dir, ".venv")); err != nil { |
| 49 | + t.Fatalf("failed to remove .venv: %v", err) |
| 50 | + } |
| 51 | + result = check.Run(context.Background()) |
| 52 | + if result.Status != StatusFail { |
| 53 | + t.Errorf("expected fail when no venv directories, got %v: %s", result.Status, result.Message) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestDepsCheck_Go_PassAndFail(t *testing.T) { |
| 58 | + dir := t.TempDir() |
| 59 | + |
| 60 | + // Pass when vendor directory exists |
| 61 | + check := &DepsCheck{Dir: dir, Stack: "go"} |
| 62 | + if err := os.Mkdir(filepath.Join(dir, "vendor"), 0o755); err != nil { |
| 63 | + t.Fatalf("failed to create vendor: %v", err) |
| 64 | + } |
| 65 | + result := check.Run(context.Background()) |
| 66 | + if result.Status != StatusPass { |
| 67 | + t.Errorf("expected pass when vendor exists, got %v: %s", result.Status, result.Message) |
| 68 | + } |
| 69 | + |
| 70 | + // When vendor is missing, pass if goCheck succeeds |
| 71 | + if err := os.RemoveAll(filepath.Join(dir, "vendor")); err != nil { |
| 72 | + t.Fatalf("failed to remove vendor: %v", err) |
| 73 | + } |
| 74 | + check = &DepsCheck{ |
| 75 | + Dir: dir, |
| 76 | + Stack: "go", |
| 77 | + goCheck: func(string) error { |
| 78 | + return nil |
| 79 | + }, |
| 80 | + } |
| 81 | + result = check.Run(context.Background()) |
| 82 | + if result.Status != StatusPass { |
| 83 | + t.Errorf("expected pass when goCheck succeeds, got %v: %s", result.Status, result.Message) |
| 84 | + } |
| 85 | + |
| 86 | + // Fail when goCheck reports an error |
| 87 | + check = &DepsCheck{ |
| 88 | + Dir: dir, |
| 89 | + Stack: "go", |
| 90 | + goCheck: func(string) error { |
| 91 | + return errors.New("download failed") |
| 92 | + }, |
| 93 | + } |
| 94 | + result = check.Run(context.Background()) |
| 95 | + if result.Status != StatusFail { |
| 96 | + t.Errorf("expected fail when goCheck fails, got %v: %s", result.Status, result.Message) |
| 97 | + } |
| 98 | +} |
| 99 | + |
0 commit comments