|
| 1 | +package detector |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/Priyans-hu/argus/pkg/types" |
| 9 | +) |
| 10 | + |
| 11 | +func TestCLIDetector_Detect_WithCobraFramework(t *testing.T) { |
| 12 | + tmpDir, err := os.MkdirTemp("", "cli-test") |
| 13 | + if err != nil { |
| 14 | + t.Fatalf("failed to create temp dir: %v", err) |
| 15 | + } |
| 16 | + defer func() { _ = os.RemoveAll(tmpDir) }() |
| 17 | + |
| 18 | + // Create cmd/app/main.go with verbose and dry-run flags |
| 19 | + cmdDir := filepath.Join(tmpDir, "cmd", "app") |
| 20 | + if err := os.MkdirAll(cmdDir, 0755); err != nil { |
| 21 | + t.Fatalf("failed to create cmd dir: %v", err) |
| 22 | + } |
| 23 | + |
| 24 | + mainContent := `package main |
| 25 | +
|
| 26 | +import ( |
| 27 | + "fmt" |
| 28 | + "github.com/spf13/cobra" |
| 29 | +) |
| 30 | +
|
| 31 | +var verbose bool |
| 32 | +var dryRun bool |
| 33 | +
|
| 34 | +func main() { |
| 35 | + rootCmd := &cobra.Command{ |
| 36 | + Use: "app", |
| 37 | + Short: "Test app", |
| 38 | + Run: func(cmd *cobra.Command, args []string) { |
| 39 | + if verbose { |
| 40 | + fmt.Println("🔍 Scanning...") |
| 41 | + } |
| 42 | + fmt.Println("✅ Success") |
| 43 | + if dryRun { |
| 44 | + fmt.Println("📄 Dry run mode") |
| 45 | + } |
| 46 | + }, |
| 47 | + } |
| 48 | + rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") |
| 49 | + rootCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "n", false, "dry run mode") |
| 50 | + rootCmd.Execute() |
| 51 | +} |
| 52 | +` |
| 53 | + if err := os.WriteFile(filepath.Join(cmdDir, "main.go"), []byte(mainContent), 0644); err != nil { |
| 54 | + t.Fatalf("failed to write main.go: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + files := []types.FileInfo{ |
| 58 | + {Path: "cmd/app/main.go", Extension: ".go"}, |
| 59 | + } |
| 60 | + |
| 61 | + techStack := &types.TechStack{ |
| 62 | + Frameworks: []types.Framework{ |
| 63 | + {Name: "Cobra", Category: "CLI"}, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + detector := NewCLIDetector(tmpDir, files, techStack) |
| 68 | + result := detector.Detect() |
| 69 | + |
| 70 | + if result == nil { |
| 71 | + t.Fatal("expected CLI info to be detected") |
| 72 | + } |
| 73 | + |
| 74 | + if result.VerboseFlag == "" { |
| 75 | + t.Error("expected verbose flag to be detected") |
| 76 | + } |
| 77 | + |
| 78 | + if result.DryRunFlag == "" { |
| 79 | + t.Error("expected dry-run flag to be detected") |
| 80 | + } |
| 81 | + |
| 82 | + if len(result.Indicators) == 0 { |
| 83 | + t.Error("expected indicators to be detected") |
| 84 | + } |
| 85 | + |
| 86 | + // Check for specific indicators |
| 87 | + hasIndicator := func(symbol string) bool { |
| 88 | + for _, ind := range result.Indicators { |
| 89 | + if ind.Symbol == symbol { |
| 90 | + return true |
| 91 | + } |
| 92 | + } |
| 93 | + return false |
| 94 | + } |
| 95 | + |
| 96 | + if !hasIndicator("✅") { |
| 97 | + t.Error("expected success indicator to be detected") |
| 98 | + } |
| 99 | + if !hasIndicator("🔍") { |
| 100 | + t.Error("expected scanning indicator to be detected") |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestCLIDetector_Detect_WithCmdDirectory(t *testing.T) { |
| 105 | + tmpDir, err := os.MkdirTemp("", "cli-cmd-test") |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("failed to create temp dir: %v", err) |
| 108 | + } |
| 109 | + defer func() { _ = os.RemoveAll(tmpDir) }() |
| 110 | + |
| 111 | + // Create cmd/main.go |
| 112 | + cmdDir := filepath.Join(tmpDir, "cmd") |
| 113 | + if err := os.MkdirAll(cmdDir, 0755); err != nil { |
| 114 | + t.Fatalf("failed to create cmd dir: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + mainContent := `package main |
| 118 | +
|
| 119 | +func main() { |
| 120 | + // Simple CLI with verbose flag |
| 121 | + verbose := "--verbose" |
| 122 | + _ = verbose |
| 123 | +} |
| 124 | +` |
| 125 | + if err := os.WriteFile(filepath.Join(cmdDir, "main.go"), []byte(mainContent), 0644); err != nil { |
| 126 | + t.Fatalf("failed to write main.go: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + files := []types.FileInfo{ |
| 130 | + {Path: "cmd/main.go", Extension: ".go"}, |
| 131 | + } |
| 132 | + |
| 133 | + // No CLI framework but has cmd/ directory |
| 134 | + techStack := &types.TechStack{ |
| 135 | + Languages: []types.Language{{Name: "Go"}}, |
| 136 | + } |
| 137 | + |
| 138 | + detector := NewCLIDetector(tmpDir, files, techStack) |
| 139 | + result := detector.Detect() |
| 140 | + |
| 141 | + if result == nil { |
| 142 | + t.Fatal("expected CLI info to be detected for cmd/ directory project") |
| 143 | + } |
| 144 | + |
| 145 | + if result.VerboseFlag == "" { |
| 146 | + t.Error("expected verbose flag to be detected") |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func TestCLIDetector_Detect_NotCLIProject(t *testing.T) { |
| 151 | + tmpDir, err := os.MkdirTemp("", "non-cli-test") |
| 152 | + if err != nil { |
| 153 | + t.Fatalf("failed to create temp dir: %v", err) |
| 154 | + } |
| 155 | + defer func() { _ = os.RemoveAll(tmpDir) }() |
| 156 | + |
| 157 | + files := []types.FileInfo{ |
| 158 | + {Path: "main.go", Extension: ".go"}, |
| 159 | + } |
| 160 | + |
| 161 | + // No CLI framework and no cmd/ directory |
| 162 | + techStack := &types.TechStack{ |
| 163 | + Languages: []types.Language{{Name: "Go"}}, |
| 164 | + Frameworks: []types.Framework{ |
| 165 | + {Name: "Gin", Category: "Web"}, |
| 166 | + }, |
| 167 | + } |
| 168 | + |
| 169 | + detector := NewCLIDetector(tmpDir, files, techStack) |
| 170 | + result := detector.Detect() |
| 171 | + |
| 172 | + if result != nil { |
| 173 | + t.Error("expected no CLI info for non-CLI project") |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func TestCLIDetector_Detect_NilTechStack(t *testing.T) { |
| 178 | + tmpDir, err := os.MkdirTemp("", "nil-techstack-test") |
| 179 | + if err != nil { |
| 180 | + t.Fatalf("failed to create temp dir: %v", err) |
| 181 | + } |
| 182 | + defer func() { _ = os.RemoveAll(tmpDir) }() |
| 183 | + |
| 184 | + detector := NewCLIDetector(tmpDir, nil, nil) |
| 185 | + result := detector.Detect() |
| 186 | + |
| 187 | + if result != nil { |
| 188 | + t.Error("expected no CLI info when techStack is nil") |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +func TestCLIDetector_IsCLIProject(t *testing.T) { |
| 193 | + tests := []struct { |
| 194 | + name string |
| 195 | + files []types.FileInfo |
| 196 | + techStack *types.TechStack |
| 197 | + expectCLI bool |
| 198 | + }{ |
| 199 | + { |
| 200 | + name: "cobra framework", |
| 201 | + files: []types.FileInfo{}, |
| 202 | + techStack: &types.TechStack{ |
| 203 | + Frameworks: []types.Framework{{Name: "Cobra"}}, |
| 204 | + }, |
| 205 | + expectCLI: true, |
| 206 | + }, |
| 207 | + { |
| 208 | + name: "click framework", |
| 209 | + files: []types.FileInfo{}, |
| 210 | + techStack: &types.TechStack{ |
| 211 | + Frameworks: []types.Framework{{Name: "Click"}}, |
| 212 | + }, |
| 213 | + expectCLI: true, |
| 214 | + }, |
| 215 | + { |
| 216 | + name: "commander framework", |
| 217 | + files: []types.FileInfo{}, |
| 218 | + techStack: &types.TechStack{ |
| 219 | + Frameworks: []types.Framework{{Name: "Commander"}}, |
| 220 | + }, |
| 221 | + expectCLI: true, |
| 222 | + }, |
| 223 | + { |
| 224 | + name: "cmd directory", |
| 225 | + files: []types.FileInfo{ |
| 226 | + {Path: "cmd/app/main.go", Extension: ".go"}, |
| 227 | + }, |
| 228 | + techStack: &types.TechStack{}, |
| 229 | + expectCLI: true, |
| 230 | + }, |
| 231 | + { |
| 232 | + name: "web framework only", |
| 233 | + files: []types.FileInfo{ |
| 234 | + {Path: "main.go", Extension: ".go"}, |
| 235 | + }, |
| 236 | + techStack: &types.TechStack{ |
| 237 | + Frameworks: []types.Framework{{Name: "Express"}}, |
| 238 | + }, |
| 239 | + expectCLI: false, |
| 240 | + }, |
| 241 | + } |
| 242 | + |
| 243 | + for _, tt := range tests { |
| 244 | + t.Run(tt.name, func(t *testing.T) { |
| 245 | + detector := &CLIDetector{ |
| 246 | + rootPath: "/tmp", |
| 247 | + files: tt.files, |
| 248 | + techStack: tt.techStack, |
| 249 | + } |
| 250 | + result := detector.isCLIProject() |
| 251 | + if result != tt.expectCLI { |
| 252 | + t.Errorf("expected isCLIProject=%v, got %v", tt.expectCLI, result) |
| 253 | + } |
| 254 | + }) |
| 255 | + } |
| 256 | +} |
0 commit comments