forked from maxbrunsfeld/counterfeiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Covers the case where both a path to a file and an interface are provided
- Loading branch information
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |