case when there are no test files or contexts
Этот коммит содержится в:
родитель
4a62479892
коммит
c7e1bf22bc
3 изменённых файлов: 55 добавлений и 65 удалений
40
builder.go
40
builder.go
|
@ -67,7 +67,7 @@ func Build() (string, error) {
|
||||||
if goos == "windows" {
|
if goos == "windows" {
|
||||||
bin += ".exe"
|
bin += ".exe"
|
||||||
}
|
}
|
||||||
src, err := buildTestMain(pkg)
|
src, anyContexts, err := buildTestMain(pkg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bin, err
|
return bin, err
|
||||||
}
|
}
|
||||||
|
@ -80,6 +80,12 @@ func Build() (string, error) {
|
||||||
return bin, fmt.Errorf("failed to compile package %s:\n%s", pkg.Name, string(out))
|
return bin, fmt.Errorf("failed to compile package %s:\n%s", pkg.Name, string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
workdir := fmt.Sprintf(filepath.Join("%s", "godog-%d"), os.TempDir(), time.Now().UnixNano())
|
||||||
|
testdir := workdir
|
||||||
|
|
||||||
|
// if none of test files exist, or there are no contexts found
|
||||||
|
// we will skip test package compilation, since it is useless
|
||||||
|
if anyContexts {
|
||||||
// let go do the dirty work and compile test
|
// let go do the dirty work and compile test
|
||||||
// package with it's dependencies. Older go
|
// package with it's dependencies. Older go
|
||||||
// versions does not accept existing file output
|
// versions does not accept existing file output
|
||||||
|
@ -99,12 +105,18 @@ func Build() (string, error) {
|
||||||
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 bin, 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 {
|
||||||
|
// still need to create temporary workdir
|
||||||
|
if err = os.MkdirAll(testdir, 0755); err != nil {
|
||||||
|
return bin, err
|
||||||
|
}
|
||||||
|
}
|
||||||
defer os.RemoveAll(workdir)
|
defer os.RemoveAll(workdir)
|
||||||
|
|
||||||
// replace _testmain.go file with our own
|
// replace _testmain.go file with our own
|
||||||
|
@ -138,10 +150,11 @@ func Build() (string, error) {
|
||||||
|
|
||||||
// collect all possible package dirs, will be
|
// collect all possible package dirs, will be
|
||||||
// used for includes and linker
|
// used for includes and linker
|
||||||
pkgDirs := []string{testdir, workdir}
|
pkgDirs := []string{workdir, testdir}
|
||||||
for _, gopath := range gopaths {
|
for _, gopath := range gopaths {
|
||||||
pkgDirs = append(pkgDirs, filepath.Join(gopath, "pkg", goos+"_"+goarch))
|
pkgDirs = append(pkgDirs, filepath.Join(gopath, "pkg", goos+"_"+goarch))
|
||||||
}
|
}
|
||||||
|
pkgDirs = uniqStringList(pkgDirs)
|
||||||
|
|
||||||
// compile godog testmain package archive
|
// compile godog testmain package archive
|
||||||
// we do not depend on CGO so a lot of checks are not necessary
|
// we do not depend on CGO so a lot of checks are not necessary
|
||||||
|
@ -203,17 +216,28 @@ func locatePackage(try []string) (*build.Package, error) {
|
||||||
return nil, fmt.Errorf("failed to find godog package in any of:\n%s", strings.Join(try, "\n"))
|
return nil, fmt.Errorf("failed to find godog package in any of:\n%s", strings.Join(try, "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func uniqStringList(strs []string) (unique []string) {
|
||||||
|
uniq := make(map[string]void, len(strs))
|
||||||
|
for _, s := range strs {
|
||||||
|
if _, ok := uniq[s]; !ok {
|
||||||
|
uniq[s] = void{}
|
||||||
|
unique = append(unique, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// buildTestPackage clones a package and adds a godog
|
// buildTestPackage clones a package and adds a godog
|
||||||
// entry point with TestMain func in order to
|
// entry point with TestMain func in order to
|
||||||
// run the test suite. If TestMain func is found in tested
|
// run the test suite. If TestMain func is found in tested
|
||||||
// source, it will be removed so it can be replaced
|
// source, it will be removed so it can be replaced
|
||||||
func buildTestMain(pkg *build.Package) ([]byte, error) {
|
func buildTestMain(pkg *build.Package) ([]byte, bool, error) {
|
||||||
contexts, err := processPackageTestFiles(
|
contexts, err := processPackageTestFiles(
|
||||||
pkg.TestGoFiles,
|
pkg.TestGoFiles,
|
||||||
pkg.XTestGoFiles,
|
pkg.XTestGoFiles,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
data := struct {
|
data := struct {
|
||||||
|
@ -224,9 +248,9 @@ func buildTestMain(pkg *build.Package) ([]byte, error) {
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if err = runnerTemplate.Execute(&buf, data); err != nil {
|
if err = runnerTemplate.Execute(&buf, data); err != nil {
|
||||||
return nil, err
|
return nil, len(contexts) > 0, err
|
||||||
}
|
}
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), len(contexts) > 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// processPackageTestFiles runs through ast of each test
|
// processPackageTestFiles runs through ast of each test
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/DATA-DOG/godog"
|
|
||||||
)
|
|
||||||
|
|
||||||
func thereAreGodogs(available int) error {
|
|
||||||
Godogs = available
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func iEat(num int) error {
|
|
||||||
if Godogs < num {
|
|
||||||
return fmt.Errorf("you cannot eat %d godogs, there are %d available", num, Godogs)
|
|
||||||
}
|
|
||||||
Godogs -= num
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func thereShouldBeRemaining(remaining int) error {
|
|
||||||
if Godogs != remaining {
|
|
||||||
return fmt.Errorf("expected %d godogs to be remaining, but there is %d", remaining, Godogs)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func FeatureContext(s *godog.Suite) {
|
|
||||||
s.Step(`^there are (\d+) godogs$`, thereAreGodogs)
|
|
||||||
s.Step(`^I eat (\d+)$`, iEat)
|
|
||||||
s.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
|
|
||||||
|
|
||||||
s.BeforeScenario(func(interface{}) {
|
|
||||||
Godogs = 0 // clean the state before every scenario
|
|
||||||
})
|
|
||||||
}
|
|
3
utils.go
3
utils.go
|
@ -6,6 +6,9 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// empty struct value takes no space allocation
|
||||||
|
type void struct{}
|
||||||
|
|
||||||
// a color code type
|
// a color code type
|
||||||
type color int
|
type color int
|
||||||
|
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче