From aea81886130ac1117ca84cbc12012535b1b884d3 Mon Sep 17 00:00:00 2001 From: gedi Date: Wed, 2 Aug 2017 11:03:12 +0300 Subject: [PATCH] improves builder to use go env in order to find compiler and linker, closes #64 --- README.md | 3 ++- builder.go | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 57f7564..a378edb 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,8 @@ files. **Godog** acts similar compared to **go test** command, by using go compiler and linker tool in order to produce test executable. Godog contexts need to be exported the same way as **Test** functions for go -tests. +tests. Note, that if you use **godog** command tool, it will use `go` +executable to determine compiler and linker. **Godog** ships gherkin parser dependency as a subpackage. This will ensure that it is always compatible with the installed version of godog. diff --git a/builder.go b/builder.go index 53a367a..7ce40cb 100644 --- a/builder.go +++ b/builder.go @@ -17,8 +17,9 @@ import ( "unicode" ) -var compiler = filepath.Join(build.ToolDir, "compile") -var linker = filepath.Join(build.ToolDir, "link") +var tooldir = findToolDir() +var compiler = filepath.Join(tooldir, "compile") +var linker = filepath.Join(tooldir, "link") var gopaths = filepath.SplitList(build.Default.GOPATH) var goarch = build.Default.GOARCH var goos = build.Default.GOOS @@ -78,7 +79,7 @@ func Build(bin string) error { // go does it better out, err := exec.Command("go", "test", "-i").CombinedOutput() if err != nil { - return fmt.Errorf("failed to compile package %s:\n%s", pkg.Name, string(out)) + return fmt.Errorf("failed to compile package: %s, reason: %v, output: %s", pkg.Name, err, string(out)) } // let go do the dirty work and compile test @@ -95,7 +96,7 @@ func Build(bin string) error { // go has built. We will reuse it for our suite workdir. out, err = exec.Command("go", "test", "-c", "-work", "-o", temp).CombinedOutput() if err != nil { - return fmt.Errorf("failed to compile tested package %s:\n%s", pkg.Name, string(out)) + return fmt.Errorf("failed to compile tested package: %s, reason: %v, output: %s", pkg.Name, err, string(out)) } defer os.Remove(temp) @@ -140,7 +141,7 @@ func Build(bin string) error { cmd.Env = os.Environ() out, err := cmd.CombinedOutput() if err != nil { - return fmt.Errorf("failed to install godog package:\n%s", string(out)) + return fmt.Errorf("failed to install godog package: %s, reason: %v", string(out), err) } // collect all possible package dirs, will be @@ -173,7 +174,7 @@ func Build(bin string) error { cmd.Env = os.Environ() out, err = cmd.CombinedOutput() if err != nil { - return fmt.Errorf("failed to compile testmain package:\n%s", string(out)) + return fmt.Errorf("failed to compile testmain package: %v - output: %s", err, string(out)) } // link test suite executable @@ -324,3 +325,10 @@ func processPackageTestFiles(packs ...[]string) ([]string, error) { } return ctxs, nil } + +func findToolDir() string { + if out, err := exec.Command("go", "env", "GOTOOLDIR").Output(); err != nil { + return filepath.Clean(strings.TrimSpace(string(out))) + } + return filepath.Clean(build.ToolDir) +}