diff --git a/pkg/workflow/checkout_optimization_test.go b/pkg/workflow/checkout_optimization_test.go index 809de7cdfd3..4b149357537 100644 --- a/pkg/workflow/checkout_optimization_test.go +++ b/pkg/workflow/checkout_optimization_test.go @@ -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" // 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 } } } diff --git a/pkg/workflow/environment_test.go b/pkg/workflow/environment_test.go index 1337d8559c3..56c8fb9c2dd 100644 --- a/pkg/workflow/environment_test.go +++ b/pkg/workflow/environment_test.go @@ -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 } diff --git a/pkg/workflow/local_action_permissions_test.go b/pkg/workflow/local_action_permissions_test.go index adeef98bfa8..a9028d5449f 100644 --- a/pkg/workflow/local_action_permissions_test.go +++ b/pkg/workflow/local_action_permissions_test.go @@ -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" // 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 } } } diff --git a/pkg/workflow/trial_mode_test.go b/pkg/workflow/trial_mode_test.go index 6ddc4a7bf2a..36273147ea1 100644 --- a/pkg/workflow/trial_mode_test.go +++ b/pkg/workflow/trial_mode_test.go @@ -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" + // 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 } } }