|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// TestPermissionsWarningInNonStrictMode tests that under-provisioned permissions |
| 13 | +// produce warnings in non-strict mode rather than errors |
| 14 | +func TestPermissionsWarningInNonStrictMode(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + content string |
| 18 | + strict bool |
| 19 | + expectError bool |
| 20 | + expectWarning bool |
| 21 | + warningMessage string |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "missing permissions in non-strict mode produces warning", |
| 25 | + content: `--- |
| 26 | +on: push |
| 27 | +permissions: |
| 28 | + contents: read |
| 29 | +tools: |
| 30 | + github: |
| 31 | + toolsets: [repos, issues] |
| 32 | + read-only: false |
| 33 | +--- |
| 34 | +
|
| 35 | +# Test Workflow |
| 36 | +`, |
| 37 | + strict: false, |
| 38 | + expectError: false, |
| 39 | + expectWarning: true, |
| 40 | + warningMessage: "Missing required permissions for github toolsets:", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "missing permissions in strict mode produces error", |
| 44 | + content: `--- |
| 45 | +on: push |
| 46 | +permissions: |
| 47 | + contents: read |
| 48 | +engine: copilot |
| 49 | +network: |
| 50 | + allowed: |
| 51 | + - "api.example.com" |
| 52 | +tools: |
| 53 | + github: |
| 54 | + toolsets: [repos, issues] |
| 55 | + read-only: false |
| 56 | +--- |
| 57 | +
|
| 58 | +# Test Workflow |
| 59 | +`, |
| 60 | + strict: true, |
| 61 | + expectError: true, |
| 62 | + expectWarning: false, |
| 63 | + warningMessage: "", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "sufficient permissions in non-strict mode produces no warning", |
| 67 | + content: `--- |
| 68 | +on: push |
| 69 | +permissions: |
| 70 | + contents: write |
| 71 | + issues: write |
| 72 | +tools: |
| 73 | + github: |
| 74 | + toolsets: [repos, issues] |
| 75 | +--- |
| 76 | +
|
| 77 | +# Test Workflow |
| 78 | +`, |
| 79 | + strict: false, |
| 80 | + expectError: false, |
| 81 | + expectWarning: false, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "sufficient permissions in strict mode produces no error", |
| 85 | + content: `--- |
| 86 | +on: push |
| 87 | +permissions: |
| 88 | + contents: read |
| 89 | + issues: read |
| 90 | +engine: copilot |
| 91 | +network: |
| 92 | + allowed: |
| 93 | + - "api.example.com" |
| 94 | +tools: |
| 95 | + github: |
| 96 | + toolsets: [repos, issues] |
| 97 | + read-only: true |
| 98 | +--- |
| 99 | +
|
| 100 | +# Test Workflow |
| 101 | +`, |
| 102 | + strict: true, |
| 103 | + expectError: false, |
| 104 | + expectWarning: false, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + for _, tt := range tests { |
| 109 | + t.Run(tt.name, func(t *testing.T) { |
| 110 | + tmpDir, err := os.MkdirTemp("", "permissions-warning-test") |
| 111 | + if err != nil { |
| 112 | + t.Fatal(err) |
| 113 | + } |
| 114 | + defer os.RemoveAll(tmpDir) |
| 115 | + |
| 116 | + testFile := filepath.Join(tmpDir, "test-workflow.md") |
| 117 | + if err := os.WriteFile(testFile, []byte(tt.content), 0644); err != nil { |
| 118 | + t.Fatal(err) |
| 119 | + } |
| 120 | + |
| 121 | + // Capture stderr to check for warnings |
| 122 | + oldStderr := os.Stderr |
| 123 | + r, w, _ := os.Pipe() |
| 124 | + os.Stderr = w |
| 125 | + |
| 126 | + compiler := NewCompiler(false, "", "test") |
| 127 | + compiler.SetStrictMode(tt.strict) |
| 128 | + err = compiler.CompileWorkflow(testFile) |
| 129 | + |
| 130 | + // Restore stderr |
| 131 | + w.Close() |
| 132 | + os.Stderr = oldStderr |
| 133 | + var buf bytes.Buffer |
| 134 | + io.Copy(&buf, r) |
| 135 | + stderrOutput := buf.String() |
| 136 | + |
| 137 | + // Check error expectation |
| 138 | + if tt.expectError && err == nil { |
| 139 | + t.Error("Expected compilation to fail but it succeeded") |
| 140 | + } else if !tt.expectError && err != nil { |
| 141 | + t.Errorf("Expected compilation to succeed but it failed: %v", err) |
| 142 | + } |
| 143 | + |
| 144 | + // Check warning expectation |
| 145 | + if tt.expectWarning { |
| 146 | + if !strings.Contains(stderrOutput, tt.warningMessage) { |
| 147 | + t.Errorf("Expected warning containing '%s', got stderr:\n%s", tt.warningMessage, stderrOutput) |
| 148 | + } |
| 149 | + if !strings.Contains(stderrOutput, "warning:") { |
| 150 | + t.Errorf("Expected 'warning:' in stderr output, got:\n%s", stderrOutput) |
| 151 | + } |
| 152 | + // Check for the new suggestion format |
| 153 | + if !strings.Contains(stderrOutput, "Option 1: Add missing permissions") { |
| 154 | + t.Errorf("Expected 'Option 1: Add missing permissions' in warning, got:\n%s", stderrOutput) |
| 155 | + } |
| 156 | + if !strings.Contains(stderrOutput, "Option 2: Reduce the required toolsets") { |
| 157 | + t.Errorf("Expected 'Option 2: Reduce the required toolsets' in warning, got:\n%s", stderrOutput) |
| 158 | + } |
| 159 | + } else { |
| 160 | + // For non-warning cases, we should not see the warning message content |
| 161 | + if tt.warningMessage != "" && strings.Contains(stderrOutput, tt.warningMessage) { |
| 162 | + t.Errorf("Unexpected warning in stderr output:\n%s", stderrOutput) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Verify warning count |
| 167 | + if tt.expectWarning { |
| 168 | + warningCount := compiler.GetWarningCount() |
| 169 | + if warningCount == 0 { |
| 170 | + t.Error("Expected warning count > 0 but got 0") |
| 171 | + } |
| 172 | + } |
| 173 | + }) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +// TestPermissionsWarningMessageFormat tests that the warning message format |
| 178 | +// includes both options for fixing the issue |
| 179 | +func TestPermissionsWarningMessageFormat(t *testing.T) { |
| 180 | + tmpDir, err := os.MkdirTemp("", "permissions-warning-format-test") |
| 181 | + if err != nil { |
| 182 | + t.Fatal(err) |
| 183 | + } |
| 184 | + defer os.RemoveAll(tmpDir) |
| 185 | + |
| 186 | + content := `--- |
| 187 | +on: push |
| 188 | +permissions: |
| 189 | + contents: read |
| 190 | +tools: |
| 191 | + github: |
| 192 | + toolsets: [repos, issues, pull_requests] |
| 193 | + read-only: false |
| 194 | +--- |
| 195 | +
|
| 196 | +# Test Workflow |
| 197 | +` |
| 198 | + |
| 199 | + testFile := filepath.Join(tmpDir, "test-workflow.md") |
| 200 | + if err := os.WriteFile(testFile, []byte(content), 0644); err != nil { |
| 201 | + t.Fatal(err) |
| 202 | + } |
| 203 | + |
| 204 | + // Capture stderr to check for warnings |
| 205 | + oldStderr := os.Stderr |
| 206 | + r, w, _ := os.Pipe() |
| 207 | + os.Stderr = w |
| 208 | + |
| 209 | + compiler := NewCompiler(false, "", "test") |
| 210 | + compiler.SetStrictMode(false) |
| 211 | + err = compiler.CompileWorkflow(testFile) |
| 212 | + |
| 213 | + // Restore stderr |
| 214 | + w.Close() |
| 215 | + os.Stderr = oldStderr |
| 216 | + var buf bytes.Buffer |
| 217 | + io.Copy(&buf, r) |
| 218 | + stderrOutput := buf.String() |
| 219 | + |
| 220 | + if err != nil { |
| 221 | + t.Fatalf("Expected compilation to succeed but it failed: %v", err) |
| 222 | + } |
| 223 | + |
| 224 | + // Check that the warning includes both options |
| 225 | + expectedPhrases := []string{ |
| 226 | + "Missing required permissions for github toolsets:", |
| 227 | + "Option 1: Add missing permissions to your workflow frontmatter:", |
| 228 | + "Option 2: Reduce the required toolsets in your workflow:", |
| 229 | + "issues", |
| 230 | + "pull_requests", |
| 231 | + "repos", |
| 232 | + "contents: write", |
| 233 | + "issues: write", |
| 234 | + "pull-requests: write", |
| 235 | + } |
| 236 | + |
| 237 | + for _, phrase := range expectedPhrases { |
| 238 | + if !strings.Contains(stderrOutput, phrase) { |
| 239 | + t.Errorf("Expected warning to contain '%s', got:\n%s", phrase, stderrOutput) |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments