/
convert_test.go
185 lines (145 loc) · 5.06 KB
/
convert_test.go
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
package ginkgo
import (
. "github.com/onsi/gomega"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)
func init() {
Describe("using ginkgo convert", func() {
BeforeEach(deleteFilesInTmp)
BeforeEach(buildGinkgo)
It("rewrites xunit tests as ginkgo tests", func() {
withTempDir(func(tempDir string) {
runGinkgoConvert()
convertedFile := readConvertedFileNamed(tempDir, "xunit_test.go")
goldmaster := readGoldMasterNamed("xunit_test.go")
Expect(convertedFile).To(Equal(goldmaster))
})
})
It("rewrites all usages of *testing.T as mr.T()", func() {
withTempDir(func(tempDir string) {
runGinkgoConvert()
convertedFile := readConvertedFileNamed(tempDir, "extra_functions_test.go")
goldmaster := readGoldMasterNamed("extra_functions_test.go")
Expect(convertedFile).To(Equal(goldmaster))
})
})
It("rewrites tests in the package dir that belong to other packages", func() {
withTempDir(func(tempDir string) {
runGinkgoConvert()
convertedFile := readConvertedFileNamed(tempDir, "outside_package_test.go")
goldMaster := readGoldMasterNamed("outside_package_test.go")
Expect(convertedFile).To(Equal(goldMaster))
})
})
It("rewrites tests in nested packages", func() {
withTempDir(func(dir string) {
runGinkgoConvert()
convertedFile := readConvertedFileNamed(dir, filepath.Join("nested", "nested_test.go"))
goldMaster := readGoldMasterNamed("nested_test.go")
Expect(convertedFile).To(Equal(goldMaster))
})
})
Context("ginkgo test suite files", func() {
It("creates a ginkgo test suite file for the package you specified", func() {
withTempDir(func(dir string) {
runGinkgoConvert()
testsuite := readConvertedFileNamed(dir, "tmp_suite_test.go")
goldmaster := readGoldMasterNamed("suite_test.go")
Expect(testsuite).To(Equal(goldmaster))
})
})
It("creates ginkgo test suites for all nested packages", func() {
withTempDir(func(dir string) {
runGinkgoConvert()
testsuite := readConvertedFileNamed(dir, "nested", "nested_suite_test.go")
goldmaster := readGoldMasterNamed("nested_suite_test.go")
Expect(testsuite).To(Equal(goldmaster))
})
})
})
It("gracefully handles existing test suite files", func() {
withTempDir(func(dir string) {
cwd, err := os.Getwd()
bytes, err := ioutil.ReadFile(filepath.Join(cwd, "convert-goldmasters", "fixtures_suite_test.go"))
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(cwd, "tmp", "tmp_suite_test.go"), bytes, 0600)
Expect(err).NotTo(HaveOccurred())
runGinkgoConvert()
})
})
})
}
func withTempDir(cb func(tempDir string)) {
cwd, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
tempDir := filepath.Join(cwd, "tmp")
err = os.MkdirAll(tempDir, os.ModeDir|os.ModeTemporary|os.ModePerm)
Expect(err).NotTo(HaveOccurred())
copyFixturesIntoTempDir("convert-fixtures", tempDir)
cb(tempDir)
}
func copyFixturesIntoTempDir(relativePathToFixtures, tempDir string) {
_, err := os.Stat(relativePathToFixtures)
if err != nil {
os.Mkdir(relativePathToFixtures, os.ModeDir|os.ModeTemporary|os.ModePerm)
}
cwd, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
files, err := ioutil.ReadDir(filepath.Join(cwd, relativePathToFixtures))
Expect(err).NotTo(HaveOccurred())
for _, f := range files {
if f.IsDir() {
nestedFixturesDir := filepath.Join(relativePathToFixtures, f.Name())
nestedTempDir := filepath.Join(tempDir, f.Name())
err = os.MkdirAll(nestedTempDir, os.ModeDir|os.ModeTemporary|os.ModePerm)
Expect(err).NotTo(HaveOccurred())
copyFixturesIntoTempDir(nestedFixturesDir, nestedTempDir)
continue
}
bytes, err := ioutil.ReadFile(filepath.Join(cwd, relativePathToFixtures, f.Name()))
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tempDir, f.Name()), bytes, 0600)
Expect(err).NotTo(HaveOccurred())
}
}
func runGinkgoConvert() {
cwd, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
pathToExecutable := filepath.Join(cwd, "tmp", "ginkgo")
err = exec.Command(pathToExecutable, "convert", "github.com/onsi/ginkgo/tmp").Run()
Expect(err).NotTo(HaveOccurred())
}
func readGoldMasterNamed(filename string) string {
cwd, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
bytes, err := ioutil.ReadFile(filepath.Join(cwd, "convert-goldmasters", filename))
Expect(err).NotTo(HaveOccurred())
return string(bytes)
}
func readConvertedFileNamed(pathComponents ...string) string {
pathToFile := filepath.Join(pathComponents...)
bytes, err := ioutil.ReadFile(pathToFile)
Expect(err).NotTo(HaveOccurred())
return string(bytes)
}
func deleteFilesInTmp() {
cwd, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
tempDir := filepath.Join(cwd, "tmp")
err = os.RemoveAll(tempDir)
Expect(err).NotTo(HaveOccurred())
}
func buildGinkgo() {
cwd, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
defer func() {
os.Chdir(cwd)
}()
err = os.Chdir(filepath.Join(cwd, "ginkgo"))
Expect(err).NotTo(HaveOccurred())
err = exec.Command("go", "build", "-o", "../tmp/ginkgo").Run()
Expect(err).NotTo(HaveOccurred())
}