Moved the builder code to a new internal pkg

Этот коммит содержится в:
Fredrik Lönnblad 2020-07-01 07:36:51 +02:00
родитель 4da375f4c0
коммит 99723f8220
10 изменённых файлов: 63 добавлений и 54 удалений

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@ -5,6 +5,7 @@ import (
"reflect" "reflect"
"regexp" "regexp"
"github.com/cucumber/godog/internal/builder"
"github.com/cucumber/messages-go/v10" "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) 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)
}