···1515 "sync"
1616 "testing"
17171818- "github.com/FollowTheProcess/test/internal/colour"
1918 "github.com/FollowTheProcess/test/internal/diff"
1919+ "github.com/fatih/color"
2020+)
2121+2222+var (
2323+ header = color.New(color.FgCyan, color.Bold)
2424+ green = color.New(color.FgGreen)
2525+ red = color.New(color.FgRed)
2026)
21272228// Equal fails if got != want.
···327333func Diff(tb testing.TB, got, want string) {
328334 tb.Helper()
329335336336+ // TODO(@FollowTheProcess): If either got or want don't end in a newline, add one
330337 if diff := diff.Diff("want", []byte(want), "got", []byte(got)); diff != nil {
331338 tb.Fatalf("\nDiff\n----\n%s\n", prettyDiff(string(diff)))
332339 }
···434441435442// prettyDiff takes a string diff in unified diff format and colourises it for easier viewing.
436443func prettyDiff(diff string) string {
444444+ // color by default will look at whether stdout is a tty and the value of the
445445+ // $NO_COLOR env var, we need to override this because go test buffers output
446446+ // so it will appear as if it's not a tty, even though the end result is to show
447447+ // the output in a terminal. It still respects the value of $NO_COLOR.
448448+ color.NoColor = false || os.Getenv("NO_COLOR") != ""
449449+437450 lines := strings.Split(diff, "\n")
438451 for i := 0; i < len(lines); i++ {
439452 trimmed := strings.TrimSpace(lines[i])
440453 if strings.HasPrefix(trimmed, "---") || strings.HasPrefix(trimmed, "- ") {
441441- lines[i] = colour.Red(lines[i])
454454+ lines[i] = red.Sprint(lines[i])
442455 }
443456444457 if strings.HasPrefix(trimmed, "@@") {
445445- lines[i] = colour.Header(lines[i])
458458+ lines[i] = header.Sprint(lines[i])
446459 }
447460448461 if strings.HasPrefix(trimmed, "+++") || strings.HasPrefix(trimmed, "+ ") {
449449- lines[i] = colour.Green(lines[i])
462462+ lines[i] = green.Sprint(lines[i])
450463 }
451464 }
452465