Skip to content

Commit 59cd5d8

Browse files
authoredNov 26, 2020
Fixed an issue when go test is parsing command-line flags (cucumber#359)
* Fixed an issue when go test is parsing command-line flags * Added some extra information for parsing flags to the README.md
1 parent 69162a0 commit 59cd5d8

File tree

17 files changed

+837
-45
lines changed

17 files changed

+837
-45
lines changed
 

‎README.md

+12
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,18 @@ You may integrate running **godog** in your **go test** command. You can run it
341341
The following example binds **godog** flags with specified prefix `godog` in order to prevent flag collisions.
342342

343343
``` go
344+
package main
345+
346+
import (
347+
"flag" // godog v0.10.0 and earlier
348+
"os"
349+
"testing"
350+
351+
"github.com/cucumber/godog"
352+
"github.com/cucumber/godog/colors"
353+
flag "github.com/spf13/pflag" // godog v0.11.0-rc1 (latest)
354+
)
355+
344356
var opts = godog.Options{
345357
Output: colors.Colored(os.Stdout),
346358
Format: "progress", // can define default values

‎_examples/api/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module api
22

33
go 1.14
44

5-
require github.com/cucumber/godog v0.10.0
5+
require github.com/cucumber/godog v0.11.0-rc1

‎_examples/api/go.sum

+120-2
Large diffs are not rendered by default.

‎_examples/assert-godogs/go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module assert-godogs
33
go 1.14
44

55
require (
6-
github.com/cucumber/godog v0.10.0
6+
github.com/cucumber/godog v0.11.0-rc1
7+
github.com/spf13/pflag v1.0.3
78
github.com/stretchr/testify v1.6.1
89
)

‎_examples/assert-godogs/go.sum

+120-2
Large diffs are not rendered by default.

‎_examples/assert-godogs/godogs_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package main
22

33
import (
4-
"flag"
54
"fmt"
65
"os"
76
"testing"
87

98
"github.com/cucumber/godog"
109
"github.com/cucumber/godog/colors"
10+
flag "github.com/spf13/pflag"
1111
"github.com/stretchr/testify/assert"
1212
)
1313

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

1616
func init() {
17-
godog.BindFlags("godog.", flag.CommandLine, &opts)
17+
godog.BindCommandLineFlags("godog.", &opts)
1818
}
1919

2020
func TestMain(m *testing.M) {
@@ -27,9 +27,6 @@ func TestMain(m *testing.M) {
2727
Options: &opts,
2828
}.Run()
2929

30-
if st := m.Run(); st > status {
31-
status = st
32-
}
3330
os.Exit(status)
3431
}
3532

‎_examples/custom-formatter/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module custom-formatter
33
go 1.14
44

55
require (
6-
github.com/cucumber/godog v0.10.0
6+
github.com/cucumber/godog v0.11.0-rc1
77
github.com/cucumber/messages-go/v10 v10.0.3
88
)

‎_examples/custom-formatter/go.sum

+120-2
Large diffs are not rendered by default.

‎_examples/db/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.14
44

55
require (
66
github.com/DATA-DOG/go-txdb v0.1.3
7-
github.com/cucumber/godog v0.10.0
7+
github.com/cucumber/godog v0.11.0-rc1
88
github.com/go-sql-driver/mysql v1.5.0
99
github.com/lib/pq v1.7.0 // indirect
1010
)

‎_examples/db/go.sum

+120-2
Large diffs are not rendered by default.

‎_examples/godogs/go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module godogs
22

33
go 1.14
44

5-
require github.com/cucumber/godog v0.10.0
5+
require (
6+
github.com/cucumber/godog v0.11.0-rc1
7+
github.com/spf13/pflag v1.0.3
8+
)

‎_examples/godogs/go.sum

+120-2
Large diffs are not rendered by default.

‎_examples/godogs/godogs_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package main
22

33
import (
4-
"flag"
54
"fmt"
65
"os"
76
"testing"
87

98
"github.com/cucumber/godog"
109
"github.com/cucumber/godog/colors"
10+
flag "github.com/spf13/pflag"
1111
)
1212

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

1515
func init() {
16-
godog.BindFlags("godog.", flag.CommandLine, &opts)
16+
godog.BindCommandLineFlags("godog.", &opts)
1717
}
1818

1919
func TestMain(m *testing.M) {
@@ -27,9 +27,6 @@ func TestMain(m *testing.M) {
2727
Options: &opts,
2828
}.Run()
2929

30-
if st := m.Run(); st > status {
31-
status = st
32-
}
3330
os.Exit(status)
3431
}
3532

‎flags_v0110.go

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package godog
22

33
import (
44
"errors"
5+
"flag"
56
"math/rand"
67
"time"
78

@@ -28,4 +29,5 @@ func flagSet(opt *Options) *pflag.FlagSet {
2829
func BindCommandLineFlags(prefix string, opts *Options) {
2930
flagSet := pflag.CommandLine
3031
flags.BindRunCmdFlags(prefix, flagSet, opts)
32+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
3133
}

‎go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ go 1.13
55
require (
66
github.com/cucumber/gherkin-go/v11 v11.0.0
77
github.com/cucumber/messages-go/v10 v10.0.3
8-
github.com/hashicorp/go-memdb v1.2.1
9-
github.com/spf13/cobra v1.0.0
10-
github.com/spf13/pflag v1.0.3
8+
github.com/hashicorp/go-memdb v1.3.0
9+
github.com/spf13/cobra v1.1.1
10+
github.com/spf13/pflag v1.0.5
1111
github.com/stretchr/testify v1.6.1
1212
)

‎go.sum

+172-16
Large diffs are not rendered by default.

‎release-notes/v0.11.0.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ godog is now able to write the report to a file.
1616

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

19+
### Executing godog from the Command Line
20+
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)
21+
22+
To run tests with godog, `godog [<feature>]` has been replaced with `godog run [<feature>]`.
23+
24+
To build a test binary, `godog --output g.test [<feature>]`has been replaced with `godog build --output g.test [<feature>]`.
25+
1926
### Upload artifacts to the github release
2027
The releases on github now include prebuilt binaries for:
2128
- Linux for amd64 and arm64
@@ -47,7 +54,34 @@ Deprecation Notices
4754
### BindFlags
4855
`BindFlags(prefix, flag.CommandLine, &opts)` has been replaced by `BindCommandLineFlags(prefix, &opts)` and will be removed in `v0.12.0`.
4956

50-
### Executing godog
57+
Using `BindCommandLineFlags(prefix, &opts)` also requires you to use `"github.com/spf13/pflag"` to parse the flags.
58+
```go
59+
package main
60+
61+
import (
62+
"fmt"
63+
"os"
64+
"testing"
65+
66+
"github.com/cucumber/godog"
67+
"github.com/cucumber/godog/colors"
68+
flag "github.com/spf13/pflag"
69+
)
70+
71+
var opts = godog.Options{Output: colors.Colored(os.Stdout)}
72+
73+
func init() {
74+
godog.BindCommandLineFlags("godog.", &opts)
75+
}
76+
77+
func TestMain(m *testing.M) {
78+
flag.Parse()
79+
opts.Paths = flag.Args()
80+
81+
// ...
82+
```
83+
84+
### Executing the godog CLI
5185
godog has received sub-commands: (build, help, run, version)
5286
5387
To run tests with godog, `godog [<feature>]` has been replaced with `godog run [<feature>]`.

0 commit comments

Comments
 (0)
Please sign in to comment.