2024-12-30 15:26:21 +00:00
|
|
|
package testdiff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-01-09 09:00:05 +00:00
|
|
|
"flag"
|
2024-12-30 15:26:21 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/internal/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2025-01-09 09:00:05 +00:00
|
|
|
var OverwriteMode = false
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&OverwriteMode, "update", false, "Overwrite golden files")
|
|
|
|
}
|
2024-12-30 15:26:21 +00:00
|
|
|
|
|
|
|
func ReadFile(t testutil.TestingT, ctx context.Context, filename string) string {
|
2025-01-02 09:49:21 +00:00
|
|
|
t.Helper()
|
2024-12-30 15:26:21 +00:00
|
|
|
data, err := os.ReadFile(filename)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return ""
|
|
|
|
}
|
2025-01-02 09:49:21 +00:00
|
|
|
assert.NoError(t, err, "Failed to read %s", filename)
|
2024-12-30 15:26:21 +00:00
|
|
|
// On CI, on Windows \n in the file somehow end up as \r\n
|
|
|
|
return NormalizeNewlines(string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteFile(t testutil.TestingT, filename, data string) {
|
2025-01-02 09:49:21 +00:00
|
|
|
t.Helper()
|
2024-12-30 15:26:21 +00:00
|
|
|
t.Logf("Overwriting %s", filename)
|
|
|
|
err := os.WriteFile(filename, []byte(data), 0o644)
|
2025-01-02 09:49:21 +00:00
|
|
|
assert.NoError(t, err, "Failed to write %s", filename)
|
2024-12-30 15:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AssertOutput(t testutil.TestingT, ctx context.Context, out, outTitle, expectedPath string) {
|
2025-01-02 09:49:21 +00:00
|
|
|
t.Helper()
|
2024-12-30 15:26:21 +00:00
|
|
|
expected := ReadFile(t, ctx, expectedPath)
|
|
|
|
|
|
|
|
out = ReplaceOutput(t, ctx, out)
|
|
|
|
|
|
|
|
if out != expected {
|
|
|
|
AssertEqualTexts(t, expectedPath, outTitle, expected, out)
|
|
|
|
|
|
|
|
if OverwriteMode {
|
|
|
|
WriteFile(t, expectedPath, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AssertOutputJQ(t testutil.TestingT, ctx context.Context, out, outTitle, expectedPath string, ignorePaths []string) {
|
2025-01-02 09:49:21 +00:00
|
|
|
t.Helper()
|
2024-12-30 15:26:21 +00:00
|
|
|
expected := ReadFile(t, ctx, expectedPath)
|
|
|
|
|
|
|
|
out = ReplaceOutput(t, ctx, out)
|
|
|
|
|
|
|
|
if out != expected {
|
|
|
|
AssertEqualJQ(t.(*testing.T), expectedPath, outTitle, expected, out, ignorePaths)
|
|
|
|
|
|
|
|
if OverwriteMode {
|
|
|
|
WriteFile(t, expectedPath, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReplaceOutput(t testutil.TestingT, ctx context.Context, out string) string {
|
2025-01-02 09:49:21 +00:00
|
|
|
t.Helper()
|
2024-12-30 15:26:21 +00:00
|
|
|
out = NormalizeNewlines(out)
|
|
|
|
replacements := GetReplacementsMap(ctx)
|
|
|
|
if replacements == nil {
|
|
|
|
t.Fatal("WithReplacementsMap was not called")
|
|
|
|
}
|
2025-01-15 11:15:23 +00:00
|
|
|
return replacements.Replace(out)
|
2024-12-30 15:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NormalizeNewlines(input string) string {
|
|
|
|
output := strings.ReplaceAll(input, "\r\n", "\n")
|
|
|
|
return strings.ReplaceAll(output, "\r", "\n")
|
|
|
|
}
|