Skip to content

Commit af0330d

Browse files
committedMar 24, 2024·
If the user sets --seed=0, make sure all parallel nodes get the same seed
1 parent 75ad73b commit af0330d

File tree

5 files changed

+56
-10
lines changed

5 files changed

+56
-10
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package seed_fixture_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestSeedFixture(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "SeedFixture Suite")
13+
}
14+
15+
var _ = BeforeSuite(func() {
16+
Ω(GinkgoRandomSeed()).Should(Equal(int64(0)))
17+
})
18+
19+
var _ = It("has the expected seed (namely, 0)", func() {
20+
Ω(GinkgoRandomSeed()).Should(Equal(int64(0)))
21+
})

‎integration/flags_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ var _ = Describe("Flags Specs", func() {
6666
Ω(orders[0]).ShouldNot(BeNumerically("<", orders[1]))
6767
})
6868

69+
It("should consistently pass in a zero seed when asked to", func() {
70+
fm.MountFixture("seed")
71+
session := startGinkgo(fm.PathTo("seed"), "--no-color", "--seed=0", "--nodes=2")
72+
Eventually(session).Should(gexec.Exit(0))
73+
})
74+
6975
It("should pass additional arguments in", func() {
7076
session := startGinkgo(fm.PathTo("flags"), "--", "--customFlag=madagascar")
7177
Eventually(session).Should(gexec.Exit(1))

‎types/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ var FlagSections = GinkgoFlagSections{
265265
// SuiteConfigFlags provides flags for the Ginkgo test process, and CLI
266266
var SuiteConfigFlags = GinkgoFlags{
267267
{KeyPath: "S.RandomSeed", Name: "seed", SectionKey: "order", UsageDefaultValue: "randomly generated by Ginkgo",
268-
Usage: "The seed used to randomize the spec suite."},
268+
Usage: "The seed used to randomize the spec suite.", AlwaysExport: true},
269269
{KeyPath: "S.RandomizeAllSpecs", Name: "randomize-all", SectionKey: "order", DeprecatedName: "randomizeAllSpecs", DeprecatedDocLink: "changed-command-line-flags",
270270
Usage: "If set, ginkgo will randomize all specs together. By default, ginkgo only randomizes the top level Describe, Context and When containers."},
271271

‎types/flags.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ type GinkgoFlag struct {
2424
DeprecatedDocLink string
2525
DeprecatedVersion string
2626

27-
ExportAs string
27+
ExportAs string
28+
AlwaysExport bool
2829
}
2930

3031
type GinkgoFlags []GinkgoFlag
@@ -431,7 +432,7 @@ func (ssv stringSliceVar) Set(s string) error {
431432
return nil
432433
}
433434

434-
//given a set of GinkgoFlags and bindings, generate flag arguments suitable to be passed to an application with that set of flags configured.
435+
// given a set of GinkgoFlags and bindings, generate flag arguments suitable to be passed to an application with that set of flags configured.
435436
func GenerateFlagArgs(flags GinkgoFlags, bindings interface{}) ([]string, error) {
436437
result := []string{}
437438
for _, flag := range flags {
@@ -451,27 +452,27 @@ func GenerateFlagArgs(flags GinkgoFlags, bindings interface{}) ([]string, error)
451452
iface := value.Interface()
452453
switch value.Type() {
453454
case reflect.TypeOf(string("")):
454-
if iface.(string) != "" {
455+
if iface.(string) != "" || flag.AlwaysExport {
455456
result = append(result, fmt.Sprintf("--%s=%s", name, iface))
456457
}
457458
case reflect.TypeOf(int64(0)):
458-
if iface.(int64) != 0 {
459+
if iface.(int64) != 0 || flag.AlwaysExport {
459460
result = append(result, fmt.Sprintf("--%s=%d", name, iface))
460461
}
461462
case reflect.TypeOf(float64(0)):
462-
if iface.(float64) != 0 {
463+
if iface.(float64) != 0 || flag.AlwaysExport {
463464
result = append(result, fmt.Sprintf("--%s=%f", name, iface))
464465
}
465466
case reflect.TypeOf(int(0)):
466-
if iface.(int) != 0 {
467+
if iface.(int) != 0 || flag.AlwaysExport {
467468
result = append(result, fmt.Sprintf("--%s=%d", name, iface))
468469
}
469470
case reflect.TypeOf(bool(true)):
470471
if iface.(bool) {
471472
result = append(result, fmt.Sprintf("--%s", name))
472473
}
473474
case reflect.TypeOf(time.Duration(0)):
474-
if iface.(time.Duration) != time.Duration(0) {
475+
if iface.(time.Duration) != time.Duration(0) || flag.AlwaysExport {
475476
result = append(result, fmt.Sprintf("--%s=%s", name, iface))
476477
}
477478

‎types/flags_test.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,10 @@ var _ = Describe("Flags", func() {
427427
}
428428
flags = types.GinkgoFlags{
429429
{Name: "string-flag", KeyPath: "A.StringProperty", DeprecatedName: "stringFlag"},
430-
{Name: "int-64-flag", KeyPath: "A.Int64Property"},
430+
{Name: "int-64-flag", KeyPath: "A.Int64Property", AlwaysExport: true},
431431
{Name: "float-64-flag", KeyPath: "A.Float64Property"},
432432
{Name: "int-flag", KeyPath: "B.IntProperty", ExportAs: "alias-int-flag"},
433-
{Name: "bool-flag", KeyPath: "B.BoolProperty", ExportAs: "alias-bool-flag"},
433+
{Name: "bool-flag", KeyPath: "B.BoolProperty", ExportAs: "alias-bool-flag", AlwaysExport: true},
434434
{Name: "string-slice-flag", KeyPath: "B.StringSliceProperty"},
435435
{DeprecatedName: "deprecated-flag", KeyPath: "B.DeprecatedProperty"},
436436
}
@@ -452,6 +452,24 @@ var _ = Describe("Flags", func() {
452452
}))
453453
})
454454

455+
It("does not include 0 values unless AlwaysExport is true", func() {
456+
A.StringProperty = ""
457+
A.Int64Property = 0
458+
B.IntProperty = 0
459+
B.BoolProperty = false
460+
461+
args, err := types.GenerateFlagArgs(flags, bindings)
462+
Ω(err).ShouldNot(HaveOccurred())
463+
464+
Ω(args).Should(Equal([]string{
465+
"--int-64-flag=0", //always export
466+
"--float-64-flag=3.141000",
467+
"--string-slice-flag=once",
468+
"--string-slice-flag=upon",
469+
"--string-slice-flag=a time",
470+
}))
471+
})
472+
455473
It("errors if there is a keypath issue", func() {
456474
flags[0] = types.GinkgoFlag{Name: "unsupported-type", KeyPath: "A.UnsupportedInt32"}
457475
args, err := types.GenerateFlagArgs(flags, bindings)

0 commit comments

Comments
 (0)
Please sign in to comment.