From fca6abdfac99b048810f8e97aeadbd178cb84e88 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Tue, 14 Jan 2025 16:50:55 +0100 Subject: [PATCH] Golden files: always include JSON-ed string (#2142) ## Changes Always include both verbatim and json version of replacement. This helps when the string in question contains \\ or other chars that need to be quoted. Needed for https://github.com/databricks/cli/pull/2118 ## Tests Existing tests. --- libs/testdiff/golden.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libs/testdiff/golden.go b/libs/testdiff/golden.go index 0ed38e686..78973b468 100644 --- a/libs/testdiff/golden.go +++ b/libs/testdiff/golden.go @@ -2,6 +2,7 @@ package testdiff import ( "context" + "encoding/json" "flag" "fmt" "os" @@ -122,6 +123,19 @@ func (r *ReplacementsContext) Set(old, new string) { if old == "" || new == "" { return } + + // Always include both verbatim and json version of replacement. + // This helps when the string in question contains \ or other chars that need to be quoted. + // In that case we cannot rely that json(old) == '"{old}"' and need to add it explicitly. + + encodedNew, err := json.Marshal(new) + if err != nil { + encodedOld, err := json.Marshal(old) + if err != nil { + r.Repls = append(r.Repls, Replacement{Old: string(encodedOld), New: string(encodedNew)}) + } + } + r.Repls = append(r.Repls, Replacement{Old: old, New: new}) }