Skip to content

Commit 0c1a193

Browse files
perf: optimize compile config validation tests (#3788)
1 parent 544dd51 commit 0c1a193

2 files changed

Lines changed: 42 additions & 20 deletions

File tree

pkg/cli/compile_command.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,32 @@ type CompilationStats struct {
173173
FailedWorkflows []string // Names of workflows that failed compilation
174174
}
175175

176+
// validateCompileConfig validates the configuration flags before compilation
177+
// This is extracted for faster testing without full compilation
178+
func validateCompileConfig(config CompileConfig) error {
179+
// Validate dependabot flag usage
180+
if config.Dependabot {
181+
if len(config.MarkdownFiles) > 0 {
182+
return fmt.Errorf("--dependabot flag cannot be used with specific workflow files")
183+
}
184+
if config.WorkflowDir != "" && config.WorkflowDir != ".github/workflows" {
185+
return fmt.Errorf("--dependabot flag cannot be used with custom --workflows-dir")
186+
}
187+
}
188+
189+
// Validate purge flag usage
190+
if config.Purge && len(config.MarkdownFiles) > 0 {
191+
return fmt.Errorf("--purge flag can only be used when compiling all markdown files (no specific files specified)")
192+
}
193+
194+
// Validate workflow directory path
195+
if config.WorkflowDir != "" && filepath.IsAbs(config.WorkflowDir) {
196+
return fmt.Errorf("workflows-dir must be a relative path, got: %s", config.WorkflowDir)
197+
}
198+
199+
return nil
200+
}
201+
176202
func CompileWorkflows(config CompileConfig) ([]*workflow.WorkflowData, error) {
177203
markdownFiles := config.MarkdownFiles
178204
verbose := config.Verbose
@@ -196,30 +222,16 @@ func CompileWorkflows(config CompileConfig) ([]*workflow.WorkflowData, error) {
196222
// Track compilation statistics
197223
stats := &CompilationStats{}
198224

199-
// Validate dependabot flag usage
200-
if dependabot {
201-
if len(markdownFiles) > 0 {
202-
return nil, fmt.Errorf("--dependabot flag cannot be used with specific workflow files")
203-
}
204-
if workflowDir != "" && workflowDir != ".github/workflows" {
205-
return nil, fmt.Errorf("--dependabot flag cannot be used with custom --workflows-dir")
206-
}
207-
}
208-
209-
// Validate purge flag usage
210-
if purge && len(markdownFiles) > 0 {
211-
return nil, fmt.Errorf("--purge flag can only be used when compiling all markdown files (no specific files specified)")
225+
// Validate configuration
226+
if err := validateCompileConfig(config); err != nil {
227+
return nil, err
212228
}
213229

214230
// Validate and set default for workflow directory
215231
if workflowDir == "" {
216232
workflowDir = ".github/workflows"
217233
compileLog.Printf("Using default workflow directory: %s", workflowDir)
218234
} else {
219-
// Ensure the path is relative
220-
if filepath.IsAbs(workflowDir) {
221-
return nil, fmt.Errorf("workflows-dir must be a relative path, got: %s", workflowDir)
222-
}
223235
// Clean the path to avoid issues with ".." or other problematic elements
224236
workflowDir = filepath.Clean(workflowDir)
225237
compileLog.Printf("Using custom workflow directory: %s", workflowDir)

pkg/cli/compile_command_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func TestCompileWorkflowWithValidation_InvalidFile(t *testing.T) {
137137
}
138138

139139
// TestCompileWorkflows_DependabotValidation tests dependabot flag validation
140+
// Uses the fast validateCompileConfig function instead of full compilation
140141
func TestCompileWorkflows_DependabotValidation(t *testing.T) {
141142
tests := []struct {
142143
name string
@@ -174,27 +175,32 @@ func TestCompileWorkflows_DependabotValidation(t *testing.T) {
174175

175176
for _, tt := range tests {
176177
t.Run(tt.name, func(t *testing.T) {
177-
_, err := CompileWorkflows(tt.config)
178+
// Use fast validation function instead of full compilation
179+
err := validateCompileConfig(tt.config)
178180

179181
if tt.expectError {
180182
if err == nil {
181183
t.Error("Expected error but got nil")
182184
} else if !strings.Contains(err.Error(), tt.errorMsg) {
183185
t.Errorf("Expected error containing %q, got %q", tt.errorMsg, err.Error())
184186
}
187+
} else if err != nil {
188+
t.Errorf("Expected no error but got: %v", err)
185189
}
186190
})
187191
}
188192
}
189193

190194
// TestCompileWorkflows_PurgeValidation tests purge flag validation
195+
// Uses the fast validateCompileConfig function instead of full compilation
191196
func TestCompileWorkflows_PurgeValidation(t *testing.T) {
192197
config := CompileConfig{
193198
Purge: true,
194199
MarkdownFiles: []string{"test.md"},
195200
}
196201

197-
_, err := CompileWorkflows(config)
202+
// Use fast validation function instead of full compilation
203+
err := validateCompileConfig(config)
198204

199205
if err == nil {
200206
t.Error("Expected error when using purge with specific files, got nil")
@@ -206,6 +212,7 @@ func TestCompileWorkflows_PurgeValidation(t *testing.T) {
206212
}
207213

208214
// TestCompileWorkflows_WorkflowDirValidation tests workflow directory validation
215+
// Uses the fast validateCompileConfig function instead of full compilation
209216
func TestCompileWorkflows_WorkflowDirValidation(t *testing.T) {
210217
tests := []struct {
211218
name string
@@ -237,14 +244,17 @@ func TestCompileWorkflows_WorkflowDirValidation(t *testing.T) {
237244
WorkflowDir: tt.workflowDir,
238245
}
239246

240-
_, err := CompileWorkflows(config)
247+
// Use fast validation function instead of full compilation
248+
err := validateCompileConfig(config)
241249

242250
if tt.expectError {
243251
if err == nil {
244252
t.Error("Expected error but got nil")
245253
} else if !strings.Contains(err.Error(), tt.errorMsg) {
246254
t.Errorf("Expected error containing %q, got %q", tt.errorMsg, err.Error())
247255
}
256+
} else if err != nil {
257+
t.Errorf("Expected no error but got: %v", err)
248258
}
249259
})
250260
}

0 commit comments

Comments
 (0)