test(agentcompat): cover CRLF workflow mutations

Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
This commit is contained in:
naiba
2026-07-20 17:06:14 +00:00
co-authored by naiba/CloudCode
parent 153dfeb569
commit 24c7a898cf
@@ -24,6 +24,14 @@ func TestPolicy_RejectsMissingRequiredDependency(t *testing.T) {
} }
func TestPolicy_RejectsInvalidRequiredAggregator(t *testing.T) { func TestPolicy_RejectsInvalidRequiredAggregator(t *testing.T) {
workflowData := readNezhaQualityWorkflow(t)
workflowVariants := []struct {
name string
data []byte
}{
{name: "LF", data: workflowData},
{name: "CRLF", data: []byte(strings.ReplaceAll(string(workflowData), "\n", "\r\n"))},
}
tests := []struct { tests := []struct {
name string name string
currentText string currentText string
@@ -80,12 +88,12 @@ func TestPolicy_RejectsInvalidRequiredAggregator(t *testing.T) {
}, "\n "), }, "\n "),
}, },
} }
for _, variant := range workflowVariants {
t.Run(variant.name, func(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
// Given // Given
data := readNezhaQualityWorkflow(t) invalidWorkflow := mutateWorkflow(t, variant.data, test.currentText, test.invalidText)
invalidWorkflow := strings.Replace(string(data), test.currentText, test.invalidText, 1)
require.NotEqual(t, string(data), invalidWorkflow, "workflow mutation must match current content")
// When // When
err := workflowpolicy.Verify([]byte(invalidWorkflow), workflowpolicy.RepositoryNezha) err := workflowpolicy.Verify([]byte(invalidWorkflow), workflowpolicy.RepositoryNezha)
@@ -96,6 +104,22 @@ func TestPolicy_RejectsInvalidRequiredAggregator(t *testing.T) {
require.True(t, policyError.Has(workflowpolicy.RuleWorkflowStructure)) require.True(t, policyError.Has(workflowpolicy.RuleWorkflowStructure))
}) })
} }
})
}
}
func mutateWorkflow(t *testing.T, data []byte, currentText, invalidText string) string {
t.Helper()
workflow := string(data)
lineEnding := "\n"
if strings.Contains(workflow, "\r\n") {
// Windows checkouts preserve CRLF, so multiline mutation snippets must use the source line ending.
lineEnding = "\r\n"
}
currentText = strings.ReplaceAll(currentText, "\n", lineEnding)
invalidText = strings.ReplaceAll(invalidText, "\n", lineEnding)
require.Equal(t, 1, strings.Count(workflow, currentText), "workflow mutation must match current content exactly once")
return strings.Replace(workflow, currentText, invalidText, 1)
} }
func TestPolicy_RejectsMissingRequiredAggregator(t *testing.T) { func TestPolicy_RejectsMissingRequiredAggregator(t *testing.T) {