Skip to content

Commit 4e1f2ad

Browse files
committed
Enforce NO_COLOR=1 on tests to make sure it passes
Closes #459 Fixes #480 Ref #343 Ref fatih/color#137
1 parent 7f92b70 commit 4e1f2ad

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add support for the `NO_COLOR` environment variable.
6+
([#459](https://github.com/go-task/task/issues/459), [fatih/color#137](https://github.com/fatih/color/pull/137)).
57
- Fix bug where sources were not considering the right directory
68
in `--watch` mode
79
([#484](https://github.com/go-task/task/issues/484), [#485](https://github.com/go-task/task/pull/485)).

cmd/task/task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func main() {
8989
pflag.StringVarP(&dir, "dir", "d", "", "sets directory of execution")
9090
pflag.StringVarP(&entrypoint, "taskfile", "t", "", `choose which Taskfile to run. Defaults to "Taskfile.yml"`)
9191
pflag.StringVarP(&output, "output", "o", "", "sets output style: [interleaved|group|prefixed]")
92-
pflag.BoolVarP(&color, "color", "c", true, "colored output")
92+
pflag.BoolVarP(&color, "color", "c", true, "colored output. Enabled by default. Set flag to false or use NO_COLOR=1 to disable")
9393
pflag.IntVarP(&concurrency, "concurrency", "C", 0, "limit number tasks to run concurrently")
9494
pflag.Parse()
9595

internal/logger/logger.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ import (
66
"github.com/fatih/color"
77
)
88

9+
type Color func() PrintFunc
910
type PrintFunc func(io.Writer, string, ...interface{})
1011

11-
var (
12-
Default PrintFunc = color.New(color.Reset).FprintfFunc()
13-
Blue PrintFunc = color.New(color.FgBlue).FprintfFunc()
14-
Green PrintFunc = color.New(color.FgGreen).FprintfFunc()
15-
Cyan PrintFunc = color.New(color.FgCyan).FprintfFunc()
16-
Yellow PrintFunc = color.New(color.FgYellow).FprintfFunc()
17-
Magenta PrintFunc = color.New(color.FgMagenta).FprintfFunc()
18-
Red PrintFunc = color.New(color.FgRed).FprintfFunc()
19-
)
12+
func Default() PrintFunc { return color.New(color.Reset).FprintfFunc() }
13+
func Blue() PrintFunc { return color.New(color.FgBlue).FprintfFunc() }
14+
func Green() PrintFunc { return color.New(color.FgGreen).FprintfFunc() }
15+
func Cyan() PrintFunc { return color.New(color.FgCyan).FprintfFunc() }
16+
func Yellow() PrintFunc { return color.New(color.FgYellow).FprintfFunc() }
17+
func Magenta() PrintFunc { return color.New(color.FgMagenta).FprintfFunc() }
18+
func Red() PrintFunc { return color.New(color.FgRed).FprintfFunc() }
2019

2120
// Logger is just a wrapper that prints stuff to STDOUT or STDERR,
2221
// with optional color.
@@ -28,37 +27,39 @@ type Logger struct {
2827
}
2928

3029
// Outf prints stuff to STDOUT.
31-
func (l *Logger) Outf(print PrintFunc, s string, args ...interface{}) {
30+
func (l *Logger) Outf(color Color, s string, args ...interface{}) {
3231
if len(args) == 0 {
3332
s, args = "%s", []interface{}{s}
3433
}
3534
if !l.Color {
36-
print = Default
35+
color = Default
3736
}
37+
print := color()
3838
print(l.Stdout, s+"\n", args...)
3939
}
4040

4141
// VerboseOutf prints stuff to STDOUT if verbose mode is enabled.
42-
func (l *Logger) VerboseOutf(print PrintFunc, s string, args ...interface{}) {
42+
func (l *Logger) VerboseOutf(color Color, s string, args ...interface{}) {
4343
if l.Verbose {
44-
l.Outf(print, s, args...)
44+
l.Outf(color, s, args...)
4545
}
4646
}
4747

4848
// Errf prints stuff to STDERR.
49-
func (l *Logger) Errf(print PrintFunc, s string, args ...interface{}) {
49+
func (l *Logger) Errf(color Color, s string, args ...interface{}) {
5050
if len(args) == 0 {
5151
s, args = "%s", []interface{}{s}
5252
}
5353
if !l.Color {
54-
print = Default
54+
color = Default
5555
}
56+
print := color()
5657
print(l.Stderr, s+"\n", args...)
5758
}
5859

5960
// VerboseErrf prints stuff to STDERR if verbose mode is enabled.
60-
func (l *Logger) VerboseErrf(print PrintFunc, s string, args ...interface{}) {
61+
func (l *Logger) VerboseErrf(color Color, s string, args ...interface{}) {
6162
if l.Verbose {
62-
l.Errf(print, s, args...)
63+
l.Errf(color, s, args...)
6364
}
6465
}

task_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import (
1717
"github.com/go-task/task/v3/taskfile"
1818
)
1919

20+
func init() {
21+
_ = os.Setenv("NO_COLOR", "1")
22+
}
23+
2024
// fileContentTest provides a basic reusable test-case for running a Taskfile
2125
// and inspect generated files.
2226
type fileContentTest struct {

0 commit comments

Comments
 (0)