Skip to content

Commit

Permalink
show snapshot test diffs in color
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 28, 2021
1 parent d06bfdb commit 471e743
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
3 changes: 2 additions & 1 deletion internal/bundler/bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ type Colors struct {
Yellow string
}

var terminalColors = Colors{
var TerminalColors = Colors{
Reset: "\033[0m",
Bold: "\033[1m",
Dim: "\033[37m",
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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
Expand Down
31 changes: 23 additions & 8 deletions internal/test/diff.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 471e743

Please sign in to comment.