handle exit status from printing twice, closes #12
Этот коммит содержится в:
родитель
d4829078a9
коммит
4157f01786
2 изменённых файлов: 34 добавлений и 16 удалений
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче