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.
This commit is contained in:
Denis Bilenko 2025-01-14 16:50:55 +01:00 committed by GitHub
parent ccb2599b42
commit fca6abdfac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 0 deletions

View File

@ -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})
}