From 83a2add87ac83ea0bc97e62e11ab55a427b5f95c Mon Sep 17 00:00:00 2001 From: alessio Date: Sat, 27 Aug 2016 23:57:26 +0200 Subject: [PATCH] go test support --- main.go | 4 +++- realize/config.go | 4 +++- realize/project.go | 28 +++++++++++++++++++++------- realize/watcher.go | 15 +++++++++++++++ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 2a529c1..65464ad 100644 --- a/main.go +++ b/main.go @@ -65,6 +65,7 @@ func main() { &cli.BoolFlag{Name: "no-run", Usage: "Disables the run"}, &cli.BoolFlag{Name: "no-bin", Usage: "Disables the installation"}, &cli.BoolFlag{Name: "no-fmt", Usage: "Disables the fmt (go fmt)"}, + &cli.BoolFlag{Name: "test", Value: false, Usage: "Enable the tests"}, &cli.BoolFlag{Name: "config", Value: false, Usage: "Take the defined settings if exist a config file."}, }, Action: func(p *cli.Context) error { @@ -84,10 +85,11 @@ func main() { Flags: []cli.Flag{ &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: r.WorkingDir(), Usage: "Project name"}, &cli.StringFlag{Name: "path", Aliases: []string{"b"}, Value: "/", Usage: "Project base path"}, - &cli.BoolFlag{Name: "build", Value: false, Usage: "Enable go build"}, + &cli.BoolFlag{Name: "build", Value: false, Usage: "Enable the build"}, &cli.BoolFlag{Name: "no-run", Usage: "Disables the run"}, &cli.BoolFlag{Name: "no-bin", Usage: "Disables the installation"}, &cli.BoolFlag{Name: "no-fmt", Usage: "Disables the fmt (go fmt)"}, + &cli.BoolFlag{Name: "test", Value: false, Usage: "Enable the tests"}, }, Action: func(p *cli.Context) error { y := r.New(p) diff --git a/realize/config.go b/realize/config.go index 5156c6e..ac6dd7e 100644 --- a/realize/config.go +++ b/realize/config.go @@ -32,6 +32,7 @@ func New(params *cli.Context) *Config { Bin: boolFlag(params.Bool("no-bin")), Run: boolFlag(params.Bool("no-run")), Fmt: boolFlag(params.Bool("no-fmt")), + Test: params.Bool("test"), Params: argsParam(params), Watcher: Watcher{ Paths: watcherPaths, @@ -90,7 +91,7 @@ func WorkingDir() string { func Duplicates(value Project, arr []Project) (Project, error) { for _, val := range arr { if value.Path == val.Path || value.Name == val.Name { - return errors.New("There is a duplicate of '" + val.Name + "'. Check your config file!"), val + return val, errors.New("There is a duplicate of '" + val.Name + "'. Check your config file!") } } return Project{}, nil @@ -147,6 +148,7 @@ func (h *Config) Add(params *cli.Context) error { Bin: boolFlag(params.Bool("no-bin")), Run: boolFlag(params.Bool("no-run")), Fmt: boolFlag(params.Bool("no-fmt")), + Test: params.Bool("test"), Params: argsParam(params), Watcher: Watcher{ Paths: watcherPaths, diff --git a/realize/project.go b/realize/project.go index 43461c2..de1896e 100644 --- a/realize/project.go +++ b/realize/project.go @@ -22,6 +22,7 @@ type Project struct { Bin bool `yaml:"app_bin,omitempty"` Build bool `yaml:"app_build,omitempty"` Fmt bool `yaml:"app_fmt,omitempty"` + Test bool `yaml:"app_test,omitempty"` Params []string `yaml:"app_params,omitempty"` Watcher Watcher `yaml:"app_watcher,omitempty"` } @@ -83,7 +84,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) } // GoBuild is an implementation of the "go build" -func (p *Project) GoBuild() (error, string) { +func (p *Project) GoBuild() (string, error) { var out bytes.Buffer var stderr bytes.Buffer build := exec.Command("go", "build") @@ -91,27 +92,27 @@ func (p *Project) GoBuild() (error, string) { build.Stdout = &out build.Stderr = &stderr if err := build.Run(); err != nil { - return err, stderr.String() + return stderr.String(), err } - return nil, "" + return "", nil } // GoInstall is an implementation of the "go install" -func (p *Project) GoInstall() (error, string) { +func (p *Project) GoInstall() (string, error) { var out bytes.Buffer var stderr bytes.Buffer err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin")) if err != nil { - return nil, "" + return "", nil } build := exec.Command("go", "install") build.Dir = p.base build.Stdout = &out build.Stderr = &stderr if err := build.Run(); err != nil { - return err, stderr.String() + return stderr.String(), err } - return nil, "" + return "", nil } // GoFmt is an implementation of the gofmt @@ -126,3 +127,16 @@ func (p *Project) GoFmt(path string) (io.Writer, error) { } return nil, nil } + +// GoTest is an implementation of the go test +func (p *Project) GoTest(path string) (io.Writer, error) { + var out bytes.Buffer + build := exec.Command("go", "test") + build.Dir = path + build.Stdout = &out + build.Stderr = &out + if err := build.Run(); err != nil { + return build.Stdout, err + } + return nil, nil +} diff --git a/realize/watcher.go b/realize/watcher.go index 282338e..54048a4 100644 --- a/realize/watcher.go +++ b/realize/watcher.go @@ -178,6 +178,16 @@ func (p *Project) fmt(path string) error { return nil } +// Build calls an implementation of the "go test" +func (p *Project) test(path string) error { + if p.Test { + if _, err := p.GoTest(path); err != nil { + log.Println(pname(p.Name, 1), Red("Go Test fails in "), ":", Magenta(path)) + } + } + return nil +} + // Walks the file tree of a project func (p *Project) walks(watcher *fsnotify.Watcher) { var files, folders int64 @@ -202,6 +212,11 @@ func (p *Project) walks(watcher *fsnotify.Watcher) { } else { folders++ + go func() { + if err := p.test(path); err != nil { + fmt.Println(err) + } + }() } } }