Skip to content

Commit f075238

Browse files
committed
Use colored diffs in test errors in case it is a TTY
1 parent f1522d1 commit f075238

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

main_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import (
2525
"path/filepath"
2626
"reflect"
2727
"strings"
28+
"syscall"
2829
"testing"
30+
"unsafe"
2931
)
3032

3133
func TestParseUsage(t *testing.T) {
@@ -428,6 +430,12 @@ func setup(t *testing.T, args ...string) string {
428430
return out
429431
}
430432

433+
func isatty() bool {
434+
var size uint64
435+
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, os.Stderr.Fd(), syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&size)))
436+
return err == 0
437+
}
438+
431439
func TestFull(t *testing.T) {
432440
cases := []string{
433441
"basic",
@@ -456,7 +464,12 @@ func TestFull(t *testing.T) {
456464
t.Fatal(err)
457465
}
458466
if !bytes.Equal(expected, actual) {
459-
cmd := exec.Command("diff", "-u", "--label=expected", "--label=got", basename+".1", out)
467+
var args []string
468+
if isatty() {
469+
args = []string{"--color=always"}
470+
}
471+
args = append(args, "-u", "--label=expected", "--label=got", basename+".1", out)
472+
cmd := exec.Command("diff", args...)
460473
diff, err := cmd.Output()
461474
exitErr := &exec.ExitError{}
462475
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 {

0 commit comments

Comments
 (0)