Skip to content

Commit

Permalink
Fixed an issue when go test is parsing command-line flags (#359)
Browse files Browse the repository at this point in the history
* Fixed an issue when go test is parsing command-line flags

* Added some extra information for parsing flags to the README.md
  • Loading branch information
lonnblad authored Nov 26, 2020
1 parent 69162a0 commit 59cd5d8
Show file tree
Hide file tree
Showing 17 changed files with 837 additions and 45 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ You may integrate running **godog** in your **go test** command. You can run it
The following example binds **godog** flags with specified prefix `godog` in order to prevent flag collisions.

``` go
package main

import (
"flag" // godog v0.10.0 and earlier
"os"
"testing"

"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
flag "github.com/spf13/pflag" // godog v0.11.0-rc1 (latest)
)

var opts = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "progress", // can define default values
Expand Down
2 changes: 1 addition & 1 deletion _examples/api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module api

go 1.14

require github.com/cucumber/godog v0.10.0
require github.com/cucumber/godog v0.11.0-rc1
122 changes: 120 additions & 2 deletions _examples/api/go.sum

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion _examples/assert-godogs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module assert-godogs
go 1.14

require (
github.com/cucumber/godog v0.10.0
github.com/cucumber/godog v0.11.0-rc1
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.6.1
)
122 changes: 120 additions & 2 deletions _examples/assert-godogs/go.sum

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions _examples/assert-godogs/godogs_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package main

import (
"flag"
"fmt"
"os"
"testing"

"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)

var opts = godog.Options{Output: colors.Colored(os.Stdout)}

func init() {
godog.BindFlags("godog.", flag.CommandLine, &opts)
godog.BindCommandLineFlags("godog.", &opts)
}

func TestMain(m *testing.M) {
Expand All @@ -27,9 +27,6 @@ func TestMain(m *testing.M) {
Options: &opts,
}.Run()

if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}

Expand Down
2 changes: 1 addition & 1 deletion _examples/custom-formatter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module custom-formatter
go 1.14

require (
github.com/cucumber/godog v0.10.0
github.com/cucumber/godog v0.11.0-rc1
github.com/cucumber/messages-go/v10 v10.0.3
)
122 changes: 120 additions & 2 deletions _examples/custom-formatter/go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion _examples/db/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.14

require (
github.com/DATA-DOG/go-txdb v0.1.3
github.com/cucumber/godog v0.10.0
github.com/cucumber/godog v0.11.0-rc1
github.com/go-sql-driver/mysql v1.5.0
github.com/lib/pq v1.7.0 // indirect
)
122 changes: 120 additions & 2 deletions _examples/db/go.sum

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion _examples/godogs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module godogs

go 1.14

require github.com/cucumber/godog v0.10.0
require (
github.com/cucumber/godog v0.11.0-rc1
github.com/spf13/pflag v1.0.3
)
122 changes: 120 additions & 2 deletions _examples/godogs/go.sum

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions _examples/godogs/godogs_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package main

import (
"flag"
"fmt"
"os"
"testing"

"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
flag "github.com/spf13/pflag"
)

var opts = godog.Options{Output: colors.Colored(os.Stdout)}

func init() {
godog.BindFlags("godog.", flag.CommandLine, &opts)
godog.BindCommandLineFlags("godog.", &opts)
}

func TestMain(m *testing.M) {
Expand All @@ -27,9 +27,6 @@ func TestMain(m *testing.M) {
Options: &opts,
}.Run()

if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}

Expand Down
2 changes: 2 additions & 0 deletions flags_v0110.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package godog

import (
"errors"
"flag"
"math/rand"
"time"

Expand All @@ -28,4 +29,5 @@ func flagSet(opt *Options) *pflag.FlagSet {
func BindCommandLineFlags(prefix string, opts *Options) {
flagSet := pflag.CommandLine
flags.BindRunCmdFlags(prefix, flagSet, opts)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.13
require (
github.com/cucumber/gherkin-go/v11 v11.0.0
github.com/cucumber/messages-go/v10 v10.0.3
github.com/hashicorp/go-memdb v1.2.1
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.3
github.com/hashicorp/go-memdb v1.3.0
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.6.1
)
188 changes: 172 additions & 16 deletions go.sum

Large diffs are not rendered by default.

36 changes: 35 additions & 1 deletion release-notes/v0.11.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ godog is now able to write the report to a file.

**Note**, godog still only support the use of one formatter.

### Executing godog from the Command Line
godog is now using [Cobra](https://pkg.go.dev/github.com/spf13/cobra) to run godog from the command line. With this update, godog has received sub-commands: (build, help, run, version)

To run tests with godog, `godog [<feature>]` has been replaced with `godog run [<feature>]`.

To build a test binary, `godog --output g.test [<feature>]`has been replaced with `godog build --output g.test [<feature>]`.

### Upload artifacts to the github release
The releases on github now include prebuilt binaries for:
- Linux for amd64 and arm64
Expand Down Expand Up @@ -47,7 +54,34 @@ Deprecation Notices
### BindFlags
`BindFlags(prefix, flag.CommandLine, &opts)` has been replaced by `BindCommandLineFlags(prefix, &opts)` and will be removed in `v0.12.0`.

### Executing godog
Using `BindCommandLineFlags(prefix, &opts)` also requires you to use `"github.com/spf13/pflag"` to parse the flags.
```go
package main

import (
"fmt"
"os"
"testing"

"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
flag "github.com/spf13/pflag"
)

var opts = godog.Options{Output: colors.Colored(os.Stdout)}

func init() {
godog.BindCommandLineFlags("godog.", &opts)
}

func TestMain(m *testing.M) {
flag.Parse()
opts.Paths = flag.Args()

// ...
```
### Executing the godog CLI
godog has received sub-commands: (build, help, run, version)
To run tests with godog, `godog [<feature>]` has been replaced with `godog run [<feature>]`.
Expand Down

0 comments on commit 59cd5d8

Please sign in to comment.