Skip to content

Commit

Permalink
Merge pull request #327 from cucumber/internal-builder-pkg
Browse files Browse the repository at this point in the history
Added an internal pkg for the builder
  • Loading branch information
lonnblad committed Jul 2, 2020
2 parents 4da375f + 99723f8 commit 28ad994
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 54 deletions.
5 changes: 3 additions & 2 deletions cmd/godog/main.go
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
"github.com/cucumber/godog/internal/builder"
)

var parsedStatus int
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion ast.go → internal/builder/ast.go
@@ -1,4 +1,4 @@
package godog
package builder

import "go/ast"

Expand Down
2 changes: 1 addition & 1 deletion ast_test.go → internal/builder/ast_test.go
@@ -1,4 +1,4 @@
package godog
package builder

import (
"go/parser"
Expand Down
49 changes: 17 additions & 32 deletions builder.go → internal/builder/builder.go
@@ -1,4 +1,4 @@
package godog
package builder

import (
"bytes"
Expand Down Expand Up @@ -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}}"
}
Expand Down Expand Up @@ -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)
}`))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")...)
}
Expand Down
@@ -1,7 +1,7 @@
// +build go1.12
// +build !go1.13

package godog_test
package builder_test

import (
"os"
Expand Down
@@ -1,6 +1,6 @@
// +build go1.13

package godog_test
package builder_test

import (
"os"
Expand Down
@@ -1,4 +1,4 @@
package godog_test
package builder_test

import (
"fmt"
Expand Down
33 changes: 18 additions & 15 deletions builder_test.go → internal/builder/builder_test.go
@@ -1,4 +1,4 @@
package godog_test
package builder_test

import (
"bytes"
Expand All @@ -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)
Expand Down Expand Up @@ -49,7 +53,6 @@ import (
"fmt"
"github.com/cucumber/godog"
messages "github.com/cucumber/messages-go/v10"
)
func thereAreGodogs(available int) error {
Expand All @@ -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
})
}
Expand All @@ -89,7 +93,6 @@ import (
"fmt"
"github.com/cucumber/godog"
messages "github.com/cucumber/messages-go/v10"
"godogs"
)
Expand All @@ -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
})
}
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions run.go
Expand Up @@ -2,6 +2,7 @@ package godog

import (
"fmt"
"go/build"
"io"
"math/rand"
"os"
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions test_context.go
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"regexp"

"github.com/cucumber/godog/internal/builder"
"github.com/cucumber/messages-go/v10"
)

Expand Down Expand Up @@ -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)
}

0 comments on commit 28ad994

Please sign in to comment.