Skip to content

Commit

Permalink
refactor(tooling): don't exit on error and output the failing install…
Browse files Browse the repository at this point in the history
… command
  • Loading branch information
sheldonhull committed Jan 24, 2022
1 parent cb20ace commit f0d0b2f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# magetools

![Coverage](https://img.shields.io/badge/Coverage-54.2%25-yellow)
![Coverage](https://img.shields.io/badge/Coverage-53.9%25-yellow)

General tooling helpers for simplifying cross repository automation using Mage.

Expand Down
28 changes: 19 additions & 9 deletions tooling/tooling.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func InstallTools(tools []string) error {
// Originally found from: https://www.yellowduck.be/posts/reading-command-output-line-by-line/ and modified.
//nolint:funlen // This is ok for now. Can refactor into smaller pieces later if needed.
func SilentInstallTools(toolList []string) error {
var errorCount int
magetoolsutils.CheckPtermDebug()
if ci.IsCI() {
pterm.DisableStyling()
Expand All @@ -76,11 +77,7 @@ func SilentInstallTools(toolList []string) error {
// WithSequence("|", "/", "-", "|", "/", "-", "\\").

// spinnerLiveText, _ := pterm.DefaultSpinner.Start("InstallTools")
defer func() {
msg := fmt.Sprintf("SilentInstallTools: %s\n", humanize.Time(start))
spin.Success(msg) // Resolve spinner with success message.
// pterm.Success.Println(msg)
}()

pterm.Info.Printf("items to iterate through: %d", len(toolList))
for _, item := range toolList {
cmd := exec.Command("go", "install", item)
Expand Down Expand Up @@ -114,9 +111,12 @@ func SilentInstallTools(toolList []string) error {
// Start the command and check for errors
err := cmd.Start()
if err != nil {
pterm.Error.Printfln("unable to install: %q %v", item, err)
errorCount++
spin.Fail(err)
_ = spin.Stop()
return err
continue
// _ = spin.Stop() // NOTE: continue installing other tools, don't stop everything, just fail this and count it
// return err
}

// Wait for all output to be processed
Expand All @@ -126,11 +126,21 @@ func SilentInstallTools(toolList []string) error {
err = cmd.Wait()
if err != nil {
spin.Fail(err)
_ = spin.Stop()
return err
errorCount++
pterm.Error.Printfln("unable to install: %q %v", item, err)
// _ = spin.Stop() // NOTE: continue installing other tools, don't stop everything, just fail this and count it
// return err
continue
}
spin.Success(item)
}

if errorCount > 0 {
pterm.Error.Printfln("SilentInstallTools: total errors: [%d] %s\n", errorCount, humanize.Time(start))
return fmt.Errorf("SilentInstallTools: total errors: [%d]", errorCount)
}
msg := fmt.Sprintf("SilentInstallTools: %s\n", humanize.Time(start))
pterm.Success.Println(msg)
return nil
}

Expand Down
63 changes: 41 additions & 22 deletions tooling/tooling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,47 @@ func TestInstallTools(t *testing.T) {
// Try cleaning a specific directory to see output if already ran
// sudo rm -rf $(go env GOPATH)/pkg/mod/github.com/fatih.
func TestGo_SilentInit(t *testing.T) {
if !strings.Contains(strings.ToLower(os.Getenv("GOTESTS")), "slow") {
t.Skip("GOTESTS should include 'slow' to run this test")
}
is := iz.New(t)
if os.Getenv("PRESENTATION_TEST") != "1" {
t.Skip("PRESENTATION_TEST != 1 so skipping")
}
// pterm.DisableColor()
// pterm.DisableStyling()
toolList := []string{
"github.com/goreleaser/goreleaser@v0.174.1",
"github.com/golangci/golangci-lint/cmd/golangci-lint@master",
"github.com/dustinkirkland/golang-petname/cmd/petname@master",
"mvdan.cc/gofumpt@latest",
"golang.org/x/tools/gopls@latest",
"github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest",
"github.com/ramya-rao-a/go-outline@latest",
"github.com/cweill/gotests/gotests@latest",
"github.com/fatih/gomodifytags@latest",
}
err := tooling.SilentInstallTools(toolList)
is.NoErr(err) // Init should not fail
t.Run("install list of tools", func(t *testing.T) {
if !strings.Contains(strings.ToLower(os.Getenv("GOTESTS")), "slow") {
t.Skip("GOTESTS should include 'slow' to run this test")
}
is := iz.New(t)
if os.Getenv("PRESENTATION_TEST") != "1" {
t.Skip("PRESENTATION_TEST != 1 so skipping")
}
// pterm.DisableColor()
// pterm.DisableStyling()
toolList := []string{
"github.com/goreleaser/goreleaser@v0.174.1",
"github.com/golangci/golangci-lint/cmd/golangci-lint@master",
"github.com/dustinkirkland/golang-petname/cmd/petname@master",
"mvdan.cc/gofumpt@latest",
"golang.org/x/tools/gopls@latest",
"github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest",
"github.com/ramya-rao-a/go-outline@latest",
"github.com/cweill/gotests/gotests@latest",
"github.com/fatih/gomodifytags@latest",
}
err := tooling.SilentInstallTools(toolList)
is.NoErr(err) // Init should not fail
})

t.Run("fail on item 2 and advise what failed", func(t *testing.T) {
if !strings.Contains(strings.ToLower(os.Getenv("GOTESTS")), "slow") {
t.Skip("GOTESTS should include 'slow' to run this test")
}
is := iz.New(t)
if os.Getenv("PRESENTATION_TEST") != "1" {
t.Skip("PRESENTATION_TEST != 1 so skipping")
}
toolList := []string{
"github.com/goreleaser/goreleaser@v0.174.1",
"github.com/golangci/golangci-lint/cmd/golangci-lintINVALID@master",
"github.com/dustinkirkland/golang-petname/cmd/petname@master",
}
err := tooling.SilentInstallTools(toolList)
is.True(err != nil) // An error should be returned with useful context should not fail
})
}

// This is a test for the minimal output, doesn't need to run unless manully invoked with:
Expand Down

0 comments on commit f0d0b2f

Please sign in to comment.