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

     1  // Copyright 2016 Peter Goetz
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package watch_test
    16  
    17  import (
    18  	"go/build"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	. "github.com/onsi/ginkgo"
    24  	. "github.com/onsi/gomega"
    25  	. "github.com/petergtz/pegomock/pegomock/testutil"
    26  	"github.com/petergtz/pegomock/pegomock/watch"
    27  )
    28  
    29  var (
    30  	joinPath = filepath.Join
    31  )
    32  
    33  func TestWatchCommand(t *testing.T) {
    34  	RegisterFailHandler(Fail)
    35  	RunSpecs(t, "Watch Suite")
    36  }
    37  
    38  var _ = Describe("NewMockFileUpdater", func() {
    39  	var (
    40  		packageDir, subPackageDir, vendorPackageDir string
    41  		origWorkingDir                              string
    42  	)
    43  
    44  	BeforeEach(func() {
    45  		packageDir = joinPath(build.Default.GOPATH, "src", "pegomocktest")
    46  		Expect(os.MkdirAll(packageDir, 0755)).To(Succeed())
    47  		subPackageDir = joinPath(packageDir, "subpackage")
    48  		Expect(os.MkdirAll(subPackageDir, 0755)).To(Succeed())
    49  		vendorPackageDir = joinPath(packageDir, "vendor", "github.com", "petergtz", "vendored_package")
    50  		Expect(os.MkdirAll(vendorPackageDir, 0755)).To(Succeed())
    51  
    52  		var e error
    53  		origWorkingDir, e = os.Getwd()
    54  		Expect(e).NotTo(HaveOccurred())
    55  		os.Chdir(packageDir)
    56  
    57  		WriteFile(joinPath(packageDir, "mydisplay.go"),
    58  			"package pegomocktest; type MyDisplay interface {  Show() }")
    59  		WriteFile(joinPath(subPackageDir, "subdisplay.go"),
    60  			"package subpackage; type SubDisplay interface {  ShowMe() }")
    61  		WriteFile(joinPath(vendorPackageDir, "iface.go"),
    62  			`package vendored_package; type Interface interface{ Foobar() }`)
    63  		WriteFile(joinPath(packageDir, "vendordisplay.go"), `package pegomocktest
    64  			import ( "github.com/petergtz/vendored_package" )
    65  			type VendorDisplay interface { Show(something vendored_package.Interface) }`)
    66  
    67  	})
    68  
    69  	AfterEach(func() {
    70  		Expect(os.RemoveAll(packageDir)).To(Succeed())
    71  		os.Chdir(origWorkingDir)
    72  	})
    73  
    74  	Context("after populating interfaces_to_mock with an actual interface", func() {
    75  		It(`Eventually creates a file mock_mydisplay_test.go starting with "package pegomocktest_test"`, func() {
    76  			WriteFile(joinPath(packageDir, "interfaces_to_mock"), "MyDisplay")
    77  
    78  			watch.NewMockFileUpdater([]string{packageDir}, false).Update()
    79  
    80  			Eventually(joinPath(packageDir, "mock_mydisplay_test.go"), "3s").Should(SatisfyAll(
    81  				BeAnExistingFile(),
    82  				BeAFileContainingSubString("package pegomocktest_test")))
    83  		})
    84  
    85  		Context("and overriding the output filepath", func() {
    86  			It(`Eventually creates a file foo.go starting with "package pegomocktest_test"`, func() {
    87  				WriteFile(joinPath(packageDir, "interfaces_to_mock"), "-o foo.go MyDisplay")
    88  
    89  				watch.NewMockFileUpdater([]string{packageDir}, false).Update()
    90  
    91  				Eventually(joinPath(packageDir, "foo.go"), "3s").Should(SatisfyAll(
    92  					BeAnExistingFile(),
    93  					BeAFileContainingSubString("package pegomocktest_test")))
    94  			})
    95  		})
    96  
    97  		Context("and overriding the package name", func() {
    98  			It(`Eventually creates a file starting with "package the_overriden_test_package"`, func() {
    99  				WriteFile(joinPath(packageDir, "interfaces_to_mock"), "--package the_overriden_test_package MyDisplay")
   100  
   101  				watch.NewMockFileUpdater([]string{packageDir}, false).Update()
   102  
   103  				Eventually(joinPath(packageDir, "mock_mydisplay_test.go"), "3s").Should(SatisfyAll(
   104  					BeAnExistingFile(),
   105  					BeAFileContainingSubString("package the_overriden_test_package")))
   106  			})
   107  		})
   108  
   109  		Context(`and specifying the vendor path`, func() {
   110  
   111  			It(`Eventually creates a file containing the import ( vendored_package "github.com/petergtz/vendored_package" )'`, func() {
   112  				WriteFile(joinPath(packageDir, "interfaces_to_mock"), "VendorDisplay")
   113  
   114  				watch.NewMockFileUpdater([]string{packageDir}, false).Update()
   115  
   116  				Expect(joinPath(packageDir, "mock_vendordisplay_test.go")).To(SatisfyAll(
   117  					BeAnExistingFile(),
   118  					BeAFileContainingSubString(`vendored_package "github.com/petergtz/vendored_package"`)))
   119  			})
   120  		})
   121  
   122  		Context("in multiple packages and providing those packages to watch", func() {
   123  			It(`Eventually creates correct files in respective directories`, func() {
   124  				os.Chdir("..")
   125  				WriteFile(joinPath(packageDir, "interfaces_to_mock"), "MyDisplay")
   126  				WriteFile(joinPath(subPackageDir, "interfaces_to_mock"), "SubDisplay")
   127  
   128  				watch.NewMockFileUpdater([]string{"pegomocktest", "pegomocktest/subpackage"}, false).Update()
   129  
   130  				Eventually(joinPath(packageDir, "mock_mydisplay_test.go"), "3s").Should(SatisfyAll(
   131  					BeAnExistingFile(),
   132  					BeAFileContainingSubString("package pegomocktest_test")))
   133  				Eventually(joinPath(subPackageDir, "mock_subdisplay_test.go"), "3s").Should(SatisfyAll(
   134  					BeAnExistingFile(),
   135  					BeAFileContainingSubString("package subpackage_test")))
   136  			})
   137  		})
   138  
   139  		Context("in one package, but providing multiple packages to create mocks from", func() {
   140  			It(`Eventually creates correct files in respective directories`, func() {
   141  				os.Chdir("..")
   142  				WriteFile(joinPath(packageDir, "interfaces_to_mock"), "MyDisplay\npegomocktest/subpackage SubDisplay")
   143  
   144  				watch.NewMockFileUpdater([]string{"pegomocktest", "pegomocktest/subpackage"}, false).Update()
   145  
   146  				Eventually(joinPath(packageDir, "mock_mydisplay_test.go"), "3s").Should(SatisfyAll(
   147  					BeAnExistingFile(),
   148  					BeAFileContainingSubString("package pegomocktest_test")))
   149  				Eventually(joinPath(packageDir, "mock_subdisplay_test.go"), "3s").Should(SatisfyAll(
   150  					BeAnExistingFile(),
   151  					BeAFileContainingSubString("package pegomocktest_test")))
   152  			})
   153  		})
   154  
   155  		Context("in multiple packages and watching --recursive", func() {
   156  			It(`Eventually creates correct files in respective directories`, func() {
   157  				WriteFile(joinPath(packageDir, "interfaces_to_mock"), "MyDisplay")
   158  				WriteFile(joinPath(subPackageDir, "interfaces_to_mock"), "SubDisplay")
   159  
   160  				watch.NewMockFileUpdater([]string{packageDir}, true).Update()
   161  
   162  				Eventually(joinPath(packageDir, "mock_mydisplay_test.go"), "3s").Should(SatisfyAll(
   163  					BeAnExistingFile(),
   164  					BeAFileContainingSubString("package pegomocktest_test")))
   165  				Eventually(joinPath(subPackageDir, "mock_subdisplay_test.go"), "3s").Should(SatisfyAll(
   166  					BeAnExistingFile(),
   167  					BeAFileContainingSubString("package subpackage_test")))
   168  			})
   169  		})
   170  
   171  	})
   172  
   173  	Context("after populating interfaces_to_mock with a Go file", func() {
   174  		It(`Eventually creates a file mock_mydisplay_test.go starting with "package pegomocktest_test"`, func() {
   175  			WriteFile(joinPath(packageDir, "interfaces_to_mock"), "mydisplay.go")
   176  
   177  			watch.NewMockFileUpdater([]string{packageDir}, false).Update()
   178  
   179  			Eventually(joinPath(packageDir, "mock_mydisplay_test.go"), "3s").Should(SatisfyAll(
   180  				BeAnExistingFile(),
   181  				BeAFileContainingSubString("package pegomocktest_test")))
   182  		})
   183  	})
   184  
   185  })