From 5ed33995bda4a3324e212e69a2d9b4eb17404c5b Mon Sep 17 00:00:00 2001 From: gedi Date: Sun, 7 Oct 2018 01:06:26 +0300 Subject: [PATCH] more explicit workdir check for builder, closes #139 --- builder.go | 40 ++++++++++++++++++++++++++-------------- builder_go110.go | 30 +++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/builder.go b/builder.go index b19dfb7..4cd745d 100644 --- a/builder.go +++ b/builder.go @@ -85,30 +85,42 @@ func Build(bin string) error { 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 - // package with it's dependencies. Older go - // versions does not accept existing file output - // so we create a temporary executable which will - // removed. - temp := fmt.Sprintf(filepath.Join("%s", "temp-%d.test"), os.TempDir(), time.Now().UnixNano()) - - // builds and compile the tested package. + // build and compile the tested package. // generated test executable will be removed // since we do not need it for godog suite. // we also print back the temp WORK directory // 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", os.DevNull).CombinedOutput() if err != nil { return fmt.Errorf("failed to compile tested package: %s, reason: %v, output: %s", pkg.Name, err, string(out)) } - defer os.Remove(temp) // extract go-build temporary directory as our workdir - workdir = strings.TrimSpace(string(out)) - if !strings.HasPrefix(workdir, "WORK=") { - return fmt.Errorf("expected WORK dir path, but got: %s", workdir) + lines := strings.Split(strings.TrimSpace(string(out)), "\n") + // it may have some compilation warnings, in the output, but these are not + // considered to be errors, since command exit status is 0 + for _, ln := range lines { + if !strings.HasPrefix(ln, "WORK=") { + continue + } + workdir = strings.Replace(ln, "WORK=", "", 1) + break + } + + // may not locate it in output + if workdir == testdir { + return fmt.Errorf("expected WORK dir path to be present in output: %s", string(out)) + } + + // check whether workdir exists + stats, err := os.Stat(workdir) + if os.IsNotExist(err) { + return fmt.Errorf("expected WORK dir: %s to be available", workdir) + } + + if !stats.IsDir() { + return fmt.Errorf("expected WORK dir: %s to be directory", workdir) } - workdir = strings.Replace(workdir, "WORK=", "", 1) testdir = filepath.Join(workdir, pkg.ImportPath, "_test") } else { // still need to create temporary workdir diff --git a/builder_go110.go b/builder_go110.go index 9635102..d8a8a7b 100644 --- a/builder_go110.go +++ b/builder_go110.go @@ -87,7 +87,7 @@ func Build(bin string) error { return fmt.Errorf("failed to compile package: %s, reason: %v, output: %s", pkg.Name, err, string(out)) } - // builds and compile the tested package. + // build and compile the tested package. // generated test executable will be removed // since we do not need it for godog suite. // we also print back the temp WORK directory @@ -98,11 +98,31 @@ func Build(bin string) error { } // extract go-build temporary directory as our workdir - workdir = strings.TrimSpace(string(out)) - if !strings.HasPrefix(workdir, "WORK=") { - return fmt.Errorf("expected WORK dir path, but got: %s", workdir) + lines := strings.Split(strings.TrimSpace(string(out)), "\n") + // it may have some compilation warnings, in the output, but these are not + // considered to be errors, since command exit status is 0 + for _, ln := range lines { + if !strings.HasPrefix(ln, "WORK=") { + continue + } + workdir = strings.Replace(ln, "WORK=", "", 1) + break + } + + // may not locate it in output + if workdir == testdir { + return fmt.Errorf("expected WORK dir path to be present in output: %s", string(out)) + } + + // check whether workdir exists + stats, err := os.Stat(workdir) + if os.IsNotExist(err) { + return fmt.Errorf("expected WORK dir: %s to be available", workdir) + } + + if !stats.IsDir() { + return fmt.Errorf("expected WORK dir: %s to be directory", workdir) } - workdir = strings.Replace(workdir, "WORK=", "", 1) testdir = filepath.Join(workdir, "b001") } else { // still need to create temporary workdir