1
0
mirror of https://github.com/go-task/task.git synced 2025-01-04 03:48:02 +02:00

Enforce NO_COLOR=1 on tests to make sure it passes

Closes #459
Fixes #480
Ref #343
Ref fatih/color#137
This commit is contained in:
Andrey Nering 2021-05-30 22:48:48 -03:00
parent 7f92b7072d
commit 4e1f2ad017
4 changed files with 25 additions and 18 deletions

View File

@ -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)).

View File

@ -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()

View File

@ -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.
@ -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...)
}
}

View File

@ -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 {