Skip to content

Commit

Permalink
Add ability to disable color in test log output (#1036)
Browse files Browse the repository at this point in the history
This adds the ability to pass a `--no-color` flag to `encore test` to
disable color output in zerolog. This is useful when displaying output
in windows which don't support colors. This is the case for VSCode test
output:

The current output of those runs look like this, which makes debugging
failed tests extremely difficult:

<img width="1211" alt="image"
src="https://github.com/encoredev/encore/assets/169475/c18c74f3-1cf6-4e38-9fd7-a9acda2c2a4c">


WARNING: This is not tested, as I couldn't quickly figure out how to do
that. Building the binary and attempting to use it caused a daemon
version mismatch error though both cli and daemon report the same
version (I assume there's some hash checking).
  • Loading branch information
Willyham authored Feb 26, 2024
1 parent ddcf9a7 commit 0daa253
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
17 changes: 16 additions & 1 deletion cli/cmd/encore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encr.dev/cli/cmd/encore/cmdutil"
"encr.dev/cli/cmd/encore/root"
daemonpb "encr.dev/proto/encore/daemon"

// Register commands
_ "encr.dev/cli/cmd/encore/app"
_ "encr.dev/cli/cmd/encore/k8s"
Expand Down Expand Up @@ -147,7 +148,18 @@ func streamCommandOutput(stream commandOutputStream, converter outputConverter)
}
}

func convertJSONLogs() outputConverter {
type convertLogOptions struct {
Color bool
}

type convertLogOption func(*convertLogOptions)

func convertJSONLogs(opts ...convertLogOption) outputConverter {
options := convertLogOptions{}
for _, opt := range opts {
opt(&options)
}

var logMutex sync.Mutex
logLineBuffer := bytes.NewBuffer(make([]byte, 0, 1024))
cout := zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
Expand All @@ -160,6 +172,9 @@ func convertJSONLogs() outputConverter {
return nil
}
})
if !options.Color {
cout.NoColor = true
}

return func(line []byte) []byte {
// If this isn't a JSON log line, just return it as-is
Expand Down
13 changes: 10 additions & 3 deletions cli/cmd/encore/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var testCmd = &cobra.Command{
var (
traceFile string
codegenDebug bool
noColor bool
)
// Support specific args but otherwise let all args be passed on to "go test"
for i := 0; i < len(args); i++ {
Expand Down Expand Up @@ -52,15 +53,19 @@ var testCmd = &cobra.Command{
codegenDebug = true
args = slices.Delete(args, i, i+1)
i--
} else if arg == "--no-color" {
noColor = true
args = slices.Delete(args, i, i+1)
i--
}
}

appRoot, relPath := determineAppRoot()
runTests(appRoot, relPath, args, traceFile, codegenDebug)
runTests(appRoot, relPath, args, traceFile, codegenDebug, noColor)
},
}

func runTests(appRoot, testDir string, args []string, traceFile string, codegenDebug bool) {
func runTests(appRoot, testDir string, args []string, traceFile string, codegenDebug bool, noColor bool) {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

Expand All @@ -70,7 +75,9 @@ func runTests(appRoot, testDir string, args []string, traceFile string, codegenD
cancel()
}()

converter := convertJSONLogs()
converter := convertJSONLogs(func(clo *convertLogOptions) {
clo.Color = !noColor
})
if slices.Contains(args, "-json") {
converter = convertTestEventOutputOnly(converter)
}
Expand Down

0 comments on commit 0daa253

Please sign in to comment.