Skip to content

Commit

Permalink
If the user sets --seed=0, make sure all parallel nodes get the same …
Browse files Browse the repository at this point in the history
…seed
  • Loading branch information
onsi committed Mar 24, 2024
1 parent 75ad73b commit af0330d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
21 changes: 21 additions & 0 deletions integration/_fixtures/seed_fixture/seed_fixture_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package seed_fixture_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestSeedFixture(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "SeedFixture Suite")
}

var _ = BeforeSuite(func() {
Ω(GinkgoRandomSeed()).Should(Equal(int64(0)))
})

var _ = It("has the expected seed (namely, 0)", func() {
Ω(GinkgoRandomSeed()).Should(Equal(int64(0)))
})
6 changes: 6 additions & 0 deletions integration/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ var _ = Describe("Flags Specs", func() {
Ω(orders[0]).ShouldNot(BeNumerically("<", orders[1]))
})

It("should consistently pass in a zero seed when asked to", func() {
fm.MountFixture("seed")
session := startGinkgo(fm.PathTo("seed"), "--no-color", "--seed=0", "--nodes=2")
Eventually(session).Should(gexec.Exit(0))
})

It("should pass additional arguments in", func() {
session := startGinkgo(fm.PathTo("flags"), "--", "--customFlag=madagascar")
Eventually(session).Should(gexec.Exit(1))
Expand Down
2 changes: 1 addition & 1 deletion types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ var FlagSections = GinkgoFlagSections{
// SuiteConfigFlags provides flags for the Ginkgo test process, and CLI
var SuiteConfigFlags = GinkgoFlags{
{KeyPath: "S.RandomSeed", Name: "seed", SectionKey: "order", UsageDefaultValue: "randomly generated by Ginkgo",
Usage: "The seed used to randomize the spec suite."},
Usage: "The seed used to randomize the spec suite.", AlwaysExport: true},
{KeyPath: "S.RandomizeAllSpecs", Name: "randomize-all", SectionKey: "order", DeprecatedName: "randomizeAllSpecs", DeprecatedDocLink: "changed-command-line-flags",
Usage: "If set, ginkgo will randomize all specs together. By default, ginkgo only randomizes the top level Describe, Context and When containers."},

Expand Down
15 changes: 8 additions & 7 deletions types/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type GinkgoFlag struct {
DeprecatedDocLink string
DeprecatedVersion string

ExportAs string
ExportAs string
AlwaysExport bool
}

type GinkgoFlags []GinkgoFlag
Expand Down Expand Up @@ -431,7 +432,7 @@ func (ssv stringSliceVar) Set(s string) error {
return nil
}

//given a set of GinkgoFlags and bindings, generate flag arguments suitable to be passed to an application with that set of flags configured.
// given a set of GinkgoFlags and bindings, generate flag arguments suitable to be passed to an application with that set of flags configured.
func GenerateFlagArgs(flags GinkgoFlags, bindings interface{}) ([]string, error) {
result := []string{}
for _, flag := range flags {
Expand All @@ -451,27 +452,27 @@ func GenerateFlagArgs(flags GinkgoFlags, bindings interface{}) ([]string, error)
iface := value.Interface()
switch value.Type() {
case reflect.TypeOf(string("")):
if iface.(string) != "" {
if iface.(string) != "" || flag.AlwaysExport {
result = append(result, fmt.Sprintf("--%s=%s", name, iface))
}
case reflect.TypeOf(int64(0)):
if iface.(int64) != 0 {
if iface.(int64) != 0 || flag.AlwaysExport {
result = append(result, fmt.Sprintf("--%s=%d", name, iface))
}
case reflect.TypeOf(float64(0)):
if iface.(float64) != 0 {
if iface.(float64) != 0 || flag.AlwaysExport {
result = append(result, fmt.Sprintf("--%s=%f", name, iface))
}
case reflect.TypeOf(int(0)):
if iface.(int) != 0 {
if iface.(int) != 0 || flag.AlwaysExport {
result = append(result, fmt.Sprintf("--%s=%d", name, iface))
}
case reflect.TypeOf(bool(true)):
if iface.(bool) {
result = append(result, fmt.Sprintf("--%s", name))
}
case reflect.TypeOf(time.Duration(0)):
if iface.(time.Duration) != time.Duration(0) {
if iface.(time.Duration) != time.Duration(0) || flag.AlwaysExport {
result = append(result, fmt.Sprintf("--%s=%s", name, iface))
}

Expand Down
22 changes: 20 additions & 2 deletions types/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,10 @@ var _ = Describe("Flags", func() {
}
flags = types.GinkgoFlags{
{Name: "string-flag", KeyPath: "A.StringProperty", DeprecatedName: "stringFlag"},
{Name: "int-64-flag", KeyPath: "A.Int64Property"},
{Name: "int-64-flag", KeyPath: "A.Int64Property", AlwaysExport: true},
{Name: "float-64-flag", KeyPath: "A.Float64Property"},
{Name: "int-flag", KeyPath: "B.IntProperty", ExportAs: "alias-int-flag"},
{Name: "bool-flag", KeyPath: "B.BoolProperty", ExportAs: "alias-bool-flag"},
{Name: "bool-flag", KeyPath: "B.BoolProperty", ExportAs: "alias-bool-flag", AlwaysExport: true},
{Name: "string-slice-flag", KeyPath: "B.StringSliceProperty"},
{DeprecatedName: "deprecated-flag", KeyPath: "B.DeprecatedProperty"},
}
Expand All @@ -452,6 +452,24 @@ var _ = Describe("Flags", func() {
}))
})

It("does not include 0 values unless AlwaysExport is true", func() {
A.StringProperty = ""
A.Int64Property = 0
B.IntProperty = 0
B.BoolProperty = false

args, err := types.GenerateFlagArgs(flags, bindings)
Ω(err).ShouldNot(HaveOccurred())

Ω(args).Should(Equal([]string{
"--int-64-flag=0", //always export
"--float-64-flag=3.141000",
"--string-slice-flag=once",
"--string-slice-flag=upon",
"--string-slice-flag=a time",
}))
})

It("errors if there is a keypath issue", func() {
flags[0] = types.GinkgoFlag{Name: "unsupported-type", KeyPath: "A.UnsupportedInt32"}
args, err := types.GenerateFlagArgs(flags, bindings)
Expand Down

0 comments on commit af0330d

Please sign in to comment.