Skip to content

Commit

Permalink
[feature] added support for visual go test rendering from https://git…
Browse files Browse the repository at this point in the history
  • Loading branch information
na4ma4 committed Jan 31, 2025
1 parent 5f315ff commit 9d794a3
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/dosquad/mage
go 1.22.2

require (
github.com/alecthomas/kong v1.6.1
github.com/alecthomas/kong v1.7.0
github.com/dosquad/go-cliversion v0.2.1
github.com/dosquad/go-giturl v0.1.1
github.com/fatih/color v1.18.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1o
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/kong v1.6.1 h1:/7bVimARU3uxPD0hbryPE8qWrS3Oz3kPQoxA/H2NKG8=
github.com/alecthomas/kong v1.6.1/go.mod h1:p2vqieVMeTAnaC83txKtXe8FLke2X07aruPWXyMPQrU=
github.com/alecthomas/kong v1.7.0 h1:MnT8+5JxFDCvISeI6vgd/mFbAJwueJ/pqQNzZMsiqZE=
github.com/alecthomas/kong v1.7.0/go.mod h1:p2vqieVMeTAnaC83txKtXe8FLke2X07aruPWXyMPQrU=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/cheggaaa/pb v2.0.7+incompatible/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
Expand Down
51 changes: 51 additions & 0 deletions golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (

"github.com/dosquad/mage/dyndep"
"github.com/dosquad/mage/helper"
"github.com/dosquad/mage/loga"
"github.com/magefile/mage/mg"
"github.com/na4ma4/go-permbits"
"github.com/princjef/mageutil/shellcmd"
)

Expand Down Expand Up @@ -56,6 +58,55 @@ func (Golang) Test(ctx context.Context) error {
return helper.FilterCoverageOutput(filepath.Join(coverPath, "cover.out"))
}

// InstallVGT installs vgt.
func (Golang) installVGT(_ context.Context) error {
return helper.BinVGT().Ensure()
}

// VisualTest runs the tests and then renders the result using vgt (https://github.com/roblaszczak/vgt).
func (Golang) VisualTest(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Golang)
dyndep.CtxDeps(ctx, dyndep.Test)

mg.CtxDeps(ctx, Golang.installVGT)

raceArg := ""
if v := helper.GoEnv("CGO_ENABLED", "0"); v == "1" {
raceArg = "-race"
}

cmd := fmt.Sprintf(""+
"go test -count=1 -json "+
"%s "+
"\"./...\"",
raceArg)

visualTestPath := helper.MustGetArtifactPath("tests")
helper.MustMakeDir(
visualTestPath,
permbits.MustString("u=rwx,go=rx"),
)
vgtFileName := filepath.Join(visualTestPath, "vgt-output.json")

var output []byte
{
var err error
loga.PrintCommandAlways("`%s` writing to %s", cmd, vgtFileName)
output, err = shellcmd.Command(cmd).Output()
if err != nil {
return err
}
}

if err := helper.FileWrite(output, vgtFileName); err != nil {
return err
}

return helper.BinVGT().Command(
"-dont-pass-output -from-file " + vgtFileName,
).Run()
}

// Generate runs go generate.
func (Golang) Generate(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Golang)
Expand Down
20 changes: 20 additions & 0 deletions helper/bins.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,23 @@ func BinCfsslJSON() *bintool.BinTool {

return cfsslJSONCmd
}

//nolint:gochecknoglobals // ignore globals
var vgtCmd *bintool.BinTool

// BinVGT returns a singleton for vgt.
func BinVGT() *bintool.BinTool {
if vgtCmd == nil {
_ = BinVerdump().Ensure()
ver := MustVersionLoadCache().GetVersion(VGTVersion)
loga.PrintInfo("vgt Version: %s", ver)
vgtCmd = bintool.Must(bintool.NewGo(
"github.com/roblaszczak/vgt",
ver,
bintool.WithFolder(MustGetGoBin()),
bintool.WithVersionCmd(MustGetGoBin("verdump")+" mod {{.FullCmd}}"),
))
}

return vgtCmd
}
4 changes: 4 additions & 0 deletions helper/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
KustomizeVersion VersionKey = "kustomize"
KubeControllerEnvTestVersion VersionKey = "kubernetes-controller-env-test"
CFSSLVersion VersionKey = "cfssl"
VGTVersion VersionKey = "vgt"
)

const (
Expand Down Expand Up @@ -83,6 +84,7 @@ func VersionLoadCache() (*VersionCache, error) {
KubeControllerGenVersion: "",
KubeControllerEnvTestVersion: "",
CFSSLVersion: "",
VGTVersion: "",
}

var f *os.File
Expand Down Expand Up @@ -188,6 +190,8 @@ func (vc VersionCache) GetVersion(key VersionKey) string {
return vc.SetVersion(key, vc.getGithubVersion("kubernetes-sigs/controller-runtime"))
case CFSSLVersion:
return vc.SetVersion(key, vc.getGithubVersion("cloudflare/cfssl"))
case VGTVersion:
return vc.SetVersion(key, vc.getGithubVersion("roblaszczak/vgt"))
}

return ""
Expand Down
21 changes: 9 additions & 12 deletions loga/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,20 @@ func PrintInfo(format string, v ...any) {
fmt.Printf("%s %s\n", color.GreenString(">"), color.New(color.Bold).Sprintf(format, v...))
}

// PrintCommand prints the passed command line to stdout in white text with magenta chevron.
//
//nolint:forbidigo // printing output
// PrintCommand prints the passed command line to stdout in white text with magenta chevron
// when Verbose or Debug is enabled.
func PrintCommand(format string, v ...any) {
if mg.Verbose() || mg.Debug() {
fmt.Printf("%s %s\n", color.MagentaString(">"), color.New(color.Bold).Sprintf(format, v...))
PrintCommandAlways(format, v...)
}
}

// // PrintCommand prints the passed command line to stdout in white text with magenta chevron.
// //
// //nolint:forbidigo // printing output
// func PrintCommandDebug(format string, v ...any) {
// if mg.Verbose() || mg.Debug() {
// PrintCommand(format, v...)
// }
// }
// PrintCommandAlways prints the passed command line to stdout in white text with magenta chevron.
//
//nolint:forbidigo // printing output
func PrintCommandAlways(format string, v ...any) {
fmt.Printf("%s %s\n", color.MagentaString(">"), color.New(color.Bold).Sprintf(format, v...))
}

// PrintDebug prints the passed debug message to stdout in white text with blue chevron.
//
Expand Down

0 comments on commit 9d794a3

Please sign in to comment.