Skip to content

Commit 5dcffa2

Browse files
authored
Merge pull request #144 from patrickvalle/master
Allow overriding flag defaults in BindFlags
2 parents e05efe0 + b636e6d commit 5dcffa2

File tree

2 files changed

+165
-11
lines changed

2 files changed

+165
-11
lines changed

flags.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,47 @@ func BindFlags(prefix string, set *flag.FlagSet, opt *Options) {
5252
}
5353
descFormatOption = strings.TrimSpace(descFormatOption)
5454

55-
set.StringVar(&opt.Format, prefix+"format", "pretty", descFormatOption)
56-
set.StringVar(&opt.Format, prefix+"f", "pretty", descFormatOption)
57-
set.StringVar(&opt.Tags, prefix+"tags", "", descTagsOption)
58-
set.StringVar(&opt.Tags, prefix+"t", "", descTagsOption)
59-
set.IntVar(&opt.Concurrency, prefix+"concurrency", 1, descConcurrencyOption)
60-
set.IntVar(&opt.Concurrency, prefix+"c", 1, descConcurrencyOption)
61-
set.BoolVar(&opt.ShowStepDefinitions, prefix+"definitions", false, "Print all available step definitions.")
62-
set.BoolVar(&opt.ShowStepDefinitions, prefix+"d", false, "Print all available step definitions.")
63-
set.BoolVar(&opt.StopOnFailure, prefix+"stop-on-failure", false, "Stop processing on first failed scenario.")
64-
set.BoolVar(&opt.Strict, prefix+"strict", false, "Fail suite when there are pending or undefined steps.")
65-
set.BoolVar(&opt.NoColors, prefix+"no-colors", false, "Disable ansi colors.")
55+
// override flag defaults if any corresponding properties were supplied on the incoming `opt`
56+
defFormatOption := "pretty"
57+
if opt.Format != "" {
58+
defFormatOption = opt.Format
59+
}
60+
defTagsOption := ""
61+
if opt.Tags != "" {
62+
defTagsOption = opt.Tags
63+
}
64+
defConcurrencyOption := 1
65+
if opt.Concurrency != 0 {
66+
defConcurrencyOption = opt.Concurrency
67+
}
68+
defShowStepDefinitions := false
69+
if opt.ShowStepDefinitions {
70+
defShowStepDefinitions = opt.ShowStepDefinitions
71+
}
72+
defStopOnFailure := false
73+
if opt.StopOnFailure {
74+
defStopOnFailure = opt.StopOnFailure
75+
}
76+
defStrict := false
77+
if opt.Strict {
78+
defStrict = opt.Strict
79+
}
80+
defNoColors := false
81+
if opt.NoColors {
82+
defNoColors = opt.NoColors
83+
}
84+
85+
set.StringVar(&opt.Format, prefix+"format", defFormatOption, descFormatOption)
86+
set.StringVar(&opt.Format, prefix+"f", defFormatOption, descFormatOption)
87+
set.StringVar(&opt.Tags, prefix+"tags", defTagsOption, descTagsOption)
88+
set.StringVar(&opt.Tags, prefix+"t", defTagsOption, descTagsOption)
89+
set.IntVar(&opt.Concurrency, prefix+"concurrency", defConcurrencyOption, descConcurrencyOption)
90+
set.IntVar(&opt.Concurrency, prefix+"c", defConcurrencyOption, descConcurrencyOption)
91+
set.BoolVar(&opt.ShowStepDefinitions, prefix+"definitions", defShowStepDefinitions, "Print all available step definitions.")
92+
set.BoolVar(&opt.ShowStepDefinitions, prefix+"d", defShowStepDefinitions, "Print all available step definitions.")
93+
set.BoolVar(&opt.StopOnFailure, prefix+"stop-on-failure", defStopOnFailure, "Stop processing on first failed scenario.")
94+
set.BoolVar(&opt.Strict, prefix+"strict", defStrict, "Fail suite when there are pending or undefined steps.")
95+
set.BoolVar(&opt.NoColors, prefix+"no-colors", defNoColors, "Disable ansi colors.")
6696
set.Var(&randomSeed{&opt.Randomize}, prefix+"random", descRandomOption)
6797
}
6898

flags_test.go

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

33
import (
44
"bytes"
5+
"flag"
56
"fmt"
67
"strings"
78
"testing"
@@ -75,3 +76,126 @@ func TestFlagsUsageShouldIncludeFormatDescriptons(t *testing.T) {
7576
}
7677
}
7778
}
79+
80+
func TestBindFlagsShouldRespectFlagDefaults(t *testing.T) {
81+
opts := Options{}
82+
83+
BindFlags("flagDefaults.", flag.CommandLine, &opts)
84+
85+
if opts.Format != "pretty" {
86+
t.Fatalf("expected Format: pretty, but it was: %s", opts.Format)
87+
}
88+
if opts.Tags != "" {
89+
t.Fatalf("expected Tags: '', but it was: %s", opts.Tags)
90+
}
91+
if opts.Concurrency != 1 {
92+
t.Fatalf("expected Concurrency: 1, but it was: %d", opts.Concurrency)
93+
}
94+
if opts.ShowStepDefinitions {
95+
t.Fatalf("expected ShowStepDefinitions: false, but it was: %t", opts.ShowStepDefinitions)
96+
}
97+
if opts.StopOnFailure {
98+
t.Fatalf("expected StopOnFailure: false, but it was: %t", opts.StopOnFailure)
99+
}
100+
if opts.Strict {
101+
t.Fatalf("expected Strict: false, but it was: %t", opts.Strict)
102+
}
103+
if opts.NoColors {
104+
t.Fatalf("expected NoColors: false, but it was: %t", opts.NoColors)
105+
}
106+
if opts.Randomize != 0 {
107+
t.Fatalf("expected Randomize: 0, but it was: %d", opts.Randomize)
108+
}
109+
}
110+
111+
func TestBindFlagsShouldRespectOptDefaults(t *testing.T) {
112+
opts := Options{
113+
Format: "progress",
114+
Tags: "test",
115+
Concurrency: 2,
116+
ShowStepDefinitions: true,
117+
StopOnFailure: true,
118+
Strict: true,
119+
NoColors: true,
120+
Randomize: int64(7),
121+
}
122+
123+
BindFlags("optDefaults.", flag.CommandLine, &opts)
124+
125+
if opts.Format != "progress" {
126+
t.Fatalf("expected Format: progress, but it was: %s", opts.Format)
127+
}
128+
if opts.Tags != "test" {
129+
t.Fatalf("expected Tags: 'test', but it was: %s", opts.Tags)
130+
}
131+
if opts.Concurrency != 2 {
132+
t.Fatalf("expected Concurrency: 2, but it was: %d", opts.Concurrency)
133+
}
134+
if !opts.ShowStepDefinitions {
135+
t.Fatalf("expected ShowStepDefinitions: true, but it was: %t", opts.ShowStepDefinitions)
136+
}
137+
if !opts.StopOnFailure {
138+
t.Fatalf("expected StopOnFailure: true, but it was: %t", opts.StopOnFailure)
139+
}
140+
if !opts.Strict {
141+
t.Fatalf("expected Strict: true, but it was: %t", opts.Strict)
142+
}
143+
if !opts.NoColors {
144+
t.Fatalf("expected NoColors: true, but it was: %t", opts.NoColors)
145+
}
146+
if opts.Randomize != 7 {
147+
t.Fatalf("expected Randomize: 7, but it was: %d", opts.Randomize)
148+
}
149+
}
150+
151+
func TestBindFlagsShouldRespectFlagOverrides(t *testing.T) {
152+
opts := Options{
153+
Format: "progress",
154+
Tags: "test",
155+
Concurrency: 2,
156+
ShowStepDefinitions: true,
157+
StopOnFailure: true,
158+
Strict: true,
159+
NoColors: true,
160+
Randomize: 11,
161+
}
162+
flagSet := flag.FlagSet{}
163+
164+
BindFlags("optOverrides.", &flagSet, &opts)
165+
166+
flagSet.Parse([]string{
167+
"--optOverrides.format=junit",
168+
"--optOverrides.tags=test2",
169+
"--optOverrides.concurrency=3",
170+
"--optOverrides.definitions=false",
171+
"--optOverrides.stop-on-failure=false",
172+
"--optOverrides.strict=false",
173+
"--optOverrides.no-colors=false",
174+
"--optOverrides.random=2",
175+
})
176+
177+
if opts.Format != "junit" {
178+
t.Fatalf("expected Format: junit, but it was: %s", opts.Format)
179+
}
180+
if opts.Tags != "test2" {
181+
t.Fatalf("expected Tags: 'test2', but it was: %s", opts.Tags)
182+
}
183+
if opts.Concurrency != 3 {
184+
t.Fatalf("expected Concurrency: 3, but it was: %d", opts.Concurrency)
185+
}
186+
if opts.ShowStepDefinitions {
187+
t.Fatalf("expected ShowStepDefinitions: true, but it was: %t", opts.ShowStepDefinitions)
188+
}
189+
if opts.StopOnFailure {
190+
t.Fatalf("expected StopOnFailure: true, but it was: %t", opts.StopOnFailure)
191+
}
192+
if opts.Strict {
193+
t.Fatalf("expected Strict: true, but it was: %t", opts.Strict)
194+
}
195+
if opts.NoColors {
196+
t.Fatalf("expected NoColors: true, but it was: %t", opts.NoColors)
197+
}
198+
if opts.Randomize != 2 {
199+
t.Fatalf("expected Randomize: 2, but it was: %d", opts.Randomize)
200+
}
201+
}

0 commit comments

Comments
 (0)