From 99723f8220bb2d8888916b7440469522ce6848af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20L=C3=B6nnblad?= Date: Wed, 1 Jul 2020 07:36:51 +0200 Subject: [PATCH] Moved the builder code to a new internal pkg --- cmd/godog/main.go | 5 +- ast.go => internal/builder/ast.go | 2 +- ast_test.go => internal/builder/ast_test.go | 2 +- builder.go => internal/builder/builder.go | 49 +++++++------------ .../builder/builder_go112_test.go | 2 +- .../builder/builder_go113_test.go | 2 +- .../builder/builder_go_module_test.go | 2 +- .../builder/builder_test.go | 33 +++++++------ run.go | 3 ++ test_context.go | 17 +++++++ 10 files changed, 63 insertions(+), 54 deletions(-) rename ast.go => internal/builder/ast.go (97%) rename ast_test.go => internal/builder/ast_test.go (99%) rename builder.go => internal/builder/builder.go (88%) rename builder_go112_test.go => internal/builder/builder_go112_test.go (97%) rename builder_go113_test.go => internal/builder/builder_go113_test.go (97%) rename builder_go_module_test.go => internal/builder/builder_go_module_test.go (99%) rename builder_test.go => internal/builder/builder_test.go (91%) diff --git a/cmd/godog/main.go b/cmd/godog/main.go index f5fce7c9..6fddd362 100644 --- a/cmd/godog/main.go +++ b/cmd/godog/main.go @@ -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) } diff --git a/ast.go b/internal/builder/ast.go similarity index 97% rename from ast.go rename to internal/builder/ast.go index 6dba3d08..c4f82407 100644 --- a/ast.go +++ b/internal/builder/ast.go @@ -1,4 +1,4 @@ -package godog +package builder import "go/ast" diff --git a/ast_test.go b/internal/builder/ast_test.go similarity index 99% rename from ast_test.go rename to internal/builder/ast_test.go index d1454af7..a1a20332 100644 --- a/ast_test.go +++ b/internal/builder/ast_test.go @@ -1,4 +1,4 @@ -package godog +package builder import ( "go/parser" diff --git a/builder.go b/internal/builder/builder.go similarity index 88% rename from builder.go rename to internal/builder/builder.go index 09a2ae7a..46d5233f 100644 --- a/builder.go +++ b/internal/builder/builder.go @@ -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")...) } diff --git a/builder_go112_test.go b/internal/builder/builder_go112_test.go similarity index 97% rename from builder_go112_test.go rename to internal/builder/builder_go112_test.go index 37275f4d..15ca6cba 100644 --- a/builder_go112_test.go +++ b/internal/builder/builder_go112_test.go @@ -1,7 +1,7 @@ // +build go1.12 // +build !go1.13 -package godog_test +package builder_test import ( "os" diff --git a/builder_go113_test.go b/internal/builder/builder_go113_test.go similarity index 97% rename from builder_go113_test.go rename to internal/builder/builder_go113_test.go index bdb5708d..bd2dd24e 100644 --- a/builder_go113_test.go +++ b/internal/builder/builder_go113_test.go @@ -1,6 +1,6 @@ // +build go1.13 -package godog_test +package builder_test import ( "os" diff --git a/builder_go_module_test.go b/internal/builder/builder_go_module_test.go similarity index 99% rename from builder_go_module_test.go rename to internal/builder/builder_go_module_test.go index 298538dc..bce33a44 100644 --- a/builder_go_module_test.go +++ b/internal/builder/builder_go_module_test.go @@ -1,4 +1,4 @@ -package godog_test +package builder_test import ( "fmt" diff --git a/builder_test.go b/internal/builder/builder_test.go similarity index 91% rename from builder_test.go rename to internal/builder/builder_test.go index c37dfb7d..ce2a73de 100644 --- a/builder_test.go +++ b/internal/builder/builder_test.go @@ -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) diff --git a/run.go b/run.go index 2d95a84e..adf4a344 100644 --- a/run.go +++ b/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 { diff --git a/test_context.go b/test_context.go index d67a31ed..c209d1b5 100644 --- a/test_context.go +++ b/test_context.go @@ -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) +}