/usr/share/gocode/src/github.com/jacobsa/oglemock/createmock/createmock_test.go is in golang-github-jacobsa-oglemock-dev 0.0~git20150428-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 203 204 205 206 207 208 209 210 211 212 213 214 215 | // Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"flag"
"fmt"
. "github.com/jacobsa/ogletest"
"go/build"
"io/ioutil"
"os"
"os/exec"
"path"
"syscall"
"testing"
)
var dumpNew = flag.Bool("dump_new", false, "Dump new golden files.")
////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////
var tempDir string
var createmockPath string
type CreateMockTest struct {
}
func TestOgletest(t *testing.T) { RunTests(t) }
func init() { RegisterTestSuite(&CreateMockTest{}) }
func (t *CreateMockTest) SetUpTestSuite() {
// Create a temporary file to hold the built createmock binary.
tempDir, err := ioutil.TempDir("", "createmock-")
if err != nil {
panic("Creating temporary directory: " + err.Error())
}
createmockPath = path.Join(tempDir, "createmock")
// Build the createmock tool so that it can be used in the tests below.
cmd := exec.Command("go", "build", "-o", createmockPath, "github.com/jacobsa/oglemock/createmock")
if output, err := cmd.CombinedOutput(); err != nil {
panic(fmt.Sprintf("Error building createmock: %v\n\n%s", err, output))
}
}
func (t *CreateMockTest) TearDownTestSuite() {
// Delete the createmock binary we built above.
os.RemoveAll(tempDir)
tempDir = ""
createmockPath = ""
}
func (t *CreateMockTest) runGoldenTest(
caseName string,
expectedReturnCode int,
createmockArgs ...string) {
// Run createmock.
cmd := exec.Command(createmockPath, createmockArgs...)
output, err := cmd.CombinedOutput()
// Make sure the process actually exited.
exitError, ok := err.(*exec.ExitError)
if err != nil && (!ok || !exitError.Exited()) {
panic("exec.Command.CombinedOutput: " + err.Error())
}
// Extract a return code.
var actualReturnCode int
if exitError != nil {
actualReturnCode = exitError.Sys().(syscall.WaitStatus).ExitStatus()
}
// Make sure the return code is correct.
ExpectEq(expectedReturnCode, actualReturnCode)
// Read the golden file.
goldenPath := path.Join("test_cases", "golden."+caseName)
goldenData := readFileOrDie(goldenPath)
// Compare the two.
identical := (string(output) == string(goldenData))
ExpectTrue(identical, "Output doesn't match for case '%s'.", caseName)
// Write out a new golden file if requested.
if !identical && *dumpNew {
writeContentsToFileOrDie(output, goldenPath)
}
}
// Ensure that when createmock is run with the supplied args, it produces
// output that can be compiled.
func (t *CreateMockTest) runCompilationTest(createmockArgs ...string) {
// Create a temporary directory inside of $GOPATH to hold generated code.
buildPkg, err := build.Import("github.com/jacobsa/oglemock", "", build.FindOnly)
AssertEq(nil, err)
tmpDir, err := ioutil.TempDir(buildPkg.SrcRoot, "tmp-createmock_test-")
AssertEq(nil, err)
defer os.RemoveAll(tmpDir)
// Create a file to hold the mock code.
codeFile, err := os.Create(path.Join(tmpDir, "mock.go"))
AssertEq(nil, err)
// Run createmock and save its output to the file created above.
stdErrBuf := new(bytes.Buffer)
cmd := exec.Command(createmockPath, createmockArgs...)
cmd.Stdout = codeFile
cmd.Stderr = stdErrBuf
err = cmd.Run()
AssertEq(nil, err, "createmock stderr output:\n\n%s", stdErrBuf.String())
codeFile.Close()
// Run 'go build' in the directory and make sure it exits with return code
// zero.
cmd = exec.Command("go", "build")
cmd.Dir = tmpDir
output, err := cmd.CombinedOutput()
ExpectEq(nil, err, "go build output:\n\n%s", output)
}
func writeContentsToFileOrDie(contents []byte, path string) {
if err := ioutil.WriteFile(path, contents, 0600); err != nil {
panic("ioutil.WriteFile: " + err.Error())
}
}
func readFileOrDie(path string) []byte {
contents, err := ioutil.ReadFile(path)
if err != nil {
panic("ioutil.ReadFile: " + err.Error())
}
return contents
}
////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////
func (t *CreateMockTest) NoPackage() {
t.runGoldenTest(
"no_package",
1)
}
func (t *CreateMockTest) NoInterfaces() {
t.runGoldenTest(
"no_interfaces",
1,
"io")
}
func (t *CreateMockTest) UnknownPackage() {
t.runGoldenTest(
"unknown_package",
1,
"foo/bar",
"Reader")
}
func (t *CreateMockTest) UnknownInterface() {
t.runGoldenTest(
"unknown_interface",
1,
"io",
"Frobnicator")
}
func (t *CreateMockTest) IoReaderAndWriter() {
t.runCompilationTest(
"io",
"Reader",
"Writer")
}
func (t *CreateMockTest) OsFileInfo() {
// Note that os is also used by the code that createmock generates; there
// should be no conflict.
t.runCompilationTest(
"os",
"FileInfo")
}
func (t *CreateMockTest) ComplicatedSamplePackage() {
t.runCompilationTest(
"github.com/jacobsa/oglemock/generate/test_cases/complicated_pkg",
"ComplicatedThing")
}
func (t *CreateMockTest) RenamedSamplePackage() {
t.runCompilationTest(
"github.com/jacobsa/oglemock/generate/test_cases/renamed_pkg",
"SomeInterface")
}
|