aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/ginkgo/integration/coverage_test.go
blob: a1d24bfed6766c123013484a0418aa8c3b1711bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package integration_test

import (
	"os/exec"

	"fmt"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	"github.com/onsi/gomega/gexec"
)

var _ = Describe("Coverage Specs", func() {
	Context("when it runs coverage analysis in series and in parallel", func() {
		AfterEach(func() {
			removeSuccessfully("./_fixtures/coverage_fixture/coverage_fixture.coverprofile")
		})
		It("works", func() {
			session := startGinkgo("./_fixtures/coverage_fixture", "-cover")
			Eventually(session).Should(gexec.Exit(0))

			Ω(session.Out).Should(gbytes.Say(("coverage: 80.0% of statements")))

			coverFile := "./_fixtures/coverage_fixture/coverage_fixture.coverprofile"
			serialCoverProfileOutput, err := exec.Command("go", "tool", "cover", fmt.Sprintf("-func=%s", coverFile)).CombinedOutput()
			Ω(err).ShouldNot(HaveOccurred())

			removeSuccessfully(coverFile)

			Eventually(startGinkgo("./_fixtures/coverage_fixture", "-cover", "-nodes=4")).Should(gexec.Exit(0))

			parallelCoverProfileOutput, err := exec.Command("go", "tool", "cover", fmt.Sprintf("-func=%s", coverFile)).CombinedOutput()
			Ω(err).ShouldNot(HaveOccurred())

			Ω(parallelCoverProfileOutput).Should(Equal(serialCoverProfileOutput))

			By("handling external packages", func() {
				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")
				Eventually(session).Should(gexec.Exit(0))

				Ω(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"))

				serialCoverProfileOutput, err = exec.Command("go", "tool", "cover", fmt.Sprintf("-func=%s", coverFile)).CombinedOutput()
				Ω(err).ShouldNot(HaveOccurred())

				removeSuccessfully("./_fixtures/coverage_fixture/coverage_fixture.coverprofile")

				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))

				parallelCoverProfileOutput, err = exec.Command("go", "tool", "cover", fmt.Sprintf("-func=%s", coverFile)).CombinedOutput()
				Ω(err).ShouldNot(HaveOccurred())

				Ω(parallelCoverProfileOutput).Should(Equal(serialCoverProfileOutput))
			})
		})
	})

	Context("when a custom profile name is specified", func() {
		AfterEach(func() {
			removeSuccessfully("./_fixtures/coverage_fixture/coverage.txt")
		})

		It("generates cover profiles with the specified name", func() {
			session := startGinkgo("./_fixtures/coverage_fixture", "-cover", "-coverprofile=coverage.txt")
			Eventually(session).Should(gexec.Exit(0))

			Ω("./_fixtures/coverage_fixture/coverage.txt").Should(BeARegularFile())
		})
	})

	Context("when run in recursive mode", func() {
		AfterEach(func() {
			removeSuccessfully("./_fixtures/combined_coverage_fixture/coverage-recursive.txt")
			removeSuccessfully("./_fixtures/combined_coverage_fixture/first_package/coverage-recursive.txt")
			removeSuccessfully("./_fixtures/combined_coverage_fixture/second_package/coverage-recursive.txt")
		})

		It("generates a coverage file per package", func() {
			session := startGinkgo("./_fixtures/combined_coverage_fixture", "-r", "-cover", "-coverprofile=coverage-recursive.txt")
			Eventually(session).Should(gexec.Exit(0))

			Ω("./_fixtures/combined_coverage_fixture/first_package/coverage-recursive.txt").Should(BeARegularFile())
			Ω("./_fixtures/combined_coverage_fixture/second_package/coverage-recursive.txt").Should(BeARegularFile())
		})
	})

	Context("when run in parallel mode", func() {
		AfterEach(func() {
			removeSuccessfully("./_fixtures/coverage_fixture/coverage-parallel.txt")
		})

		It("works", func() {
			session := startGinkgo("./_fixtures/coverage_fixture", "-p", "-cover", "-coverprofile=coverage-parallel.txt")

			Eventually(session).Should(gexec.Exit(0))

			Ω("./_fixtures/coverage_fixture/coverage-parallel.txt").Should(BeARegularFile())
		})
	})

	Context("when run in recursive mode specifying a coverprofile", func() {
		AfterEach(func() {
			removeSuccessfully("./_fixtures/combined_coverage_fixture/coverprofile-recursive.txt")
			removeSuccessfully("./_fixtures/combined_coverage_fixture/first_package/coverprofile-recursive.txt")
			removeSuccessfully("./_fixtures/combined_coverage_fixture/second_package/coverprofile-recursive.txt")
		})

		It("combines the coverages", func() {
			session := startGinkgo("./_fixtures/combined_coverage_fixture", "-outputdir=./", "-r", "-cover", "-coverprofile=coverprofile-recursive.txt")
			Eventually(session).Should(gexec.Exit(0))

			By("generating a combined coverage file", func() {
				Ω("./_fixtures/combined_coverage_fixture/coverprofile-recursive.txt").Should(BeARegularFile())
			})

			By("also generating the single package coverage files", func() {
				Ω("./_fixtures/combined_coverage_fixture/first_package/coverprofile-recursive.txt").Should(BeARegularFile())
				Ω("./_fixtures/combined_coverage_fixture/second_package/coverprofile-recursive.txt").Should(BeARegularFile())
			})
		})
	})

	It("Fails with an error if output dir and coverprofile were set, but the output dir did not exist", func() {
		session := startGinkgo("./_fixtures/combined_coverage_fixture", "-outputdir=./all/profiles/here", "-r", "-cover", "-coverprofile=coverage.txt")

		Eventually(session).Should(gexec.Exit(1))
		output := session.Out.Contents()
		Ω(string(output)).Should(ContainSubstring("Unable to create combined profile, outputdir does not exist: ./all/profiles/here"))
	})

	Context("when only output dir was set", func() {
		AfterEach(func() {
			removeSuccessfully("./_fixtures/combined_coverage_fixture/first_package.coverprofile")
			removeSuccessfully("./_fixtures/combined_coverage_fixture/first_package/coverage.txt")
			removeSuccessfully("./_fixtures/combined_coverage_fixture/second_package.coverprofile")
			removeSuccessfully("./_fixtures/combined_coverage_fixture/second_package/coverage.txt")
		})
		It("moves coverages", func() {
			session := startGinkgo("./_fixtures/combined_coverage_fixture", "-outputdir=./", "-r", "-cover")
			Eventually(session).Should(gexec.Exit(0))

			Ω("./_fixtures/combined_coverage_fixture/first_package.coverprofile").Should(BeARegularFile())
			Ω("./_fixtures/combined_coverage_fixture/second_package.coverprofile").Should(BeARegularFile())
		})
	})
})