Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an internal pkg for the builder #327

Merged
merged 1 commit into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/godog/main.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// +build go1.12
// +build !go1.13

package godog_test
package builder_test

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build go1.13

package godog_test
package builder_test

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
}