|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package workflow |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// TestCreatePullRequestWithCustomBaseBranch tests end-to-end workflow compilation with custom base-branch |
| 13 | +func TestCreatePullRequestWithCustomBaseBranch(t *testing.T) { |
| 14 | + tmpDir, err := os.MkdirTemp("", "base-branch-test") |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 17 | + } |
| 18 | + defer os.RemoveAll(tmpDir) |
| 19 | + |
| 20 | + // Create test workflow with custom base-branch |
| 21 | + workflowContent := `--- |
| 22 | +on: push |
| 23 | +permissions: |
| 24 | + contents: read |
| 25 | + actions: read |
| 26 | + issues: read |
| 27 | + pull-requests: read |
| 28 | +engine: copilot |
| 29 | +safe-outputs: |
| 30 | + create-pull-request: |
| 31 | + target-repo: "microsoft/vscode-docs" |
| 32 | + base-branch: vnext |
| 33 | + draft: true |
| 34 | +--- |
| 35 | +
|
| 36 | +# Test Workflow |
| 37 | +
|
| 38 | +Create a pull request targeting vnext branch in cross-repo. |
| 39 | +` |
| 40 | + |
| 41 | + workflowPath := filepath.Join(tmpDir, "test-workflow.md") |
| 42 | + if err := os.WriteFile(workflowPath, []byte(workflowContent), 0644); err != nil { |
| 43 | + t.Fatalf("Failed to write workflow file: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Compile the workflow |
| 47 | + compiler := NewCompiler() |
| 48 | + if err := compiler.CompileWorkflow(workflowPath); err != nil { |
| 49 | + t.Fatalf("Failed to compile workflow: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + // Read the compiled output |
| 53 | + outputFile := filepath.Join(tmpDir, "test-workflow.lock.yml") |
| 54 | + compiledBytes, err := os.ReadFile(outputFile) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("Failed to read compiled output: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + compiledContent := string(compiledBytes) |
| 60 | + |
| 61 | + // Verify GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG contains base_branch set to "vnext" |
| 62 | + // The JSON is escaped in YAML, so we need to look for the escaped version |
| 63 | + if !strings.Contains(compiledContent, `\"base_branch\":\"vnext\"`) { |
| 64 | + t.Error("Expected handler config to contain base_branch set to vnext in compiled workflow") |
| 65 | + } |
| 66 | + |
| 67 | + // Verify it does NOT contain the default github.ref_name expression |
| 68 | + if strings.Contains(compiledContent, `\"base_branch\":\"${{ github.ref_name }}\"`) { |
| 69 | + t.Error("Did not expect handler config to use github.ref_name when base-branch is explicitly set") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// TestCreatePullRequestWithDefaultBaseBranch tests workflow compilation with default base-branch |
| 74 | +func TestCreatePullRequestWithDefaultBaseBranch(t *testing.T) { |
| 75 | + tmpDir, err := os.MkdirTemp("", "default-base-branch-test") |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 78 | + } |
| 79 | + defer os.RemoveAll(tmpDir) |
| 80 | + |
| 81 | + // Create test workflow without base-branch field |
| 82 | + workflowContent := `--- |
| 83 | +on: push |
| 84 | +permissions: |
| 85 | + contents: read |
| 86 | + actions: read |
| 87 | + issues: read |
| 88 | + pull-requests: read |
| 89 | +engine: copilot |
| 90 | +safe-outputs: |
| 91 | + create-pull-request: |
| 92 | + draft: true |
| 93 | +--- |
| 94 | +
|
| 95 | +# Test Workflow |
| 96 | +
|
| 97 | +Create a pull request with default base branch. |
| 98 | +` |
| 99 | + |
| 100 | + workflowPath := filepath.Join(tmpDir, "test-default.md") |
| 101 | + if err := os.WriteFile(workflowPath, []byte(workflowContent), 0644); err != nil { |
| 102 | + t.Fatalf("Failed to write workflow file: %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + // Compile the workflow |
| 106 | + compiler := NewCompiler() |
| 107 | + if err := compiler.CompileWorkflow(workflowPath); err != nil { |
| 108 | + t.Fatalf("Failed to compile workflow: %v", err) |
| 109 | + } |
| 110 | + |
| 111 | + // Read the compiled output |
| 112 | + outputFile := filepath.Join(tmpDir, "test-default.lock.yml") |
| 113 | + compiledBytes, err := os.ReadFile(outputFile) |
| 114 | + if err != nil { |
| 115 | + t.Fatalf("Failed to read compiled output: %v", err) |
| 116 | + } |
| 117 | + |
| 118 | + compiledContent := string(compiledBytes) |
| 119 | + |
| 120 | + // Verify GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG uses github.ref_name by default |
| 121 | + // The JSON is escaped in YAML, so we need to look for the escaped version |
| 122 | + if !strings.Contains(compiledContent, `\"base_branch\":\"${{ github.ref_name }}\"`) { |
| 123 | + t.Error("Expected handler config to use github.ref_name when base-branch is not specified") |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// TestCreatePullRequestWithBranchSlash tests workflow compilation with branch containing slash |
| 128 | +func TestCreatePullRequestWithBranchSlash(t *testing.T) { |
| 129 | + tmpDir, err := os.MkdirTemp("", "branch-slash-test") |
| 130 | + if err != nil { |
| 131 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 132 | + } |
| 133 | + defer os.RemoveAll(tmpDir) |
| 134 | + |
| 135 | + // Create test workflow with base-branch containing slash |
| 136 | + workflowContent := `--- |
| 137 | +on: push |
| 138 | +permissions: |
| 139 | + contents: read |
| 140 | + actions: read |
| 141 | + issues: read |
| 142 | + pull-requests: read |
| 143 | +engine: copilot |
| 144 | +safe-outputs: |
| 145 | + create-pull-request: |
| 146 | + base-branch: release/v1.0 |
| 147 | + draft: true |
| 148 | +--- |
| 149 | +
|
| 150 | +# Test Workflow |
| 151 | +
|
| 152 | +Create a pull request targeting release/v1.0 branch. |
| 153 | +` |
| 154 | + |
| 155 | + workflowPath := filepath.Join(tmpDir, "test-slash.md") |
| 156 | + if err := os.WriteFile(workflowPath, []byte(workflowContent), 0644); err != nil { |
| 157 | + t.Fatalf("Failed to write workflow file: %v", err) |
| 158 | + } |
| 159 | + |
| 160 | + // Compile the workflow |
| 161 | + compiler := NewCompiler() |
| 162 | + if err := compiler.CompileWorkflow(workflowPath); err != nil { |
| 163 | + t.Fatalf("Failed to compile workflow: %v", err) |
| 164 | + } |
| 165 | + |
| 166 | + // Read the compiled output |
| 167 | + outputFile := filepath.Join(tmpDir, "test-slash.lock.yml") |
| 168 | + compiledBytes, err := os.ReadFile(outputFile) |
| 169 | + if err != nil { |
| 170 | + t.Fatalf("Failed to read compiled output: %v", err) |
| 171 | + } |
| 172 | + |
| 173 | + compiledContent := string(compiledBytes) |
| 174 | + |
| 175 | + // Verify GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG contains base_branch set to "release/v1.0" |
| 176 | + // The JSON is escaped in YAML, so we need to look for the escaped version |
| 177 | + if !strings.Contains(compiledContent, `\"base_branch\":\"release/v1.0\"`) { |
| 178 | + t.Error("Expected handler config to contain base_branch set to release/v1.0 in compiled workflow") |
| 179 | + } |
| 180 | +} |
0 commit comments