-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
151 lines (119 loc) · 3.44 KB
/
main.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
// Copyright 2013 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"syscall"
"github.com/qur/withmock/lib"
)
var (
raw = flag.Bool("raw", false, "don't rewrite the command output")
work = flag.Bool("work", false, "print the name of the temporary work directory and do not delete it when exiting")
gocov = flag.Bool("gocov", false, "install gocov package into temporary GOPATH")
pkgFile = flag.String("P", "", "install extra packages listed in the given file")
exclFile = flag.String("exclude", "", "any package listed in the given file will not be mocked, even if marked in test code.")
cfgFile = flag.String("c", "", "load config from the specified file")
debug = flag.Bool("debug", false, "enable extra output for debugging mock genertion issues")
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s [options] <command> [arguments]*\n",
os.Args[0])
fmt.Fprintf(os.Stderr, "\nRun the specified command in an environment "+
"where imports of the package in the current directory which are "+
"marked for mocking are replacement by automatically generated mock "+
"versions for use with gomock.\n\n")
fmt.Fprintf(os.Stderr, "options:\n\n")
flag.PrintDefaults()
}
func main() {
err := doit()
if exit, ok := err.(*exec.ExitError); ok {
ws := exit.Sys().(syscall.WaitStatus)
os.Exit(ws.ExitStatus())
}
if c, ok := err.(lib.Cerr); *debug && ok {
fmt.Fprintf(os.Stderr, "ERROR(%s): %s\n", c.Context(), err)
os.Exit(1)
}
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
}
func doit() error {
// Before we get to work, parse the command line
flag.Usage = usage
flag.Parse()
if !*debug {
// Debug not enabled, so send logging into the void
w, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Failed to open null output: %s", err)
os.Exit(1)
}
defer w.Close()
log.SetOutput(w)
}
// We need at least one argument
if flag.NArg() < 1 {
usage()
os.Exit(1)
}
// First we need to create a context
ctxt, err := lib.NewContext()
if err != nil {
return err
}
defer ctxt.Close()
if *work {
ctxt.KeepWork()
}
if *raw {
ctxt.DisableRewrite()
}
// Load the excluded packages file if configured
if *exclFile != "" {
if err := ctxt.ExcludePackagesFromFile(*exclFile); err != nil {
return err
}
}
// Load the config file if specified
if *cfgFile != "" {
if err := ctxt.LoadConfig(*cfgFile); err != nil {
return err
}
}
// Now we add the package that we want to test to the context, this will
// install the imports used by that package (mocking them as approprite).
pkg, err := lib.GetOutput("go", "list", ".")
if err != nil {
return err
}
testPkg, err := ctxt.AddPackage(pkg)
if err != nil {
return err
}
// Add extra packages if configured
if *pkgFile != "" {
if err := ctxt.LinkPackagesFromFile(*pkgFile); err != nil {
return err
}
}
// Add in the gocov library, so that we can run with gocov if we want.
if flag.Arg(0) == "gocov" || *gocov {
if err := ctxt.LinkPackage("github.com/axw/gocov"); err != nil {
return err
}
}
// Finally we can chdir into the test code, and run the command inside the
// context
if err := ctxt.Chdir(testPkg); err != nil {
return err
}
return ctxt.Run(flag.Arg(0), flag.Args()[1:]...)
}