A lightweight test helper package
0

Configure Feed

Select the types of activity you want to include in your feed.

Fix colouring (#53)

authored by

Tom Fleet and committed by
GitHub
(Jan 11, 2025, 11:48 AM UTC) b25a14c8 68715662

+18 -33
-2
codecov.yml
··· 1 - ignore: 2 - - internal/colour
+17 -4
test.go
··· 15 15 "sync" 16 16 "testing" 17 17 18 - "github.com/FollowTheProcess/test/internal/colour" 19 18 "github.com/FollowTheProcess/test/internal/diff" 19 + "github.com/fatih/color" 20 + ) 21 + 22 + var ( 23 + header = color.New(color.FgCyan, color.Bold) 24 + green = color.New(color.FgGreen) 25 + red = color.New(color.FgRed) 20 26 ) 21 27 22 28 // Equal fails if got != want. ··· 327 333 func Diff(tb testing.TB, got, want string) { 328 334 tb.Helper() 329 335 336 + // TODO(@FollowTheProcess): If either got or want don't end in a newline, add one 330 337 if diff := diff.Diff("want", []byte(want), "got", []byte(got)); diff != nil { 331 338 tb.Fatalf("\nDiff\n----\n%s\n", prettyDiff(string(diff))) 332 339 } ··· 434 441 435 442 // prettyDiff takes a string diff in unified diff format and colourises it for easier viewing. 436 443 func prettyDiff(diff string) string { 444 + // color by default will look at whether stdout is a tty and the value of the 445 + // $NO_COLOR env var, we need to override this because go test buffers output 446 + // so it will appear as if it's not a tty, even though the end result is to show 447 + // the output in a terminal. It still respects the value of $NO_COLOR. 448 + color.NoColor = false || os.Getenv("NO_COLOR") != "" 449 + 437 450 lines := strings.Split(diff, "\n") 438 451 for i := 0; i < len(lines); i++ { 439 452 trimmed := strings.TrimSpace(lines[i]) 440 453 if strings.HasPrefix(trimmed, "---") || strings.HasPrefix(trimmed, "- ") { 441 - lines[i] = colour.Red(lines[i]) 454 + lines[i] = red.Sprint(lines[i]) 442 455 } 443 456 444 457 if strings.HasPrefix(trimmed, "@@") { 445 - lines[i] = colour.Header(lines[i]) 458 + lines[i] = header.Sprint(lines[i]) 446 459 } 447 460 448 461 if strings.HasPrefix(trimmed, "+++") || strings.HasPrefix(trimmed, "+ ") { 449 - lines[i] = colour.Green(lines[i]) 462 + lines[i] = green.Sprint(lines[i]) 450 463 } 451 464 } 452 465
+1
test_test.go
··· 434 434 t.Run(tt.name, func(t *testing.T) { 435 435 buf := &bytes.Buffer{} 436 436 tb := &TB{out: buf} 437 + t.Setenv("NO_COLOR", "true") 437 438 snap := snapshot.New(t, snapshot.Update(*update)) 438 439 439 440 if tb.failed {
-27
internal/colour/colour.go
··· 1 - // Package colour implements basic text colouring for showing text diffs. 2 - package colour 3 - 4 - import ( 5 - "github.com/fatih/color" 6 - ) 7 - 8 - var ( 9 - header = color.New(color.FgCyan, color.Bold) 10 - green = color.New(color.FgGreen) 11 - red = color.New(color.FgRed) 12 - ) 13 - 14 - // Header returns a diff header styled string. 15 - func Header(text string) string { 16 - return header.Sprint(text) 17 - } 18 - 19 - // Green returns a green styled string. 20 - func Green(text string) string { 21 - return green.Sprint(text) 22 - } 23 - 24 - // Red returns a red styled string. 25 - func Red(text string) string { 26 - return red.Sprint(text) 27 - }