Skip to content

Commit

Permalink
Add more clean test output for mage with os/exec.Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Mo-Fatah committed Aug 22, 2023
1 parent 08149dd commit 5774a81
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion magefiles/tests.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -83,13 +84,28 @@ func Tests() error {
"--format", "short-verbose",
"--junitfile", "test-reports/unit-tests.xml",
"--jsonfile", "test-reports/unit-tests.json",
"--no-color=false",
"--", "-coverprofile=test-reports/coverage.out",
"-covermode=atomic", "./cmd/...",
"./pkg/...",
}
cmd = append(cmd, internalPackages...)

if err = sh.Run(Gotestsum, cmd...); err != nil {
testCmd := exec.Command(Gotestsum, cmd...)

// If -verbose was set, we let os.Stdout handles the output.
// Otherwise, we need to capture the tests output and print it in the case of failures.
var buffer bytes.Buffer
if os.Getenv("MAGEFILE_VERBOSE") == "1" {
testCmd.Stdout = os.Stdout
} else {
testCmd.Stdout = &buffer
}

if err := testCmd.Run(); err != nil {
if os.Getenv("MAGEFILE_VERBOSE") == "0" {
fmt.Println(buffer.String())
}
return err
}

Expand Down

0 comments on commit 5774a81

Please sign in to comment.