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

     1  package testingtproxy_test
     2  
     3  import (
     4  	"os"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  
     9  	"github.com/onsi/gomega/gbytes"
    10  
    11  	"github.com/onsi/ginkgo/internal/testingtproxy"
    12  	"github.com/onsi/ginkgo/types"
    13  )
    14  
    15  type messagedCall struct {
    16  	message    string
    17  	callerSkip []int
    18  }
    19  
    20  var _ = Describe("Testingtproxy", func() {
    21  	var t GinkgoTInterface
    22  
    23  	var failFunc func(message string, callerSkip ...int)
    24  	var skipFunc func(message string, callerSkip ...int)
    25  	var reportFunc func() types.SpecReport
    26  
    27  	var failFuncCall messagedCall
    28  	var skipFuncCall messagedCall
    29  	var offset int
    30  	var reportToReturn types.SpecReport
    31  	var buf *gbytes.Buffer
    32  
    33  	BeforeEach(func() {
    34  		failFuncCall = messagedCall{}
    35  		skipFuncCall = messagedCall{}
    36  		offset = 3
    37  		reportToReturn = types.SpecReport{}
    38  
    39  		failFunc = func(message string, callerSkip ...int) {
    40  			failFuncCall.message = message
    41  			failFuncCall.callerSkip = callerSkip
    42  		}
    43  
    44  		skipFunc = func(message string, callerSkip ...int) {
    45  			skipFuncCall.message = message
    46  			skipFuncCall.callerSkip = callerSkip
    47  		}
    48  
    49  		reportFunc = func() types.SpecReport {
    50  			return reportToReturn
    51  		}
    52  
    53  		buf = gbytes.NewBuffer()
    54  
    55  		t = testingtproxy.New(buf, failFunc, skipFunc, DeferCleanup, reportFunc, offset)
    56  	})
    57  
    58  	Describe("Cleanup", Ordered, func() {
    59  		var didCleanupAfter bool
    60  		It("supports cleanup", func() {
    61  			Ω(didCleanupAfter).Should(BeFalse())
    62  			t.Cleanup(func() {
    63  				didCleanupAfter = true
    64  			})
    65  		})
    66  
    67  		It("ran cleanup after the last test", func() {
    68  			Ω(didCleanupAfter).Should(BeTrue())
    69  		})
    70  	})
    71  
    72  	Describe("Setenv", func() {
    73  		Context("when the environment variable does not exist", Ordered, func() {
    74  			const key = "FLOOP_FLARP_WIBBLE_BLARP"
    75  
    76  			BeforeAll(func() {
    77  				os.Unsetenv(key)
    78  			})
    79  
    80  			It("sets the environment variable", func() {
    81  				t.Setenv(key, "HELLO")
    82  				Ω(os.Getenv(key)).Should(Equal("HELLO"))
    83  			})
    84  
    85  			It("cleans up after itself", func() {
    86  				_, exists := os.LookupEnv(key)
    87  				Ω(exists).Should(BeFalse())
    88  			})
    89  		})
    90  
    91  		Context("when the environment variable does exist", Ordered, func() {
    92  			const key = "FLOOP_FLARP_WIBBLE_BLARP"
    93  			const originalValue = "HOLA"
    94  
    95  			BeforeAll(func() {
    96  				os.Setenv(key, originalValue)
    97  			})
    98  
    99  			It("sets it", func() {
   100  				t.Setenv(key, "HELLO")
   101  				Ω(os.Getenv(key)).Should(Equal("HELLO"))
   102  			})
   103  
   104  			It("cleans up after itself", func() {
   105  				Ω(os.Getenv(key)).Should(Equal("HOLA"))
   106  			})
   107  
   108  			AfterAll(func() {
   109  				os.Unsetenv(key)
   110  			})
   111  		})
   112  	})
   113  
   114  	Describe("TempDir", Ordered, func() {
   115  		var tempDirA, tempDirB string
   116  
   117  		It("creates temporary directories", func() {
   118  			tempDirA = t.TempDir()
   119  			tempDirB = t.TempDir()
   120  			Ω(tempDirA).Should(BeADirectory())
   121  			Ω(tempDirB).Should(BeADirectory())
   122  			Ω(tempDirA).ShouldNot(Equal(tempDirB))
   123  		})
   124  
   125  		It("cleans up after itself", func() {
   126  			Ω(tempDirA).ShouldNot(BeADirectory())
   127  			Ω(tempDirB).ShouldNot(BeADirectory())
   128  		})
   129  	})
   130  
   131  	It("supports Error", func() {
   132  		t.Error("a", 17)
   133  		Ω(failFuncCall.message).Should(Equal("a 17\n"))
   134  		Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
   135  	})
   136  
   137  	It("supports Errorf", func() {
   138  		t.Errorf("%s %d!", "a", 17)
   139  		Ω(failFuncCall.message).Should(Equal("a 17!"))
   140  		Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
   141  	})
   142  
   143  	It("supports Fail", func() {
   144  		t.Fail()
   145  		Ω(failFuncCall.message).Should(Equal("failed"))
   146  		Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
   147  	})
   148  
   149  	It("supports FailNow", func() {
   150  		t.Fail()
   151  		Ω(failFuncCall.message).Should(Equal("failed"))
   152  		Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
   153  	})
   154  
   155  	It("supports Fatal", func() {
   156  		t.Fatal("a", 17)
   157  		Ω(failFuncCall.message).Should(Equal("a 17\n"))
   158  		Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
   159  	})
   160  
   161  	It("supports Fatalf", func() {
   162  		t.Fatalf("%s %d!", "a", 17)
   163  		Ω(failFuncCall.message).Should(Equal("a 17!"))
   164  		Ω(failFuncCall.callerSkip).Should(Equal([]int{offset}))
   165  	})
   166  
   167  	It("ignores Helper", func() {
   168  		GinkgoT().Helper() //is a no-op
   169  	})
   170  
   171  	It("supports Log", func() {
   172  		t.Log("a", 17)
   173  		Ω(string(buf.Contents())).Should(Equal("a 17\n"))
   174  	})
   175  
   176  	It("supports Logf", func() {
   177  		t.Logf("%s %d!", "a", 17)
   178  		Ω(string(buf.Contents())).Should(Equal("a 17!\n"))
   179  	})
   180  
   181  	It("supports Name", func() {
   182  		reportToReturn.ContainerHierarchyTexts = []string{"C.S."}
   183  		reportToReturn.LeafNodeText = "Lewis"
   184  		Ω(t.Name()).Should(Equal("C.S. Lewis"))
   185  		Ω(GinkgoT().Name()).Should(ContainSubstring("supports Name"))
   186  	})
   187  
   188  	It("ignores Parallel", func() {
   189  		GinkgoT().Parallel() //is a no-op
   190  	})
   191  
   192  	It("supports Skip", func() {
   193  		t.Skip("a", 17)
   194  		Ω(skipFuncCall.message).Should(Equal("a 17\n"))
   195  		Ω(skipFuncCall.callerSkip).Should(Equal([]int{offset}))
   196  	})
   197  
   198  	It("supports SkipNow", func() {
   199  		t.SkipNow()
   200  		Ω(skipFuncCall.message).Should(Equal("skip"))
   201  		Ω(skipFuncCall.callerSkip).Should(Equal([]int{offset}))
   202  	})
   203  
   204  	It("supports Skipf", func() {
   205  		t.Skipf("%s %d!", "a", 17)
   206  		Ω(skipFuncCall.message).Should(Equal("a 17!"))
   207  		Ω(skipFuncCall.callerSkip).Should(Equal([]int{offset}))
   208  	})
   209  
   210  	It("returns the state of the test when asked if it was skipped", func() {
   211  		reportToReturn.State = types.SpecStatePassed
   212  		Ω(t.Skipped()).Should(BeFalse())
   213  		reportToReturn.State = types.SpecStateSkipped
   214  		Ω(t.Skipped()).Should(BeTrue())
   215  	})
   216  })