|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// TestCompileWithDirFlag tests the --dir flag functionality |
| 11 | +func TestCompileWithDirFlag(t *testing.T) { |
| 12 | + // Save current directory and defer restoration |
| 13 | + originalWd, err := os.Getwd() |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("Failed to get current working directory: %v", err) |
| 16 | + } |
| 17 | + defer func() { |
| 18 | + _ = os.Chdir(originalWd) |
| 19 | + }() |
| 20 | + |
| 21 | + // Create a temporary git repository with custom workflow directory |
| 22 | + tmpDir, err := os.MkdirTemp("", "dir-flag-test") |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("Failed to create temp directory: %v", err) |
| 25 | + } |
| 26 | + defer os.RemoveAll(tmpDir) |
| 27 | + |
| 28 | + // Change to temp directory |
| 29 | + if err := os.Chdir(tmpDir); err != nil { |
| 30 | + t.Fatalf("Failed to change to temp directory: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + // Initialize git repository properly |
| 34 | + cmd := exec.Command("git", "init") |
| 35 | + cmd.Dir = tmpDir |
| 36 | + if err := cmd.Run(); err != nil { |
| 37 | + t.Fatalf("Failed to initialize git repository: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + // Create custom workflow directory |
| 41 | + customDir := "my-workflows" |
| 42 | + if err := os.MkdirAll(customDir, 0755); err != nil { |
| 43 | + t.Fatalf("Failed to create custom workflow directory: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Create a test workflow file |
| 47 | + workflowContent := `--- |
| 48 | +on: push |
| 49 | +--- |
| 50 | +
|
| 51 | +# Test Workflow |
| 52 | +
|
| 53 | +This is a test workflow in a custom directory. |
| 54 | +` |
| 55 | + workflowFile := filepath.Join(customDir, "test.md") |
| 56 | + if err := os.WriteFile(workflowFile, []byte(workflowContent), 0644); err != nil { |
| 57 | + t.Fatalf("Failed to create test workflow file: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + // Test: Compile with --dir flag should work |
| 61 | + config := CompileConfig{ |
| 62 | + MarkdownFiles: []string{}, |
| 63 | + Verbose: false, |
| 64 | + EngineOverride: "", |
| 65 | + Validate: false, |
| 66 | + Watch: false, |
| 67 | + WorkflowDir: customDir, |
| 68 | + SkipInstructions: false, |
| 69 | + NoEmit: false, |
| 70 | + Purge: false, |
| 71 | + TrialMode: false, |
| 72 | + TrialLogicalRepoSlug: "", |
| 73 | + } |
| 74 | + _, err = CompileWorkflows(config) |
| 75 | + if err != nil { |
| 76 | + t.Errorf("CompileWorkflows with --dir should succeed, got error: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + // Verify the lock file was created |
| 80 | + lockFile := filepath.Join(customDir, "test.lock.yml") |
| 81 | + if _, err := os.Stat(lockFile); os.IsNotExist(err) { |
| 82 | + t.Error("Expected lock file to be created in custom directory") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// TestCompileDirFlagValidation tests the validation of --dir flag |
| 87 | +func TestCompileDirFlagValidation(t *testing.T) { |
| 88 | + tests := []struct { |
| 89 | + name string |
| 90 | + workflowDir string |
| 91 | + expectError bool |
| 92 | + errorMsg string |
| 93 | + }{ |
| 94 | + { |
| 95 | + name: "relative path is valid", |
| 96 | + workflowDir: "custom/workflows", |
| 97 | + expectError: false, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "absolute path is invalid", |
| 101 | + workflowDir: "/absolute/path", |
| 102 | + expectError: true, |
| 103 | + errorMsg: "--dir must be a relative path, got: /absolute/path", |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "empty string defaults to .github/workflows", |
| 107 | + workflowDir: "", |
| 108 | + expectError: false, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + for _, tt := range tests { |
| 113 | + t.Run(tt.name, func(t *testing.T) { |
| 114 | + // Create a temporary directory for each test |
| 115 | + tmpDir, err := os.MkdirTemp("", "dir-flag-validation-test") |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("Failed to create temp directory: %v", err) |
| 118 | + } |
| 119 | + defer os.RemoveAll(tmpDir) |
| 120 | + |
| 121 | + originalWd, err := os.Getwd() |
| 122 | + if err != nil { |
| 123 | + t.Fatalf("Failed to get current working directory: %v", err) |
| 124 | + } |
| 125 | + defer func() { |
| 126 | + _ = os.Chdir(originalWd) |
| 127 | + }() |
| 128 | + |
| 129 | + if err := os.Chdir(tmpDir); err != nil { |
| 130 | + t.Fatalf("Failed to change to temp directory: %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + // Initialize git repository properly |
| 134 | + cmd := exec.Command("git", "init") |
| 135 | + cmd.Dir = tmpDir |
| 136 | + if err := cmd.Run(); err != nil { |
| 137 | + t.Fatalf("Failed to initialize git repository: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + // For non-error cases, create the expected directory |
| 141 | + if !tt.expectError { |
| 142 | + expectedDir := tt.workflowDir |
| 143 | + if expectedDir == "" { |
| 144 | + expectedDir = ".github/workflows" |
| 145 | + } |
| 146 | + if err := os.MkdirAll(expectedDir, 0755); err != nil { |
| 147 | + t.Fatalf("Failed to create workflow directory: %v", err) |
| 148 | + } |
| 149 | + // Create a dummy workflow file |
| 150 | + workflowFile := filepath.Join(expectedDir, "test.md") |
| 151 | + workflowContent := `--- |
| 152 | +on: push |
| 153 | +--- |
| 154 | +
|
| 155 | +# Test Workflow |
| 156 | +` |
| 157 | + if err := os.WriteFile(workflowFile, []byte(workflowContent), 0644); err != nil { |
| 158 | + t.Fatalf("Failed to create test workflow file: %v", err) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + // Test the compilation |
| 163 | + config := CompileConfig{ |
| 164 | + MarkdownFiles: []string{}, |
| 165 | + Verbose: false, |
| 166 | + EngineOverride: "", |
| 167 | + Validate: false, |
| 168 | + Watch: false, |
| 169 | + WorkflowDir: tt.workflowDir, |
| 170 | + SkipInstructions: false, |
| 171 | + NoEmit: false, |
| 172 | + Purge: false, |
| 173 | + TrialMode: false, |
| 174 | + TrialLogicalRepoSlug: "", |
| 175 | + } |
| 176 | + _, err = CompileWorkflows(config) |
| 177 | + |
| 178 | + if tt.expectError { |
| 179 | + if err == nil { |
| 180 | + t.Errorf("Expected error for --dir '%s', but got none", tt.workflowDir) |
| 181 | + } else if err.Error() != tt.errorMsg { |
| 182 | + t.Errorf("Expected error message '%s', got '%s'", tt.errorMsg, err.Error()) |
| 183 | + } |
| 184 | + } else { |
| 185 | + if err != nil { |
| 186 | + t.Errorf("Expected no error for --dir '%s', but got: %v", tt.workflowDir, err) |
| 187 | + } |
| 188 | + } |
| 189 | + }) |
| 190 | + } |
| 191 | +} |
0 commit comments