diff --git a/go.mod b/go.mod index 6871205..6d7236b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 4897093..064283b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/golang.go b/golang.go index 55674e0..1d6aa36 100644 --- a/golang.go +++ b/golang.go @@ -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" ) @@ -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) diff --git a/helper/bins.go b/helper/bins.go index bbd560f..dba00b4 100644 --- a/helper/bins.go +++ b/helper/bins.go @@ -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 +} diff --git a/helper/versions.go b/helper/versions.go index 6fa037d..b950ec3 100644 --- a/helper/versions.go +++ b/helper/versions.go @@ -46,6 +46,7 @@ const ( KustomizeVersion VersionKey = "kustomize" KubeControllerEnvTestVersion VersionKey = "kubernetes-controller-env-test" CFSSLVersion VersionKey = "cfssl" + VGTVersion VersionKey = "vgt" ) const ( @@ -83,6 +84,7 @@ func VersionLoadCache() (*VersionCache, error) { KubeControllerGenVersion: "", KubeControllerEnvTestVersion: "", CFSSLVersion: "", + VGTVersion: "", } var f *os.File @@ -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 "" diff --git a/loga/print.go b/loga/print.go index ee071b8..6180916 100644 --- a/loga/print.go +++ b/loga/print.go @@ -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. //