Skip to content

Commit

Permalink
Enforce NO_COLOR=1 on tests to make sure it passes
Browse files Browse the repository at this point in the history
Closes #459
Fixes #480
Ref #343
Ref fatih/color#137
  • Loading branch information
andreynering committed May 31, 2021
1 parent 7f92b70 commit 4e1f2ad
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add support for the `NO_COLOR` environment variable.
([#459](https://github.com/go-task/task/issues/459), [fatih/color#137](https://github.com/fatih/color/pull/137)).
- Fix bug where sources were not considering the right directory
in `--watch` mode
([#484](https://github.com/go-task/task/issues/484), [#485](https://github.com/go-task/task/pull/485)).
Expand Down
2 changes: 1 addition & 1 deletion cmd/task/task.go
Expand Up @@ -89,7 +89,7 @@ func main() {
pflag.StringVarP(&dir, "dir", "d", "", "sets directory of execution")
pflag.StringVarP(&entrypoint, "taskfile", "t", "", `choose which Taskfile to run. Defaults to "Taskfile.yml"`)
pflag.StringVarP(&output, "output", "o", "", "sets output style: [interleaved|group|prefixed]")
pflag.BoolVarP(&color, "color", "c", true, "colored output")
pflag.BoolVarP(&color, "color", "c", true, "colored output. Enabled by default. Set flag to false or use NO_COLOR=1 to disable")
pflag.IntVarP(&concurrency, "concurrency", "C", 0, "limit number tasks to run concurrently")
pflag.Parse()

Expand Down
35 changes: 18 additions & 17 deletions internal/logger/logger.go
Expand Up @@ -6,17 +6,16 @@ import (
"github.com/fatih/color"
)

type Color func() PrintFunc
type PrintFunc func(io.Writer, string, ...interface{})

var (
Default PrintFunc = color.New(color.Reset).FprintfFunc()
Blue PrintFunc = color.New(color.FgBlue).FprintfFunc()
Green PrintFunc = color.New(color.FgGreen).FprintfFunc()
Cyan PrintFunc = color.New(color.FgCyan).FprintfFunc()
Yellow PrintFunc = color.New(color.FgYellow).FprintfFunc()
Magenta PrintFunc = color.New(color.FgMagenta).FprintfFunc()
Red PrintFunc = color.New(color.FgRed).FprintfFunc()
)
func Default() PrintFunc { return color.New(color.Reset).FprintfFunc() }
func Blue() PrintFunc { return color.New(color.FgBlue).FprintfFunc() }
func Green() PrintFunc { return color.New(color.FgGreen).FprintfFunc() }
func Cyan() PrintFunc { return color.New(color.FgCyan).FprintfFunc() }
func Yellow() PrintFunc { return color.New(color.FgYellow).FprintfFunc() }
func Magenta() PrintFunc { return color.New(color.FgMagenta).FprintfFunc() }
func Red() PrintFunc { return color.New(color.FgRed).FprintfFunc() }

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

// Outf prints stuff to STDOUT.
func (l *Logger) Outf(print PrintFunc, s string, args ...interface{}) {
func (l *Logger) Outf(color Color, s string, args ...interface{}) {
if len(args) == 0 {
s, args = "%s", []interface{}{s}
}
if !l.Color {
print = Default
color = Default
}
print := color()
print(l.Stdout, s+"\n", args...)
}

// VerboseOutf prints stuff to STDOUT if verbose mode is enabled.
func (l *Logger) VerboseOutf(print PrintFunc, s string, args ...interface{}) {
func (l *Logger) VerboseOutf(color Color, s string, args ...interface{}) {
if l.Verbose {
l.Outf(print, s, args...)
l.Outf(color, s, args...)
}
}

// Errf prints stuff to STDERR.
func (l *Logger) Errf(print PrintFunc, s string, args ...interface{}) {
func (l *Logger) Errf(color Color, s string, args ...interface{}) {
if len(args) == 0 {
s, args = "%s", []interface{}{s}
}
if !l.Color {
print = Default
color = Default
}
print := color()
print(l.Stderr, s+"\n", args...)
}

// VerboseErrf prints stuff to STDERR if verbose mode is enabled.
func (l *Logger) VerboseErrf(print PrintFunc, s string, args ...interface{}) {
func (l *Logger) VerboseErrf(color Color, s string, args ...interface{}) {
if l.Verbose {
l.Errf(print, s, args...)
l.Errf(color, s, args...)
}
}
4 changes: 4 additions & 0 deletions task_test.go
Expand Up @@ -17,6 +17,10 @@ import (
"github.com/go-task/task/v3/taskfile"
)

func init() {
_ = os.Setenv("NO_COLOR", "1")
}

// fileContentTest provides a basic reusable test-case for running a Taskfile
// and inspect generated files.
type fileContentTest struct {
Expand Down

0 comments on commit 4e1f2ad

Please sign in to comment.