Skip to content

Commit

Permalink
fix comparison on Windows: ignore slash difference, ignore terraform …
Browse files Browse the repository at this point in the history
…path
  • Loading branch information
denik committed Dec 20, 2024
1 parent 42a4ad4 commit e1db924
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions integration/bundle/init_default_python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 29 additions & 6 deletions libs/testdiff/testdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,47 @@ 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 ==> "))
}
}
}

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 {
Expand Down

0 comments on commit e1db924

Please sign in to comment.