/
shell.go
174 lines (136 loc) · 5.48 KB
/
shell.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
package system
import (
"log"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
///////////////////////////////////////////////////////////////////////////////
// Integration: ///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
type Shell struct {
coverage bool
gobin string
reportsPath string
defaultTimeout string
}
func NewShell(gobin, reportsPath string, coverage bool, defaultTimeout string) *Shell {
return &Shell{
coverage: coverage,
gobin: gobin,
reportsPath: reportsPath,
defaultTimeout: defaultTimeout,
}
}
func (self *Shell) GoTest(directory, packageName string, tags, arguments []string) (output string, err error) {
reportFilename := strings.Replace(packageName, "/", "-", -1)
reportPath := filepath.Join(self.reportsPath, reportFilename)
reportData := reportPath + ".txt"
reportHTML := reportPath + ".html"
tagsArg := "-tags=" + strings.Join(tags, ",")
goconvey := findGoConvey(directory, self.gobin, packageName, tagsArg).Execute()
compilation := compile(directory, self.gobin, tagsArg).Execute()
withCoverage := runWithCoverage(compilation, goconvey, self.coverage, reportData, directory, self.gobin, self.defaultTimeout, tagsArg, arguments).Execute()
final := runWithoutCoverage(compilation, withCoverage, goconvey, directory, self.gobin, self.defaultTimeout, tagsArg, arguments).Execute()
go generateReports(final, self.coverage, directory, self.gobin, reportData, reportHTML).Execute()
return final.Output, final.Error
}
///////////////////////////////////////////////////////////////////////////////
// Functional Core:////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
func findGoConvey(directory, gobin, packageName, tagsArg string) Command {
return NewCommand(directory, gobin, "list", "-f", "'{{.TestImports}}{{.XTestImports}}'", tagsArg, packageName)
}
func compile(directory, gobin, tagsArg string) Command {
return NewCommand(directory, gobin, "test", "-i", tagsArg)
}
func runWithCoverage(compile, goconvey Command, coverage bool, reportPath, directory, gobin, defaultTimeout, tagsArg string, customArguments []string) Command {
if compile.Error != nil || goconvey.Error != nil {
return compile
}
if !coverage {
return compile
}
arguments := []string{"test", "-v", "-coverprofile=" + reportPath, tagsArg}
customArgsText := strings.Join(customArguments, "\t")
if !strings.Contains(customArgsText, "-covermode=") {
arguments = append(arguments, "-covermode=set")
}
if !strings.Contains(customArgsText, "-timeout=") {
arguments = append(arguments, "-timeout="+defaultTimeout)
}
if strings.Contains(goconvey.Output, goconveyDSLImport) {
arguments = append(arguments, "-convey-json")
}
arguments = append(arguments, customArguments...)
return NewCommand(directory, gobin, arguments...)
}
func runWithoutCoverage(compile, withCoverage, goconvey Command, directory, gobin, defaultTimeout, tagsArg string, customArguments []string) Command {
if compile.Error != nil {
return compile
}
if goconvey.Error != nil {
log.Println(gopathProblem, goconvey.Output, goconvey.Error)
return goconvey
}
if coverageStatementRE.MatchString(withCoverage.Output) {
return withCoverage
}
log.Printf("Coverage output: %v", withCoverage.Output)
log.Print("Run without coverage")
arguments := []string{"test", "-v", tagsArg}
customArgsText := strings.Join(customArguments, "\t")
if !strings.Contains(customArgsText, "-timeout=") {
arguments = append(arguments, "-timeout="+defaultTimeout)
}
if strings.Contains(goconvey.Output, goconveyDSLImport) {
arguments = append(arguments, "-convey-json")
}
arguments = append(arguments, customArguments...)
return NewCommand(directory, gobin, arguments...)
}
func generateReports(previous Command, coverage bool, directory, gobin, reportData, reportHTML string) Command {
if previous.Error != nil {
return previous
}
if !coverage {
return previous
}
return NewCommand(directory, gobin, "tool", "cover", "-html="+reportData, "-o", reportHTML)
}
///////////////////////////////////////////////////////////////////////////////
// Imperative Shell: //////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
type Command struct {
directory string
executable string
arguments []string
Output string
Error error
}
func NewCommand(directory, executable string, arguments ...string) Command {
return Command{
directory: directory,
executable: executable,
arguments: arguments,
}
}
func (this Command) Execute() Command {
if len(this.executable) == 0 {
return this
}
if len(this.Output) > 0 || this.Error != nil {
return this
}
command := exec.Command(this.executable, this.arguments...)
command.Dir = this.directory
var rawOutput []byte
rawOutput, this.Error = command.CombinedOutput()
this.Output = string(rawOutput)
return this
}
///////////////////////////////////////////////////////////////////////////////
const goconveyDSLImport = "github.com/smartystreets/goconvey/convey " // note the trailing space: we don't want to target packages nested in the /convey package.
const gopathProblem = "Please run goconvey from within $GOPATH/src (also, symlinks might be problematic). Output and Error: "
var coverageStatementRE = regexp.MustCompile(`(?m)^coverage: \d+\.\d% of statements(.*)$|^panic: test timed out after `)