File: vendor_check_go15_test.go

package info (click to toggle)
golang-ginkgo 1.2.0%2Bgit20161006.acfa16a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,324 kB
  • ctags: 1,210
  • sloc: makefile: 12
file content (202 lines) | stat: -rw-r--r-- 6,227 bytes parent folder | download | duplicates (2)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// +build !go1.6

package testsuite_test

import (
	"io/ioutil"
	"os"
	"path/filepath"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/ginkgo/ginkgo/testsuite"
	. "github.com/onsi/gomega"
)

var _ = Describe("TestSuite", func() {
	var tmpDir string
	var relTmpDir string

	writeFile := func(folder string, filename string, content string, mode os.FileMode) {
		path := filepath.Join(tmpDir, folder)
		err := os.MkdirAll(path, 0700)
		Ω(err).ShouldNot(HaveOccurred())

		path = filepath.Join(path, filename)
		ioutil.WriteFile(path, []byte(content), mode)
	}

	var origVendor string

	BeforeSuite(func() {
		origVendor = os.Getenv("GO15VENDOREXPERIMENT")
	})

	AfterSuite(func() {
		os.Setenv("GO15VENDOREXPERIMENT", origVendor)
	})

	BeforeEach(func() {
		var err error
		tmpDir, err = ioutil.TempDir("/tmp", "ginkgo")
		Ω(err).ShouldNot(HaveOccurred())

		cwd, err := os.Getwd()
		Ω(err).ShouldNot(HaveOccurred())
		relTmpDir, err = filepath.Rel(cwd, tmpDir)
		relTmpDir = "./" + relTmpDir
		Ω(err).ShouldNot(HaveOccurred())

		//go files in the root directory (no tests)
		writeFile("/", "main.go", "package main", 0666)

		//non-go files in a nested directory
		writeFile("/redherring", "big_test.jpg", "package ginkgo", 0666)

		//non-ginkgo tests in a nested directory
		writeFile("/professorplum", "professorplum_test.go", `import "testing"`, 0666)

		//ginkgo tests in a nested directory
		writeFile("/colonelmustard", "colonelmustard_test.go", `import "github.com/onsi/ginkgo"`, 0666)

		//ginkgo tests in a deeply nested directory
		writeFile("/colonelmustard/library", "library_test.go", `import "github.com/onsi/ginkgo"`, 0666)

		//ginkgo tests deeply nested in a vendored dependency
		writeFile("/vendor/mrspeacock/lounge", "lounge_test.go", `import "github.com/onsi/ginkgo"`, 0666)

		//a precompiled ginkgo test
		writeFile("/precompiled-dir", "precompiled.test", `fake-binary-file`, 0777)
		writeFile("/precompiled-dir", "some-other-binary", `fake-binary-file`, 0777)
		writeFile("/precompiled-dir", "nonexecutable.test", `fake-binary-file`, 0666)
	})

	AfterEach(func() {
		os.RemoveAll(tmpDir)
	})

	Describe("Finding precompiled test suites", func() {
		Context("if pointed at an executable file that ends with .test", func() {
			It("should return a precompiled test suite", func() {
				suite, err := PrecompiledTestSuite(filepath.Join(tmpDir, "precompiled-dir", "precompiled.test"))
				Ω(err).ShouldNot(HaveOccurred())
				Ω(suite).Should(Equal(TestSuite{
					Path:        relTmpDir + "/precompiled-dir",
					PackageName: "precompiled",
					IsGinkgo:    true,
					Precompiled: true,
				}))
			})
		})

		Context("if pointed at a directory", func() {
			It("should error", func() {
				suite, err := PrecompiledTestSuite(filepath.Join(tmpDir, "precompiled-dir"))
				Ω(suite).Should(BeZero())
				Ω(err).Should(HaveOccurred())
			})
		})

		Context("if pointed at an executable that doesn't have .test", func() {
			It("should error", func() {
				suite, err := PrecompiledTestSuite(filepath.Join(tmpDir, "precompiled-dir", "some-other-binary"))
				Ω(suite).Should(BeZero())
				Ω(err).Should(HaveOccurred())
			})
		})

		Context("if pointed at a .test that isn't executable", func() {
			It("should error", func() {
				suite, err := PrecompiledTestSuite(filepath.Join(tmpDir, "precompiled-dir", "nonexecutable.test"))
				Ω(suite).Should(BeZero())
				Ω(err).Should(HaveOccurred())
			})
		})

		Context("if pointed at a nonexisting file", func() {
			It("should error", func() {
				suite, err := PrecompiledTestSuite(filepath.Join(tmpDir, "precompiled-dir", "nope-nothing-to-see-here"))
				Ω(suite).Should(BeZero())
				Ω(err).Should(HaveOccurred())
			})
		})
	})

	Describe("scanning for suites in a directory", func() {
		Context("when there are no tests in the specified directory", func() {
			It("should come up empty", func() {
				suites := SuitesInDir(tmpDir, false)
				Ω(suites).Should(BeEmpty())
			})
		})

		Context("when there are ginkgo tests in the specified directory", func() {
			It("should return an appropriately configured suite", func() {
				suites := SuitesInDir(filepath.Join(tmpDir, "colonelmustard"), false)
				Ω(suites).Should(HaveLen(1))

				Ω(suites[0].Path).Should(Equal(relTmpDir + "/colonelmustard"))
				Ω(suites[0].PackageName).Should(Equal("colonelmustard"))
				Ω(suites[0].IsGinkgo).Should(BeTrue())
				Ω(suites[0].Precompiled).Should(BeFalse())
			})
		})

		Context("when there are non-ginkgo tests in the specified directory", func() {
			It("should return an appropriately configured suite", func() {
				suites := SuitesInDir(filepath.Join(tmpDir, "professorplum"), false)
				Ω(suites).Should(HaveLen(1))

				Ω(suites[0].Path).Should(Equal(relTmpDir + "/professorplum"))
				Ω(suites[0].PackageName).Should(Equal("professorplum"))
				Ω(suites[0].IsGinkgo).Should(BeFalse())
				Ω(suites[0].Precompiled).Should(BeFalse())
			})
		})

		Context("given GO15VENDOREXPERIMENT", func() {
			BeforeEach(func() {
				os.Setenv("GO15VENDOREXPERIMENT", "1")
			})

			AfterEach(func() {
				os.Setenv("GO15VENDOREXPERIMENT", "")
			})

			It("should skip vendor dirs", func() {
				suites := SuitesInDir(filepath.Join(tmpDir+"/vendor"), false)
				Ω(suites).Should(HaveLen(0))
			})

			It("should not recurse into vendor dirs", func() {
				suites := SuitesInDir(filepath.Join(tmpDir), true)
				Ω(suites).Should(HaveLen(3))
			})
		})

		Context("when recursively scanning", func() {
			It("should return suites for corresponding test suites, only", func() {
				suites := SuitesInDir(tmpDir, true)
				Ω(suites).Should(HaveLen(4))

				Ω(suites).Should(ContainElement(TestSuite{
					Path:        relTmpDir + "/colonelmustard",
					PackageName: "colonelmustard",
					IsGinkgo:    true,
					Precompiled: false,
				}))
				Ω(suites).Should(ContainElement(TestSuite{
					Path:        relTmpDir + "/professorplum",
					PackageName: "professorplum",
					IsGinkgo:    false,
					Precompiled: false,
				}))
				Ω(suites).Should(ContainElement(TestSuite{
					Path:        relTmpDir + "/colonelmustard/library",
					PackageName: "library",
					IsGinkgo:    true,
					Precompiled: false,
				}))
			})
		})
	})
})