reorganize the output binary command -o
Этот коммит содержится в:
родитель
4fa1662f1d
коммит
eac32e6f48
3 изменённых файлов: 41 добавлений и 48 удалений
34
builder.go
34
builder.go
|
@ -41,7 +41,7 @@ func main() {
|
||||||
os.Exit(status)
|
os.Exit(status)
|
||||||
}`))
|
}`))
|
||||||
|
|
||||||
// Build creates a test package like go test command.
|
// Build creates a test package like go test command at given target path.
|
||||||
// If there are no go files in tested directory, then
|
// If there are no go files in tested directory, then
|
||||||
// it simply builds a godog executable to scan features.
|
// it simply builds a godog executable to scan features.
|
||||||
//
|
//
|
||||||
|
@ -53,16 +53,10 @@ func main() {
|
||||||
// of tested package.
|
// of tested package.
|
||||||
//
|
//
|
||||||
// Returns the path to generated executable
|
// Returns the path to generated executable
|
||||||
func Build() (string, error) {
|
func Build(bin string) error {
|
||||||
abs, err := filepath.Abs(".")
|
abs, err := filepath.Abs(".")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
bin := filepath.Join(abs, "godog.test")
|
|
||||||
// suffix with .exe for windows
|
|
||||||
if goos == "windows" {
|
|
||||||
bin += ".exe"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we allow package to be nil, if godog is run only when
|
// we allow package to be nil, if godog is run only when
|
||||||
|
@ -70,7 +64,7 @@ func Build() (string, error) {
|
||||||
pkg := importPackage(abs)
|
pkg := importPackage(abs)
|
||||||
src, anyContexts, err := buildTestMain(pkg)
|
src, anyContexts, err := buildTestMain(pkg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bin, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
workdir := fmt.Sprintf(filepath.Join("%s", "godog-%d"), os.TempDir(), time.Now().UnixNano())
|
workdir := fmt.Sprintf(filepath.Join("%s", "godog-%d"), os.TempDir(), time.Now().UnixNano())
|
||||||
|
@ -84,7 +78,7 @@ func Build() (string, error) {
|
||||||
// go does it better
|
// go does it better
|
||||||
out, err := exec.Command("go", "test", "-i").CombinedOutput()
|
out, err := exec.Command("go", "test", "-i").CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bin, fmt.Errorf("failed to compile package %s:\n%s", pkg.Name, string(out))
|
return fmt.Errorf("failed to compile package %s:\n%s", pkg.Name, string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
// let go do the dirty work and compile test
|
// let go do the dirty work and compile test
|
||||||
|
@ -101,21 +95,21 @@ func Build() (string, error) {
|
||||||
// go has built. We will reuse it for our suite workdir.
|
// go has built. We will reuse it for our suite workdir.
|
||||||
out, err = exec.Command("go", "test", "-c", "-work", "-o", temp).CombinedOutput()
|
out, err = exec.Command("go", "test", "-c", "-work", "-o", temp).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bin, fmt.Errorf("failed to compile tested package %s:\n%s", pkg.Name, string(out))
|
return fmt.Errorf("failed to compile tested package %s:\n%s", pkg.Name, string(out))
|
||||||
}
|
}
|
||||||
defer os.Remove(temp)
|
defer os.Remove(temp)
|
||||||
|
|
||||||
// extract go-build temporary directory as our workdir
|
// extract go-build temporary directory as our workdir
|
||||||
workdir = strings.TrimSpace(string(out))
|
workdir = strings.TrimSpace(string(out))
|
||||||
if !strings.HasPrefix(workdir, "WORK=") {
|
if !strings.HasPrefix(workdir, "WORK=") {
|
||||||
return bin, fmt.Errorf("expected WORK dir path, but got: %s", workdir)
|
return fmt.Errorf("expected WORK dir path, but got: %s", workdir)
|
||||||
}
|
}
|
||||||
workdir = strings.Replace(workdir, "WORK=", "", 1)
|
workdir = strings.Replace(workdir, "WORK=", "", 1)
|
||||||
testdir = filepath.Join(workdir, pkg.ImportPath, "_test")
|
testdir = filepath.Join(workdir, pkg.ImportPath, "_test")
|
||||||
} else {
|
} else {
|
||||||
// still need to create temporary workdir
|
// still need to create temporary workdir
|
||||||
if err = os.MkdirAll(testdir, 0755); err != nil {
|
if err = os.MkdirAll(testdir, 0755); err != nil {
|
||||||
return bin, err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(workdir)
|
defer os.RemoveAll(workdir)
|
||||||
|
@ -124,7 +118,7 @@ func Build() (string, error) {
|
||||||
testmain := filepath.Join(testdir, "_testmain.go")
|
testmain := filepath.Join(testdir, "_testmain.go")
|
||||||
err = ioutil.WriteFile(testmain, src, 0644)
|
err = ioutil.WriteFile(testmain, src, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bin, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// godog library may not be imported in tested package
|
// godog library may not be imported in tested package
|
||||||
|
@ -137,7 +131,7 @@ func Build() (string, error) {
|
||||||
}
|
}
|
||||||
godogPkg, err := locatePackage(try)
|
godogPkg, err := locatePackage(try)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bin, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure godog package archive is installed, gherkin
|
// make sure godog package archive is installed, gherkin
|
||||||
|
@ -146,7 +140,7 @@ func Build() (string, error) {
|
||||||
cmd.Env = os.Environ()
|
cmd.Env = os.Environ()
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bin, fmt.Errorf("failed to install godog package:\n%s", string(out))
|
return fmt.Errorf("failed to install godog package:\n%s", string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
// collect all possible package dirs, will be
|
// collect all possible package dirs, will be
|
||||||
|
@ -179,7 +173,7 @@ func Build() (string, error) {
|
||||||
cmd.Env = os.Environ()
|
cmd.Env = os.Environ()
|
||||||
out, err = cmd.CombinedOutput()
|
out, err = cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bin, fmt.Errorf("failed to compile testmain package:\n%s", string(out))
|
return fmt.Errorf("failed to compile testmain package:\n%s", string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
// link test suite executable
|
// link test suite executable
|
||||||
|
@ -199,10 +193,10 @@ func Build() (string, error) {
|
||||||
msg := `failed to link test executable:
|
msg := `failed to link test executable:
|
||||||
reason: %s
|
reason: %s
|
||||||
command: %s`
|
command: %s`
|
||||||
return bin, fmt.Errorf(msg, string(out), linker+" '"+strings.Join(args, "' '")+"'")
|
return fmt.Errorf(msg, string(out), linker+" '"+strings.Join(args, "' '")+"'")
|
||||||
}
|
}
|
||||||
|
|
||||||
return bin, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func locatePackage(try []string) (*build.Package, error) {
|
func locatePackage(try []string) (*build.Package, error) {
|
||||||
|
|
|
@ -2,9 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go/build"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
@ -18,23 +20,20 @@ var parsedStatus int
|
||||||
var stdout = io.Writer(os.Stdout)
|
var stdout = io.Writer(os.Stdout)
|
||||||
var stderr = statusOutputFilter(os.Stderr)
|
var stderr = statusOutputFilter(os.Stderr)
|
||||||
|
|
||||||
func buildAndRun(output string) (int, error) {
|
func buildAndRun() (int, error) {
|
||||||
var status int
|
var status int
|
||||||
|
|
||||||
bin, err := godog.Build()
|
bin, err := filepath.Abs("godog.test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 1, err
|
return 1, err
|
||||||
}
|
}
|
||||||
|
if build.Default.GOOS == "windows" {
|
||||||
|
bin += ".exe"
|
||||||
|
}
|
||||||
|
if err = godog.Build(bin); err != nil {
|
||||||
|
return 1, err
|
||||||
|
}
|
||||||
defer os.Remove(bin)
|
defer os.Remove(bin)
|
||||||
//output the generated binary file and exit if the output option was provided
|
|
||||||
if output != ""{
|
|
||||||
err = Copy(output,bin)
|
|
||||||
status := 0
|
|
||||||
if err != nil {
|
|
||||||
status = 1
|
|
||||||
}
|
|
||||||
return status, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := exec.Command(bin, os.Args[1:]...)
|
cmd := exec.Command(bin, os.Args[1:]...)
|
||||||
cmd.Stdout = stdout
|
cmd.Stdout = stdout
|
||||||
|
@ -78,6 +77,19 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(output) > 0 {
|
||||||
|
bin, err := filepath.Abs(output)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(stderr, "could not locate absolute path for:", output, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if err = godog.Build(bin); err != nil {
|
||||||
|
fmt.Fprintln(stderr, "could not build binary at:", output, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
if noclr {
|
if noclr {
|
||||||
stdout = noColorsWriter(stdout)
|
stdout = noColorsWriter(stdout)
|
||||||
stderr = noColorsWriter(stderr)
|
stderr = noColorsWriter(stderr)
|
||||||
|
@ -91,7 +103,7 @@ func main() {
|
||||||
os.Exit(0) // should it be 0?
|
os.Exit(0) // should it be 0?
|
||||||
}
|
}
|
||||||
|
|
||||||
status, err := buildAndRun(output)
|
status, err := buildAndRun()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(stderr, err)
|
fmt.Fprintln(stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -120,16 +132,3 @@ type writerFunc func([]byte) (int, error)
|
||||||
func (w writerFunc) Write(b []byte) (int, error) {
|
func (w writerFunc) Write(b []byte) (int, error) {
|
||||||
return w(b)
|
return w(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Copy(dst, src string) error {
|
|
||||||
in, err := os.Open(src)
|
|
||||||
if err != nil { return err }
|
|
||||||
defer in.Close()
|
|
||||||
out, err := os.Create(dst)
|
|
||||||
if err != nil { return err }
|
|
||||||
defer out.Close()
|
|
||||||
_, err = io.Copy(out, in)
|
|
||||||
cerr := out.Close()
|
|
||||||
if err != nil { return err }
|
|
||||||
return cerr
|
|
||||||
}
|
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче