Debian Codegraph @

createmock/createmock_test.go

raw symbols SCIP
1 // Copyright 2012 Aaron Jacobs. All Rights Reserved.
2 // Author: [email protected] (Aaron Jacobs)
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 package main
17
18 import (
19 "bytes"
20 "flag"
21 "fmt"
22 "go/build"
23 "io/ioutil"
24 "os"
25 "os/exec"
26 "path"
27 "syscall"
28 "testing"
29
30 . "github.com/jacobsa/ogletest"
31 )
32
33 var dumpNew = flag.Bool("dump_new", false, "Dump new golden files.")
34
35 ////////////////////////////////////////////////////////////
36 // Helpers
37 ////////////////////////////////////////////////////////////
38
39 var tempDir string
40 var createmockPath string
41
42 type CreateMockTest struct {
43 }
44
45 func TestOgletest(t *testing.T) { RunTests(t) }
46 func init() { RegisterTestSuite(&CreateMockTest{}) }
47
48 func (t *CreateMockTest) SetUpTestSuite() {
49 // Create a temporary file to hold the built createmock binary.
50 tempDir, err := ioutil.TempDir("", "createmock-")
51 if err != nil {
52 panic("Creating temporary directory: " + err.Error())
53 }
54
55 createmockPath = path.Join(tempDir, "createmock")
56
57 // Build the createmock tool so that it can be used in the tests below.
58 cmd := exec.Command("go", "build", "-o", createmockPath, "github.com/jacobsa/oglemock/createmock")
59 if output, err := cmd.CombinedOutput(); err != nil {
60 panic(fmt.Sprintf("Error building createmock: %v\n\n%s", err, output))
61 }
62 }
63
64 func (t *CreateMockTest) TearDownTestSuite() {
65 // Delete the createmock binary we built above.
66 os.RemoveAll(tempDir)
67 tempDir = ""
68 createmockPath = ""
69 }
70
71 func (t *CreateMockTest) runGoldenTest(
72 caseName string,
73 expectedReturnCode int,
74 createmockArgs ...string) {
75 // Run createmock.
76 cmd := exec.Command(createmockPath, createmockArgs...)
77 output, err := cmd.CombinedOutput()
78
79 // Make sure the process actually exited.
80 exitError, ok := err.(*exec.ExitError)
81 if err != nil && (!ok || !exitError.Exited()) {
82 panic("exec.Command.CombinedOutput: " + err.Error())
83 }
84
85 // Extract a return code.
86 var actualReturnCode int
87 if exitError != nil {
88 actualReturnCode = exitError.Sys().(syscall.WaitStatus).ExitStatus()
89 }
90
91 // Make sure the return code is correct.
92 ExpectEq(expectedReturnCode, actualReturnCode)
93
94 // Read the golden file.
95 goldenPath := path.Join("testdata", "golden."+caseName)
96 goldenData := readFileOrDie(goldenPath)
97
98 // Compare the two.
99 identical := (string(output) == string(goldenData))
100 ExpectTrue(identical, "Output doesn't match for case '%s'.", caseName)
101
102 // Write out a new golden file if requested.
103 if !identical && *dumpNew {
104 writeContentsToFileOrDie(output, goldenPath)
105 }
106 }
107
108 // Ensure that when createmock is run with the supplied args, it produces
109 // output that can be compiled.
110 func (t *CreateMockTest) runCompilationTest(createmockArgs ...string) {
111 // Create a temporary directory inside of $GOPATH to hold generated code.
112 buildPkg, err := build.Import("github.com/jacobsa/oglemock", "", build.FindOnly)
113 AssertEq(nil, err)
114
115 tmpDir, err := ioutil.TempDir(buildPkg.SrcRoot, "tmp-createmock_test-")
116 AssertEq(nil, err)
117 defer os.RemoveAll(tmpDir)
118
119 // Create a file to hold the mock code.
120 codeFile, err := os.Create(path.Join(tmpDir, "mock.go"))
121 AssertEq(nil, err)
122
123 // Run createmock and save its output to the file created above.
124 stdErrBuf := new(bytes.Buffer)
125
126 cmd := exec.Command(createmockPath, createmockArgs...)
127 cmd.Stdout = codeFile
128 cmd.Stderr = stdErrBuf
129
130 err = cmd.Run()
131 AssertEq(nil, err, "createmock stderr output:\n\n%s", stdErrBuf.String())
132 codeFile.Close()
133
134 // Run 'go build' in the directory and make sure it exits with return code
135 // zero.
136 cmd = exec.Command("go", "build")
137 cmd.Dir = tmpDir
138 output, err := cmd.CombinedOutput()
139
140 ExpectEq(nil, err, "go build output:\n\n%s", output)
141 }
142
143 func writeContentsToFileOrDie(contents []byte, path string) {
144 if err := ioutil.WriteFile(path, contents, 0600); err != nil {
145 panic("ioutil.WriteFile: " + err.Error())
146 }
147 }
148
149 func readFileOrDie(path string) []byte {
150 contents, err := ioutil.ReadFile(path)
151 if err != nil {
152 panic("ioutil.ReadFile: " + err.Error())
153 }
154
155 return contents
156 }
157
158 ////////////////////////////////////////////////////////////
159 // Tests
160 ////////////////////////////////////////////////////////////
161
162 func (t *CreateMockTest) NoPackage() {
163 t.runGoldenTest(
164 "no_package",
165 1)
166 }
167
168 func (t *CreateMockTest) NoInterfaces() {
169 t.runGoldenTest(
170 "no_interfaces",
171 1,
172 "io")
173 }
174
175 func (t *CreateMockTest) UnknownPackage() {
176 t.runGoldenTest(
177 "unknown_package",
178 1,
179 "foo/bar",
180 "Reader")
181 }
182
183 func (t *CreateMockTest) UnknownInterface() {
184 t.runGoldenTest(
185 "unknown_interface",
186 1,
187 "io",
188 "Frobnicator")
189 }
190
191 func (t *CreateMockTest) GCSBucket() {
192 t.runGoldenTest(
193 "gcs_bucket",
194 0,
195 "github.com/jacobsa/oglemock/createmock/testdata/gcs",
196 "Bucket")
197 }
198
199 func (t *CreateMockTest) GCSBucket_SamePackage() {
200 t.runGoldenTest(
201 "gcs_bucket_same_package",
202 0,
203 "--same_package",
204 "github.com/jacobsa/oglemock/createmock/testdata/gcs",
205 "Bucket")
206 }
207
208 func (t *CreateMockTest) IoReaderAndWriter() {
209 t.runCompilationTest(
210 "io",
211 "Reader",
212 "Writer")
213 }
214
215 func (t *CreateMockTest) OsFileInfo() {
216 // Note that os is also used by the code that createmock generates; there
217 // should be no conflict.
218 t.runCompilationTest(
219 "os",
220 "FileInfo")
221 }
222
223 func (t *CreateMockTest) ComplicatedSamplePackage() {
224 t.runCompilationTest(
225 "github.com/jacobsa/oglemock/generate/testdata/complicated_pkg",
226 "ComplicatedThing")
227 }
228
229 func (t *CreateMockTest) RenamedSamplePackage() {
230 t.runCompilationTest(
231 "github.com/jacobsa/oglemock/generate/testdata/renamed_pkg",
232 "SomeInterface")
233 }