-
Notifications
You must be signed in to change notification settings - Fork 2
/
BaseSpec.groovy
225 lines (189 loc) · 6.79 KB
/
BaseSpec.groovy
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
216
217
218
219
220
221
222
223
224
225
package grails.functional.tests
import geb.spock.GebSpec
import grails.functional.tests.internal.GrailsExecutor
import geb.Browser
import org.apache.commons.io.FileUtils
import geb.spock.GebReportingSpec
import org.openqa.selenium.htmlunit.HtmlUnitDriver
@WebReport
abstract class BaseSpec extends GebReportingSpec{
static PORT = 8184
static PROCESS_TIMEOUT_MILLS = 1000 * 60 * 5 // 5 minutes
static upgradedProjects = []
static grailsHome = new File(requiredSysProp('grailsHome', "../grails-master")).canonicalPath
static grailsWorkDir = requiredSysProp('grailsWorkDir', new File("build/grails-work").canonicalPath)
static projectsBaseDir = requiredSysProp('projectsBaseDir', findChildOfRoot("apps"))
static autostartBaseDir = findChildOfRoot("autostart")
static projectWorkDir = requiredSysProp('projectWorkDir', new File("build/project-work").canonicalPath)
static outputDir = requiredSysProp('outputDir',new File("build/output").canonicalPath)
static {
if(!System.getProperty("geb.build.reportsDir")) {
System.setProperty("geb.build.reportsDir",outputDir)
}
}
@Lazy static grailsVersion = {
new File(grailsHome, "build.properties").withReader { def p = new Properties(); p.load(it); p.'grails.version' }
}()
static requiredSysProp(prop, defaultValue = null) {
def value = System.getProperty(prop) ?: defaultValue
assert value != null
return value
}
static findChildOfRoot(String name) {
def current = new File(".").canonicalFile
while(current != null && !new File(current, "build.gradle").exists()) {
current = current.parentFile
}
return new File(current, name).absolutePath
}
/**
* Whether to start the servlet container in debug mode
*/
boolean debug
def command
def exitStatus
def output
def projectDir
static processes = []
static cleanupDirectories = []
protected portInternal
protected projectInternal
String getProject() {
def classProject = System.getProperty("${this.getClass().name}.project")
if(classProject) {
return classProject
}
else if(this.projectInternal != null) {
return this.projectInternal
}
}
void setProject(String p) {
this.projectInternal = p
}
def getPort() {
def classPort = System.getProperty("${this.getClass().name}.port")
if(this.portInternal != null) {
return this.portInternal
}
else if(classPort) {
return classPort
}
else {
return BaseSpec.PORT
}
}
void setPort(p) {
this.portInternal = p
}
void setup() {
if(getPort() != null && getProject() != null) {
def url = "http://localhost:${getPort()}/${getProject()}/"
browser.baseUrl = url
}
}
void cleanup() {
// workaround for Geb bug which fails with NPE on cleanup if no browser created
if(browser == null) {
browser = new Browser()
}
}
void cleanupSpec() {
for(Process p in processes) {
p.destroy()
}
processes.clear()
for(File dir in cleanupDirectories) {
silentDelete(dir)
}
cleanupDirectories.clear()
silentDelete(new File(grailsWorkDir))
silentDelete(new File(outputDir))
}
protected silentDelete(File dir) {
if(dir.exists()) {
try {
FileUtils.deleteDirectory(dir)
} catch (e) {
// ignore
}
}
}
def grails(Closure callable) {
def executor = new GrailsExecutor(parent:this)
callable.delegate = executor
callable.resolveStrategy = Closure.DELEGATE_ONLY
callable.call()
}
def grailsDebug(Closure callable) {
def executor = new GrailsExecutor(parent:this, debug:true)
callable.delegate = executor
callable.resolveStrategy = Closure.DELEGATE_ONLY
callable.call()
}
def execute(String project, CharSequence[] command) {
return executeInternal(false, command, project)
}
def executeDebug(String project, CharSequence[] command) {
return executeInternal(true, command, project)
}
private int executeInternal(boolean debug, CharSequence[] command, String project) {
this.command = command.toList() + "--plainOutput"
def process = createProcess(debug,project, * this.command)
def outputBuffer = new StringBuffer()
process.consumeProcessOutputStream(outputBuffer)
process.waitForOrKill(PROCESS_TIMEOUT_MILLS)
exitStatus = process.exitValue()
output = outputBuffer.toString()
return exitStatus
}
Process executeAsync(String project, CharSequence[] command) {
this.command = command.toList() + "--plainOutput"
def process = createProcess(false, project, *this.command)
return process
}
Process executeDebugAsync(String project, CharSequence[] command) {
this.command = command.toList() + "--plainOutput"
def process = createProcess(true, project, *this.command)
return process
}
def upgradeProject(project) {
upgradedProjects << project
def result = execute(project, 'upgrade', '--non-interactive')
assert output?.contains('Project upgraded')
assert result == 0
}
void assertStatus(code) {
if(browser.driver instanceof HtmlUnitDriver) {
statusCode == code
}
}
int getStatusCode() {
browser.driver.webClient.webWindows.first().enclosedPage.webResponse.statusCode
}
private Process createProcess(boolean debug,String project, CharSequence[] command) {
if (project != null && !(project in upgradedProjects)) { upgradeProject(project) }
def completeCommand = ["${grailsHome}/bin/${debug ? 'grails-debug' : 'grails'}", "-Dgrails.work.dir=${grailsWorkDir} -Dgrails.project.work.dir=${projectWorkDir}/${project}", "--non-interactive", '--stacktrace']
if(port != null) {
completeCommand << "-Dgrails.server.port.http=${port}"
}
completeCommand.addAll(command.toList()*.toString())
def toExecute = completeCommand as String[]
def dir
if(project)
dir = new File(projectsBaseDir, project)
else
dir = new File(projectsBaseDir)
if(!dir.exists()) {
dir = new File(autostartBaseDir, project)
}
println "Running command: ${toExecute.join(" ")}"
println "Base directory: $dir"
projectDir = dir
(Process)new ProcessBuilder(toExecute).with {
redirectErrorStream(true)
directory(dir)
environment()["GRAILS_HOME"] = grailsHome
start()
}
}
}