Skip to content

Commit 5d5cfd3

Browse files
committed
fix comparison on Windows: ignore slash difference, ignore terraform path
1 parent 26520ce commit 5d5cfd3

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

integration/bundle/init_default_python_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func testDefaultPython(t *testing.T, pythonVersion string) {
102102
})
103103

104104
ignoredFields := []string{
105+
"/bundle/terraform/exec_path",
105106
"/resources/jobs/project_name_$UNIQUE_PRJ_job/email_notifications",
106107
"/resources/jobs/project_name_$UNIQUE_PRJ_job/job_clusters/0/new_cluster/node_type_id",
107108
"/resources/jobs/project_name_$UNIQUE_PRJ_job/url",

libs/testdiff/testdiff.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,47 @@ func AssertEqualJQ(t *testing.T, expectedName, outName, expected, out string, ig
3737
} else {
3838
diff := UnifiedDiff(expectedName, outName, expected, out)
3939
t.Logf("Diff:\n%s", diff)
40-
ignoredDiffs := []string{}
40+
allowedDiffs := []string{}
4141
erroredDiffs := []string{}
4242
for _, op := range patch {
43-
if matchesPrefixes(ignorePaths, op.Path) {
44-
ignoredDiffs = append(ignoredDiffs, fmt.Sprintf("%7s %s %v", op.Type, op.Path, op.Value))
43+
if allowDifference(ignorePaths, op) {
44+
allowedDiffs = append(allowedDiffs, fmt.Sprintf("%7s %s %v old=%v", op.Type, op.Path, op.Value, op.OldValue))
4545
} else {
46-
erroredDiffs = append(erroredDiffs, fmt.Sprintf("%7s %s %v", op.Type, op.Path, op.Value))
46+
erroredDiffs = append(erroredDiffs, fmt.Sprintf("%7s %s %v old=%v", op.Type, op.Path, op.Value, op.OldValue))
4747
}
4848
}
49-
if len(ignoredDiffs) > 0 {
50-
t.Logf("Ignored differences between %s and %s:\n ==> %s", expectedName, outName, strings.Join(ignoredDiffs, "\n ==> "))
49+
if len(allowedDiffs) > 0 {
50+
t.Logf("Allowed differences between %s and %s:\n ==> %s", expectedName, outName, strings.Join(allowedDiffs, "\n ==> "))
5151
}
5252
if len(erroredDiffs) > 0 {
5353
t.Errorf("Unexpected differences between %s and %s:\n ==> %s", expectedName, outName, strings.Join(erroredDiffs, "\n ==> "))
5454
}
5555
}
5656
}
5757

58+
func allowDifference(ignorePaths []string, op jsondiff.Operation) bool {
59+
if matchesPrefixes(ignorePaths, op.Path) {
60+
return true
61+
}
62+
if op.Type == "replace" && almostSameStrings(op.OldValue, op.Value) {
63+
return true
64+
}
65+
return false
66+
}
67+
68+
// compare strings and ignore forward vs backward slashes
69+
func almostSameStrings(v1, v2 any) bool {
70+
s1, ok := v1.(string)
71+
if !ok {
72+
return false
73+
}
74+
s2, ok := v2.(string)
75+
if !ok {
76+
return false
77+
}
78+
return strings.ReplaceAll(s1, "\\", "/") == strings.ReplaceAll(s2, "\\", "/")
79+
}
80+
5881
func matchesPrefixes(prefixes []string, path string) bool {
5982
for _, p := range prefixes {
6083
if p == path {

0 commit comments

Comments
 (0)