github.com/petergtz/pegomock@v2.9.1-0.20230424204322-eb0e044013df+incompatible/pegomock/package_name_test.go (about)

     1  package main_test
     2  
     3  import (
     4  	"go/build"
     5  	"os"
     6  
     7  	. "github.com/petergtz/pegomock/pegomock/testutil"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	main "github.com/petergtz/pegomock/pegomock"
    12  )
    13  
    14  var _ = Describe("DetermineBla", func() {
    15  	var (
    16  		packageDir string
    17  	)
    18  	BeforeEach(func() {
    19  		packageDir = joinPath(build.Default.GOPATH, "src", "package_dir")
    20  	})
    21  
    22  	JustBeforeEach(func() {
    23  		Expect(os.MkdirAll(packageDir, 0755)).To(Succeed())
    24  	})
    25  
    26  	AfterEach(func() {
    27  		Expect(os.RemoveAll(packageDir)).To(Succeed())
    28  	})
    29  
    30  	XContext("only one go file", func() {
    31  		It("names the package name after package name + _test suffix", func() {
    32  			WriteFile(joinPath(packageDir, "mydisplay.go"), "package package_name")
    33  
    34  			Expect(main.DeterminePackageNameIn(packageDir)).To(Equal("package_name_test"))
    35  		})
    36  	})
    37  
    38  	XContext("multiple go files with different package names", func() {
    39  		It("fails", func() {
    40  			WriteFile(joinPath(packageDir, "mydisplay.go"), "package package_name")
    41  			WriteFile(joinPath(packageDir, "other.go"), "package other_package_name")
    42  
    43  			_, e := main.DeterminePackageNameIn(packageDir)
    44  			Expect(e).To(MatchError(ContainSubstring("Error while determining Go package")))
    45  		})
    46  	})
    47  
    48  	XContext("go file and go test file", func() {
    49  		It("determines the package name from the test file", func() {
    50  			WriteFile(joinPath(packageDir, "mydisplay.go"), "package package_name")
    51  			WriteFile(joinPath(packageDir, "mydisplay_test.go"), "package non_conventional_package_name_test")
    52  
    53  			Expect(main.DeterminePackageNameIn(packageDir)).To(Equal("non_conventional_package_name_test"))
    54  		})
    55  	})
    56  
    57  	Context("no files", func() {
    58  		It("names the package after the directory name base", func() {
    59  			Expect(main.DeterminePackageNameIn(packageDir)).To(Equal("package_dir_test"))
    60  		})
    61  
    62  		Context("current dir with dashes in name", func() {
    63  			BeforeEach(func() {
    64  				packageDir = joinPath(build.Default.GOPATH, "src", "package-dir-with-dashes")
    65  			})
    66  
    67  			It("names the package after the directory name base, but replace dashes with underscores", func() {
    68  				Expect(main.DeterminePackageNameIn(packageDir)).To(Equal("package_dir_with_dashes_test"))
    69  			})
    70  		})
    71  	})
    72  
    73  })