From 4157f017869fb36fcf9ab4862e8e0e5115adb9fc Mon Sep 17 00:00:00 2001 From: gedi Date: Fri, 3 Jul 2015 17:16:12 +0300 Subject: [PATCH] handle exit status from printing twice, closes #12 --- README.md | 5 ++--- cmd/godog/main.go | 45 ++++++++++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index d67ae80..a31addf 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ # Godog +**The API is likely to change a few times before we reach 1.0.0** + **Godog** is an open source behavior-driven development framework for [go][golang] programming language. What is behavior-driven development, you ask? It’s the idea that you start by writing human-readable sentences that describe a feature of your application and how it should work, and only then implement this behavior in software. @@ -16,9 +18,6 @@ to functionally test your application while maintaining all test related source and replaces **main** func with it's own and runs the build to test described application behavior in feature files. Production builds remain clean without any test related source code. -The public [API][godoc] is small and should be stable for the future releases. Something may be added or exported, but -not changed most likely. I'll try to respect **backward compatibility** as much as possible. - ### Install go get github.com/DATA-DOG/godog/cmd/godog diff --git a/cmd/godog/main.go b/cmd/godog/main.go index cb00c40..397f8f0 100644 --- a/cmd/godog/main.go +++ b/cmd/godog/main.go @@ -1,53 +1,72 @@ package main import ( + "bytes" "fmt" + "io" "os" "os/exec" + "regexp" + "strconv" + "strings" "time" "github.com/DATA-DOG/godog" "github.com/shiena/ansicolor" ) -func buildAndRun() error { +var statusMatch = regexp.MustCompile("^exit status (\\d+)$") + +func buildAndRun() (status int, err error) { // will support Ansi colors for windows stdout := ansicolor.NewAnsiColorWriter(os.Stdout) + buffer := bytes.NewBuffer([]byte("")) + stderr := ansicolor.NewAnsiColorWriter(buffer) builtFile := fmt.Sprintf("%s/%dgodog.go", os.TempDir(), time.Now().UnixNano()) - // @TODO: then there is a suite error or panic, it may - // be interesting to see the built file. But we - // even cannot determine the status of exit error - // so leaving it for the future buf, err := godog.Build() if err != nil { - return err + return } + w, err := os.Create(builtFile) if err != nil { - return err + return } defer os.Remove(builtFile) + if _, err = w.Write(buf); err != nil { w.Close() - return err + return } w.Close() cmd := exec.Command("go", append([]string{"run", builtFile}, os.Args[1:]...)...) cmd.Stdout = stdout - cmd.Stderr = stdout + cmd.Stderr = stderr - return cmd.Run() + defer func() { + s := strings.TrimSpace(buffer.String()) + if s == "" { + status = 0 + } else if m := statusMatch.FindStringSubmatch(s); len(m) > 1 { + status, _ = strconv.Atoi(m[1]) + } else { + io.Copy(stdout, buffer) + } + }() + + return status, cmd.Run() } func main() { - switch err := buildAndRun().(type) { + status, err := buildAndRun() + switch e := err.(type) { case nil: case *exec.ExitError: - os.Exit(1) + os.Exit(status) default: - panic(err) + panic(e) } }