github.com/ManabuSeki/goa-v1@v1.4.3/goagen/codegen/workspace_test.go (about)

     1  package codegen_test
     2  
     3  import (
     4  	"fmt"
     5  	"go/build"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/goadesign/goa/goagen/codegen"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  func abs(elems ...string) string {
    17  	r, err := filepath.Abs(filepath.Join(append([]string{""}, elems...)...))
    18  	if err != nil {
    19  		panic("abs: " + err.Error())
    20  	}
    21  	return r
    22  }
    23  
    24  var _ = Describe("Workspace", func() {
    25  	Describe("WorkspaceFor", func() {
    26  		oldGOPATH := build.Default.GOPATH
    27  		xx := abs("xx")
    28  		BeforeEach(func() {
    29  			os.Setenv("GOPATH", xx)
    30  		})
    31  		AfterEach(func() {
    32  			os.Setenv("GOPATH", oldGOPATH)
    33  		})
    34  
    35  		var (
    36  			err    error
    37  			gopath string
    38  		)
    39  		Context("with GOMOD", func() {
    40  			var (
    41  				f *os.File
    42  			)
    43  			BeforeEach(func() {
    44  				f, err = os.OpenFile("go.mod", os.O_CREATE, 0755)
    45  				Ω(err).ShouldNot(HaveOccurred())
    46  				Ω(f.Close()).ShouldNot(HaveOccurred())
    47  			})
    48  			AfterEach(func() {
    49  				Ω(os.RemoveAll("go.mod")).ShouldNot(HaveOccurred())
    50  			})
    51  
    52  			Context("with GO111MODULE=auto", func() {
    53  				oldGO111MODULE := os.Getenv("GO111MODULE")
    54  				BeforeEach(func() {
    55  					os.Unsetenv("GO111MODULE")
    56  				})
    57  				AfterEach(func() {
    58  					os.Setenv("GO111MODULE", oldGO111MODULE)
    59  				})
    60  
    61  				Context("inside GOPATH", func() {
    62  					It("should return a GOPATH mode workspace", func() {
    63  						workspace, err := codegen.WorkspaceFor(abs("", "xx", "bar", "xx", "42"))
    64  						Ω(err).ShouldNot(HaveOccurred())
    65  						Expect(workspace.Path).To(Equal(xx))
    66  					})
    67  				})
    68  
    69  				Context("outside GOPATH", func() {
    70  					BeforeEach(func() {
    71  						gopath, err = ioutil.TempDir(".", "go")
    72  						Ω(err).ShouldNot(HaveOccurred())
    73  						os.Setenv("GOPATH", gopath)
    74  					})
    75  					AfterEach(func() {
    76  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
    77  					})
    78  
    79  					It("should return a Module mode workspace", func() {
    80  						abs, err := filepath.Abs(".")
    81  						Ω(err).ShouldNot(HaveOccurred())
    82  						workspace, err := codegen.WorkspaceFor(abs)
    83  						Ω(err).ShouldNot(HaveOccurred())
    84  						Expect(workspace.Path).To(Equal(abs))
    85  					})
    86  				})
    87  			})
    88  
    89  			Context("with GO111MODULE=on", func() {
    90  				oldGO111MODULE := os.Getenv("GO111MODULE")
    91  				BeforeEach(func() {
    92  					os.Setenv("GO111MODULE", "on")
    93  				})
    94  				AfterEach(func() {
    95  					os.Setenv("GO111MODULE", oldGO111MODULE)
    96  				})
    97  
    98  				Context("inside GOPATH", func() {
    99  					It("should return a Module mode workspace", func() {
   100  						abs, err := filepath.Abs(".")
   101  						Ω(err).ShouldNot(HaveOccurred())
   102  						workspace, err := codegen.WorkspaceFor(abs)
   103  						Ω(err).ShouldNot(HaveOccurred())
   104  						Expect(workspace.Path).To(Equal(abs))
   105  					})
   106  				})
   107  
   108  				Context("outside GOPATH", func() {
   109  					BeforeEach(func() {
   110  						gopath, err = ioutil.TempDir(".", "go")
   111  						Ω(err).ShouldNot(HaveOccurred())
   112  						os.Setenv("GOPATH", gopath)
   113  					})
   114  					AfterEach(func() {
   115  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   116  					})
   117  
   118  					It("should return a Module mode workspace", func() {
   119  						abs, err := filepath.Abs(".")
   120  						Ω(err).ShouldNot(HaveOccurred())
   121  						workspace, err := codegen.WorkspaceFor(abs)
   122  						Ω(err).ShouldNot(HaveOccurred())
   123  						Expect(workspace.Path).To(Equal(abs))
   124  					})
   125  				})
   126  			})
   127  
   128  			Context("with GO111MODULE=off", func() {
   129  				oldGO111MODULE := os.Getenv("GO111MODULE")
   130  				BeforeEach(func() {
   131  					os.Setenv("GO111MODULE", "off")
   132  				})
   133  				AfterEach(func() {
   134  					os.Setenv("GO111MODULE", oldGO111MODULE)
   135  				})
   136  
   137  				Context("inside GOPATH", func() {
   138  					It("should return a GOPATH mode workspace", func() {
   139  						workspace, err := codegen.WorkspaceFor(abs("", "xx", "bar", "xx", "42"))
   140  						Ω(err).ShouldNot(HaveOccurred())
   141  						Expect(workspace.Path).To(Equal(xx))
   142  					})
   143  				})
   144  
   145  				Context("outside GOPATH", func() {
   146  					BeforeEach(func() {
   147  						gopath, err = ioutil.TempDir(".", "go")
   148  						Ω(err).ShouldNot(HaveOccurred())
   149  						os.Setenv("GOPATH", gopath)
   150  					})
   151  					AfterEach(func() {
   152  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   153  					})
   154  
   155  					It("should return an error", func() {
   156  						_, err := codegen.WorkspaceFor(abs("", "bar", "xx", "42"))
   157  						Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("", "bar", "xx", "42"), gopath)))
   158  					})
   159  				})
   160  			})
   161  		})
   162  
   163  		Context("with no GOMOD", func() {
   164  			Context("with GO111MODULE=auto", func() {
   165  				oldGO111MODULE := os.Getenv("GO111MODULE")
   166  				BeforeEach(func() {
   167  					os.Unsetenv("GO111MODULE")
   168  				})
   169  				AfterEach(func() {
   170  					os.Setenv("GO111MODULE", oldGO111MODULE)
   171  				})
   172  
   173  				Context("inside GOPATH", func() {
   174  					It("should return a GOPATH mode workspace", func() {
   175  						workspace, err := codegen.WorkspaceFor(abs("xx", "bar", "xx", "42"))
   176  						Ω(err).ShouldNot(HaveOccurred())
   177  						Expect(workspace.Path).To(Equal(abs("xx")))
   178  					})
   179  				})
   180  
   181  				Context("outside GOPATH", func() {
   182  					BeforeEach(func() {
   183  						gopath, err = ioutil.TempDir(".", "go")
   184  						Ω(err).ShouldNot(HaveOccurred())
   185  						os.Setenv("GOPATH", gopath)
   186  					})
   187  					AfterEach(func() {
   188  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   189  					})
   190  
   191  					It("should return an error", func() {
   192  						_, err := codegen.WorkspaceFor(abs("bar", "xx", "42"))
   193  						Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), gopath)))
   194  					})
   195  				})
   196  			})
   197  
   198  			Context("with GO111MODULE=on", func() {
   199  				oldGO111MODULE := os.Getenv("GO111MODULE")
   200  				BeforeEach(func() {
   201  					os.Setenv("GO111MODULE", "on")
   202  				})
   203  				AfterEach(func() {
   204  					os.Setenv("GO111MODULE", oldGO111MODULE)
   205  				})
   206  
   207  				Context("inside GOPATH", func() {
   208  					It("should return an error", func() {
   209  						_, err := codegen.WorkspaceFor(abs("bar", "xx", "42"))
   210  						Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), abs("xx"))))
   211  					})
   212  				})
   213  
   214  				Context("outside GOPATH", func() {
   215  					BeforeEach(func() {
   216  						gopath, err = ioutil.TempDir(".", "go")
   217  						Ω(err).ShouldNot(HaveOccurred())
   218  						os.Setenv("GOPATH", gopath)
   219  					})
   220  					AfterEach(func() {
   221  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   222  					})
   223  
   224  					It("should return an error", func() {
   225  						_, err := codegen.WorkspaceFor(abs("bar", "xx", "42"))
   226  						Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), gopath)))
   227  					})
   228  				})
   229  			})
   230  
   231  			Context("with GO111MODULE=off", func() {
   232  				oldGO111MODULE := os.Getenv("GO111MODULE")
   233  				BeforeEach(func() {
   234  					os.Setenv("GO111MODULE", "off")
   235  				})
   236  				AfterEach(func() {
   237  					os.Setenv("GO111MODULE", oldGO111MODULE)
   238  				})
   239  
   240  				Context("inside GOPATH", func() {
   241  					It("should return a GOPATH mode workspace", func() {
   242  						workspace, err := codegen.WorkspaceFor(abs("xx", "bar", "xx", "42"))
   243  						Ω(err).ShouldNot(HaveOccurred())
   244  						Expect(workspace.Path).To(Equal(abs("xx")))
   245  					})
   246  				})
   247  
   248  				Context("outside GOPATH", func() {
   249  					BeforeEach(func() {
   250  						gopath, err = ioutil.TempDir(".", "go")
   251  						Ω(err).ShouldNot(HaveOccurred())
   252  						os.Setenv("GOPATH", gopath)
   253  					})
   254  					AfterEach(func() {
   255  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   256  					})
   257  
   258  					It("should return an error", func() {
   259  						_, err := codegen.WorkspaceFor(abs("bar", "xx", "42"))
   260  						Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), gopath)))
   261  					})
   262  				})
   263  			})
   264  		})
   265  	})
   266  
   267  	Describe("PackageFor", func() {
   268  		oldGOPATH := build.Default.GOPATH
   269  		BeforeEach(func() {
   270  			os.Setenv("GOPATH", abs("xx"))
   271  		})
   272  		AfterEach(func() {
   273  			os.Setenv("GOPATH", oldGOPATH)
   274  		})
   275  
   276  		var (
   277  			err    error
   278  			gopath string
   279  		)
   280  		Context("with GOMOD", func() {
   281  			var (
   282  				f *os.File
   283  			)
   284  			BeforeEach(func() {
   285  				f, err = os.OpenFile("go.mod", os.O_CREATE, 0755)
   286  				Ω(err).ShouldNot(HaveOccurred())
   287  				Ω(f.Close()).ShouldNot(HaveOccurred())
   288  			})
   289  			AfterEach(func() {
   290  				Ω(os.RemoveAll("go.mod")).ShouldNot(HaveOccurred())
   291  			})
   292  
   293  			Context("with GO111MODULE=auto", func() {
   294  				oldGO111MODULE := os.Getenv("GO111MODULE")
   295  				BeforeEach(func() {
   296  					os.Unsetenv("GO111MODULE")
   297  				})
   298  				AfterEach(func() {
   299  					os.Setenv("GO111MODULE", oldGO111MODULE)
   300  				})
   301  
   302  				Context("inside GOPATH", func() {
   303  					It("should return a GOPATH mode package", func() {
   304  						pkg, err := codegen.PackageFor(abs("xx", "src", "bar", "xx", "42"))
   305  						Ω(err).ShouldNot(HaveOccurred())
   306  						Expect(pkg.Path).To(Equal("bar/xx"))
   307  					})
   308  				})
   309  
   310  				Context("outside GOPATH", func() {
   311  					BeforeEach(func() {
   312  						gopath, err = ioutil.TempDir(".", "go")
   313  						Ω(err).ShouldNot(HaveOccurred())
   314  						os.Setenv("GOPATH", gopath)
   315  					})
   316  					AfterEach(func() {
   317  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   318  					})
   319  
   320  					It("should return a Module mode package", func() {
   321  						ab, err := filepath.Abs(".")
   322  						Ω(err).ShouldNot(HaveOccurred())
   323  						pkg, err := codegen.PackageFor(abs(ab, "bar", "xx", "42"))
   324  						Ω(err).ShouldNot(HaveOccurred())
   325  						Expect(pkg.Path).To(Equal("bar/xx"))
   326  					})
   327  				})
   328  			})
   329  
   330  			Context("with GO111MODULE=on", func() {
   331  				oldGO111MODULE := os.Getenv("GO111MODULE")
   332  				BeforeEach(func() {
   333  					os.Setenv("GO111MODULE", "on")
   334  				})
   335  				AfterEach(func() {
   336  					os.Setenv("GO111MODULE", oldGO111MODULE)
   337  				})
   338  
   339  				Context("inside GOPATH", func() {
   340  					It("should return a Module mode package", func() {
   341  						ab, err := filepath.Abs(".")
   342  						Ω(err).ShouldNot(HaveOccurred())
   343  						pkg, err := codegen.PackageFor(abs(ab, "bar", "xx", "42"))
   344  						Ω(err).ShouldNot(HaveOccurred())
   345  						Expect(pkg.Path).To(Equal("bar/xx"))
   346  					})
   347  				})
   348  
   349  				Context("outside GOPATH", func() {
   350  					BeforeEach(func() {
   351  						gopath, err = ioutil.TempDir(".", "go")
   352  						Ω(err).ShouldNot(HaveOccurred())
   353  						os.Setenv("GOPATH", gopath)
   354  					})
   355  					AfterEach(func() {
   356  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   357  					})
   358  
   359  					It("should return a Module mode package", func() {
   360  						ab, err := filepath.Abs(".")
   361  						Ω(err).ShouldNot(HaveOccurred())
   362  						pkg, err := codegen.PackageFor(abs(ab, "bar", "xx", "42"))
   363  						Ω(err).ShouldNot(HaveOccurred())
   364  						Expect(pkg.Path).To(Equal("bar/xx"))
   365  					})
   366  				})
   367  			})
   368  
   369  			Context("with GO111MODULE=off", func() {
   370  				oldGO111MODULE := os.Getenv("GO111MODULE")
   371  				BeforeEach(func() {
   372  					os.Setenv("GO111MODULE", "off")
   373  				})
   374  				AfterEach(func() {
   375  					os.Setenv("GO111MODULE", oldGO111MODULE)
   376  				})
   377  
   378  				Context("inside GOPATH", func() {
   379  					It("should return a GOPATH mode package", func() {
   380  						pkg, err := codegen.PackageFor(abs("xx", "src", "bar", "xx", "42"))
   381  						Ω(err).ShouldNot(HaveOccurred())
   382  						Expect(pkg.Path).To(Equal("bar/xx"))
   383  					})
   384  				})
   385  
   386  				Context("outside GOPATH", func() {
   387  					BeforeEach(func() {
   388  						gopath, err = ioutil.TempDir(".", "go")
   389  						Ω(err).ShouldNot(HaveOccurred())
   390  						os.Setenv("GOPATH", gopath)
   391  					})
   392  					AfterEach(func() {
   393  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   394  					})
   395  
   396  					It("should return an error", func() {
   397  						_, err := codegen.PackageFor(abs("bar", "xx", "42"))
   398  						Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), gopath)))
   399  					})
   400  				})
   401  			})
   402  		})
   403  
   404  		Context("with no GOMOD", func() {
   405  			Context("with GO111MODULE=auto", func() {
   406  				oldGO111MODULE := os.Getenv("GO111MODULE")
   407  				BeforeEach(func() {
   408  					os.Unsetenv("GO111MODULE")
   409  				})
   410  				AfterEach(func() {
   411  					os.Setenv("GO111MODULE", oldGO111MODULE)
   412  				})
   413  
   414  				Context("inside GOPATH", func() {
   415  					It("should return a GOPATH mode package", func() {
   416  						pkg, err := codegen.PackageFor(abs("xx", "src", "bar", "xx", "42"))
   417  						Ω(err).ShouldNot(HaveOccurred())
   418  						Expect(pkg.Path).To(Equal("bar/xx"))
   419  					})
   420  				})
   421  
   422  				Context("outside GOPATH", func() {
   423  					BeforeEach(func() {
   424  						gopath, err = ioutil.TempDir(".", "go")
   425  						Ω(err).ShouldNot(HaveOccurred())
   426  						os.Setenv("GOPATH", gopath)
   427  					})
   428  					AfterEach(func() {
   429  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   430  					})
   431  
   432  					It("should return an error", func() {
   433  						_, err := codegen.PackageFor(abs("bar", "xx", "42"))
   434  						Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), gopath)))
   435  					})
   436  				})
   437  			})
   438  
   439  			Context("with GO111MODULE=on", func() {
   440  				oldGO111MODULE := os.Getenv("GO111MODULE")
   441  				BeforeEach(func() {
   442  					os.Setenv("GO111MODULE", "on")
   443  				})
   444  				AfterEach(func() {
   445  					os.Setenv("GO111MODULE", oldGO111MODULE)
   446  				})
   447  
   448  				Context("inside GOPATH", func() {
   449  					It("should return an error", func() {
   450  						_, err := codegen.PackageFor(abs("bar", "xx", "42"))
   451  						Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), abs("xx"))))
   452  					})
   453  				})
   454  
   455  				Context("outside GOPATH", func() {
   456  					BeforeEach(func() {
   457  						gopath, err = ioutil.TempDir(".", "go")
   458  						Ω(err).ShouldNot(HaveOccurred())
   459  						os.Setenv("GOPATH", gopath)
   460  					})
   461  					AfterEach(func() {
   462  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   463  					})
   464  
   465  					It("should return an error", func() {
   466  						_, err := codegen.PackageFor(abs("bar", "xx", "42"))
   467  						Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), gopath)))
   468  					})
   469  				})
   470  			})
   471  
   472  			Context("with GO111MODULE=off", func() {
   473  				oldGO111MODULE := os.Getenv("GO111MODULE")
   474  				BeforeEach(func() {
   475  					os.Setenv("GO111MODULE", "off")
   476  				})
   477  				AfterEach(func() {
   478  					os.Setenv("GO111MODULE", oldGO111MODULE)
   479  				})
   480  
   481  				Context("inside GOPATH", func() {
   482  					It("should return a GOPATH mode package", func() {
   483  						pkg, err := codegen.PackageFor(abs("xx", "src", "bar", "xx", "42"))
   484  						Ω(err).ShouldNot(HaveOccurred())
   485  						Expect(pkg.Path).To(Equal("bar/xx"))
   486  					})
   487  				})
   488  
   489  				Context("outside GOPATH", func() {
   490  					BeforeEach(func() {
   491  						gopath, err = ioutil.TempDir(".", "go")
   492  						Ω(err).ShouldNot(HaveOccurred())
   493  						os.Setenv("GOPATH", gopath)
   494  					})
   495  					AfterEach(func() {
   496  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   497  					})
   498  
   499  					It("should return an error", func() {
   500  						_, err := codegen.PackageFor(abs("bar", "xx", "42"))
   501  						Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), gopath)))
   502  					})
   503  				})
   504  			})
   505  		})
   506  	})
   507  
   508  	Describe("Package.Abs", func() {
   509  		var (
   510  			err            error
   511  			gopath         string
   512  			f              *os.File
   513  			oldGOPATH      = build.Default.GOPATH
   514  			oldGO111MODULE = os.Getenv("GO111MODULE")
   515  		)
   516  		BeforeEach(func() {
   517  			os.Setenv("GOPATH", "/xx")
   518  			f, err = os.OpenFile("go.mod", os.O_CREATE, 0755)
   519  			Ω(err).ShouldNot(HaveOccurred())
   520  			Ω(f.Close()).ShouldNot(HaveOccurred())
   521  			os.Unsetenv("GO111MODULE")
   522  		})
   523  		AfterEach(func() {
   524  			os.Setenv("GOPATH", oldGOPATH)
   525  			Ω(os.RemoveAll("go.mod")).ShouldNot(HaveOccurred())
   526  			os.Setenv("GO111MODULE", oldGO111MODULE)
   527  		})
   528  
   529  		Context("inside GOPATH", func() {
   530  			It("should return the absolute path to the GOPATH directory", func() {
   531  				pkg, err := codegen.PackageFor(abs("xx", "src", "bar", "xx", "42"))
   532  				Ω(err).ShouldNot(HaveOccurred())
   533  				Expect(pkg.Abs()).To(Equal(abs("xx", "src", "bar", "xx")))
   534  			})
   535  		})
   536  
   537  		Context("outside GOPATH", func() {
   538  			BeforeEach(func() {
   539  				gopath, err = ioutil.TempDir(".", "go")
   540  				Ω(err).ShouldNot(HaveOccurred())
   541  				os.Setenv("GOPATH", gopath)
   542  			})
   543  			AfterEach(func() {
   544  				Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   545  			})
   546  
   547  			It("should return the absolute path to the Module directory", func() {
   548  				ab, err := filepath.Abs(".")
   549  				Ω(err).ShouldNot(HaveOccurred())
   550  				pkg, err := codegen.PackageFor(abs(ab, "bar", "xx", "42"))
   551  				Ω(err).ShouldNot(HaveOccurred())
   552  				Expect(pkg.Abs()).To(Equal(abs(ab, "bar", "xx")))
   553  			})
   554  		})
   555  	})
   556  
   557  	Describe("PackagePath", func() {
   558  		oldGOPATH := build.Default.GOPATH
   559  		BeforeEach(func() {
   560  			os.Setenv("GOPATH", abs("xx"))
   561  		})
   562  		AfterEach(func() {
   563  			os.Setenv("GOPATH", oldGOPATH)
   564  		})
   565  
   566  		var (
   567  			err    error
   568  			gopath string
   569  		)
   570  		Context("with GOMOD", func() {
   571  			var (
   572  				f *os.File
   573  			)
   574  			BeforeEach(func() {
   575  				f, err = os.OpenFile("go.mod", os.O_CREATE, 0755)
   576  				Ω(err).ShouldNot(HaveOccurred())
   577  				Ω(f.Close()).ShouldNot(HaveOccurred())
   578  			})
   579  			AfterEach(func() {
   580  				Ω(os.RemoveAll("go.mod")).ShouldNot(HaveOccurred())
   581  			})
   582  
   583  			Context("with GO111MODULE=auto", func() {
   584  				oldGO111MODULE := os.Getenv("GO111MODULE")
   585  				BeforeEach(func() {
   586  					os.Unsetenv("GO111MODULE")
   587  				})
   588  				AfterEach(func() {
   589  					os.Setenv("GO111MODULE", oldGO111MODULE)
   590  				})
   591  
   592  				Context("inside GOPATH", func() {
   593  					It("should return a GOPATH mode package path", func() {
   594  						p, err := codegen.PackagePath(abs("xx", "src", "bar", "xx", "42"))
   595  						Ω(err).ShouldNot(HaveOccurred())
   596  						Expect(p).To(Equal("bar/xx/42"))
   597  					})
   598  				})
   599  
   600  				Context("outside GOPATH", func() {
   601  					BeforeEach(func() {
   602  						gopath, err = ioutil.TempDir(".", "go")
   603  						Ω(err).ShouldNot(HaveOccurred())
   604  						os.Setenv("GOPATH", gopath)
   605  					})
   606  					AfterEach(func() {
   607  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   608  					})
   609  
   610  					It("should return a Module mode package path", func() {
   611  						ab, err := filepath.Abs(".")
   612  						Ω(err).ShouldNot(HaveOccurred())
   613  						p, err := codegen.PackagePath(abs(ab, "bar", "xx", "42"))
   614  						Ω(err).ShouldNot(HaveOccurred())
   615  						Expect(p).To(Equal("bar/xx/42"))
   616  					})
   617  				})
   618  			})
   619  
   620  			Context("with GO111MODULE=on", func() {
   621  				oldGO111MODULE := os.Getenv("GO111MODULE")
   622  				BeforeEach(func() {
   623  					os.Setenv("GO111MODULE", "on")
   624  				})
   625  				AfterEach(func() {
   626  					os.Setenv("GO111MODULE", oldGO111MODULE)
   627  				})
   628  
   629  				Context("inside GOPATH", func() {
   630  					It("should return a Module mode package path", func() {
   631  						ab, err := filepath.Abs(".")
   632  						Ω(err).ShouldNot(HaveOccurred())
   633  						p, err := codegen.PackagePath(abs(ab, "bar", "xx", "42"))
   634  						Ω(err).ShouldNot(HaveOccurred())
   635  						Expect(p).To(Equal("bar/xx/42"))
   636  					})
   637  				})
   638  
   639  				Context("outside GOPATH", func() {
   640  					BeforeEach(func() {
   641  						gopath, err = ioutil.TempDir(".", "go")
   642  						Ω(err).ShouldNot(HaveOccurred())
   643  						os.Setenv("GOPATH", gopath)
   644  					})
   645  					AfterEach(func() {
   646  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   647  					})
   648  
   649  					It("should return a Module mode package path", func() {
   650  						ab, err := filepath.Abs(".")
   651  						Ω(err).ShouldNot(HaveOccurred())
   652  						p, err := codegen.PackagePath(abs(ab, "bar", "xx", "42"))
   653  						Ω(err).ShouldNot(HaveOccurred())
   654  						Expect(p).To(Equal("bar/xx/42"))
   655  					})
   656  				})
   657  			})
   658  
   659  			Context("with GO111MODULE=off", func() {
   660  				oldGO111MODULE := os.Getenv("GO111MODULE")
   661  				BeforeEach(func() {
   662  					os.Setenv("GO111MODULE", "off")
   663  				})
   664  				AfterEach(func() {
   665  					os.Setenv("GO111MODULE", oldGO111MODULE)
   666  				})
   667  
   668  				Context("inside GOPATH", func() {
   669  					It("should return a GOPATH mode package path", func() {
   670  						p, err := codegen.PackagePath(abs("xx", "src", "bar", "xx", "42"))
   671  						Ω(err).ShouldNot(HaveOccurred())
   672  						Expect(p).To(Equal("bar/xx/42"))
   673  					})
   674  				})
   675  
   676  				Context("outside GOPATH", func() {
   677  					BeforeEach(func() {
   678  						gopath, err = ioutil.TempDir(".", "go")
   679  						Ω(err).ShouldNot(HaveOccurred())
   680  						os.Setenv("GOPATH", gopath)
   681  					})
   682  					AfterEach(func() {
   683  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   684  					})
   685  
   686  					It("should return an error", func() {
   687  						_, err := codegen.PackagePath(abs("bar", "xx", "42"))
   688  						Ω(err).Should(Equal(fmt.Errorf("%s does not contain a Go package", abs("bar", "xx", "42"))))
   689  					})
   690  				})
   691  			})
   692  		})
   693  
   694  		Context("with no GOMOD", func() {
   695  			Context("with GO111MODULE=auto", func() {
   696  				oldGO111MODULE := os.Getenv("GO111MODULE")
   697  				BeforeEach(func() {
   698  					os.Unsetenv("GO111MODULE")
   699  				})
   700  				AfterEach(func() {
   701  					os.Setenv("GO111MODULE", oldGO111MODULE)
   702  				})
   703  
   704  				Context("inside GOPATH", func() {
   705  					It("should return a GOPATH mode package path", func() {
   706  						p, err := codegen.PackagePath(abs("xx", "src", "bar", "xx", "42"))
   707  						Ω(err).ShouldNot(HaveOccurred())
   708  						Expect(p).To(Equal("bar/xx/42"))
   709  					})
   710  				})
   711  
   712  				Context("outside GOPATH", func() {
   713  					BeforeEach(func() {
   714  						gopath, err = ioutil.TempDir(".", "go")
   715  						Ω(err).ShouldNot(HaveOccurred())
   716  						os.Setenv("GOPATH", gopath)
   717  					})
   718  					AfterEach(func() {
   719  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   720  					})
   721  
   722  					It("should return an error", func() {
   723  						_, err := codegen.PackagePath(abs("bar", "xx", "42"))
   724  						Ω(err).Should(Equal(fmt.Errorf("%s does not contain a Go package", abs("bar", "xx", "42"))))
   725  					})
   726  				})
   727  			})
   728  
   729  			Context("with GO111MODULE=on", func() {
   730  				oldGO111MODULE := os.Getenv("GO111MODULE")
   731  				BeforeEach(func() {
   732  					os.Setenv("GO111MODULE", "on")
   733  				})
   734  				AfterEach(func() {
   735  					os.Setenv("GO111MODULE", oldGO111MODULE)
   736  				})
   737  
   738  				Context("inside GOPATH", func() {
   739  					It("should return an error", func() {
   740  						_, err := codegen.PackagePath(abs("bar", "xx", "42"))
   741  						Ω(err).Should(Equal(fmt.Errorf("%s does not contain a Go package", abs("bar", "xx", "42"))))
   742  					})
   743  				})
   744  
   745  				Context("outside GOPATH", func() {
   746  					BeforeEach(func() {
   747  						gopath, err = ioutil.TempDir(".", "go")
   748  						Ω(err).ShouldNot(HaveOccurred())
   749  						os.Setenv("GOPATH", gopath)
   750  					})
   751  					AfterEach(func() {
   752  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   753  					})
   754  
   755  					It("should return an error", func() {
   756  						_, err := codegen.PackagePath(abs("bar", "xx", "42"))
   757  						Ω(err).Should(Equal(fmt.Errorf("%s does not contain a Go package", abs("bar", "xx", "42"))))
   758  					})
   759  				})
   760  			})
   761  
   762  			Context("with GO111MODULE=off", func() {
   763  				oldGO111MODULE := os.Getenv("GO111MODULE")
   764  				BeforeEach(func() {
   765  					os.Setenv("GO111MODULE", "off")
   766  				})
   767  				AfterEach(func() {
   768  					os.Setenv("GO111MODULE", oldGO111MODULE)
   769  				})
   770  
   771  				Context("inside GOPATH", func() {
   772  					It("should return a GOPATH mode package path", func() {
   773  						p, err := codegen.PackagePath(abs("xx", "src", "bar", "xx", "42"))
   774  						Ω(err).ShouldNot(HaveOccurred())
   775  						Expect(p).To(Equal("bar/xx/42"))
   776  					})
   777  				})
   778  
   779  				Context("outside GOPATH", func() {
   780  					BeforeEach(func() {
   781  						gopath, err = ioutil.TempDir(".", "go")
   782  						Ω(err).ShouldNot(HaveOccurred())
   783  						os.Setenv("GOPATH", gopath)
   784  					})
   785  					AfterEach(func() {
   786  						Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred())
   787  					})
   788  
   789  					It("should return an error", func() {
   790  						_, err := codegen.PackagePath(abs("bar", "xx", "42"))
   791  						Ω(err).Should(Equal(fmt.Errorf("%s does not contain a Go package", abs("bar", "xx", "42"))))
   792  					})
   793  				})
   794  			})
   795  		})
   796  	})
   797  
   798  })