go test support

This commit is contained in:
alessio 2016-08-27 23:57:26 +02:00
parent 05e869c8de
commit 7bd6de35a7
4 changed files with 42 additions and 9 deletions

View File

@ -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)

View File

@ -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,

View File

@ -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
}

View File

@ -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)
}
}()
}
}
}