From e1db92488271f1789a7a7554151b4afb1f652c99 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 19:01:07 +0100 Subject: [PATCH] fix comparison on Windows: ignore slash difference, ignore terraform path --- .../bundle/init_default_python_test.go | 1 + libs/testdiff/testdiff.go | 35 +++++++++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/integration/bundle/init_default_python_test.go b/integration/bundle/init_default_python_test.go index 3011fb9528..fed1355f96 100644 --- a/integration/bundle/init_default_python_test.go +++ b/integration/bundle/init_default_python_test.go @@ -102,6 +102,7 @@ func testDefaultPython(t *testing.T, pythonVersion string) { }) ignoredFields := []string{ + "/bundle/terraform/exec_path", "/resources/jobs/project_name_$UNIQUE_PRJ_job/email_notifications", "/resources/jobs/project_name_$UNIQUE_PRJ_job/job_clusters/0/new_cluster/node_type_id", "/resources/jobs/project_name_$UNIQUE_PRJ_job/url", diff --git a/libs/testdiff/testdiff.go b/libs/testdiff/testdiff.go index fad12c7680..b4ec6fe7d4 100644 --- a/libs/testdiff/testdiff.go +++ b/libs/testdiff/testdiff.go @@ -37,17 +37,17 @@ func AssertEqualJQ(t *testing.T, expectedName, outName, expected, out string, ig } else { diff := UnifiedDiff(expectedName, outName, expected, out) t.Logf("Diff:\n%s", diff) - ignoredDiffs := []string{} + allowedDiffs := []string{} erroredDiffs := []string{} for _, op := range patch { - if matchesPrefixes(ignorePaths, op.Path) { - ignoredDiffs = append(ignoredDiffs, fmt.Sprintf("%7s %s %v", op.Type, op.Path, op.Value)) + if allowDifference(ignorePaths, op) { + allowedDiffs = append(allowedDiffs, fmt.Sprintf("%7s %s %v old=%v", op.Type, op.Path, op.Value, op.OldValue)) } else { - erroredDiffs = append(erroredDiffs, fmt.Sprintf("%7s %s %v", op.Type, op.Path, op.Value)) + erroredDiffs = append(erroredDiffs, fmt.Sprintf("%7s %s %v old=%v", op.Type, op.Path, op.Value, op.OldValue)) } } - if len(ignoredDiffs) > 0 { - t.Logf("Ignored differences between %s and %s:\n ==> %s", expectedName, outName, strings.Join(ignoredDiffs, "\n ==> ")) + if len(allowedDiffs) > 0 { + t.Logf("Allowed differences between %s and %s:\n ==> %s", expectedName, outName, strings.Join(allowedDiffs, "\n ==> ")) } if len(erroredDiffs) > 0 { t.Errorf("Unexpected differences between %s and %s:\n ==> %s", expectedName, outName, strings.Join(erroredDiffs, "\n ==> ")) @@ -55,6 +55,29 @@ func AssertEqualJQ(t *testing.T, expectedName, outName, expected, out string, ig } } +func allowDifference(ignorePaths []string, op jsondiff.Operation) bool { + if matchesPrefixes(ignorePaths, op.Path) { + return true + } + if op.Type == "replace" && almostSameStrings(op.OldValue, op.Value) { + return true + } + return false +} + +// compare strings and ignore forward vs backward slashes +func almostSameStrings(v1, v2 any) bool { + s1, ok := v1.(string) + if !ok { + return false + } + s2, ok := v2.(string) + if !ok { + return false + } + return strings.ReplaceAll(s1, "\\", "/") == strings.ReplaceAll(s2, "\\", "/") +} + func matchesPrefixes(prefixes []string, path string) bool { for _, p := range prefixes { if p == path {