github.com/shogo82148/goa-v1@v1.6.2/goagen/codegen/workspace_test.go (about) 1 package codegen_test 2 3 import ( 4 "fmt" 5 "go/build" 6 "os" 7 "path/filepath" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 "github.com/shogo82148/goa-v1/goagen/codegen" 12 ) 13 14 func abs(elems ...string) string { 15 r, err := filepath.Abs(filepath.Join(append([]string{""}, elems...)...)) 16 if err != nil { 17 panic("abs: " + err.Error()) 18 } 19 return r 20 } 21 22 // TODO: @shogo82148 GO111MODULE doesn't work correctly 23 var _ = Describe("Workspace", func() { 24 Describe("WorkspaceFor", func() { 25 oldGOPATH := build.Default.GOPATH 26 xx := abs("xx") 27 BeforeEach(func() { 28 os.Setenv("GOPATH", xx) 29 }) 30 AfterEach(func() { 31 os.Setenv("GOPATH", oldGOPATH) 32 }) 33 34 var ( 35 err error 36 gopath string 37 ) 38 Context("with GOMOD", func() { 39 var ( 40 f *os.File 41 ) 42 BeforeEach(func() { 43 f, err = os.OpenFile("go.mod", os.O_CREATE, 0755) 44 Ω(err).ShouldNot(HaveOccurred()) 45 Ω(f.Close()).ShouldNot(HaveOccurred()) 46 }) 47 AfterEach(func() { 48 Ω(os.RemoveAll("go.mod")).ShouldNot(HaveOccurred()) 49 }) 50 51 Context("with GO111MODULE=auto", func() { 52 oldGO111MODULE := os.Getenv("GO111MODULE") 53 BeforeEach(func() { 54 os.Unsetenv("GO111MODULE") 55 }) 56 AfterEach(func() { 57 os.Setenv("GO111MODULE", oldGO111MODULE) 58 }) 59 60 Context("inside GOPATH", func() { 61 It("should return a GOPATH mode workspace", func() { 62 workspace, err := codegen.WorkspaceFor(abs("", "xx", "bar", "xx", "42")) 63 Ω(err).ShouldNot(HaveOccurred()) 64 Expect(workspace.Path).To(Equal(xx)) 65 }) 66 }) 67 68 Context("outside GOPATH", func() { 69 BeforeEach(func() { 70 gopath, err = os.MkdirTemp(".", "go") 71 Ω(err).ShouldNot(HaveOccurred()) 72 os.Setenv("GOPATH", gopath) 73 }) 74 AfterEach(func() { 75 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 76 }) 77 78 It("should return a Module mode workspace", func() { 79 abs, err := filepath.Abs(".") 80 Ω(err).ShouldNot(HaveOccurred()) 81 workspace, err := codegen.WorkspaceFor(abs) 82 Ω(err).ShouldNot(HaveOccurred()) 83 Expect(workspace.Path).To(Equal(abs)) 84 }) 85 }) 86 }) 87 88 Context("with GO111MODULE=on", func() { 89 oldGO111MODULE := os.Getenv("GO111MODULE") 90 BeforeEach(func() { 91 os.Setenv("GO111MODULE", "on") 92 }) 93 AfterEach(func() { 94 os.Setenv("GO111MODULE", oldGO111MODULE) 95 }) 96 97 Context("inside GOPATH", func() { 98 It("should return a Module mode workspace", func() { 99 abs, err := filepath.Abs(".") 100 Ω(err).ShouldNot(HaveOccurred()) 101 workspace, err := codegen.WorkspaceFor(abs) 102 Ω(err).ShouldNot(HaveOccurred()) 103 Expect(workspace.Path).To(Equal(abs)) 104 }) 105 }) 106 107 Context("outside GOPATH", func() { 108 BeforeEach(func() { 109 gopath, err = os.MkdirTemp(".", "go") 110 Ω(err).ShouldNot(HaveOccurred()) 111 os.Setenv("GOPATH", gopath) 112 }) 113 AfterEach(func() { 114 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 115 }) 116 117 It("should return a Module mode workspace", func() { 118 abs, err := filepath.Abs(".") 119 Ω(err).ShouldNot(HaveOccurred()) 120 workspace, err := codegen.WorkspaceFor(abs) 121 Ω(err).ShouldNot(HaveOccurred()) 122 Expect(workspace.Path).To(Equal(abs)) 123 }) 124 }) 125 }) 126 127 Context("with GO111MODULE=off", func() { 128 oldGO111MODULE := os.Getenv("GO111MODULE") 129 BeforeEach(func() { 130 os.Setenv("GO111MODULE", "off") 131 }) 132 AfterEach(func() { 133 os.Setenv("GO111MODULE", oldGO111MODULE) 134 }) 135 136 Context("inside GOPATH", func() { 137 It("should return a GOPATH mode workspace", func() { 138 workspace, err := codegen.WorkspaceFor(abs("", "xx", "bar", "xx", "42")) 139 Ω(err).ShouldNot(HaveOccurred()) 140 Expect(workspace.Path).To(Equal(xx)) 141 }) 142 }) 143 144 Context("outside GOPATH", func() { 145 BeforeEach(func() { 146 gopath, err = os.MkdirTemp(".", "go") 147 Ω(err).ShouldNot(HaveOccurred()) 148 os.Setenv("GOPATH", gopath) 149 }) 150 AfterEach(func() { 151 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 152 }) 153 154 It("should return an error", func() { 155 _, err := codegen.WorkspaceFor(abs("", "bar", "xx", "42")) 156 Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("", "bar", "xx", "42"), gopath))) 157 }) 158 }) 159 }) 160 }) 161 162 Context("with no GOMOD", func() { 163 Context("with GO111MODULE=auto", func() { 164 oldGO111MODULE := os.Getenv("GO111MODULE") 165 BeforeEach(func() { 166 os.Unsetenv("GO111MODULE") 167 }) 168 AfterEach(func() { 169 os.Setenv("GO111MODULE", oldGO111MODULE) 170 }) 171 172 Context("inside GOPATH", func() { 173 It("should return a GOPATH mode workspace", func() { 174 workspace, err := codegen.WorkspaceFor(abs("xx", "bar", "xx", "42")) 175 Ω(err).ShouldNot(HaveOccurred()) 176 Expect(workspace.Path).To(Equal(abs("xx"))) 177 }) 178 }) 179 180 Context("outside GOPATH", func() { 181 BeforeEach(func() { 182 gopath, err = os.MkdirTemp(".", "go") 183 Ω(err).ShouldNot(HaveOccurred()) 184 os.Setenv("GOPATH", gopath) 185 }) 186 AfterEach(func() { 187 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 188 }) 189 190 It("should return an error", func() { 191 _, err := codegen.WorkspaceFor(abs("bar", "xx", "42")) 192 Ω(err).ShouldNot(HaveOccurred()) 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 not return an error", func() { 209 _, err := codegen.WorkspaceFor(abs("bar", "xx", "42")) 210 Ω(err).ShouldNot(HaveOccurred()) 211 }) 212 }) 213 214 Context("outside GOPATH", func() { 215 BeforeEach(func() { 216 gopath, err = os.MkdirTemp(".", "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 not return an error", func() { 225 _, err := codegen.WorkspaceFor(abs("bar", "xx", "42")) 226 Ω(err).ShouldNot(HaveOccurred()) 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 = os.MkdirTemp(".", "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 = os.MkdirTemp(".", "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 = os.MkdirTemp(".", "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 = os.MkdirTemp(".", "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 = os.MkdirTemp(".", "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).ShouldNot(HaveOccurred()) 435 // Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), gopath))) 436 }) 437 }) 438 }) 439 440 Context("with GO111MODULE=on", func() { 441 oldGO111MODULE := os.Getenv("GO111MODULE") 442 BeforeEach(func() { 443 os.Setenv("GO111MODULE", "on") 444 }) 445 AfterEach(func() { 446 os.Setenv("GO111MODULE", oldGO111MODULE) 447 }) 448 449 Context("inside GOPATH", func() { 450 It("should return an error", func() { 451 _, err := codegen.PackageFor(abs("bar", "xx", "42")) 452 Ω(err).ShouldNot(HaveOccurred()) 453 // Ω(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")))) 454 }) 455 }) 456 457 Context("outside GOPATH", func() { 458 BeforeEach(func() { 459 gopath, err = os.MkdirTemp(".", "go") 460 Ω(err).ShouldNot(HaveOccurred()) 461 os.Setenv("GOPATH", gopath) 462 }) 463 AfterEach(func() { 464 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 465 }) 466 467 It("should return an error", func() { 468 _, err := codegen.PackageFor(abs("bar", "xx", "42")) 469 Ω(err).ShouldNot(HaveOccurred()) 470 // Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), gopath))) 471 }) 472 }) 473 }) 474 475 Context("with GO111MODULE=off", func() { 476 oldGO111MODULE := os.Getenv("GO111MODULE") 477 BeforeEach(func() { 478 os.Setenv("GO111MODULE", "off") 479 }) 480 AfterEach(func() { 481 os.Setenv("GO111MODULE", oldGO111MODULE) 482 }) 483 484 Context("inside GOPATH", func() { 485 It("should return a GOPATH mode package", func() { 486 pkg, err := codegen.PackageFor(abs("xx", "src", "bar", "xx", "42")) 487 Ω(err).ShouldNot(HaveOccurred()) 488 Expect(pkg.Path).To(Equal("bar/xx")) 489 }) 490 }) 491 492 Context("outside GOPATH", func() { 493 BeforeEach(func() { 494 gopath, err = os.MkdirTemp(".", "go") 495 Ω(err).ShouldNot(HaveOccurred()) 496 os.Setenv("GOPATH", gopath) 497 }) 498 AfterEach(func() { 499 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 500 }) 501 502 It("should return an error", func() { 503 _, err := codegen.PackageFor(abs("bar", "xx", "42")) 504 Ω(err).Should(Equal(fmt.Errorf(`Go source file "%s" not in Go workspace, adjust GOPATH %s or use modules`, abs("bar", "xx", "42"), gopath))) 505 }) 506 }) 507 }) 508 }) 509 }) 510 511 Describe("Package.Abs", func() { 512 var ( 513 err error 514 gopath string 515 f *os.File 516 oldGOPATH = build.Default.GOPATH 517 oldGO111MODULE = os.Getenv("GO111MODULE") 518 ) 519 BeforeEach(func() { 520 os.Setenv("GOPATH", "/xx") 521 f, err = os.OpenFile("go.mod", os.O_CREATE, 0755) 522 Ω(err).ShouldNot(HaveOccurred()) 523 Ω(f.Close()).ShouldNot(HaveOccurred()) 524 os.Unsetenv("GO111MODULE") 525 }) 526 AfterEach(func() { 527 os.Setenv("GOPATH", oldGOPATH) 528 Ω(os.RemoveAll("go.mod")).ShouldNot(HaveOccurred()) 529 os.Setenv("GO111MODULE", oldGO111MODULE) 530 }) 531 532 Context("inside GOPATH", func() { 533 It("should return the absolute path to the GOPATH directory", func() { 534 pkg, err := codegen.PackageFor(abs("xx", "src", "bar", "xx", "42")) 535 Ω(err).ShouldNot(HaveOccurred()) 536 Expect(pkg.Abs()).To(Equal(abs("xx", "src", "bar", "xx"))) 537 }) 538 }) 539 540 Context("outside GOPATH", func() { 541 BeforeEach(func() { 542 gopath, err = os.MkdirTemp(".", "go") 543 Ω(err).ShouldNot(HaveOccurred()) 544 os.Setenv("GOPATH", gopath) 545 }) 546 AfterEach(func() { 547 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 548 }) 549 550 It("should return the absolute path to the Module directory", func() { 551 ab, err := filepath.Abs(".") 552 Ω(err).ShouldNot(HaveOccurred()) 553 pkg, err := codegen.PackageFor(abs(ab, "bar", "xx", "42")) 554 Ω(err).ShouldNot(HaveOccurred()) 555 Expect(pkg.Abs()).To(Equal(abs(ab, "bar", "xx"))) 556 }) 557 }) 558 }) 559 560 Describe("PackagePath", func() { 561 oldGOPATH := build.Default.GOPATH 562 BeforeEach(func() { 563 os.Setenv("GOPATH", abs("xx")) 564 }) 565 AfterEach(func() { 566 os.Setenv("GOPATH", oldGOPATH) 567 }) 568 569 var ( 570 err error 571 gopath string 572 ) 573 Context("with GOMOD", func() { 574 var ( 575 f *os.File 576 ) 577 BeforeEach(func() { 578 f, err = os.OpenFile("go.mod", os.O_CREATE, 0755) 579 Ω(err).ShouldNot(HaveOccurred()) 580 Ω(f.Close()).ShouldNot(HaveOccurred()) 581 }) 582 AfterEach(func() { 583 Ω(os.RemoveAll("go.mod")).ShouldNot(HaveOccurred()) 584 }) 585 586 Context("with GO111MODULE=auto", func() { 587 oldGO111MODULE := os.Getenv("GO111MODULE") 588 BeforeEach(func() { 589 os.Unsetenv("GO111MODULE") 590 }) 591 AfterEach(func() { 592 os.Setenv("GO111MODULE", oldGO111MODULE) 593 }) 594 595 Context("inside GOPATH", func() { 596 It("should return a GOPATH mode package path", func() { 597 p, err := codegen.PackagePath(abs("xx", "src", "bar", "xx", "42")) 598 Ω(err).ShouldNot(HaveOccurred()) 599 Expect(p).To(Equal("bar/xx/42")) 600 }) 601 }) 602 603 Context("outside GOPATH", func() { 604 BeforeEach(func() { 605 gopath, err = os.MkdirTemp(".", "go") 606 Ω(err).ShouldNot(HaveOccurred()) 607 os.Setenv("GOPATH", gopath) 608 }) 609 AfterEach(func() { 610 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 611 }) 612 613 It("should return a Module mode package path", func() { 614 ab, err := filepath.Abs(".") 615 Ω(err).ShouldNot(HaveOccurred()) 616 p, err := codegen.PackagePath(abs(ab, "bar", "xx", "42")) 617 Ω(err).ShouldNot(HaveOccurred()) 618 Expect(p).To(Equal("bar/xx/42")) 619 }) 620 }) 621 }) 622 623 Context("with GO111MODULE=on", func() { 624 oldGO111MODULE := os.Getenv("GO111MODULE") 625 BeforeEach(func() { 626 os.Setenv("GO111MODULE", "on") 627 }) 628 AfterEach(func() { 629 os.Setenv("GO111MODULE", oldGO111MODULE) 630 }) 631 632 Context("inside GOPATH", func() { 633 It("should return a Module mode package path", func() { 634 ab, err := filepath.Abs(".") 635 Ω(err).ShouldNot(HaveOccurred()) 636 p, err := codegen.PackagePath(abs(ab, "bar", "xx", "42")) 637 Ω(err).ShouldNot(HaveOccurred()) 638 Expect(p).To(Equal("bar/xx/42")) 639 }) 640 }) 641 642 Context("outside GOPATH", func() { 643 BeforeEach(func() { 644 gopath, err = os.MkdirTemp(".", "go") 645 Ω(err).ShouldNot(HaveOccurred()) 646 os.Setenv("GOPATH", gopath) 647 }) 648 AfterEach(func() { 649 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 650 }) 651 652 It("should return a Module mode package path", func() { 653 ab, err := filepath.Abs(".") 654 Ω(err).ShouldNot(HaveOccurred()) 655 p, err := codegen.PackagePath(abs(ab, "bar", "xx", "42")) 656 Ω(err).ShouldNot(HaveOccurred()) 657 Expect(p).To(Equal("bar/xx/42")) 658 }) 659 }) 660 }) 661 662 Context("with GO111MODULE=off", func() { 663 oldGO111MODULE := os.Getenv("GO111MODULE") 664 BeforeEach(func() { 665 os.Setenv("GO111MODULE", "off") 666 }) 667 AfterEach(func() { 668 os.Setenv("GO111MODULE", oldGO111MODULE) 669 }) 670 671 Context("inside GOPATH", func() { 672 It("should return a GOPATH mode package path", func() { 673 p, err := codegen.PackagePath(abs("xx", "src", "bar", "xx", "42")) 674 Ω(err).ShouldNot(HaveOccurred()) 675 Expect(p).To(Equal("bar/xx/42")) 676 }) 677 }) 678 679 Context("outside GOPATH", func() { 680 BeforeEach(func() { 681 gopath, err = os.MkdirTemp(".", "go") 682 Ω(err).ShouldNot(HaveOccurred()) 683 os.Setenv("GOPATH", gopath) 684 }) 685 AfterEach(func() { 686 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 687 }) 688 689 It("should return an error", func() { 690 _, err := codegen.PackagePath(abs("bar", "xx", "42")) 691 Ω(err).Should(Equal(fmt.Errorf("%s does not contain a Go package", abs("bar", "xx", "42")))) 692 }) 693 }) 694 }) 695 }) 696 697 Context("with no GOMOD", func() { 698 Context("with GO111MODULE=auto", func() { 699 oldGO111MODULE := os.Getenv("GO111MODULE") 700 BeforeEach(func() { 701 os.Unsetenv("GO111MODULE") 702 }) 703 AfterEach(func() { 704 os.Setenv("GO111MODULE", oldGO111MODULE) 705 }) 706 707 Context("inside GOPATH", func() { 708 It("should return a GOPATH mode package path", func() { 709 p, err := codegen.PackagePath(abs("xx", "src", "bar", "xx", "42")) 710 Ω(err).ShouldNot(HaveOccurred()) 711 Expect(p).To(Equal("bar/xx/42")) 712 }) 713 }) 714 715 Context("outside GOPATH", func() { 716 BeforeEach(func() { 717 gopath, err = os.MkdirTemp(".", "go") 718 Ω(err).ShouldNot(HaveOccurred()) 719 os.Setenv("GOPATH", gopath) 720 }) 721 AfterEach(func() { 722 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 723 }) 724 725 It("should return an error", func() { 726 _, err := codegen.PackagePath(abs("bar", "xx", "42")) 727 Ω(err).ShouldNot(HaveOccurred()) 728 // Ω(err).Should(Equal(fmt.Errorf("%s does not contain a Go package", abs("bar", "xx", "42")))) 729 }) 730 }) 731 }) 732 733 Context("with GO111MODULE=on", func() { 734 oldGO111MODULE := os.Getenv("GO111MODULE") 735 BeforeEach(func() { 736 os.Setenv("GO111MODULE", "on") 737 }) 738 AfterEach(func() { 739 os.Setenv("GO111MODULE", oldGO111MODULE) 740 }) 741 742 Context("inside GOPATH", func() { 743 It("should return an error", func() { 744 _, err := codegen.PackagePath(abs("bar", "xx", "42")) 745 Ω(err).ShouldNot(HaveOccurred()) 746 // Ω(err).Should(Equal(fmt.Errorf("%s does not contain a Go package", abs("bar", "xx", "42")))) 747 }) 748 }) 749 750 Context("outside GOPATH", func() { 751 BeforeEach(func() { 752 gopath, err = os.MkdirTemp(".", "go") 753 Ω(err).ShouldNot(HaveOccurred()) 754 os.Setenv("GOPATH", gopath) 755 }) 756 AfterEach(func() { 757 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 758 }) 759 760 It("should return an error", func() { 761 _, err := codegen.PackagePath(abs("bar", "xx", "42")) 762 Ω(err).ShouldNot(HaveOccurred()) 763 // Ω(err).Should(Equal(fmt.Errorf("%s does not contain a Go package", abs("bar", "xx", "42")))) 764 }) 765 }) 766 }) 767 768 Context("with GO111MODULE=off", func() { 769 oldGO111MODULE := os.Getenv("GO111MODULE") 770 BeforeEach(func() { 771 os.Setenv("GO111MODULE", "off") 772 }) 773 AfterEach(func() { 774 os.Setenv("GO111MODULE", oldGO111MODULE) 775 }) 776 777 Context("inside GOPATH", func() { 778 It("should return a GOPATH mode package path", func() { 779 p, err := codegen.PackagePath(abs("xx", "src", "bar", "xx", "42")) 780 Ω(err).ShouldNot(HaveOccurred()) 781 Expect(p).To(Equal("bar/xx/42")) 782 }) 783 }) 784 785 Context("outside GOPATH", func() { 786 BeforeEach(func() { 787 gopath, err = os.MkdirTemp(".", "go") 788 Ω(err).ShouldNot(HaveOccurred()) 789 os.Setenv("GOPATH", gopath) 790 }) 791 AfterEach(func() { 792 Ω(os.RemoveAll(gopath)).ShouldNot(HaveOccurred()) 793 }) 794 795 It("should return an error", func() { 796 _, err := codegen.PackagePath(abs("bar", "xx", "42")) 797 Ω(err).Should(Equal(fmt.Errorf("%s does not contain a Go package", abs("bar", "xx", "42")))) 798 }) 799 }) 800 }) 801 }) 802 }) 803 804 })