github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/internal_integration/report_each_test.go (about)

     1  package internal_integration_test
     2  
     3  import (
     4  	"time"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	"github.com/onsi/ginkgo/internal/interrupt_handler"
     8  	. "github.com/onsi/ginkgo/internal/test_helpers"
     9  	"github.com/onsi/ginkgo/types"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Sending reports to ReportBeforeEach and ReportAfterEach nodes", func() {
    14  	var reports map[string]Reports
    15  	BeforeEach(func() {
    16  		conf.SkipStrings = []string{"flag-skipped"}
    17  		reports = map[string]Reports{}
    18  		success, hPF := RunFixture("suite with reporting nodes", func() {
    19  			BeforeSuite(rt.T("before-suite"))
    20  			AfterSuite(rt.T("after-suite"))
    21  			ReportAfterEach(func(report types.SpecReport) {
    22  				rt.Run("outer-RAE")
    23  				reports["outer-RAE"] = append(reports["outer-RAE"], report)
    24  			})
    25  			Describe("top-level container", func() {
    26  				ReportBeforeEach(func(report types.SpecReport) {
    27  					rt.Run("inner-RBE")
    28  					reports["inner-RBE"] = append(reports["inner-RBE"], report)
    29  				})
    30  				ReportAfterEach(func(report types.SpecReport) {
    31  					rt.Run("inner-RAE")
    32  					reports["inner-RAE"] = append(reports["inner-RAE"], report)
    33  				})
    34  				It("passes", rt.T("passes"))
    35  				It("fails", rt.T("fails", func() {
    36  					F("fail")
    37  				}))
    38  				It("panics", rt.T("panics", func() {
    39  					panic("boom")
    40  				}))
    41  				PIt("is pending", rt.T("pending"))
    42  				It("is Skip()ed", func() {
    43  					rt.Run("skipped")
    44  					FixtureSkip("nah...")
    45  				})
    46  				It("is flag-skipped", rt.T("flag-skipped"))
    47  				Context("when the ReportAfterEach node fails", func() {
    48  					It("also passes", rt.T("also-passes"))
    49  					ReportAfterEach(func(report types.SpecReport) {
    50  						rt.Run("failing-RAE")
    51  						reports["failing-RAE"] = append(reports["failing-RAE"], report)
    52  						F("fail")
    53  					})
    54  				})
    55  				Context("when the ReportAfterEach node fails in a skipped test", func() {
    56  					It("is also flag-skipped", rt.T("also-flag-skipped"))
    57  					ReportAfterEach(func(report types.SpecReport) {
    58  						rt.Run("failing-in-skip-RAE")
    59  						reports["failing-skipped-RAE"] = append(reports["failing-skipped-RAE"], report)
    60  						F("fail")
    61  					})
    62  				})
    63  				Context("when stuff is emitted to writers and stdout/stderr", func() {
    64  					It("writes stuff", rt.T("writer", func() {
    65  						writer.Println("GinkgoWriter from It")
    66  						outputInterceptor.AppendInterceptedOutput("Output from It\n")
    67  					}))
    68  					ReportAfterEach(func(report types.SpecReport) {
    69  						rt.Run("writing-reporter")
    70  						reports["writing"] = append(reports["writing"], report)
    71  						writer.Println("GinkgoWriter from ReportAfterEach")
    72  						outputInterceptor.AppendInterceptedOutput("Output from ReportAfterEach\n")
    73  					})
    74  				})
    75  				Context("when a ReportBeforeEach fails", func() {
    76  					ReportBeforeEach(func(report types.SpecReport) {
    77  						rt.Run("failing-RBE")
    78  						reports["failing-RBE"] = append(reports["failing-RBE"], report)
    79  						F("fail")
    80  					})
    81  					ReportBeforeEach(func(report types.SpecReport) {
    82  						rt.Run("not-failing-RBE")
    83  						reports["not-failing-RBE"] = append(reports["not-failing-RBE"], report)
    84  					})
    85  
    86  					It("does not run", rt.T("does-not-run"))
    87  				})
    88  				Context("when a reporter is interrupted", func() {
    89  					It("passes yet again", rt.T("passes-yet-again"))
    90  					It("skipped by interrupt", rt.T("skipped-by-interrupt"))
    91  					ReportAfterEach(func(report types.SpecReport) {
    92  						interruptHandler.Interrupt(interrupt_handler.InterruptCauseTimeout)
    93  						time.Sleep(100 * time.Millisecond)
    94  						rt.RunWithData("interrupt-reporter", "interrupt-message", interruptHandler.EmittedInterruptPlaceholderMessage())
    95  						reports["interrupt"] = append(reports["interrupt"], report)
    96  					})
    97  				})
    98  			})
    99  			ReportBeforeEach(func(report types.SpecReport) {
   100  				rt.Run("outer-RBE")
   101  				reports["outer-RBE"] = append(reports["outer-RBE"], report)
   102  			})
   103  		})
   104  		Ω(success).Should(BeFalse())
   105  		Ω(hPF).Should(BeFalse())
   106  	})
   107  
   108  	It("runs ReportAfterEach blocks in the correct order", func() {
   109  		Ω(rt).Should(HaveTracked(
   110  			"before-suite",
   111  			"outer-RBE", "inner-RBE", "passes", "inner-RAE", "outer-RAE",
   112  			"outer-RBE", "inner-RBE", "fails", "inner-RAE", "outer-RAE",
   113  			"outer-RBE", "inner-RBE", "panics", "inner-RAE", "outer-RAE",
   114  			"outer-RBE", "inner-RBE", "inner-RAE", "outer-RAE", //pending test
   115  			"outer-RBE", "inner-RBE", "skipped", "inner-RAE", "outer-RAE",
   116  			"outer-RBE", "inner-RBE", "inner-RAE", "outer-RAE", //flag-skipped test
   117  			"outer-RBE", "inner-RBE", "also-passes", "failing-RAE", "inner-RAE", "outer-RAE",
   118  			"outer-RBE", "inner-RBE", "failing-in-skip-RAE", "inner-RAE", "outer-RAE", //is also flag-skipped
   119  			"outer-RBE", "inner-RBE", "writer", "writing-reporter", "inner-RAE", "outer-RAE",
   120  			"outer-RBE", "inner-RBE", "failing-RBE", "not-failing-RBE", "inner-RAE", "outer-RAE",
   121  			"outer-RBE", "inner-RBE", "passes-yet-again", "interrupt-reporter", "inner-RAE", "outer-RAE",
   122  			"outer-RBE", "inner-RBE", "interrupt-reporter", "inner-RAE", "outer-RAE", //skipped by interrupt
   123  			"after-suite",
   124  		))
   125  	})
   126  
   127  	It("does not include the before-suite or after-suite reports", func() {
   128  		Ω(reports["outer-RAE"].FindByLeafNodeType(types.NodeTypeBeforeSuite)).Should(BeZero())
   129  		Ω(reports["outer-RAE"].FindByLeafNodeType(types.NodeTypeAfterSuite)).Should(BeZero())
   130  	})
   131  
   132  	It("submits the correct reports to the reporters", func() {
   133  		for _, name := range []string{"passes", "fails", "panics", "is Skip()ed", "is flag-skipped", "is also flag-skipped"} {
   134  			Ω(reports["outer-RAE"].Find(name)).Should(Equal(reporter.Did.Find(name)))
   135  			Ω(reports["inner-RAE"].Find(name)).Should(Equal(reporter.Did.Find(name)))
   136  		}
   137  
   138  		Ω(reports["outer-RBE"].Find("passes")).ShouldNot(BeZero())
   139  		Ω(reports["outer-RBE"].Find("fails")).ShouldNot(BeZero())
   140  		Ω(reports["outer-RBE"].Find("panics")).ShouldNot(BeZero())
   141  		Ω(reports["outer-RBE"].Find("is pending")).Should(BePending())
   142  		Ω(reports["outer-RAE"].Find("is flag-skipped")).Should(HaveBeenSkipped())
   143  
   144  		Ω(reports["outer-RAE"].Find("passes")).Should(HavePassed())
   145  		Ω(reports["outer-RAE"].Find("fails")).Should(HaveFailed("fail"))
   146  		Ω(reports["outer-RAE"].Find("panics")).Should(HavePanicked("boom"))
   147  		Ω(reports["outer-RAE"].Find("is pending")).Should(BePending())
   148  		Ω(reports["outer-RAE"].Find("is Skip()ed").State).Should(Equal(types.SpecStateSkipped))
   149  		Ω(reports["outer-RAE"].Find("is Skip()ed").Failure.Message).Should(Equal("nah..."))
   150  		Ω(reports["outer-RAE"].Find("is flag-skipped")).Should(HaveBeenSkipped())
   151  	})
   152  
   153  	It("handles reporters that fail", func() {
   154  		Ω(reports["failing-RAE"].Find("also passes")).Should(HavePassed())
   155  		Ω(reports["outer-RAE"].Find("also passes")).Should(HaveFailed("fail"))
   156  		Ω(reports["inner-RAE"].Find("also passes")).Should(HaveFailed("fail"))
   157  		Ω(reporter.Did.Find("also passes")).Should(HaveFailed("fail"), FailureNodeType(types.NodeTypeReportAfterEach))
   158  
   159  		Ω(reports["failing-RBE"].Find("does not run")).ShouldNot(BeZero())
   160  		Ω(reports["not-failing-RBE"].Find("does not run")).Should(HaveFailed("fail"))
   161  		Ω(reports["outer-RAE"].Find("does not run")).Should(HaveFailed("fail"))
   162  		Ω(reports["inner-RAE"].Find("does not run")).Should(HaveFailed("fail"))
   163  		Ω(reporter.Did.Find("does not run")).Should(HaveFailed("fail", FailureNodeType(types.NodeTypeReportBeforeEach)))
   164  	})
   165  
   166  	It("handles reporters that fail, even in skipped specs", func() {
   167  		Ω(reports["failing-skipped-RAE"].Find("is also flag-skipped")).Should(HaveBeenSkipped())
   168  		Ω(reports["outer-RAE"].Find("is also flag-skipped")).Should(HaveFailed("fail"))
   169  		Ω(reports["inner-RAE"].Find("is also flag-skipped")).Should(HaveFailed("fail"))
   170  		Ω(reporter.Did.Find("is also flag-skipped")).Should(HaveFailed("fail"))
   171  	})
   172  
   173  	It("captures output from reporter nodes, but only sends them to the DefaultReporter, not the subsequent nodes", func() {
   174  		Ω(reports["writing"].Find("writes stuff").CapturedGinkgoWriterOutput).Should((Equal("GinkgoWriter from It\n")))
   175  		Ω(reports["writing"].Find("writes stuff").CapturedStdOutErr).Should((Equal("Output from It\n")))
   176  		Ω(reports["outer-RAE"].Find("writes stuff").CapturedGinkgoWriterOutput).Should(Equal("GinkgoWriter from It\nGinkgoWriter from ReportAfterEach\n"))
   177  		Ω(reports["outer-RAE"].Find("writes stuff").CapturedStdOutErr).Should(Equal("Output from It\nOutput from ReportAfterEach\n"))
   178  		Ω(reports["inner-RAE"].Find("writes stuff").CapturedGinkgoWriterOutput).Should(Equal("GinkgoWriter from It\nGinkgoWriter from ReportAfterEach\n"))
   179  		Ω(reports["inner-RAE"].Find("writes stuff").CapturedStdOutErr).Should(Equal("Output from It\nOutput from ReportAfterEach\n"))
   180  
   181  		//but a report containing the additional output will be send to Ginkgo's reporter...
   182  		Ω(reporter.Did.Find("writes stuff").CapturedGinkgoWriterOutput).Should((Equal("GinkgoWriter from It\nGinkgoWriter from ReportAfterEach\n")))
   183  		Ω(reporter.Did.Find("writes stuff").CapturedStdOutErr).Should((Equal("Output from It\nOutput from ReportAfterEach\n")))
   184  	})
   185  
   186  	It("ignores interrupts and soldiers on", func() {
   187  		//The "interrupt" reporter is interrupted by the user - but keeps running (instead, the user sees a message emitted that they are attempting to interrupt a reporter and will just need to wait)
   188  		//The interrupt is, however, honored and subsequent tests are skipped.  These skipped tests, however, are still reported to the reporter node
   189  		Ω(reports["interrupt"].Find("passes yet again")).ShouldNot(BeZero())
   190  		Ω(reports["interrupt"].Find("passes yet again")).Should(HavePassed())
   191  		Ω(reports["interrupt"].Find("skipped by interrupt")).Should(HaveBeenSkipped())
   192  		Ω(reports["interrupt"].Find("passes yet again")).Should(Equal(reports["inner-RAE"].Find("passes yet again")))
   193  		Ω(reports["interrupt"].Find("passes yet again")).Should(Equal(reports["outer-RAE"].Find("passes yet again")))
   194  		Ω(reports["interrupt"].Find("skipped by interrupt")).Should(Equal(reports["inner-RAE"].Find("skipped by interrupt")))
   195  		Ω(reports["interrupt"].Find("skipped by interrupt")).Should(Equal(reports["outer-RAE"].Find("skipped by interrupt")))
   196  
   197  		cl := types.NewCodeLocation(0)
   198  		Ω(rt.DataFor("interrupt-reporter")["interrupt-message"]).Should(ContainSubstring("The running ReportAfterEach node is at:\n%s", cl.FileName))
   199  	})
   200  })