Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions pkg/workflow/checkout_optimization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,31 +146,27 @@ strict: false
}
} else {
// For other test cases, check if checkout step is present in the agent job
// Extract the agent job section
agentJobStart := strings.Index(lockContentStr, "agent:")
if agentJobStart == -1 {
// Extract the agent job section using exact YAML job marker
agentJobMarker := "\n agent:\n"
markerIdx := strings.Index(lockContentStr, agentJobMarker)
if markerIdx == -1 {
t.Fatalf("Agent job not found in compiled workflow")
}
agentJobStart := markerIdx + len("\n ") // point to "agent:\n"

Comment on lines +149 to 156
Copy link

Copilot AI Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test reimplements job-section extraction with manual string indexing and hard-coded offsets (e.g., len("\n ")). There is already an extractJobSection(yamlContent, jobName) helper in pkg/workflow/compiler_test_helpers_test.go that extracts a job by YAML indentation, which would simplify this logic and reduce brittleness if formatting changes.

See below for a potential fix:

				agentJobSection := extractJobSection(lockContentStr, "agent")
				if agentJobSection == "" {
					t.Fatalf("Agent job not found in compiled workflow")
				}

Copilot uses AI. Check for mistakes.
// Find the next job or end of file to bound the agent job section
agentJobEnd := len(lockContentStr)
nextJobIdx := strings.Index(lockContentStr[agentJobStart+6:], "\n ")
if nextJobIdx != -1 {
// Look for the start of the next job (a line starting with two spaces followed by a word and colon)
searchStart := agentJobStart + 6 + nextJobIdx
for idx := searchStart; idx < len(lockContentStr); idx++ {
if lockContentStr[idx] == '\n' {
// Check if the next line starts a new job (at same indentation level as "agent:")
lineStart := idx + 1
if lineStart < len(lockContentStr) && lineStart+2 < len(lockContentStr) {
if lockContentStr[lineStart:lineStart+2] == " " && lockContentStr[lineStart+2] != ' ' {
// Found a line that starts with exactly 2 spaces (not more)
// and has a non-space character after, indicating a new job
colonIdx := strings.Index(lockContentStr[lineStart:], ":")
if colonIdx > 0 && colonIdx < 50 { // Job names are typically short
agentJobEnd = idx
break
}
// Search for next top-level job (line starting with exactly 2 spaces + non-space)
searchStart := markerIdx + len(agentJobMarker)
for idx := searchStart; idx < len(lockContentStr); idx++ {
if lockContentStr[idx] == '\n' {
lineStart := idx + 1
if lineStart+2 < len(lockContentStr) {
if lockContentStr[lineStart:lineStart+2] == " " && lockContentStr[lineStart+2] != ' ' {
colonIdx := strings.Index(lockContentStr[lineStart:], ":")
if colonIdx > 0 && colonIdx < 50 {
agentJobEnd = idx
break
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/workflow/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ This is a test.`,
inMainJob := false
foundEnvironment := false

agentJobLine := " " + string(constants.AgentJobName) + ":"
for i, line := range lines {
if strings.Contains(line, string(constants.AgentJobName)+":") {
if line == agentJobLine {
inMainJob = true
continue
}
if inMainJob && strings.HasPrefix(line, " ") && !strings.HasPrefix(line, " ") && line != " "+string(constants.AgentJobName)+":" {
if inMainJob && strings.HasPrefix(line, " ") && !strings.HasPrefix(line, " ") && line != agentJobLine {
// Found next job, stop looking
break
}
Expand Down
35 changes: 15 additions & 20 deletions pkg/workflow/local_action_permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,34 +86,29 @@ strict: false

lockContentStr := string(lockContent)

// Verify the job exists
jobMarker := tt.jobName + ":"
if !strings.Contains(lockContentStr, jobMarker) {
// Verify the job exists and extract using exact YAML job marker
jobMarker := "\n " + tt.jobName + ":\n"
markerIdx := strings.Index(lockContentStr, jobMarker)
if markerIdx == -1 {
t.Errorf("Expected %s job to be present", tt.jobName)
return
}

// Extract the job section
jobStart := strings.Index(lockContentStr, jobMarker)
if jobStart == -1 {
t.Fatalf("%s job not found in compiled workflow", tt.jobName)
}
jobStart := markerIdx + len("\n ") // point to "jobName:\n"

Comment on lines +89 to 99
Copy link

Copilot AI Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test duplicates YAML job extraction logic using string markers and manual scanning. There is an extractJobSection(yamlContent, jobName) helper in pkg/workflow/compiler_test_helpers_test.go that can extract tt.jobName's job body by indentation and would remove the need for the marker/index arithmetic here.

See below for a potential fix:

			// Verify the job exists and extract its YAML section using the shared helper.
			jobSection := extractJobSection(lockContentStr, tt.jobName)
			if jobSection == "" {
				t.Errorf("Expected %s job to be present", tt.jobName)
				return
			}

Copilot uses AI. Check for mistakes.
// Find the next job or end of file
jobEnd := len(lockContentStr)
nextJobIdx := strings.Index(lockContentStr[jobStart+len(jobMarker):], "\n ")
if nextJobIdx != -1 {
searchStart := jobStart + len(jobMarker) + nextJobIdx
for idx := searchStart; idx < len(lockContentStr); idx++ {
if lockContentStr[idx] == '\n' {
lineStart := idx + 1
if lineStart < len(lockContentStr) && lineStart+2 < len(lockContentStr) {
if lockContentStr[lineStart:lineStart+2] == " " && lockContentStr[lineStart+2] != ' ' {
colonIdx := strings.Index(lockContentStr[lineStart:], ":")
if colonIdx > 0 && colonIdx < 50 {
jobEnd = idx
break
}
searchStart := markerIdx + len(jobMarker)
for idx := searchStart; idx < len(lockContentStr); idx++ {
if lockContentStr[idx] == '\n' {
lineStart := idx + 1
if lineStart+2 < len(lockContentStr) {
if lockContentStr[lineStart:lineStart+2] == " " && lockContentStr[lineStart+2] != ' ' {
colonIdx := strings.Index(lockContentStr[lineStart:], ":")
if colonIdx > 0 && colonIdx < 50 {
jobEnd = idx
break
}
}
}
Expand Down
32 changes: 16 additions & 16 deletions pkg/workflow/trial_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,28 @@ This is a test workflow for trial mode compilation.
}

// Checkout in agent job should include token in trial mode
// Extract the agent job section first
agentJobStart := strings.Index(lockContent, "agent:")
if agentJobStart == -1 {
// Extract the agent job section using exact YAML job marker
agentJobMarker := "\n agent:\n"
markerIdx := strings.Index(lockContent, agentJobMarker)
if markerIdx == -1 {
t.Error("Expected agent job in trial mode")
return
}

agentJobStart := markerIdx + len("\n ") // point to "agent:\n"

Comment on lines +137 to +146
Copy link

Copilot AI Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The agent job section is extracted via a custom marker/index scan and magic offsets. Consider reusing the existing extractJobSection(yamlContent, jobName) helper from pkg/workflow/compiler_test_helpers_test.go to avoid duplicating YAML job-boundary logic and make the test less sensitive to minor formatting changes.

See below for a potential fix:

		agentJobSection := extractJobSection(lockContent, "agent")
		if agentJobSection == "" {
			t.Error("Expected agent job in trial mode")
			return
		}

Copilot uses AI. Check for mistakes.
// Find the end of the agent job (next job or end of file)
agentJobEnd := len(lockContent)
nextJobStart := strings.Index(lockContent[agentJobStart+6:], "\n ")
if nextJobStart != -1 {
searchPos := agentJobStart + 6 + nextJobStart
for idx := searchPos; idx < len(lockContent); idx++ {
if lockContent[idx] == '\n' {
lineStart := idx + 1
if lineStart < len(lockContent) && lineStart+2 < len(lockContent) {
if lockContent[lineStart:lineStart+2] == " " && lockContent[lineStart+2] != ' ' {
colonIdx := strings.Index(lockContent[lineStart:], ":")
if colonIdx > 0 && colonIdx < 50 {
agentJobEnd = idx
break
}
searchPos := markerIdx + len(agentJobMarker)
for idx := searchPos; idx < len(lockContent); idx++ {
if lockContent[idx] == '\n' {
lineStart := idx + 1
if lineStart+2 < len(lockContent) {
if lockContent[lineStart:lineStart+2] == " " && lockContent[lineStart+2] != ' ' {
colonIdx := strings.Index(lockContent[lineStart:], ":")
if colonIdx > 0 && colonIdx < 50 {
agentJobEnd = idx
break
}
}
}
Expand Down
Loading