From 05de518c14f861af3fee25d177d25d4920f8c37b Mon Sep 17 00:00:00 2001 From: maxcleme Date: Thu, 24 Aug 2023 01:00:38 +0200 Subject: [PATCH] feat: allow MustPassRepeatedly decorator to be set at suite level (#1266) --- .../config_override_fixture_suite_test.go | 2 +- .../config_override_fixture_suite_test.go | 21 +++++++ integration/flags_test.go | 4 +- integration/repeat_test.go | 11 ++++ internal/group.go | 5 +- .../config_must_pass_repeatedly_test.go | 57 +++++++++++++++++++ types/config.go | 1 + 7 files changed, 97 insertions(+), 4 deletions(-) rename integration/_fixtures/{config_override_fixture => config_override_label_filter_fixture}/config_override_fixture_suite_test.go (90%) create mode 100644 integration/_fixtures/config_override_must_pass_repeatedly_fixture/config_override_fixture_suite_test.go create mode 100644 internal/internal_integration/config_must_pass_repeatedly_test.go diff --git a/integration/_fixtures/config_override_fixture/config_override_fixture_suite_test.go b/integration/_fixtures/config_override_label_filter_fixture/config_override_fixture_suite_test.go similarity index 90% rename from integration/_fixtures/config_override_fixture/config_override_fixture_suite_test.go rename to integration/_fixtures/config_override_label_filter_fixture/config_override_fixture_suite_test.go index c2267b20f..ac1c40757 100644 --- a/integration/_fixtures/config_override_fixture/config_override_fixture_suite_test.go +++ b/integration/_fixtures/config_override_label_filter_fixture/config_override_fixture_suite_test.go @@ -1,4 +1,4 @@ -package config_override_fixture_test +package config_override_label_filter_fixture_test import ( "testing" diff --git a/integration/_fixtures/config_override_must_pass_repeatedly_fixture/config_override_fixture_suite_test.go b/integration/_fixtures/config_override_must_pass_repeatedly_fixture/config_override_fixture_suite_test.go new file mode 100644 index 000000000..e11d33e9e --- /dev/null +++ b/integration/_fixtures/config_override_must_pass_repeatedly_fixture/config_override_fixture_suite_test.go @@ -0,0 +1,21 @@ +package config_override_label_filter_fixture_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestConfigOverrideFixture(t *testing.T) { + RegisterFailHandler(Fail) + suiteConfig, reporterConfig := GinkgoConfiguration() + suiteConfig.MustPassRepeatedly = 10 + RunSpecs(t, "ConfigOverrideFixture Suite", suiteConfig, reporterConfig) +} + +var _ = Describe("tests", func() { + It("suite config overrides decorator", MustPassRepeatedly(2), func() { + Ω(CurrentSpecReport().MaxMustPassRepeatedly).Should(Equal(10)) + }) +}) diff --git a/integration/flags_test.go b/integration/flags_test.go index b4d63192f..72d48c099 100644 --- a/integration/flags_test.go +++ b/integration/flags_test.go @@ -135,8 +135,8 @@ var _ = Describe("Flags Specs", func() { }) It("should allow configuration overrides", func() { - fm.MountFixture("config_override") - session := startGinkgo(fm.PathTo("config_override"), "--label-filter=NORUN", "--no-color") + fm.MountFixture("config_override_label_filter") + session := startGinkgo(fm.PathTo("config_override_label_filter"), "--label-filter=NORUN", "--no-color") Eventually(session).Should(gexec.Exit(0), "Succeeds because --label-filter is overridden by the test suite itself.") output := string(session.Out.Contents()) Ω(output).Should(ContainSubstring("2 Specs")) diff --git a/integration/repeat_test.go b/integration/repeat_test.go index 5a36c0731..c82c0bff9 100644 --- a/integration/repeat_test.go +++ b/integration/repeat_test.go @@ -98,4 +98,15 @@ var _ = Describe("Repeat", func() { Ω(session.Err).Should(gbytes.Say("--repeat and --until-it-fails are both set")) }) }) + + Context("if MustPassRepeatedly is set at suite config level", func() { + BeforeEach(func() { + fm.MountFixture("config_override_must_pass_repeatedly") + }) + + It("it should override node decorator", func() { + session := startGinkgo(fm.PathTo("config_override_must_pass_repeatedly")) + Eventually(session).Should(gexec.Exit(0)) + }) + }) }) diff --git a/internal/group.go b/internal/group.go index ae1b7b011..02c9fe4fc 100644 --- a/internal/group.go +++ b/internal/group.go @@ -321,7 +321,10 @@ func (g *group) run(specs Specs) { if !skip { var maxAttempts = 1 - if g.suite.currentSpecReport.MaxMustPassRepeatedly > 0 { + if g.suite.config.MustPassRepeatedly > 0 { + maxAttempts = g.suite.config.MustPassRepeatedly + g.suite.currentSpecReport.MaxMustPassRepeatedly = maxAttempts + } else if g.suite.currentSpecReport.MaxMustPassRepeatedly > 0 { maxAttempts = max(1, spec.MustPassRepeatedly()) } else if g.suite.config.FlakeAttempts > 0 { maxAttempts = g.suite.config.FlakeAttempts diff --git a/internal/internal_integration/config_must_pass_repeatedly_test.go b/internal/internal_integration/config_must_pass_repeatedly_test.go new file mode 100644 index 000000000..640af7d6d --- /dev/null +++ b/internal/internal_integration/config_must_pass_repeatedly_test.go @@ -0,0 +1,57 @@ +package internal_integration_test + +import ( + "fmt" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/ginkgo/v2/internal/test_helpers" + . "github.com/onsi/gomega" +) + +var _ = Describe("when config.MustPassRepeatedly is greater than 1", func() { + var success bool + JustBeforeEach(func() { + var counterB int + success, _ = RunFixture("flakey success", func() { + It("A", func() {}) + It("B", func() { + counterB += 1 + if counterB == 8 { + F(fmt.Sprintf("C - %d", counterB)) + } + }) + }) + }) + + Context("when all tests pass", func() { + BeforeEach(func() { + conf.MustPassRepeatedly = 5 + }) + + It("reports that the suite passed", func() { + Ω(success).Should(BeTrue()) + Ω(reporter.End).Should(BeASuiteSummary(NSpecs(2), NFailed(0), NPassed(2))) + }) + + It("reports that the tests passed with the correct number of attempts", func() { + Ω(reporter.Did.Find("A")).Should(HavePassed(NumAttempts(5))) + Ω(reporter.Did.Find("B")).Should(HavePassed(NumAttempts(5))) + }) + }) + + Context("when a test fails", func() { + BeforeEach(func() { + conf.MustPassRepeatedly = 10 + }) + + It("reports that the suite failed", func() { + Ω(success).Should(BeFalse()) + Ω(reporter.End).Should(BeASuiteSummary(NSpecs(2), NFailed(1), NPassed(1))) + }) + + It("reports that the tests failed with the correct number of attempts", func() { + Ω(reporter.Did.Find("A")).Should(HavePassed(NumAttempts(10))) + Ω(reporter.Did.Find("B")).Should(HaveFailed(NumAttempts(8))) + }) + }) +}) diff --git a/types/config.go b/types/config.go index 1014c7b49..c88fc85a7 100644 --- a/types/config.go +++ b/types/config.go @@ -27,6 +27,7 @@ type SuiteConfig struct { FailOnPending bool FailFast bool FlakeAttempts int + MustPassRepeatedly int DryRun bool PollProgressAfter time.Duration PollProgressInterval time.Duration