Skip to content

Commit

Permalink
feat: colorized diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
dervoeti committed Nov 5, 2024
1 parent e1f0782 commit 9f1ab92
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 41 deletions.
16 changes: 15 additions & 1 deletion pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,19 @@ func countLines(k string, v interface{}) (int, error) {
return strings.Count(buf.String(), "\n"), nil
}

// We did not find a good library to create colorized diffs, so we do it ourselves the cheap way
func colorizeDiff(diff string) string {
diffLines := strings.Split(diff, "\n")
for i, line := range diffLines {
if strings.HasPrefix(line, "+") {
diffLines[i] = "\033[32m" + line + "\033[0m"
} else if strings.HasPrefix(line, "-") {
diffLines[i] = "\033[31m" + line + "\033[0m"
}
}
return strings.Join(diffLines, "\n")
}

// PrettyDiff creates a unified diff highlighting the differences between two Kubernetes resources
func PrettyDiff(expected *unstructured.Unstructured, actual *unstructured.Unstructured) (string, error) {
actualPruned := pruneLargeAdditions(expected, actual)
Expand All @@ -450,7 +463,8 @@ func PrettyDiff(expected *unstructured.Unstructured, actual *unstructured.Unstru
Context: 3,
}

return difflib.GetUnifiedDiffString(diffed)
diffStr, err := difflib.GetUnifiedDiffString(diffed)
return "\n" + colorizeDiff(diffStr), err
}

// ConvertUnstructured converts an unstructured object to the known struct. If the type is not known, then
Expand Down
78 changes: 38 additions & 40 deletions pkg/test/utils/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,44 +526,42 @@ func TestPrettyDiff(t *testing.T) {

result, err := PrettyDiff(expected[0].(*unstructured.Unstructured), actual[0].(*unstructured.Unstructured))
assert.NoError(t, err)
assert.Equal(t, `--- Deployment:/central
+++ Deployment:kuttl-test-thorough-hermit/central
@@ -1,7 +1,35 @@
apiVersion: apps/v1
kind: Deployment
metadata:
+ annotations:
+ email: [email protected]
+ meta.helm.sh/release-name: stackrox-central-services
+ meta.helm.sh/release-namespace: kuttl-test-thorough-hermit
+ owner: stackrox
+ labels:
+ app: central
+ app.kubernetes.io/component: central
+ app.kubernetes.io/instance: stackrox-central-services
+ app.kubernetes.io/managed-by: Helm
+ app.kubernetes.io/name: stackrox
+ app.kubernetes.io/part-of: stackrox-central-services
+ app.kubernetes.io/version: 4.3.x-160-g465d734c11
+ helm.sh/chart: stackrox-central-services-400.3.0-160-g465d734c11
+ managedFields: '[... elided field over 10 lines long ...]'
name: central
+ namespace: kuttl-test-thorough-hermit
+ ownerReferences:
+ - apiVersion: platform.stackrox.io/v1alpha1
+ blockOwnerDeletion: true
+ controller: true
+ kind: Central
+ name: stackrox-central-services
+ uid: ff834d91-0853-42b3-9460-7ebf1c659f8a
+spec: '[... elided field over 10 lines long ...]'
status:
- availableReplicas: 1
+ conditions: '[... elided field over 10 lines long ...]'
+ observedGeneration: 2
+ replicas: 1
+ unavailableReplicas: 1
+ updatedReplicas: 1
`, result)
assert.Equal(t, "\n\033[31m--- Deployment:/central\033[0m\n"+
"\033[32m+++ Deployment:kuttl-test-thorough-hermit/central\033[0m\n"+
"@@ -1,7 +1,35 @@\n"+
" apiVersion: apps/v1\n"+
" kind: Deployment\n"+
" metadata:\n"+
"\033[32m+ annotations:\033[0m\n"+
"\033[32m+ email: [email protected]\033[0m\n"+
"\033[32m+ meta.helm.sh/release-name: stackrox-central-services\033[0m\n"+
"\033[32m+ meta.helm.sh/release-namespace: kuttl-test-thorough-hermit\033[0m\n"+
"\033[32m+ owner: stackrox\033[0m\n"+
"\033[32m+ labels:\033[0m\n"+
"\033[32m+ app: central\033[0m\n"+
"\033[32m+ app.kubernetes.io/component: central\033[0m\n"+
"\033[32m+ app.kubernetes.io/instance: stackrox-central-services\033[0m\n"+
"\033[32m+ app.kubernetes.io/managed-by: Helm\033[0m\n"+
"\033[32m+ app.kubernetes.io/name: stackrox\033[0m\n"+
"\033[32m+ app.kubernetes.io/part-of: stackrox-central-services\033[0m\n"+
"\033[32m+ app.kubernetes.io/version: 4.3.x-160-g465d734c11\033[0m\n"+
"\033[32m+ helm.sh/chart: stackrox-central-services-400.3.0-160-g465d734c11\033[0m\n"+
"\033[32m+ managedFields: '[... elided field over 10 lines long ...]'\033[0m\n"+
" name: central\n"+
"\033[32m+ namespace: kuttl-test-thorough-hermit\033[0m\n"+
"\033[32m+ ownerReferences:\033[0m\n"+
"\033[32m+ - apiVersion: platform.stackrox.io/v1alpha1\033[0m\n"+
"\033[32m+ blockOwnerDeletion: true\033[0m\n"+
"\033[32m+ controller: true\033[0m\n"+
"\033[32m+ kind: Central\033[0m\n"+
"\033[32m+ name: stackrox-central-services\033[0m\n"+
"\033[32m+ uid: ff834d91-0853-42b3-9460-7ebf1c659f8a\033[0m\n"+
"\033[32m+spec: '[... elided field over 10 lines long ...]'\033[0m\n"+
" status:\n"+
"\033[31m- availableReplicas: 1\033[0m\n"+
"\033[32m+ conditions: '[... elided field over 10 lines long ...]'\033[0m\n"+
"\033[32m+ observedGeneration: 2\033[0m\n"+
"\033[32m+ replicas: 1\033[0m\n"+
"\033[32m+ unavailableReplicas: 1\033[0m\n"+
"\033[32m+ updatedReplicas: 1\033[0m\n \n", result)
}

0 comments on commit 9f1ab92

Please sign in to comment.