Skip to content

Commit

Permalink
Fix issue#383 (#468)
Browse files Browse the repository at this point in the history
* Add example about incorrect project structure

This is to help reproduce #383

* Added some debugging statements

* Update go.sum for example project

* Made a cmd_run_test.go file in order to test and run the builderAndRunGodog function in order to see it fail

* added new assertion test

* Matt and I added debugging

Co-authored-by: Matt Wynne <matt@mattwynne.net>

* Matt and I tried to logging through the cobra command

by using cmd.OutOrStdout( )

* Improved some debugging

* Add a failing test for Builder that reproduces #383

* added new test for IncorrectProjectStructure #383

* Revert "Add a failing test for Builder that reproduces #383"

This reverts commit e5b2693.

* ignored vscode files

Co-authored-by: Matt Wynne <matt@cucumber.io>

* undid debugging changes

* undid debugging changes

* removed redundant test

* added check for incorrect project structure

we examined the output from running `go test` which tells us if we didn't
find any test files.
we tweaked the error message to follow the capitalization rules

Co-authored-by: Matt Wynne <matt@cucumber.io>

* Update internal/builder/builder_test.go

Co-authored-by: Matt Wynne <matt@mattwynne.net>
Co-authored-by: Matt Wynne <matt@cucumber.io>
Co-authored-by: Viacheslav Poturaev <nanopeni@gmail.com>
  • Loading branch information
4 people committed Apr 28, 2022
1 parent 237a855 commit 1a795f1
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,5 +6,6 @@ Gopkg.toml

.DS_Store
.idea
.vscode

_artifacts
6 changes: 6 additions & 0 deletions _examples/incorrect-project-structure/README.md
@@ -0,0 +1,6 @@
This example is to help reproduce issue [#383](https://github.com/cucumber/godog/issues/383)

To run the example:

cd _examples/incorrect-project-structure
go run ../../cmd/godog
7 changes: 7 additions & 0 deletions _examples/incorrect-project-structure/go.mod
@@ -0,0 +1,7 @@
module incorrect-project-structure

go 1.13

require github.com/cucumber/godog v0.12.0

replace github.com/cucumber/godog => ../../
315 changes: 315 additions & 0 deletions _examples/incorrect-project-structure/go.sum

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions _examples/incorrect-project-structure/main.go
@@ -0,0 +1,7 @@
package main

import "github.com/cucumber/godog"

func InitializeScenario(ctx *godog.ScenarioContext) {

}
5 changes: 5 additions & 0 deletions internal/builder/builder.go
Expand Up @@ -151,6 +151,10 @@ func Build(bin string) error {
break
}

if strings.Contains(string(testOutput), "[no test files]") {
return fmt.Errorf("incorrect project structure: no test files found")
}

// may not locate it in output
if workdir == testdir {
return fmt.Errorf("expected WORK dir path to be present in output: %s", string(testOutput))
Expand Down Expand Up @@ -182,6 +186,7 @@ func Build(bin string) error {
// we do not depend on CGO so a lot of checks are not necessary
linkerCfg := filepath.Join(testdir, "importcfg.link")
compilerCfg := linkerCfg

if vendored != nil {
data, err := ioutil.ReadFile(linkerCfg)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions internal/builder/builder_test.go
Expand Up @@ -25,6 +25,7 @@ func Test_GodogBuild(t *testing.T) {
t.Run("WithinGopath", testWithinGopath)
t.Run("WithVendoredGodogWithoutModule", testWithVendoredGodogWithoutModule)
t.Run("WithVendoredGodogAndMod", testWithVendoredGodogAndMod)
t.Run("WithIncorrectProjectStructure", testWithIncorrectProjectStructure)

t.Run("WithModule", func(t *testing.T) {
t.Run("OutsideGopathAndHavingOnlyFeature", testOutsideGopathAndHavingOnlyFeature)
Expand Down Expand Up @@ -135,6 +136,13 @@ func main() {
}
`

var emptyBuilderTestFile = `package godogs
import "github.com/cucumber/godog"
func InitializeScenario(ctx *godog.ScenarioContext) {}
`

var builderModFile = `module godogs`

func buildTestPackage(dir string, files map[string]string) error {
Expand Down Expand Up @@ -237,6 +245,38 @@ func testWithoutTestSourceNotInGoPath(t *testing.T) {
builderTC.run(t)
}

func testWithIncorrectProjectStructure(t *testing.T) {
dir := filepath.Join(os.TempDir(), t.Name(), "godogs")
files := map[string]string{
"godogs.go": emptyBuilderTestFile,
"go.mod": builderModFile,
}

err := buildTestPackage(dir, files)
defer os.RemoveAll(dir)
require.Nil(t, err)

prevDir, err := os.Getwd()
require.Nil(t, err)
err = os.Chdir(dir)
require.Nil(t, err)
defer os.Chdir(prevDir)

testBin, err := filepath.Abs(filepath.Join(dir, "godog.test"))
require.Nil(t, err)

if build.Default.GOOS == "windows" {
testBin += ".exe"
}

// call the builder - we should get an error
err = builder.Build(testBin)
// check that we even got an error at all
require.NotNil(t, err)
// now check the details of the error message
require.EqualError(t, err, "incorrect project structure: no test files found")
}

func testWithinGopath(t *testing.T) {
builderTC := builderTestCase{}

Expand Down

0 comments on commit 1a795f1

Please sign in to comment.