From a763be942e340ad8a9486cc8c16b4b3af88930c2 Mon Sep 17 00:00:00 2001 From: Tim Jarratt Date: Tue, 13 Oct 2015 22:14:49 -0700 Subject: [PATCH] Add an integration test Covers the case where both a path to a file and an interface are provided --- integration/integration_test.go | 116 ++++++++++++++++++++++++++++++++ integration/suite_test.go | 13 ++++ 2 files changed, 129 insertions(+) create mode 100644 integration/integration_test.go create mode 100644 integration/suite_test.go diff --git a/integration/integration_test.go b/integration/integration_test.go new file mode 100644 index 0000000..4c35815 --- /dev/null +++ b/integration/integration_test.go @@ -0,0 +1,116 @@ +package integration_test + +import ( + "io" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + + "github.com/onsi/gomega/gexec" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("The counterfeiter CLI", func() { + var pathToCLI string + + BeforeEach(func() { + pathToCLI = tmpPath("counterfeiter") + copyIn("something.go", pathToCLI) + }) + + Describe("when given two arguments", func() { + It("writes a fake for the given interface from the provided file", func() { + session := startCounterfeiter(pathToCLI, "something.go", "Something") + + Eventually(session).Should(gexec.Exit(0)) + output := string(session.Out.Contents()) + + Expect(output).To(ContainSubstring("Wrote `FakeSomething`")) + }) + }) + + Describe("when provided three arguments", func() { + It("writes the fake to stdout", func() { + session := startCounterfeiter(pathToCLI, "something.go", "Something", "-") + + Eventually(session).Should(gexec.Exit(0)) + output := string(session.Out.Contents()) + + Expect(output).To(ContainSubstring("// This file was generated by counterfeiter")) + }) + }) +}) + +// helper functions + +func tmpPath(destination string) string { + return filepath.Join(tmpDir, "src", destination) +} + +func copyIn(fixture string, destination string) { + err := os.MkdirAll(destination, 0777) + Expect(err).ToNot(HaveOccurred()) + + filepath.Walk(filepath.Join("..", "fixtures", fixture), func(path string, info os.FileInfo, err error) error { + if info.IsDir() { + return nil + } + + base := filepath.Base(path) + + src, err := os.Open(path) + Expect(err).ToNot(HaveOccurred()) + + dst, err := os.Create(filepath.Join(destination, base)) + Expect(err).ToNot(HaveOccurred()) + + _, err = io.Copy(dst, src) + Expect(err).ToNot(HaveOccurred()) + return nil + }) +} + +func startCounterfeiter(workingDir string, args ...string) *gexec.Session { + fakeGoPathDir := filepath.Dir(filepath.Dir(workingDir)) + absPath, _ := filepath.Abs(fakeGoPathDir) + absPathWithSymlinks, _ := filepath.EvalSymlinks(absPath) + + cmd := exec.Command(pathToCounterfeiter, args...) + cmd.Dir = workingDir + cmd.Env = []string{"GOPATH=" + absPathWithSymlinks} + + session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) + Expect(err).ToNot(HaveOccurred()) + return session +} + +// gexec setup + +var tmpDir string +var pathToCounterfeiter string + +var _ = SynchronizedBeforeSuite(func() []byte { + pathToCounterfeiter, err := gexec.Build("github.com/maxbrunsfeld/counterfeiter") + Expect(err).ToNot(HaveOccurred()) + return []byte(pathToCounterfeiter) +}, func(computedPath []byte) { + pathToCounterfeiter = string(computedPath) +}) + +var _ = SynchronizedAfterSuite(func() {}, func() { + gexec.CleanupBuildArtifacts() +}) + +var _ = BeforeEach(func() { + var err error + tmpDir, err = ioutil.TempDir("", "counterfeiter-integration") + Expect(err).ToNot(HaveOccurred()) +}) + +var _ = AfterEach(func() { + err := os.RemoveAll(tmpDir) + Expect(err).ToNot(HaveOccurred()) +}) diff --git a/integration/suite_test.go b/integration/suite_test.go new file mode 100644 index 0000000..a39e117 --- /dev/null +++ b/integration/suite_test.go @@ -0,0 +1,13 @@ +package integration_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "testing" +) + +func TestCounterfeiterCLI(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Counterfeiter CLI Suite") +}