handle exit status from printing twice, closes #12

Этот коммит содержится в:
gedi 2015-07-03 17:16:12 +03:00
родитель d4829078a9
коммит 4157f01786
2 изменённых файлов: 34 добавлений и 16 удалений

Просмотреть файл

@ -3,6 +3,8 @@
# Godog # 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. **Godog** is an open source behavior-driven development framework for [go][golang] programming language.
What is behavior-driven development, you ask? Its the idea that you start by writing human-readable sentences that What is behavior-driven development, you ask? Its 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. 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. 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. 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 ### Install
go get github.com/DATA-DOG/godog/cmd/godog go get github.com/DATA-DOG/godog/cmd/godog

Просмотреть файл

@ -1,53 +1,72 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"io"
"os" "os"
"os/exec" "os/exec"
"regexp"
"strconv"
"strings"
"time" "time"
"github.com/DATA-DOG/godog" "github.com/DATA-DOG/godog"
"github.com/shiena/ansicolor" "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 // will support Ansi colors for windows
stdout := ansicolor.NewAnsiColorWriter(os.Stdout) stdout := ansicolor.NewAnsiColorWriter(os.Stdout)
buffer := bytes.NewBuffer([]byte(""))
stderr := ansicolor.NewAnsiColorWriter(buffer)
builtFile := fmt.Sprintf("%s/%dgodog.go", os.TempDir(), time.Now().UnixNano()) 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() buf, err := godog.Build()
if err != nil { if err != nil {
return err return
} }
w, err := os.Create(builtFile) w, err := os.Create(builtFile)
if err != nil { if err != nil {
return err return
} }
defer os.Remove(builtFile) defer os.Remove(builtFile)
if _, err = w.Write(buf); err != nil { if _, err = w.Write(buf); err != nil {
w.Close() w.Close()
return err return
} }
w.Close() w.Close()
cmd := exec.Command("go", append([]string{"run", builtFile}, os.Args[1:]...)...) cmd := exec.Command("go", append([]string{"run", builtFile}, os.Args[1:]...)...)
cmd.Stdout = stdout 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() { func main() {
switch err := buildAndRun().(type) { status, err := buildAndRun()
switch e := err.(type) {
case nil: case nil:
case *exec.ExitError: case *exec.ExitError:
os.Exit(1) os.Exit(status)
default: default:
panic(err) panic(e)
} }
} }