Skip to content

Commit

Permalink
more explicit workdir check for builder, closes #139
Browse files Browse the repository at this point in the history
  • Loading branch information
l3pp4rd committed Oct 8, 2018
1 parent ea9eeaa commit 5ed3399
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
40 changes: 26 additions & 14 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 25 additions & 5 deletions builder_go110.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 5ed3399

Please sign in to comment.