Skip to content

Commit 4e34ebc

Browse files
CopilotMossakapelikhan
authored
Fix broken integration tests by stripping YAML comment header before assertions (#4890)
* Initial plan * Fix broken tests by stripping comment header before string checks Co-authored-by: Mossaka <5447827+Mossaka@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Mossaka <5447827+Mossaka@users.noreply.github.com> Co-authored-by: Peli de Halleux <pelikhan@users.noreply.github.com>
1 parent a514b4a commit 4e34ebc

File tree

4 files changed

+78
-4
lines changed

4 files changed

+78
-4
lines changed

pkg/testutil/tempdir.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"strings"
78
"sync"
89
"testing"
910
"time"
@@ -64,3 +65,18 @@ func TempDir(t *testing.T, pattern string) string {
6465

6566
return tempDir
6667
}
68+
69+
// StripYAMLCommentHeader removes the comment header from generated YAML files
70+
// and returns only the non-comment YAML content. This is useful for tests that
71+
// need to verify content without matching strings in the comment header.
72+
func StripYAMLCommentHeader(yamlContent string) string {
73+
lines := strings.Split(yamlContent, "\n")
74+
for i, line := range lines {
75+
// Find the first non-comment, non-empty line (start of actual YAML)
76+
trimmed := strings.TrimSpace(line)
77+
if trimmed != "" && !strings.HasPrefix(trimmed, "#") {
78+
return strings.Join(lines[i:], "\n")
79+
}
80+
}
81+
return yamlContent
82+
}

pkg/testutil/tempdir_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,57 @@ func TestTempDirCleanup(t *testing.T) {
8282
t.Error("tempDir should have been set by subtest")
8383
}
8484
}
85+
86+
func TestStripYAMLCommentHeader(t *testing.T) {
87+
tests := []struct {
88+
name string
89+
input string
90+
expected string
91+
}{
92+
{
93+
name: "strips comment header",
94+
input: `#
95+
# Header comment
96+
# More comments
97+
#
98+
name: my-workflow
99+
on: push`,
100+
expected: `name: my-workflow
101+
on: push`,
102+
},
103+
{
104+
name: "handles no comments",
105+
input: `name: my-workflow`,
106+
expected: `name: my-workflow`,
107+
},
108+
{
109+
name: "handles empty lines before YAML",
110+
input: `#
111+
# Comment
112+
113+
name: my-workflow`,
114+
expected: `name: my-workflow`,
115+
},
116+
{
117+
name: "handles empty input",
118+
input: "",
119+
expected: "",
120+
},
121+
{
122+
name: "handles only comments",
123+
input: `# Only comments
124+
# No YAML`,
125+
expected: `# Only comments
126+
# No YAML`,
127+
},
128+
}
129+
130+
for _, tt := range tests {
131+
t.Run(tt.name, func(t *testing.T) {
132+
result := testutil.StripYAMLCommentHeader(tt.input)
133+
if result != tt.expected {
134+
t.Errorf("StripYAMLCommentHeader(%q) = %q, want %q", tt.input, result, tt.expected)
135+
}
136+
})
137+
}
138+
}

pkg/workflow/github_token_precedence_integration_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,17 @@ Test that safe-outputs github-token overrides top-level.
102102
}
103103

104104
yamlContent := string(content)
105+
// Strip the comment header to check only the actual YAML content
106+
yamlContentNoComments := testutil.StripYAMLCommentHeader(yamlContent)
105107

106108
// Verify that safe-outputs token is used in the create_issue job
107-
if !strings.Contains(yamlContent, "github-token: ${{ secrets.SAFE_OUTPUTS_PAT }}") {
109+
if !strings.Contains(yamlContentNoComments, "github-token: ${{ secrets.SAFE_OUTPUTS_PAT }}") {
108110
t.Error("Expected safe-outputs github-token to be used in create_issue job")
109111
t.Logf("Generated YAML:\n%s", yamlContent)
110112
}
111113

112114
// Verify that top-level token is NOT used in safe-outputs job
113-
if strings.Contains(yamlContent, "github-token: ${{ secrets.TOPLEVEL_PAT }}") {
115+
if strings.Contains(yamlContentNoComments, "github-token: ${{ secrets.TOPLEVEL_PAT }}") {
114116
t.Error("Top-level github-token should not be used when safe-outputs token is present")
115117
}
116118
})

pkg/workflow/neutral_tools_integration_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Test workflow with neutral tools format.
5656
t.Fatalf("Failed to read compiled workflow: %v", err)
5757
}
5858
yamlContent := string(yamlBytes)
59+
// Strip the comment header to check only the actual YAML content
60+
yamlContentNoComments := testutil.StripYAMLCommentHeader(yamlContent)
5961

6062
// Should contain Claude tools that were converted from neutral tools
6163
expectedClaudeTools := []string{
@@ -95,8 +97,8 @@ Test workflow with neutral tools format.
9597
}
9698
}
9799

98-
// Verify that the old format is not present in the compiled output
99-
if strings.Contains(yamlContent, "bash:") || strings.Contains(yamlContent, "web-fetch:") {
100+
// Verify that the old format is not present in the compiled output (excluding comments)
101+
if strings.Contains(yamlContentNoComments, "bash:") || strings.Contains(yamlContentNoComments, "web-fetch:") {
100102
t.Error("Compiled YAML should not contain neutral tool names directly")
101103
}
102104
}

0 commit comments

Comments
 (0)