1package integration_test
2
3import (
4	"os/exec"
5
6	"fmt"
7
8	. "github.com/onsi/ginkgo"
9	. "github.com/onsi/gomega"
10	"github.com/onsi/gomega/gbytes"
11	"github.com/onsi/gomega/gexec"
12)
13
14var _ = Describe("Coverage Specs", func() {
15	Context("when it runs coverage analysis in series and in parallel", func() {
16		AfterEach(func() {
17			removeSuccessfully("./_fixtures/coverage_fixture/coverage_fixture.coverprofile")
18		})
19		It("works", func() {
20			session := startGinkgo("./_fixtures/coverage_fixture", "-cover")
21			Eventually(session).Should(gexec.Exit(0))
22
23			Ω(session.Out).Should(gbytes.Say(("coverage: 80.0% of statements")))
24
25			coverFile := "./_fixtures/coverage_fixture/coverage_fixture.coverprofile"
26			serialCoverProfileOutput, err := exec.Command("go", "tool", "cover", fmt.Sprintf("-func=%s", coverFile)).CombinedOutput()
27			Ω(err).ShouldNot(HaveOccurred())
28
29			removeSuccessfully(coverFile)
30
31			Eventually(startGinkgo("./_fixtures/coverage_fixture", "-cover", "-nodes=4")).Should(gexec.Exit(0))
32
33			parallelCoverProfileOutput, err := exec.Command("go", "tool", "cover", fmt.Sprintf("-func=%s", coverFile)).CombinedOutput()
34			Ω(err).ShouldNot(HaveOccurred())
35
36			Ω(parallelCoverProfileOutput).Should(Equal(serialCoverProfileOutput))
37
38			By("handling external packages", func() {
39				session = startGinkgo("./_fixtures/coverage_fixture", "-coverpkg=github.com/onsi/ginkgo/integration/_fixtures/coverage_fixture,github.com/onsi/ginkgo/integration/_fixtures/coverage_fixture/external_coverage_fixture")
40				Eventually(session).Should(gexec.Exit(0))
41
42				Ω(session.Out).Should(gbytes.Say("coverage: 71.4% of statements in github.com/onsi/ginkgo/integration/_fixtures/coverage_fixture, github.com/onsi/ginkgo/integration/_fixtures/coverage_fixture/external_coverage_fixture"))
43
44				serialCoverProfileOutput, err = exec.Command("go", "tool", "cover", fmt.Sprintf("-func=%s", coverFile)).CombinedOutput()
45				Ω(err).ShouldNot(HaveOccurred())
46
47				removeSuccessfully("./_fixtures/coverage_fixture/coverage_fixture.coverprofile")
48
49				Eventually(startGinkgo("./_fixtures/coverage_fixture", "-coverpkg=github.com/onsi/ginkgo/integration/_fixtures/coverage_fixture,github.com/onsi/ginkgo/integration/_fixtures/coverage_fixture/external_coverage_fixture", "-nodes=4")).Should(gexec.Exit(0))
50
51				parallelCoverProfileOutput, err = exec.Command("go", "tool", "cover", fmt.Sprintf("-func=%s", coverFile)).CombinedOutput()
52				Ω(err).ShouldNot(HaveOccurred())
53
54				Ω(parallelCoverProfileOutput).Should(Equal(serialCoverProfileOutput))
55			})
56		})
57	})
58
59	Context("when a custom profile name is specified", func() {
60		AfterEach(func() {
61			removeSuccessfully("./_fixtures/coverage_fixture/coverage.txt")
62		})
63
64		It("generates cover profiles with the specified name", func() {
65			session := startGinkgo("./_fixtures/coverage_fixture", "-cover", "-coverprofile=coverage.txt")
66			Eventually(session).Should(gexec.Exit(0))
67
68			Ω("./_fixtures/coverage_fixture/coverage.txt").Should(BeARegularFile())
69		})
70	})
71
72	Context("when run in recursive mode", func() {
73		AfterEach(func() {
74			removeSuccessfully("./_fixtures/combined_coverage_fixture/coverage-recursive.txt")
75			removeSuccessfully("./_fixtures/combined_coverage_fixture/first_package/coverage-recursive.txt")
76			removeSuccessfully("./_fixtures/combined_coverage_fixture/second_package/coverage-recursive.txt")
77		})
78
79		It("generates a coverage file per package", func() {
80			session := startGinkgo("./_fixtures/combined_coverage_fixture", "-r", "-cover", "-coverprofile=coverage-recursive.txt")
81			Eventually(session).Should(gexec.Exit(0))
82
83			Ω("./_fixtures/combined_coverage_fixture/first_package/coverage-recursive.txt").Should(BeARegularFile())
84			Ω("./_fixtures/combined_coverage_fixture/second_package/coverage-recursive.txt").Should(BeARegularFile())
85		})
86	})
87
88	Context("when run in parallel mode", func() {
89		AfterEach(func() {
90			removeSuccessfully("./_fixtures/coverage_fixture/coverage-parallel.txt")
91		})
92
93		It("works", func() {
94			session := startGinkgo("./_fixtures/coverage_fixture", "-p", "-cover", "-coverprofile=coverage-parallel.txt")
95
96			Eventually(session).Should(gexec.Exit(0))
97
98			Ω("./_fixtures/coverage_fixture/coverage-parallel.txt").Should(BeARegularFile())
99		})
100	})
101
102	Context("when run in recursive mode specifying a coverprofile", func() {
103		AfterEach(func() {
104			removeSuccessfully("./_fixtures/combined_coverage_fixture/coverprofile-recursive.txt")
105			removeSuccessfully("./_fixtures/combined_coverage_fixture/first_package/coverprofile-recursive.txt")
106			removeSuccessfully("./_fixtures/combined_coverage_fixture/second_package/coverprofile-recursive.txt")
107		})
108
109		It("combines the coverages", func() {
110			session := startGinkgo("./_fixtures/combined_coverage_fixture", "-outputdir=./", "-r", "-cover", "-coverprofile=coverprofile-recursive.txt")
111			Eventually(session).Should(gexec.Exit(0))
112
113			By("generating a combined coverage file", func() {
114				Ω("./_fixtures/combined_coverage_fixture/coverprofile-recursive.txt").Should(BeARegularFile())
115			})
116
117			By("also generating the single package coverage files", func() {
118				Ω("./_fixtures/combined_coverage_fixture/first_package/coverprofile-recursive.txt").Should(BeARegularFile())
119				Ω("./_fixtures/combined_coverage_fixture/second_package/coverprofile-recursive.txt").Should(BeARegularFile())
120			})
121		})
122	})
123
124	It("Fails with an error if output dir and coverprofile were set, but the output dir did not exist", func() {
125		session := startGinkgo("./_fixtures/combined_coverage_fixture", "-outputdir=./all/profiles/here", "-r", "-cover", "-coverprofile=coverage.txt")
126
127		Eventually(session).Should(gexec.Exit(1))
128		output := session.Out.Contents()
129		Ω(string(output)).Should(ContainSubstring("Unable to create combined profile, outputdir does not exist: ./all/profiles/here"))
130	})
131
132	Context("when only output dir was set", func() {
133		AfterEach(func() {
134			removeSuccessfully("./_fixtures/combined_coverage_fixture/first_package.coverprofile")
135			removeSuccessfully("./_fixtures/combined_coverage_fixture/first_package/coverage.txt")
136			removeSuccessfully("./_fixtures/combined_coverage_fixture/second_package.coverprofile")
137			removeSuccessfully("./_fixtures/combined_coverage_fixture/second_package/coverage.txt")
138		})
139		It("moves coverages", func() {
140			session := startGinkgo("./_fixtures/combined_coverage_fixture", "-outputdir=./", "-r", "-cover")
141			Eventually(session).Should(gexec.Exit(0))
142
143			Ω("./_fixtures/combined_coverage_fixture/first_package.coverprofile").Should(BeARegularFile())
144			Ω("./_fixtures/combined_coverage_fixture/second_package.coverprofile").Should(BeARegularFile())
145		})
146	})
147})
148