TestFixDirFlagDefaultBehavior tests that empty dir defaults to .github/workflows
(t *testing.T)
| 134 | |
| 135 | // TestFixDirFlagDefaultBehavior tests that empty dir defaults to .github/workflows |
| 136 | func TestFixDirFlagDefaultBehavior(t *testing.T) { |
| 137 | // Create a temporary directory for test |
| 138 | tmpDir := t.TempDir() |
| 139 | defaultDir := filepath.Join(tmpDir, ".github", "workflows") |
| 140 | if err := os.MkdirAll(defaultDir, 0755); err != nil { |
| 141 | t.Fatalf("Failed to create default workflow directory: %v", err) |
| 142 | } |
| 143 | |
| 144 | // Create a test workflow |
| 145 | workflowContent := `--- |
| 146 | on: workflow_dispatch |
| 147 | timeout_minutes: 30 |
| 148 | --- |
| 149 | # Test Workflow |
| 150 | ` |
| 151 | workflowFile := filepath.Join(defaultDir, "test.md") |
| 152 | if err := os.WriteFile(workflowFile, []byte(workflowContent), 0644); err != nil { |
| 153 | t.Fatalf("Failed to create test workflow file: %v", err) |
| 154 | } |
| 155 | |
| 156 | // Save original directory and change to tmpDir |
| 157 | originalWd, err := os.Getwd() |
| 158 | if err != nil { |
| 159 | t.Fatalf("Failed to get current working directory: %v", err) |
| 160 | } |
| 161 | defer func() { |
| 162 | _ = os.Chdir(originalWd) |
| 163 | }() |
| 164 | |
| 165 | if err := os.Chdir(tmpDir); err != nil { |
| 166 | t.Fatalf("Failed to change to temp directory: %v", err) |
| 167 | } |
| 168 | |
| 169 | // Run fix command without --dir flag (should default to .github/workflows) |
| 170 | config := FixConfig{ |
| 171 | WorkflowIDs: []string{}, |
| 172 | Write: true, |
| 173 | Verbose: false, |
| 174 | WorkflowDir: "", // Empty should default to .github/workflows |
| 175 | } |
| 176 | |
| 177 | err = RunFix(config) |
| 178 | if err != nil { |
| 179 | t.Fatalf("RunFix with default dir should succeed, got error: %v", err) |
| 180 | } |
| 181 | |
| 182 | // Verify the file was fixed |
| 183 | updatedContent, err := os.ReadFile(workflowFile) |
| 184 | if err != nil { |
| 185 | t.Fatalf("Failed to read updated file: %v", err) |
| 186 | } |
| 187 | |
| 188 | if !strings.Contains(string(updatedContent), "timeout-minutes: 30") { |
| 189 | t.Error("Expected file in default directory to be fixed") |
| 190 | } |
| 191 | } |