update screenshots, allow godog to execute in empty directory
Этот коммит содержится в:
родитель
c7e1bf22bc
коммит
337a551087
6 изменённых файлов: 72 добавлений и 25 удалений
|
@ -107,7 +107,7 @@ Since we need a working implementation, we may start by implementing only what i
|
||||||
|
|
||||||
#### Step 3
|
#### Step 3
|
||||||
|
|
||||||
We only need a number of **godogs** for now. Let's define steps.
|
We only need a number of **godogs** for now. Lets keep it simple.
|
||||||
|
|
||||||
``` go
|
``` go
|
||||||
/* file: examples/godogs/godog.go */
|
/* file: examples/godogs/godog.go */
|
||||||
|
@ -121,7 +121,8 @@ func main() { /* usual main func */ }
|
||||||
|
|
||||||
#### Step 4
|
#### Step 4
|
||||||
|
|
||||||
Now let's finish our step implementations in order to test our feature requirements:
|
Now lets implement our step definitions, which we can copy from generated
|
||||||
|
console output snippets in order to test our feature requirements:
|
||||||
|
|
||||||
``` go
|
``` go
|
||||||
/* file: examples/godogs/godog_test.go */
|
/* file: examples/godogs/godog_test.go */
|
||||||
|
|
53
builder.go
53
builder.go
|
@ -57,35 +57,35 @@ func Build() (string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
pkg, err := build.ImportDir(abs, 0)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
bin := filepath.Join(pkg.Dir, "godog.test")
|
bin := filepath.Join(abs, "godog.test")
|
||||||
// suffix with .exe for windows
|
// suffix with .exe for windows
|
||||||
if goos == "windows" {
|
if goos == "windows" {
|
||||||
bin += ".exe"
|
bin += ".exe"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// we allow package to be nil, if godog is run only when
|
||||||
|
// there is a feature file in empty directory
|
||||||
|
pkg, _ := build.ImportDir(abs, 0)
|
||||||
src, anyContexts, err := buildTestMain(pkg)
|
src, anyContexts, err := buildTestMain(pkg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bin, err
|
return bin, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// first of all compile test package dependencies
|
|
||||||
// that will save was many compilations for dependencies
|
|
||||||
// go does it better
|
|
||||||
out, err := exec.Command("go", "test", "-i").CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
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())
|
workdir := fmt.Sprintf(filepath.Join("%s", "godog-%d"), os.TempDir(), time.Now().UnixNano())
|
||||||
testdir := workdir
|
testdir := workdir
|
||||||
|
|
||||||
// if none of test files exist, or there are no contexts found
|
// if none of test files exist, or there are no contexts found
|
||||||
// we will skip test package compilation, since it is useless
|
// we will skip test package compilation, since it is useless
|
||||||
if anyContexts {
|
if anyContexts {
|
||||||
|
// first of all compile test package dependencies
|
||||||
|
// that will save was many compilations for dependencies
|
||||||
|
// go does it better
|
||||||
|
out, err := exec.Command("go", "test", "-i").CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return bin, 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
|
||||||
// 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
|
||||||
|
@ -130,7 +130,7 @@ func Build() (string, error) {
|
||||||
// but we need it for our testmain package.
|
// but we need it for our testmain package.
|
||||||
// So we look it up in available source paths
|
// So we look it up in available source paths
|
||||||
// including vendor directory, supported since 1.5.
|
// including vendor directory, supported since 1.5.
|
||||||
try := []string{filepath.Join(pkg.Dir, "vendor", godogImportPath)}
|
try := []string{filepath.Join(abs, "vendor", godogImportPath)}
|
||||||
for _, d := range build.Default.SrcDirs() {
|
for _, d := range build.Default.SrcDirs() {
|
||||||
try = append(try, filepath.Join(d, godogImportPath))
|
try = append(try, filepath.Join(d, godogImportPath))
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ func Build() (string, error) {
|
||||||
// will be installed as dependency of godog
|
// will be installed as dependency of godog
|
||||||
cmd := exec.Command("go", "install", godogPkg.ImportPath)
|
cmd := exec.Command("go", "install", godogPkg.ImportPath)
|
||||||
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 bin, fmt.Errorf("failed to install godog package:\n%s", string(out))
|
||||||
}
|
}
|
||||||
|
@ -193,6 +193,7 @@ func Build() (string, error) {
|
||||||
args = append(args, testMainPkgOut)
|
args = append(args, testMainPkgOut)
|
||||||
cmd = exec.Command(linker, args...)
|
cmd = exec.Command(linker, args...)
|
||||||
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 link test executable:\n%s", string(out))
|
return bin, fmt.Errorf("failed to link test executable:\n%s", string(out))
|
||||||
|
@ -232,22 +233,28 @@ func uniqStringList(strs []string) (unique []string) {
|
||||||
// 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, bool, error) {
|
func buildTestMain(pkg *build.Package) ([]byte, bool, error) {
|
||||||
contexts, err := processPackageTestFiles(
|
var contexts []string
|
||||||
pkg.TestGoFiles,
|
var importPath string
|
||||||
pkg.XTestGoFiles,
|
if nil != pkg {
|
||||||
)
|
ctxs, err := processPackageTestFiles(
|
||||||
if err != nil {
|
pkg.TestGoFiles,
|
||||||
return nil, false, err
|
pkg.XTestGoFiles,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
contexts = ctxs
|
||||||
|
importPath = pkg.ImportPath
|
||||||
}
|
}
|
||||||
|
|
||||||
data := struct {
|
data := struct {
|
||||||
Name string
|
Name string
|
||||||
Contexts []string
|
Contexts []string
|
||||||
ImportPath string
|
ImportPath string
|
||||||
}{pkg.Name, contexts, pkg.ImportPath}
|
}{pkg.Name, contexts, importPath}
|
||||||
|
|
||||||
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, len(contexts) > 0, err
|
return nil, len(contexts) > 0, err
|
||||||
}
|
}
|
||||||
return buf.Bytes(), len(contexts) > 0, nil
|
return buf.Bytes(), len(contexts) > 0, nil
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
/* file: examples/godogs/godog.go */
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// Godogs to eat
|
// Godogs to eat
|
||||||
|
|
38
examples/godogs/godog_test.go
Обычный файл
38
examples/godogs/godog_test.go
Обычный файл
|
@ -0,0 +1,38 @@
|
||||||
|
/* file: examples/godogs/godog_test.go */
|
||||||
|
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
|
||||||
|
})
|
||||||
|
}
|
Двоичные данные
screenshots/passed.png
Двоичные данные
screenshots/passed.png
Двоичный файл не отображается.
До Ширина: | Высота: | Размер: 54 КиБ После Ширина: | Высота: | Размер: 80 КиБ |
Двоичные данные
screenshots/undefined.png
Двоичные данные
screenshots/undefined.png
Двоичный файл не отображается.
До Ширина: | Высота: | Размер: 109 КиБ После Ширина: | Высота: | Размер: 105 КиБ |
Загрузка…
Создание таблицы
Сослаться в новой задаче