Skip to content

Commit

Permalink
Use separate fixture JSON files in tests
Browse files Browse the repository at this point in the history
Previously relied on a build/build.json file which was shared amongst
tests -- a fragile approach that couldn't be parallelised safely.

[#4]

Signed-off-by: Jacques Chester <jchester@pivotal.io>
  • Loading branch information
jchester authored and jchesterpivotal committed Aug 18, 2018
1 parent 78d895f commit 0be7b64
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions cmd/build-pass-fail/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ func TestBuildPassFail(t *testing.T) {
if err != nil {
gt.Expect(err).NotTo(gomega.MatchError("build: file exists"))
}

cmd = exec.Command(compiledPath)
})

it.After(func() {
Expand All @@ -46,6 +44,7 @@ func TestBuildPassFail(t *testing.T) {
when("a path to json file is not given", func() {
when("there is no build/build.json", func() {
it.Before(func() {
cmd = exec.Command(compiledPath)
session, err = gexec.Start(cmd, it.Out(), it.Out())
gt.Expect(err).NotTo(gomega.HaveOccurred())
})
Expand Down Expand Up @@ -87,10 +86,10 @@ func TestBuildPassFail(t *testing.T) {
})
})

when("there is a build/build.json", func() {
when("build.json represents a successful build", func() {
when("there is a well-formed JSON file", func() {
when("the file represents a successful build", func() {
it.Before(func() {
completed, err := os.Create(filepath.Join("build", "build.json"))
completed, err := os.Create(filepath.Join("build", "successful-build.json"))
gt.Expect(err).NotTo(gomega.HaveOccurred())

_, err = completed.WriteString(`{
Expand All @@ -102,6 +101,7 @@ func TestBuildPassFail(t *testing.T) {
}`)
gt.Expect(err).NotTo(gomega.HaveOccurred())

cmd = exec.Command(compiledPath, "build/successful-build.json")
session, err = gexec.Start(cmd, it.Out(), it.Out())
gt.Expect(err).NotTo(gomega.HaveOccurred())
})
Expand All @@ -115,9 +115,9 @@ func TestBuildPassFail(t *testing.T) {
})
})

when("build.json represents an unsuccessful build", func() {
when("the file represents an unsuccessful build", func() {
it.Before(func() {
completed, err := os.Create(filepath.Join("build", "build.json"))
completed, err := os.Create(filepath.Join("build", "unsuccessful-build.json"))
gt.Expect(err).NotTo(gomega.HaveOccurred())

_, err = completed.WriteString(`{
Expand All @@ -129,6 +129,7 @@ func TestBuildPassFail(t *testing.T) {
}`)
gt.Expect(err).NotTo(gomega.HaveOccurred())

cmd = exec.Command(compiledPath, "build/unsuccessful-build.json")
session, err = gexec.Start(cmd, it.Out(), it.Out())
gt.Expect(err).NotTo(gomega.HaveOccurred())
})
Expand All @@ -141,22 +142,23 @@ func TestBuildPassFail(t *testing.T) {
gt.Eventually(session).Should(gexec.Exit(1))
})
})
})

when("the build.json file is malformed", func() {
it.Before(func() {
malformed, err := os.Create(filepath.Join("build", "build.json"))
gt.Expect(err).NotTo(gomega.HaveOccurred())
when("the JSON file is malformed", func() {
it.Before(func() {
malformed, err := os.Create(filepath.Join("build", "malformed-build.json"))
gt.Expect(err).NotTo(gomega.HaveOccurred())

_, err = malformed.WriteString(`} { [] {{ malformed JSON file: ""`)
gt.Expect(err).NotTo(gomega.HaveOccurred())
_, err = malformed.WriteString(`} { [] {{ malformed JSON file: ""`)
gt.Expect(err).NotTo(gomega.HaveOccurred())

session, err = gexec.Start(cmd, it.Out(), it.Out())
gt.Expect(err).NotTo(gomega.HaveOccurred())
})
cmd = exec.Command(compiledPath, "build/malformed-build.json")
session, err = gexec.Start(cmd, it.Out(), it.Out())
gt.Expect(err).NotTo(gomega.HaveOccurred())
})

it("fails with an error", func() {
gt.Eventually(session.Err).Should(gbytes.Say("could not parse build/build.json"))
})
it("fails with an error", func() {
gt.Eventually(session.Err).Should(gbytes.Say("could not parse build/malformed-build.json"))
})
})
}, spec.Report(report.Terminal{}))
Expand Down

0 comments on commit 0be7b64

Please sign in to comment.