Skip to content

Commit c35ea0b

Browse files
authored
Remove deprecation from flags, update example to use CLI flags (#498)
* Remove deprecation from flags, update example to use CLI flags * Add comment to ShowHelp option * Fix test * Update CHANGELOG.md
1 parent 2028828 commit c35ea0b

File tree

9 files changed

+102
-33
lines changed

9 files changed

+102
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
1010

1111
### Changed
1212
- README example is updated with `context.Context` and `go test` usage. ([477](https://github.com/cucumber/godog/pull/477) - [vearutop](https://github.com/vearutop))
13+
- Removed deprecation of `godog.BindFlags` ([498](https://github.com/cucumber/godog/pull/498) - [vearutop](https://github.com/vearutop))
1314

1415
### Fixed
1516
- Fixed a bug which would ignore the context returned from a substep.([488](https://github.com/cucumber/godog/pull/488) - [wichert](https://github.com/wichert))

_examples/godogs/features/godogs.feature

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# file: $GOPATH/godogs/features/godogs.feature
21
Feature: eat godogs
32
In order to be happy
43
As a hungry gopher
@@ -8,3 +7,8 @@ Feature: eat godogs
87
Given there are 12 godogs
98
When I eat 5
109
Then there should be 7 remaining
10+
11+
Scenario: Eat 12 out of 12
12+
Given there are 12 godogs
13+
When I eat 12
14+
Then there should be none remaining
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Feature: do not eat godogs
2+
In order to be fit
3+
As a well-fed gopher
4+
I need to be able to avoid godogs
5+
6+
Scenario: Eat 0 out of 12
7+
Given there are 12 godogs
8+
When I eat 0
9+
Then there should be 12 remaining
10+
11+
Scenario: Eat 0 out of 0
12+
Given there are 0 godogs
13+
When I eat 0
14+
Then there should be none remaining

_examples/godogs/godogs.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
package main
1+
package godogs
22

3-
// Godogs available to eat
3+
// Godogs available to eat.
44
var Godogs int
5-
6-
func main() { /* usual main func */ }

_examples/godogs/godogs_test.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
1-
package main
1+
package godogs
2+
3+
// This example shows how to set up test suite runner with Go subtests and godog command line parameters.
4+
// Sample commands:
5+
// * run all scenarios from default directory (features): go test -test.run "^TestFeatures/"
6+
// * run all scenarios and list subtest names: go test -test.v -test.run "^TestFeatures/"
7+
// * run all scenarios from one feature file: go test -test.v -godog.paths features/nodogs.feature -test.run "^TestFeatures/"
8+
// * run all scenarios from multiple feature files: go test -test.v -godog.paths features/nodogs.feature,features/godogs.feature -test.run "^TestFeatures/"
9+
// * run single scenario as a subtest: go test -test.v -test.run "^TestFeatures/Eat_5_out_of_12$"
10+
// * show usage help: go test -godog.help
11+
// * show usage help if there were other test files in directory: go test -godog.help godogs_test.go
12+
// * run scenarios with multiple formatters: go test -test.v -godog.format cucumber:cuc.json,pretty -test.run "^TestFeatures/"
213

314
import (
415
"context"
16+
"flag"
517
"fmt"
618
"os"
719
"testing"
820

921
"github.com/cucumber/godog"
1022
"github.com/cucumber/godog/colors"
11-
"github.com/spf13/pflag"
1223
)
1324

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

1627
func init() {
17-
godog.BindCommandLineFlags("godog.", &opts)
28+
godog.BindFlags("godog.", flag.CommandLine, &opts)
1829
}
1930

20-
func TestMain(m *testing.M) {
21-
pflag.Parse()
22-
opts.Paths = pflag.Args()
31+
func TestFeatures(t *testing.T) {
32+
o := opts
33+
o.TestingT = t
2334

2435
status := godog.TestSuite{
2536
Name: "godogs",
37+
Options: &o,
2638
TestSuiteInitializer: InitializeTestSuite,
2739
ScenarioInitializer: InitializeScenario,
28-
Options: &opts,
2940
}.Run()
3041

31-
os.Exit(status)
42+
if status == 2 {
43+
t.SkipNow()
44+
}
45+
46+
if status != 0 {
47+
t.Fatalf("zero status code expected, %d received", status)
48+
}
3249
}
3350

3451
func thereAreGodogs(available int) error {
@@ -51,6 +68,10 @@ func thereShouldBeRemaining(remaining int) error {
5168
return nil
5269
}
5370

71+
func thereShouldBeNoneRemaining() error {
72+
return thereShouldBeRemaining(0)
73+
}
74+
5475
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
5576
ctx.BeforeSuite(func() { Godogs = 0 })
5677
}
@@ -64,4 +85,5 @@ func InitializeScenario(ctx *godog.ScenarioContext) {
6485
ctx.Step(`^there are (\d+) godogs$`, thereAreGodogs)
6586
ctx.Step(`^I eat (\d+)$`, iEat)
6687
ctx.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
88+
ctx.Step(`^there should be none remaining$`, thereShouldBeNoneRemaining)
6789
}

flags_deprecated.go renamed to flags.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"flag"
55
"fmt"
66
"io"
7+
"sort"
78
"strconv"
89
"strings"
910

@@ -18,7 +19,8 @@ var descFeaturesArgument = "Optional feature(s) to run. Can be:\n" +
1819
s(4) + "- dir " + colors.Yellow("(features/)") + "\n" +
1920
s(4) + "- feature " + colors.Yellow("(*.feature)") + "\n" +
2021
s(4) + "- scenario at specific line " + colors.Yellow("(*.feature:10)") + "\n" +
21-
"If no feature paths are listed, suite tries " + colors.Yellow("features") + " path by default.\n"
22+
"If no feature paths are listed, suite tries " + colors.Yellow("features") + " path by default.\n" +
23+
"Multiple comma-separated values can be provided.\n"
2224

2325
var descConcurrencyOption = "Run the test suite with concurrency level:\n" +
2426
s(4) + "- " + colors.Yellow(`= 1`) + ": supports all types of formats.\n" +
@@ -48,15 +50,30 @@ func FlagSet(opt *Options) *flag.FlagSet {
4850

4951
// BindFlags binds godog flags to given flag set prefixed
5052
// by given prefix, without overriding usage
51-
//
52-
// Deprecated: Use BindCommandLineFlags(prefix, &opts)
53-
// instead of BindFlags(prefix, flag.CommandLine, &opts)
5453
func BindFlags(prefix string, set *flag.FlagSet, opt *Options) {
54+
set.Usage = usage(set, set.Output())
55+
5556
descFormatOption := "How to format tests output. Built-in formats:\n"
56-
// @TODO: sort by name
57+
58+
type fm struct {
59+
name string
60+
desc string
61+
}
62+
var fms []fm
5763
for name, desc := range AvailableFormatters() {
58-
descFormatOption += s(4) + "- " + colors.Yellow(name) + ": " + desc + "\n"
64+
fms = append(fms, fm{
65+
name: name,
66+
desc: desc,
67+
})
5968
}
69+
sort.Slice(fms, func(i, j int) bool {
70+
return fms[i].name < fms[j].name
71+
})
72+
73+
for _, fm := range fms {
74+
descFormatOption += s(4) + "- " + colors.Yellow(fm.name) + ": " + fm.desc + "\n"
75+
}
76+
6077
descFormatOption = strings.TrimSpace(descFormatOption)
6178

6279
// override flag defaults if any corresponding properties were supplied on the incoming `opt`
@@ -107,6 +124,14 @@ func BindFlags(prefix string, set *flag.FlagSet, opt *Options) {
107124
set.BoolVar(&opt.Strict, prefix+"strict", defStrict, "Fail suite when there are pending or undefined steps.")
108125
set.BoolVar(&opt.NoColors, prefix+"no-colors", defNoColors, "Disable ansi colors.")
109126
set.Var(&randomSeed{&opt.Randomize}, prefix+"random", descRandomOption)
127+
set.BoolVar(&opt.ShowHelp, "godog.help", false, "Show usage help.")
128+
set.Func(prefix+"paths", descFeaturesArgument, func(paths string) error {
129+
if paths != "" {
130+
opt.Paths = strings.Split(paths, ",")
131+
}
132+
133+
return nil
134+
})
110135
}
111136

112137
type flagged struct {
@@ -183,15 +208,7 @@ func usage(set *flag.FlagSet, w io.Writer) func() {
183208

184209
// --- GENERAL ---
185210
fmt.Fprintln(w, colors.Yellow("Usage:"))
186-
fmt.Fprintf(w, s(2)+"godog [options] [<features>]\n\n")
187-
// description
188-
fmt.Fprintln(w, "Builds a test package and runs given feature files.")
189-
fmt.Fprintf(w, "Command should be run from the directory of tested package and contain buildable go source.\n\n")
190-
191-
// --- ARGUMENTS ---
192-
fmt.Fprintln(w, colors.Yellow("Arguments:"))
193-
// --> features
194-
fmt.Fprintln(w, opt("features", descFeaturesArgument))
211+
fmt.Fprintf(w, s(2)+"go test [options]\n\n")
195212

196213
// --- OPTIONS ---
197214
fmt.Fprintln(w, colors.Yellow("Options:"))

flags_deprecated_test.go renamed to flags_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ func TestBindFlagsShouldRespectOptDefaults(t *testing.T) {
121121
Randomize: int64(7),
122122
}
123123

124-
BindFlags("optDefaults.", flag.CommandLine, &opts)
124+
flagSet := flag.FlagSet{}
125+
126+
BindFlags("optDefaults.", &flagSet, &opts)
125127

126128
if opts.Format != "progress" {
127129
t.Fatalf("expected Format: progress, but it was: %s", opts.Format)

internal/flags/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ type Options struct {
6969
// where the contents of each feature is stored as a byte slice
7070
// in a map entry
7171
FeatureContents []Feature
72+
73+
// ShowHelp enables suite to show CLI flags usage help and exit.
74+
ShowHelp bool
7275
}
7376

7477
type Feature struct {

run.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package godog
22

33
import (
44
"context"
5+
"flag"
56
"fmt"
67
"go/build"
78
"io"
@@ -309,10 +310,11 @@ type TestSuite struct {
309310
// all configuration options from flags.
310311
//
311312
// The exit codes may vary from:
312-
// 0 - success
313-
// 1 - failed
314-
// 2 - command line usage error
315-
// 128 - or higher, os signal related error exit codes
313+
//
314+
// 0 - success
315+
// 1 - failed
316+
// 2 - command line usage error
317+
// 128 - or higher, os signal related error exit codes
316318
//
317319
// If there are flag related errors they will be directed to os.Stderr
318320
func (ts TestSuite) Run() int {
@@ -323,6 +325,12 @@ func (ts TestSuite) Run() int {
323325
return exitOptionError
324326
}
325327
}
328+
if ts.Options.ShowHelp {
329+
flag.CommandLine.Usage()
330+
331+
return 0
332+
}
333+
326334
r := runner{testSuiteInitializer: ts.TestSuiteInitializer, scenarioInitializer: ts.ScenarioInitializer}
327335
return runWithOptions(ts.Name, r, *ts.Options)
328336
}

0 commit comments

Comments
 (0)