Skip to content

Commit

Permalink
Add an integration test
Browse files Browse the repository at this point in the history
Covers the case where both a path to a file and an interface are provided
  • Loading branch information
tjarratt authored and Tim Jarratt committed Oct 20, 2015
1 parent 3215e58 commit a763be9
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
116 changes: 116 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
13 changes: 13 additions & 0 deletions integration/suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit a763be9

Please sign in to comment.