Merge pull request #327 from cucumber/internal-builder-pkg

Added an internal pkg for the builder
Этот коммит содержится в:
Fredrik Lönnblad 2020-07-02 08:21:59 +02:00 коммит произвёл GitHub
родитель 4da375f4c0 99723f8220
коммит 28ad994ad1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 63 добавлений и 54 удалений

Просмотреть файл

@ -10,6 +10,7 @@ import (
"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
"github.com/cucumber/godog/internal/builder"
)
var parsedStatus int
@ -24,7 +25,7 @@ func buildAndRun() (int, error) {
if build.Default.GOOS == "windows" {
bin += ".exe"
}
if err = godog.Build(bin); err != nil {
if err = builder.Build(bin); err != nil {
return 1, err
}
defer os.Remove(bin)
@ -79,7 +80,7 @@ func main() {
fmt.Fprintln(os.Stderr, "could not locate absolute path for:", output, err)
os.Exit(1)
}
if err = godog.Build(bin); err != nil {
if err = builder.Build(bin); err != nil {
fmt.Fprintln(os.Stderr, "could not build binary at:", output, err)
os.Exit(1)
}

Просмотреть файл

@ -1,4 +1,4 @@
package godog
package builder
import "go/ast"

Просмотреть файл

@ -1,4 +1,4 @@
package godog
package builder
import (
"go/parser"

Просмотреть файл

@ -1,4 +1,4 @@
package godog
package builder
import (
"bytes"
@ -30,13 +30,13 @@ var (
import (
"github.com/cucumber/godog"
{{if or .DeprecatedFeatureContexts .TestSuiteContexts .ScenarioContexts}}_test "{{.ImportPath}}"{{end}}
{{if or .XDeprecatedFeatureContexts .XTestSuiteContexts .XScenarioContexts}}_xtest "{{.ImportPath}}_test"{{end}}
{{if or .XDeprecatedFeatureContexts .XTestSuiteContexts .XScenarioContexts}}"testing/internal/testdeps"{{end}}
{{if or .TestSuiteContexts .ScenarioContexts}}_test "{{.ImportPath}}"{{end}}
{{if or .XTestSuiteContexts .XScenarioContexts}}_xtest "{{.ImportPath}}_test"{{end}}
{{if or .XTestSuiteContexts .XScenarioContexts}}"testing/internal/testdeps"{{end}}
"os"
)
{{if or .XDeprecatedFeatureContexts .XTestSuiteContexts .XScenarioContexts}}
{{if or .XTestSuiteContexts .XScenarioContexts}}
func init() {
testdeps.ImportPath = "{{.ImportPath}}"
}
@ -64,16 +64,6 @@ func main() {
{{end}}
},
}.Run()
{{else}}
status := godog.Run("{{ .Name }}", func (suite *godog.Suite) {
os.Setenv("GODOG_TESTED_PACKAGE", "{{.ImportPath}}")
{{range .DeprecatedFeatureContexts}}
_test.{{ . }}(suite)
{{end}}
{{range .XDeprecatedFeatureContexts}}
_xtest.{{ . }}(suite)
{{end}}
})
{{end}}
os.Exit(status)
}`))
@ -352,23 +342,19 @@ func buildTestMain(pkg *build.Package) ([]byte, error) {
name = "main"
}
data := struct {
Name string
ImportPath string
DeprecatedFeatureContexts []string
TestSuiteContexts []string
ScenarioContexts []string
XDeprecatedFeatureContexts []string
XTestSuiteContexts []string
XScenarioContexts []string
Name string
ImportPath string
TestSuiteContexts []string
ScenarioContexts []string
XTestSuiteContexts []string
XScenarioContexts []string
}{
Name: name,
ImportPath: importPath,
DeprecatedFeatureContexts: ctxs.deprecatedFeatureCtxs,
TestSuiteContexts: ctxs.testSuiteCtxs,
ScenarioContexts: ctxs.scenarioCtxs,
XDeprecatedFeatureContexts: xctxs.deprecatedFeatureCtxs,
XTestSuiteContexts: xctxs.testSuiteCtxs,
XScenarioContexts: xctxs.scenarioCtxs,
Name: name,
ImportPath: importPath,
TestSuiteContexts: ctxs.testSuiteCtxs,
ScenarioContexts: ctxs.scenarioCtxs,
XTestSuiteContexts: xctxs.testSuiteCtxs,
XScenarioContexts: xctxs.scenarioCtxs,
}
var buf bytes.Buffer
@ -452,7 +438,6 @@ func processPackageTestFiles(packs ...[]string) (ctxs contexts, _ error) {
return ctxs, err
}
ctxs.deprecatedFeatureCtxs = append(ctxs.deprecatedFeatureCtxs, astContexts(node, "Suite")...)
ctxs.testSuiteCtxs = append(ctxs.testSuiteCtxs, astContexts(node, "TestSuiteContext")...)
ctxs.scenarioCtxs = append(ctxs.scenarioCtxs, astContexts(node, "ScenarioContext")...)
}

Просмотреть файл

@ -1,7 +1,7 @@
// +build go1.12
// +build !go1.13
package godog_test
package builder_test
import (
"os"

Просмотреть файл

@ -1,6 +1,6 @@
// +build go1.13
package godog_test
package builder_test
import (
"os"

Просмотреть файл

@ -1,4 +1,4 @@
package godog_test
package builder_test
import (
"fmt"

Просмотреть файл

@ -1,4 +1,4 @@
package godog_test
package builder_test
import (
"bytes"
@ -10,10 +10,14 @@ import (
"strings"
"testing"
"github.com/cucumber/godog"
"github.com/stretchr/testify/require"
"github.com/cucumber/godog"
"github.com/cucumber/godog/internal/builder"
)
func InitializeScenario(ctx *godog.ScenarioContext) {}
func Test_GodogBuild(t *testing.T) {
t.Run("WithSourceNotInGoPath", testWithSourceNotInGoPath)
t.Run("WithoutSourceNotInGoPath", testWithoutSourceNotInGoPath)
@ -49,7 +53,6 @@ import (
"fmt"
"github.com/cucumber/godog"
messages "github.com/cucumber/messages-go/v10"
)
func thereAreGodogs(available int) error {
@ -72,12 +75,13 @@ func thereShouldBeRemaining(remaining int) error {
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(*messages.Pickle) {
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step("^there are (\\d+) godogs$", thereAreGodogs)
ctx.Step("^I eat (\\d+)$", iEat)
ctx.Step("^there should be (\\d+) remaining$", thereShouldBeRemaining)
ctx.BeforeScenario(func(*godog.Scenario) {
Godogs = 0 // clean the state before every scenario
})
}
@ -89,7 +93,6 @@ import (
"fmt"
"github.com/cucumber/godog"
messages "github.com/cucumber/messages-go/v10"
"godogs"
)
@ -114,12 +117,12 @@ func thereShouldBeRemaining(remaining int) error {
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)
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step("^there are (\\d+) godogs$", thereAreGodogs)
ctx.Step("^I eat (\\d+)$", iEat)
ctx.Step("^there should be (\\d+) remaining$", thereShouldBeRemaining)
s.BeforeScenario(func(*messages.Pickle) {
ctx.BeforeScenario(func(*godog.Scenario) {
godogs.Godogs = 0 // clean the state before every scenario
})
}
@ -159,7 +162,7 @@ func buildTestCommand(t *testing.T, wd, featureFile string) *exec.Cmd {
testBin += ".exe"
}
err = godog.Build(testBin)
err = builder.Build(testBin)
require.Nil(t, err)
featureFilePath := filepath.Join(wd, featureFile)

3
run.go
Просмотреть файл

@ -2,6 +2,7 @@ package godog
import (
"fmt"
"go/build"
"io"
"math/rand"
"os"
@ -217,6 +218,8 @@ func runWithOptions(suiteName string, runner runner, opt Options) int {
func runsFromPackage(fp string) string {
dir := filepath.Dir(fp)
gopaths := filepath.SplitList(build.Default.GOPATH)
for _, gp := range gopaths {
gp = filepath.Join(gp, "src")
if strings.Index(dir, gp) == 0 {

Просмотреть файл

@ -5,6 +5,7 @@ import (
"reflect"
"regexp"
"github.com/cucumber/godog/internal/builder"
"github.com/cucumber/messages-go/v10"
)
@ -173,3 +174,19 @@ func (ctx *ScenarioContext) Step(expr, stepFunc interface{}) {
ctx.suite.steps = append(ctx.suite.steps, def)
}
// Build creates a test package like go test command at given target path.
// If there are no go files in tested directory, then
// it simply builds a godog executable to scan features.
//
// If there are go test files, it first builds a test
// package with standard go test command.
//
// Finally it generates godog suite executable which
// registers exported godog contexts from the test files
// of tested package.
//
// Returns the path to generated executable
func Build(bin string) error {
return builder.Build(bin)
}