From 471e743fcaf27b8fcd8b1c3dabd2e5cb15c60ce7 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Sun, 28 Mar 2021 00:37:35 -0700 Subject: [PATCH] show snapshot test diffs in color --- internal/bundler/bundler_test.go | 3 ++- internal/logger/logger.go | 6 +++--- internal/test/diff.go | 31 +++++++++++++++++++++++-------- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/internal/bundler/bundler_test.go b/internal/bundler/bundler_test.go index 9784ddf1fb3..6db0d38d99f 100644 --- a/internal/bundler/bundler_test.go +++ b/internal/bundler/bundler_test.go @@ -37,7 +37,8 @@ func assertEqual(t *testing.T, a interface{}, b interface{}) { stringA := fmt.Sprintf("%v", a) stringB := fmt.Sprintf("%v", b) if strings.Contains(stringA, "\n") { - t.Fatal(test.Diff(stringB, stringA)) + color := !fs.CheckIfWindows() + t.Fatal(test.Diff(stringB, stringA, color)) } else { t.Fatalf("%s != %s", a, b) } diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 3f3f33e55ac..1f749e1abf7 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -509,7 +509,7 @@ type Colors struct { Yellow string } -var terminalColors = Colors{ +var TerminalColors = Colors{ Reset: "\033[0m", Bold: "\033[1m", Dim: "\033[37m", @@ -548,7 +548,7 @@ func PrintTextWithColor(file *os.File, useColor UseColor, callback func(Colors) var colors Colors if useColorEscapes { - colors = terminalColors + colors = TerminalColors } writeStringWithColor(file, callback(colors)) } @@ -872,7 +872,7 @@ func emptyMarginText(maxMargin int, isLast bool) string { func msgString(includeSource bool, terminalInfo TerminalInfo, kind MsgKind, data MsgData, maxMargin int) string { var colors Colors if terminalInfo.UseColorEscapes { - colors = terminalColors + colors = TerminalColors } var kindColor string diff --git a/internal/test/diff.go b/internal/test/diff.go index 17d28d376f9..1554a8a348b 100644 --- a/internal/test/diff.go +++ b/internal/test/diff.go @@ -1,32 +1,47 @@ package test import ( + "fmt" "strings" + + "github.com/evanw/esbuild/internal/logger" ) -func Diff(old string, new string) string { - return strings.Join(diffRec(nil, strings.Split(old, "\n"), strings.Split(new, "\n")), "\n") +func Diff(old string, new string, color bool) string { + return strings.Join(diffRec(nil, strings.Split(old, "\n"), strings.Split(new, "\n"), color), "\n") } // This is a simple recursive line-by-line diff implementation -func diffRec(result []string, old []string, new []string) []string { +func diffRec(result []string, old []string, new []string, color bool) []string { o, n, common := lcSubstr(old, new) if common == 0 { // Everything changed for _, line := range old { - result = append(result, "-"+line) + if color { + result = append(result, fmt.Sprintf("%s-%s%s", logger.TerminalColors.Red, line, logger.TerminalColors.Reset)) + } else { + result = append(result, "-"+line) + } } for _, line := range new { - result = append(result, "+"+line) + if color { + result = append(result, fmt.Sprintf("%s+%s%s", logger.TerminalColors.Green, line, logger.TerminalColors.Reset)) + } else { + result = append(result, "+"+line) + } } } else { // Something in the middle stayed the same - result = diffRec(result, old[:o], new[:n]) + result = diffRec(result, old[:o], new[:n], color) for _, line := range old[o : o+common] { - result = append(result, " "+line) + if color { + result = append(result, fmt.Sprintf("%s %s%s", logger.TerminalColors.Dim, line, logger.TerminalColors.Reset)) + } else { + result = append(result, " "+line) + } } - result = diffRec(result, old[o+common:], new[n+common:]) + result = diffRec(result, old[o+common:], new[n+common:], color) } return result