From 5f48eed598aed6713ffbbd8a0d17f86c3dec2029 Mon Sep 17 00:00:00 2001 From: Kamil Samigullin Date: Fri, 29 Jan 2021 00:29:30 +0300 Subject: [PATCH] fix #2: implement testit go compile --- internal/cmd/golang.go | 63 ++++++++++++++++++++++++++++++++++++++++++ internal/cmd/root.go | 13 +++++---- 2 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 internal/cmd/golang.go diff --git a/internal/cmd/golang.go b/internal/cmd/golang.go new file mode 100644 index 0000000..909fe74 --- /dev/null +++ b/internal/cmd/golang.go @@ -0,0 +1,63 @@ +package cmd + +import ( + "bufio" + "fmt" + "io" + "os/exec" + "strings" + + "github.com/spf13/cobra" + "go.octolab.org/safe" + "go.octolab.org/unsafe" +) + +func Golang() *cobra.Command { + main := cobra.Command{ + Use: "go", + Short: "`go test` proxy with extra features", + Long: "`go test` proxy with extra features.", + + Args: cobra.NoArgs, + } + + compile := cobra.Command{ + Use: "compile", + Short: "make sure that all code is compiled.", + Long: "Make sure that all code is compiled.", + + Args: cobra.NoArgs, + + RunE: func(cmd *cobra.Command, args []string) error { + bin := exec.CommandContext(cmd.Context(), "go", "test", "-run", "^Fake$$", "./...") + bin.Stderr = cmd.ErrOrStderr() + reader, err := bin.StdoutPipe() + if err != nil { + return err + } + + go safe.Do(bin.Run, func(err error) {}) + scanner := bufio.NewScanner(reader) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + line := scanner.Text() + + if strings.Contains(line, "no test files") { + continue + } + if strings.Contains(line, "no tests to run") { + continue + } + + unsafe.DoSilent(io.Copy(cmd.OutOrStdout(), strings.NewReader(line))) + unsafe.DoSilent(fmt.Fprintln(cmd.OutOrStdout())) + } + + return nil + }, + } + + main.AddCommand(&compile) + + return &main +} diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 16a19e9..090d050 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -5,16 +5,19 @@ import "github.com/spf13/cobra" // New returns the new root command. func New() *cobra.Command { command := cobra.Command{ - Use: "%template%", - Short: "%template%", - Long: "%template%", + Use: "testit", + Short: "extended `go test` for better experience", + Long: "Extended `go test` for better experience.", Args: cobra.NoArgs, SilenceErrors: false, SilenceUsage: true, } - /* configure instance */ - command.AddCommand( /* related commands */ ) + + command.AddCommand( + Golang(), + ) + return &command }