|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/drgsn/filefusion/internal/core" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestValidateAndGetConfig(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + pattern string |
| 15 | + exclude string |
| 16 | + maxFileSize string |
| 17 | + maxOutputSize string |
| 18 | + outputPath string |
| 19 | + expectError bool |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "Valid config", |
| 23 | + pattern: "*.go,*.json", |
| 24 | + exclude: "vendor/*", |
| 25 | + maxFileSize: "10MB", |
| 26 | + maxOutputSize: "50MB", |
| 27 | + outputPath: "output.xml", |
| 28 | + expectError: false, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "Empty pattern", |
| 32 | + pattern: "", |
| 33 | + maxFileSize: "10MB", |
| 34 | + maxOutputSize: "50MB", |
| 35 | + expectError: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "Invalid max file size", |
| 39 | + pattern: "*.go", |
| 40 | + maxFileSize: "invalid", |
| 41 | + maxOutputSize: "50MB", |
| 42 | + expectError: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Invalid max output size", |
| 46 | + pattern: "*.go", |
| 47 | + maxFileSize: "10MB", |
| 48 | + maxOutputSize: "invalid", |
| 49 | + expectError: true, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "Invalid output extension", |
| 53 | + pattern: "*.go", |
| 54 | + maxFileSize: "10MB", |
| 55 | + maxOutputSize: "50MB", |
| 56 | + outputPath: "output.invalid", |
| 57 | + expectError: true, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tt := range tests { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + // Set up test flags |
| 64 | + pattern = tt.pattern |
| 65 | + exclude = tt.exclude |
| 66 | + maxFileSize = tt.maxFileSize |
| 67 | + maxOutputSize = tt.maxOutputSize |
| 68 | + outputPath = tt.outputPath |
| 69 | + |
| 70 | + config, err := validateAndGetConfig([]string{}) |
| 71 | + |
| 72 | + if tt.expectError { |
| 73 | + assert.Error(t, err) |
| 74 | + } else { |
| 75 | + assert.NoError(t, err) |
| 76 | + assert.NotNil(t, config) |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestValidateAndGetOutputType(t *testing.T) { |
| 83 | + tests := []struct { |
| 84 | + name string |
| 85 | + outputPath string |
| 86 | + expectType core.OutputType |
| 87 | + expectError bool |
| 88 | + }{ |
| 89 | + { |
| 90 | + name: "XML output", |
| 91 | + outputPath: "output.xml", |
| 92 | + expectType: core.OutputTypeXML, |
| 93 | + expectError: false, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "JSON output", |
| 97 | + outputPath: "output.json", |
| 98 | + expectType: core.OutputTypeJSON, |
| 99 | + expectError: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "YAML output", |
| 103 | + outputPath: "output.yaml", |
| 104 | + expectType: core.OutputTypeYAML, |
| 105 | + expectError: false, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "YML output", |
| 109 | + outputPath: "output.yml", |
| 110 | + expectType: core.OutputTypeYAML, |
| 111 | + expectError: false, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "Empty path defaults to XML", |
| 115 | + outputPath: "", |
| 116 | + expectType: core.OutputTypeXML, |
| 117 | + expectError: false, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "Invalid extension", |
| 121 | + outputPath: "output.txt", |
| 122 | + expectError: true, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + for _, tt := range tests { |
| 127 | + t.Run(tt.name, func(t *testing.T) { |
| 128 | + outputType, err := validateAndGetOutputType(tt.outputPath) |
| 129 | + |
| 130 | + if tt.expectError { |
| 131 | + assert.Error(t, err) |
| 132 | + } else { |
| 133 | + assert.NoError(t, err) |
| 134 | + assert.Equal(t, tt.expectType, outputType) |
| 135 | + } |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestGetCleanerOptions(t *testing.T) { |
| 141 | + tests := []struct { |
| 142 | + name string |
| 143 | + cleanEnabled bool |
| 144 | + wantNil bool |
| 145 | + }{ |
| 146 | + { |
| 147 | + name: "Cleaner disabled", |
| 148 | + cleanEnabled: false, |
| 149 | + wantNil: true, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "Cleaner enabled", |
| 153 | + cleanEnabled: true, |
| 154 | + wantNil: false, |
| 155 | + }, |
| 156 | + } |
| 157 | + |
| 158 | + for _, tt := range tests { |
| 159 | + t.Run(tt.name, func(t *testing.T) { |
| 160 | + cleanEnabled = tt.cleanEnabled |
| 161 | + opts := getCleanerOptions() |
| 162 | + |
| 163 | + if tt.wantNil { |
| 164 | + assert.Nil(t, opts) |
| 165 | + } else { |
| 166 | + assert.NotNil(t, opts) |
| 167 | + } |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func TestRunMix(t *testing.T) { |
| 173 | + // Save current working directory |
| 174 | + origWd, err := os.Getwd() |
| 175 | + if err != nil { |
| 176 | + t.Fatal(err) |
| 177 | + } |
| 178 | + defer os.Chdir(origWd) |
| 179 | + |
| 180 | + // Create a temporary directory for test files |
| 181 | + tmpDir, err := os.MkdirTemp("", "filefusion-test-*") |
| 182 | + if err != nil { |
| 183 | + t.Fatal(err) |
| 184 | + } |
| 185 | + defer os.RemoveAll(tmpDir) |
| 186 | + |
| 187 | + // Change to temp directory |
| 188 | + if err := os.Chdir(tmpDir); err != nil { |
| 189 | + t.Fatal(err) |
| 190 | + } |
| 191 | + |
| 192 | + // Create a test file |
| 193 | + testFile := "test.go" |
| 194 | + if err := os.WriteFile(testFile, []byte("package test"), 0644); err != nil { |
| 195 | + t.Fatal(err) |
| 196 | + } |
| 197 | + |
| 198 | + // Create output directory |
| 199 | + if err := os.MkdirAll("output", 0755); err != nil { |
| 200 | + t.Fatal(err) |
| 201 | + } |
| 202 | + |
| 203 | + tests := []struct { |
| 204 | + name string |
| 205 | + args []string |
| 206 | + pattern string |
| 207 | + outputPath string |
| 208 | + dryRun bool |
| 209 | + expectError bool |
| 210 | + }{ |
| 211 | + { |
| 212 | + name: "Dry run", |
| 213 | + args: []string{"."}, |
| 214 | + pattern: "*.go", |
| 215 | + dryRun: true, |
| 216 | + expectError: false, |
| 217 | + }, |
| 218 | + { |
| 219 | + name: "Valid run with output", |
| 220 | + args: []string{"."}, |
| 221 | + pattern: "*.go", |
| 222 | + outputPath: "output/output.xml", |
| 223 | + expectError: false, |
| 224 | + }, |
| 225 | + { |
| 226 | + name: "Invalid pattern", |
| 227 | + args: []string{"."}, |
| 228 | + pattern: "[", |
| 229 | + expectError: true, |
| 230 | + }, |
| 231 | + } |
| 232 | + |
| 233 | + for _, tt := range tests { |
| 234 | + t.Run(tt.name, func(t *testing.T) { |
| 235 | + // Set up test flags |
| 236 | + pattern = tt.pattern |
| 237 | + outputPath = tt.outputPath |
| 238 | + dryRun = tt.dryRun |
| 239 | + |
| 240 | + err := runMix(rootCmd, tt.args) |
| 241 | + |
| 242 | + if tt.expectError { |
| 243 | + assert.Error(t, err) |
| 244 | + } else { |
| 245 | + assert.NoError(t, err) |
| 246 | + if !tt.dryRun && tt.outputPath != "" { |
| 247 | + // Verify output file exists and is not empty |
| 248 | + info, err := os.Stat(tt.outputPath) |
| 249 | + assert.NoError(t, err) |
| 250 | + assert.Greater(t, info.Size(), int64(0)) |
| 251 | + } |
| 252 | + } |
| 253 | + }) |
| 254 | + } |
| 255 | +} |
0 commit comments