...

Source file src/github.com/onsi/ginkgo/v2/integration/subcommand_test.go

Documentation: github.com/onsi/ginkgo/v2/integration

     1  package integration_test
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	. "github.com/onsi/ginkgo/v2"
     8  	"github.com/onsi/ginkgo/v2/types"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("Subcommand", func() {
    14  	Describe("ginkgo bootstrap", func() {
    15  		var pkg string
    16  
    17  		BeforeEach(func() {
    18  			pkg = "foo"
    19  			fm.MkEmpty(pkg)
    20  		})
    21  
    22  		It("should generate a bootstrap file, as long as one does not exist", func() {
    23  			session := startGinkgo(fm.PathTo(pkg), "bootstrap")
    24  			Eventually(session).Should(gexec.Exit(0))
    25  			output := session.Out.Contents()
    26  
    27  			Ω(output).Should(ContainSubstring("foo_suite_test.go"))
    28  
    29  			content := fm.ContentOf(pkg, "foo_suite_test.go")
    30  			Ω(content).Should(ContainSubstring("package foo_test"))
    31  			Ω(content).Should(ContainSubstring("func TestFoo(t *testing.T) {"))
    32  			Ω(content).Should(ContainSubstring("RegisterFailHandler"))
    33  			Ω(content).Should(ContainSubstring("RunSpecs"))
    34  
    35  			Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo/v2"`))
    36  			Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
    37  
    38  			session = startGinkgo(fm.PathTo(pkg))
    39  			Eventually(session).Should(gexec.Exit(0))
    40  
    41  			session = startGinkgo(fm.PathTo(pkg), "bootstrap")
    42  			Eventually(session).Should(gexec.Exit(1))
    43  			output = session.Err.Contents()
    44  			Ω(output).Should(ContainSubstring("foo_suite_test.go"))
    45  			Ω(output).Should(ContainSubstring("already exists"))
    46  		})
    47  
    48  		It("should generate a bootstrap file with a working package name if the folder starts with a numeral", func() {
    49  			fm.MkEmpty("7")
    50  			session := startGinkgo(fm.PathTo("7"), "bootstrap")
    51  			Eventually(session).Should(gexec.Exit(0))
    52  
    53  			content := fm.ContentOf("7", "7_suite_test.go")
    54  			pkg := strings.Split(content, "\n")[0]
    55  			Ω(pkg).Should(Equal("package seven_test"))
    56  
    57  			session = startGinkgo(fm.PathTo("7"))
    58  			Eventually(session).Should(gexec.Exit(0))
    59  		})
    60  
    61  		It("should import nodot declarations when told to", func() {
    62  			session := startGinkgo(fm.PathTo(pkg), "bootstrap", "--nodot")
    63  			Eventually(session).Should(gexec.Exit(0))
    64  			output := session.Out.Contents()
    65  
    66  			Ω(output).Should(ContainSubstring("foo_suite_test.go"))
    67  
    68  			content := fm.ContentOf(pkg, "foo_suite_test.go")
    69  			Ω(content).Should(ContainSubstring("package foo_test"))
    70  			Ω(content).Should(ContainSubstring("func TestFoo(t *testing.T) {"))
    71  			Ω(content).Should(ContainSubstring("gomega.RegisterFailHandler"))
    72  			Ω(content).Should(ContainSubstring("ginkgo.RunSpecs"))
    73  
    74  			Ω(content).Should(ContainSubstring("\t" + `"github.com/onsi/ginkgo/v2"`))
    75  			Ω(content).Should(ContainSubstring("\t" + `"github.com/onsi/gomega"`))
    76  
    77  			session = startGinkgo(fm.PathTo(pkg))
    78  			Eventually(session).Should(gexec.Exit(0))
    79  		})
    80  
    81  		It("should generate a bootstrap file using a template when told to", func() {
    82  			fm.WriteFile(pkg, ".bootstrap", `package {{.Package}}
    83  
    84  			import (
    85  				{{.GinkgoImport}}
    86  				{{.GomegaImport}}
    87  
    88  				"testing"
    89  				"binary"
    90  			)
    91  
    92  			func Test{{.FormattedName}}(t *testing.T) {
    93  				// This is a {{.Package}} test
    94  			}`)
    95  			session := startGinkgo(fm.PathTo(pkg), "bootstrap", "--template", ".bootstrap")
    96  			Eventually(session).Should(gexec.Exit(0))
    97  			output := session.Out.Contents()
    98  
    99  			Ω(output).Should(ContainSubstring("foo_suite_test.go"))
   100  
   101  			content := fm.ContentOf(pkg, "foo_suite_test.go")
   102  			Ω(content).Should(ContainSubstring("package foo_test"))
   103  			Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo/v2"`))
   104  			Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
   105  			Ω(content).Should(ContainSubstring(`"binary"`))
   106  			Ω(content).Should(ContainSubstring("// This is a foo_test test"))
   107  		})
   108  
   109  		It("should generate a bootstrap file using a template that contains functions when told to", func() {
   110  			fm.WriteFile(pkg, ".bootstrap", `package {{.Package}}
   111  
   112  			import (
   113  				{{.GinkgoImport}}
   114  				{{.GomegaImport}}
   115  
   116  				"testing"
   117  				"binary"
   118  			)
   119  
   120  			func Test{{.FormattedName}}(t *testing.T) {
   121  				// This is a {{.Package | repeat 3}} test
   122  			}`)
   123  			session := startGinkgo(fm.PathTo(pkg), "bootstrap", "--template", ".bootstrap")
   124  			Eventually(session).Should(gexec.Exit(0))
   125  			output := session.Out.Contents()
   126  
   127  			Ω(output).Should(ContainSubstring("foo_suite_test.go"))
   128  
   129  			content := fm.ContentOf(pkg, "foo_suite_test.go")
   130  			Ω(content).Should(ContainSubstring("package foo_test"))
   131  			Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo/v2"`))
   132  			Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
   133  			Ω(content).Should(ContainSubstring(`"binary"`))
   134  			Ω(content).Should(ContainSubstring("// This is a foo_testfoo_testfoo_test test"))
   135  		})
   136  
   137  		It("should generate a bootstrap file using a template and custom template data when told to", func() {
   138  			fm.WriteFile(pkg, ".bootstrap", `package {{.Package}}
   139  
   140  			import (
   141  				{{.GinkgoImport}}
   142  				{{.GomegaImport}}
   143  
   144  				"testing"
   145  				"binary"
   146  			)
   147  
   148  			func Test{{.FormattedName}}(t *testing.T) {
   149  				// This is a {{.Package | repeat 3}} test
   150  				// This is a custom data {{.CustomData.suitename}} test
   151  			}`)
   152  			fm.WriteFile(pkg, "custom.json", `{"suitename": "integration"}`)
   153  			session := startGinkgo(fm.PathTo(pkg), "bootstrap", "--template", ".bootstrap", "--template-data", "custom.json")
   154  			Eventually(session).Should(gexec.Exit(0))
   155  			output := session.Out.Contents()
   156  
   157  			Ω(output).Should(ContainSubstring("foo_suite_test.go"))
   158  
   159  			content := fm.ContentOf(pkg, "foo_suite_test.go")
   160  			Ω(content).Should(ContainSubstring("package foo_test"))
   161  			Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo/v2"`))
   162  			Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
   163  			Ω(content).Should(ContainSubstring(`"binary"`))
   164  			Ω(content).Should(ContainSubstring("// This is a foo_testfoo_testfoo_test test"))
   165  			Ω(content).Should(ContainSubstring("// This is a custom data integration test"))
   166  		})
   167  
   168  		It("should fail to render a bootstrap file using a template and custom template data when accessing a missing key", func() {
   169  			fm.WriteFile(pkg, ".bootstrap", `package {{.Package}}
   170  
   171  			import (
   172  				{{.GinkgoImport}}
   173  				{{.GomegaImport}}
   174  
   175  				"testing"
   176  				"binary"
   177  			)
   178  
   179  			func Test{{.FormattedName}}(t *testing.T) {
   180  				// This is a {{.Package | repeat 3}} test
   181  				// This is a custom data {{.CustomData.component}} test
   182  			}`)
   183  			fm.WriteFile(pkg, "custom.json", `{"suitename": "integration"}`)
   184  			session := startGinkgo(fm.PathTo(pkg), "bootstrap", "--template", ".bootstrap", "--template-data", "custom.json")
   185  			Eventually(session).Should(gexec.Exit(1))
   186  			output := string(session.Err.Contents())
   187  
   188  			Ω(output).Should(ContainSubstring(`executing "bootstrap" at <.CustomData.component>: map has no entry for key "component"`))
   189  
   190  		})
   191  
   192  		It("should fail to render a bootstrap file using a template and custom template data when data is invalid JSON", func() {
   193  			fm.WriteFile(pkg, ".bootstrap", `package {{.Package}}
   194  
   195  			import (
   196  				{{.GinkgoImport}}
   197  				{{.GomegaImport}}
   198  
   199  				"testing"
   200  				"binary"
   201  			)
   202  
   203  			func Test{{.FormattedName}}(t *testing.T) {
   204  				// This is a {{.Package | repeat 3}} test
   205  				// This is a custom data {{.CustomData.component}} test
   206  			}`)
   207  			fm.WriteFile(pkg, "custom.json", `{'suitename': 'integration']`)
   208  			session := startGinkgo(fm.PathTo(pkg), "bootstrap", "--template", ".bootstrap", "--template-data", "custom.json")
   209  			Eventually(session).Should(gexec.Exit(1))
   210  			output := string(session.Err.Contents())
   211  
   212  			Ω(output).Should(ContainSubstring(`Invalid JSON object in custom data file.`))
   213  
   214  		})
   215  	})
   216  
   217  	Describe("ginkgo generate", func() {
   218  		var pkg string
   219  
   220  		BeforeEach(func() {
   221  			pkg = "foo_bar"
   222  			fm.MkEmpty(pkg)
   223  			Eventually(startGinkgo(fm.PathTo(pkg), "bootstrap")).Should(gexec.Exit(0))
   224  		})
   225  
   226  		Context("with no arguments", func() {
   227  			It("should generate a test file named after the package", func() {
   228  				session := startGinkgo(fm.PathTo(pkg), "generate")
   229  				Eventually(session).Should(gexec.Exit(0))
   230  				output := session.Out.Contents()
   231  
   232  				Ω(output).Should(ContainSubstring("foo_bar_test.go"))
   233  
   234  				By("having the correct content")
   235  				content := fm.ContentOf(pkg, "foo_bar_test.go")
   236  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   237  				Ω(content).Should(ContainSubstring(`var _ = Describe("FooBar", func() {`))
   238  				Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo/v2"`))
   239  				Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
   240  
   241  				By("compiling correctly (we append to the file to make sure gomega is used)")
   242  				fm.WriteFile(pkg, "foo_bar.go", "package foo_bar\nvar TRUE=true\n")
   243  				fm.AppendToFile(pkg, "foo_bar_test.go", strings.Join([]string{``,
   244  					`var _ = It("works", func() {`,
   245  					`    Expect(foo_bar.TRUE).To(BeTrue())`,
   246  					`})`,
   247  				}, "\n"))
   248  				Eventually(startGinkgo(fm.PathTo(pkg))).Should(gexec.Exit(0))
   249  
   250  				By("refusing to overwrite the file if generate is called again")
   251  				session = startGinkgo(fm.PathTo(pkg), "generate")
   252  				Eventually(session).Should(gexec.Exit(1))
   253  				output = session.Err.Contents()
   254  
   255  				Ω(output).Should(ContainSubstring("foo_bar_test.go"))
   256  				Ω(output).Should(ContainSubstring("already exists"))
   257  			})
   258  		})
   259  
   260  		Context("with template argument", func() {
   261  			It("should generate a test file using a template", func() {
   262  				fm.WriteFile(pkg, ".generate", `package {{.Package}}
   263  				import (
   264  					{{.GinkgoImport}}
   265  					{{.GomegaImport}}
   266  
   267  					{{if .ImportPackage}}"{{.PackageImportPath}}"{{end}}
   268  				)
   269  
   270  				var _ = Describe("{{.Subject}}", func() {
   271  					// This is a {{.Package}} test
   272  				})`)
   273  				session := startGinkgo(fm.PathTo(pkg), "generate", "--template", ".generate")
   274  				Eventually(session).Should(gexec.Exit(0))
   275  				output := session.Out.Contents()
   276  
   277  				Ω(output).Should(ContainSubstring("foo_bar_test.go"))
   278  
   279  				content := fm.ContentOf(pkg, "foo_bar_test.go")
   280  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   281  				Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo/v2"`))
   282  				Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
   283  				Ω(content).Should(ContainSubstring(`/foo_bar"`))
   284  				Ω(content).Should(ContainSubstring("// This is a foo_bar_test test"))
   285  			})
   286  
   287  			It("should generate a test file using a template that contains functions", func() {
   288  				fm.WriteFile(pkg, ".generate", `package {{.Package}}
   289  				import (
   290  					{{.GinkgoImport}}
   291  					{{.GomegaImport}}
   292  
   293  					{{if .ImportPackage}}"{{.PackageImportPath}}"{{end}}
   294  				)
   295  
   296  				var _ = Describe("{{.Subject}}", func() {
   297  					// This is a {{.Package | repeat 3 }} test
   298  				})`)
   299  				session := startGinkgo(fm.PathTo(pkg), "generate", "--template", ".generate")
   300  				Eventually(session).Should(gexec.Exit(0))
   301  				output := session.Out.Contents()
   302  
   303  				Ω(output).Should(ContainSubstring("foo_bar_test.go"))
   304  
   305  				content := fm.ContentOf(pkg, "foo_bar_test.go")
   306  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   307  				Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo/v2"`))
   308  				Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
   309  				Ω(content).Should(ContainSubstring(`/foo_bar"`))
   310  				Ω(content).Should(ContainSubstring("// This is a foo_bar_testfoo_bar_testfoo_bar_test test"))
   311  			})
   312  
   313  			It("should generate a test file using a template and custom data when told to", func() {
   314  				fm.WriteFile(pkg, ".generate", `package {{.Package}}
   315  				import (
   316  					{{.GinkgoImport}}
   317  					{{.GomegaImport}}
   318  
   319  					{{if .ImportPackage}}"{{.PackageImportPath}}"{{end}}
   320  				)
   321  
   322  				var _ = Describe("{{.Subject}}", Label("{{.CustomData.label}}"), func() {
   323  					// This is a {{.Package | repeat 3 }} test
   324  				})`)
   325  				fm.WriteFile(pkg, "custom_spec.json", `{"label": "integration"}`)
   326  				session := startGinkgo(fm.PathTo(pkg), "generate", "--template", ".generate", "--template-data", "custom_spec.json")
   327  				Eventually(session).Should(gexec.Exit(0))
   328  				output := session.Out.Contents()
   329  
   330  				Ω(output).Should(ContainSubstring("foo_bar_test.go"))
   331  
   332  				content := fm.ContentOf(pkg, "foo_bar_test.go")
   333  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   334  				Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo/v2"`))
   335  				Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
   336  				Ω(content).Should(ContainSubstring(`/foo_bar"`))
   337  				Ω(content).Should(ContainSubstring("// This is a foo_bar_testfoo_bar_testfoo_bar_test test"))
   338  				Ω(content).Should(ContainSubstring(`Label("integration")`))
   339  			})
   340  
   341  			It("should fail to render a test file using a template and custom template data when accessing a missing key", func() {
   342  				fm.WriteFile(pkg, ".generate", `package {{.Package}}
   343  				import (
   344  					{{.GinkgoImport}}
   345  					{{.GomegaImport}}
   346  
   347  					{{if .ImportPackage}}"{{.PackageImportPath}}"{{end}}
   348  				)
   349  
   350  				var _ = Describe("{{.Subject}}", Label("{{.CustomData.component}}"), func() {
   351  					// This is a {{.Package | repeat 3 }} test
   352  				})`)
   353  				fm.WriteFile(pkg, "custom.json", `{"label": "integration"}`)
   354  				session := startGinkgo(fm.PathTo(pkg), "generate", "--template", ".generate", "--template-data", "custom.json")
   355  				Eventually(session).Should(gexec.Exit(1))
   356  				output := string(session.Err.Contents())
   357  
   358  				Ω(output).Should(ContainSubstring(`executing "spec" at <.CustomData.component>: map has no entry for key "component"`))
   359  
   360  			})
   361  
   362  			It("should fail to render a test file using a template and custom template data when data is invalid JSON", func() {
   363  				fm.WriteFile(pkg, ".generate", `package {{.Package}}
   364  				import (
   365  					{{.GinkgoImport}}
   366  					{{.GomegaImport}}
   367  
   368  					{{if .ImportPackage}}"{{.PackageImportPath}}"{{end}}
   369  				)
   370  
   371  				var _ = Describe("{{.Subject}}", Label("{{.CustomData.label}}"), func() {
   372  					// This is a {{.Package | repeat 3 }} test
   373  				})`)
   374  				fm.WriteFile(pkg, "custom.json", `{'label': 'integration']`)
   375  				session := startGinkgo(fm.PathTo(pkg), "generate", "--template", ".generate", "--template-data", "custom.json")
   376  				Eventually(session).Should(gexec.Exit(1))
   377  				output := string(session.Err.Contents())
   378  
   379  				Ω(output).Should(ContainSubstring(`Invalid JSON object in custom data file.`))
   380  
   381  			})
   382  
   383  		})
   384  
   385  		Context("with an argument of the form: foo", func() {
   386  			It("should generate a test file named after the argument", func() {
   387  				session := startGinkgo(fm.PathTo(pkg), "generate", "baz_buzz")
   388  				Eventually(session).Should(gexec.Exit(0))
   389  				output := session.Out.Contents()
   390  
   391  				Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
   392  
   393  				content := fm.ContentOf(pkg, "baz_buzz_test.go")
   394  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   395  				Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
   396  			})
   397  		})
   398  
   399  		Context("with an argument of the form: foo.go", func() {
   400  			It("should generate a test file named after the argument", func() {
   401  				session := startGinkgo(fm.PathTo(pkg), "generate", "baz_buzz.go")
   402  				Eventually(session).Should(gexec.Exit(0))
   403  				output := session.Out.Contents()
   404  
   405  				Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
   406  
   407  				content := fm.ContentOf(pkg, "baz_buzz_test.go")
   408  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   409  				Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
   410  
   411  			})
   412  		})
   413  
   414  		Context("with an argument of the form: foo_test", func() {
   415  			It("should generate a test file named after the argument", func() {
   416  				session := startGinkgo(fm.PathTo(pkg), "generate", "baz_buzz_test")
   417  				Eventually(session).Should(gexec.Exit(0))
   418  				output := session.Out.Contents()
   419  
   420  				Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
   421  
   422  				content := fm.ContentOf(pkg, "baz_buzz_test.go")
   423  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   424  				Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
   425  			})
   426  		})
   427  
   428  		Context("with an argument of the form: foo-test", func() {
   429  			It("should generate a test file named after the argument", func() {
   430  				session := startGinkgo(fm.PathTo(pkg), "generate", "baz-buzz-test")
   431  				Eventually(session).Should(gexec.Exit(0))
   432  				output := session.Out.Contents()
   433  
   434  				Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
   435  
   436  				content := fm.ContentOf(pkg, "baz_buzz_test.go")
   437  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   438  				Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
   439  			})
   440  		})
   441  
   442  		Context("with an argument of the form: foo_test.go", func() {
   443  			It("should generate a test file named after the argument", func() {
   444  				session := startGinkgo(fm.PathTo(pkg), "generate", "baz_buzz_test.go")
   445  				Eventually(session).Should(gexec.Exit(0))
   446  				output := session.Out.Contents()
   447  
   448  				Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
   449  
   450  				content := fm.ContentOf(pkg, "baz_buzz_test.go")
   451  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   452  				Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
   453  			})
   454  		})
   455  
   456  		Context("with multiple arguments", func() {
   457  			It("should generate a test file named after the argument", func() {
   458  				session := startGinkgo(fm.PathTo(pkg), "generate", "baz", "buzz")
   459  				Eventually(session).Should(gexec.Exit(0))
   460  				output := session.Out.Contents()
   461  
   462  				Ω(output).Should(ContainSubstring("baz_test.go"))
   463  				Ω(output).Should(ContainSubstring("buzz_test.go"))
   464  
   465  				content := fm.ContentOf(pkg, "baz_test.go")
   466  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   467  				Ω(content).Should(ContainSubstring(`var _ = Describe("Baz", func() {`))
   468  
   469  				content = fm.ContentOf(pkg, "buzz_test.go")
   470  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   471  				Ω(content).Should(ContainSubstring(`var _ = Describe("Buzz", func() {`))
   472  			})
   473  		})
   474  
   475  		Context("with nodot", func() {
   476  			It("should not import ginkgo or gomega", func() {
   477  				session := startGinkgo(fm.PathTo(pkg), "generate", "--nodot")
   478  				Eventually(session).Should(gexec.Exit(0))
   479  				output := session.Out.Contents()
   480  
   481  				Ω(output).Should(ContainSubstring("foo_bar_test.go"))
   482  
   483  				content := fm.ContentOf(pkg, "foo_bar_test.go")
   484  				Ω(content).Should(ContainSubstring("package foo_bar_test"))
   485  				Ω(content).ShouldNot(ContainSubstring("\t" + `. "github.com/onsi/ginkgo/v2"`))
   486  				Ω(content).ShouldNot(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
   487  				Ω(content).Should(ContainSubstring("\t" + `"github.com/onsi/ginkgo/v2"`))
   488  				Ω(content).Should(ContainSubstring("\t" + `"github.com/onsi/gomega"`))
   489  
   490  				By("compiling correctly (we append to the file to make sure gomega is used)")
   491  				fm.WriteFile(pkg, "foo_bar.go", "package foo_bar\nvar TRUE=true\n")
   492  				fm.AppendToFile(pkg, "foo_bar_test.go", strings.Join([]string{``,
   493  					`var _ = ginkgo.It("works", func() {`,
   494  					`    gomega.Expect(foo_bar.TRUE).To(gomega.BeTrue())`,
   495  					`})`,
   496  				}, "\n"))
   497  				Eventually(startGinkgo(fm.PathTo(pkg))).Should(gexec.Exit(0))
   498  			})
   499  		})
   500  	})
   501  
   502  	Describe("ginkgo bootstrap/generate", func() {
   503  		var pkg string
   504  		BeforeEach(func() {
   505  			pkg = "some-crazy-thing"
   506  			fm.MkEmpty(pkg)
   507  		})
   508  
   509  		Context("when the working directory is empty", func() {
   510  			It("generates correctly named bootstrap and generate files with a package name derived from the directory", func() {
   511  				session := startGinkgo(fm.PathTo(pkg), "bootstrap")
   512  				Eventually(session).Should(gexec.Exit(0))
   513  
   514  				content := fm.ContentOf(pkg, "some_crazy_thing_suite_test.go")
   515  				Ω(content).Should(ContainSubstring("package some_crazy_thing_test"))
   516  				Ω(content).Should(ContainSubstring("SomeCrazyThing Suite"))
   517  
   518  				session = startGinkgo(fm.PathTo(pkg), "generate")
   519  				Eventually(session).Should(gexec.Exit(0))
   520  
   521  				content = fm.ContentOf(pkg, "some_crazy_thing_test.go")
   522  				Ω(content).Should(ContainSubstring("package some_crazy_thing_test"))
   523  				Ω(content).Should(ContainSubstring("SomeCrazyThing"))
   524  			})
   525  		})
   526  
   527  		Context("when the working directory contains a file with a package name", func() {
   528  			BeforeEach(func() {
   529  				fm.WriteFile(pkg, "foo.go", "package main\n\nfunc main() {}")
   530  			})
   531  
   532  			It("generates correctly named bootstrap and generate files with the package name", func() {
   533  				session := startGinkgo(fm.PathTo(pkg), "bootstrap")
   534  				Eventually(session).Should(gexec.Exit(0))
   535  
   536  				content := fm.ContentOf(pkg, "some_crazy_thing_suite_test.go")
   537  				Ω(content).Should(ContainSubstring("package main_test"))
   538  				Ω(content).Should(ContainSubstring("SomeCrazyThing Suite"))
   539  
   540  				session = startGinkgo(fm.PathTo(pkg), "generate")
   541  				Eventually(session).Should(gexec.Exit(0))
   542  
   543  				content = fm.ContentOf(pkg, "some_crazy_thing_test.go")
   544  				Ω(content).Should(ContainSubstring("package main_test"))
   545  				Ω(content).Should(ContainSubstring("SomeCrazyThing"))
   546  			})
   547  		})
   548  	})
   549  
   550  	Describe("Go module and ginkgo bootstrap/generate", func() {
   551  		var (
   552  			pkg         string
   553  			savedGoPath string
   554  		)
   555  
   556  		BeforeEach(func() {
   557  			pkg = "myamazingmodule"
   558  			fm.MkEmpty(pkg)
   559  			fm.WriteFile(pkg, "go.mod", "module fake.com/me/myamazingmodule\n")
   560  			savedGoPath = os.Getenv("GOPATH")
   561  			Expect(os.Setenv("GOPATH", "")).To(Succeed())
   562  			Expect(os.Setenv("GO111MODULE", "on")).To(Succeed()) // needed pre-Go 1.13
   563  		})
   564  
   565  		AfterEach(func() {
   566  			Expect(os.Setenv("GOPATH", savedGoPath)).To(Succeed())
   567  			Expect(os.Setenv("GO111MODULE", "")).To(Succeed())
   568  		})
   569  
   570  		It("generates correctly named bootstrap and generate files with the module name", func() {
   571  			session := startGinkgo(fm.PathTo(pkg), "bootstrap")
   572  			Eventually(session).Should(gexec.Exit(0))
   573  
   574  			content := fm.ContentOf(pkg, "myamazingmodule_suite_test.go")
   575  			Expect(content).To(ContainSubstring("package myamazingmodule_test"), string(content))
   576  			Expect(content).To(ContainSubstring("Myamazingmodule Suite"), string(content))
   577  
   578  			session = startGinkgo(fm.PathTo(pkg), "generate")
   579  			Eventually(session).Should(gexec.Exit(0))
   580  
   581  			content = fm.ContentOf(pkg, "myamazingmodule_test.go")
   582  			Expect(content).To(ContainSubstring("package myamazingmodule_test"), string(content))
   583  			Expect(content).To(ContainSubstring("fake.com/me/myamazingmodule"), string(content))
   584  			Expect(content).To(ContainSubstring("Myamazingmodule"), string(content))
   585  		})
   586  	})
   587  
   588  	Describe("ginkgo unfocus", func() {
   589  		It("should unfocus tests", Label("slow"), func() {
   590  			fm.MountFixture("focused")
   591  
   592  			session := startGinkgo(fm.PathTo("focused"), "--no-color", "-r")
   593  			Eventually(session).Should(gexec.Exit(types.GINKGO_FOCUS_EXIT_CODE))
   594  			output := session.Out.Contents()
   595  
   596  			Ω(string(output)).Should(ContainSubstring("Detected Programmatic Focus"))
   597  
   598  			session = startGinkgo(fm.PathTo("focused"), "unfocus")
   599  			Eventually(session).Should(gexec.Exit(0))
   600  			output = session.Out.Contents()
   601  			Ω(string(output)).ShouldNot(ContainSubstring("expected 'package'"))
   602  
   603  			session = startGinkgo(fm.PathTo("focused"), "--no-color", "-r")
   604  			Eventually(session).Should(gexec.Exit(0))
   605  			output = session.Out.Contents()
   606  			Ω(string(output)).Should(ContainSubstring("Ginkgo ran 2 suites"))
   607  			Ω(string(output)).Should(ContainSubstring("Test Suite Passed"))
   608  			Ω(string(output)).ShouldNot(ContainSubstring("Detected Programmatic Focus"))
   609  
   610  			original := fm.ContentOfFixture("focused", "README.md")
   611  			updated := fm.ContentOf("focused", "README.md")
   612  			Ω(original).Should(Equal(updated))
   613  		})
   614  
   615  		It("should ignore the 'vendor' folder", func() {
   616  			fm.MountFixture("focused_with_vendor")
   617  
   618  			session := startGinkgo(fm.PathTo("focused_with_vendor"), "unfocus")
   619  			Eventually(session).Should(gexec.Exit(0))
   620  
   621  			session = startGinkgo(fm.PathTo("focused_with_vendor"), "--no-color")
   622  			Eventually(session).Should(gexec.Exit(0))
   623  			output := session.Out.Contents()
   624  			Expect(string(output)).To(ContainSubstring("11 Passed"))
   625  			Expect(string(output)).To(ContainSubstring("0 Skipped"))
   626  
   627  			originalVendorPath := fm.PathToFixtureFile("focused_with_vendor", "vendor")
   628  			updatedVendorPath := fm.PathTo("focused_with_vendor", "vendor")
   629  
   630  			Expect(sameFolder(originalVendorPath, updatedVendorPath)).To(BeTrue())
   631  		})
   632  	})
   633  
   634  	Describe("ginkgo version", func() {
   635  		It("should print out the version info", func() {
   636  			session := startGinkgo("", "version")
   637  			Eventually(session).Should(gexec.Exit(0))
   638  			output := session.Out.Contents()
   639  
   640  			Ω(output).Should(MatchRegexp(`Ginkgo Version \d+\.\d+\.\d+`))
   641  		})
   642  	})
   643  
   644  	Describe("ginkgo help", func() {
   645  		It("should print out usage information", func() {
   646  			session := startGinkgo("", "help")
   647  			Eventually(session).Should(gexec.Exit(0))
   648  			output := string(session.Out.Contents())
   649  
   650  			Ω(output).Should(MatchRegexp(`Ginkgo Version \d+\.\d+\.\d+`))
   651  			Ω(output).Should(ContainSubstring("watch"))
   652  			Ω(output).Should(ContainSubstring("generate"))
   653  			Ω(output).Should(ContainSubstring("run"))
   654  		})
   655  
   656  		It("should print out usage information for subcommands", func() {
   657  			session := startGinkgo("", "help", "run")
   658  			Eventually(session).Should(gexec.Exit(0))
   659  			output := string(session.Out.Contents())
   660  
   661  			Ω(output).Should(ContainSubstring("-succinct"))
   662  			Ω(output).Should(ContainSubstring("-procs"))
   663  		})
   664  	})
   665  })
   666  

View as plain text