-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
ForkingGradleExecuter.java
214 lines (177 loc) · 7.63 KB
/
ForkingGradleExecuter.java
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
/*
* Copyright 2009 the original author or authors.
*
* 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 org.gradle.integtests;
import org.gradle.util.GUtil;
import org.gradle.util.exec.ExecHandleBuilder;
import org.gradle.util.exec.ExecHandle;
import static org.gradle.util.Matchers.*;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.apache.tools.ant.taskdefs.condition.Os;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.util.*;
// todo: implement more of the unsupported methods
public class ForkingGradleExecuter extends AbstractGradleExecuter {
private static final Logger LOG = LoggerFactory.getLogger(ForkingGradleExecuter.class);
private final GradleDistribution distribution;
private Map<String, String> environmentVars = new HashMap<String, String>();
private String command;
public ForkingGradleExecuter(GradleDistribution distribution) {
this.distribution = distribution;
inDirectory(distribution.getTestDir());
}
public GradleExecuter withEnvironmentVars(Map<String, ?> environment) {
environmentVars.clear();
for (Map.Entry<String, ?> entry : environment.entrySet()) {
environmentVars.put(entry.getKey(), entry.getValue().toString());
}
return this;
}
public GradleExecuter usingExecutable(String script) {
command = script;
return this;
}
public ExecutionResult run() {
Map result = doRun(false);
return new ForkedExecutionResult(result);
}
public ExecutionFailure runWithFailure() {
Map result = doRun(true);
return new ForkedExecutionFailure(result);
}
private Map doRun(boolean expectFailure) {
String windowsCmd;
String unixCmd;
if (command != null) {
windowsCmd = command.replace('/', File.separatorChar);
unixCmd = String.format("%s/%s", getWorkingDir().getAbsolutePath(), command);
} else {
windowsCmd = "gradle";
unixCmd = String.format("%s/bin/gradle", distribution.getGradleHomeDir().getAbsolutePath());
}
return executeInternal(windowsCmd, unixCmd, expectFailure);
}
@Override
protected List<String> getAllArgs() {
return GUtil.addLists(getExtraArgs(), super.getAllArgs());
}
private List<String> getExtraArgs() {
List<String> args = new ArrayList<String>();
if (!isDisableTestGradleUserHome()) {
args.add("--gradle-user-home");
args.add(distribution.getUserHomeDir().getAbsolutePath());
}
return args;
}
Map executeInternal(String windowsCommandSnippet, String unixCommandSnippet, boolean expectFailure) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ByteArrayOutputStream errStream = new ByteArrayOutputStream();
String gradleHome = distribution.getGradleHomeDir().toString();
ExecHandleBuilder builder = new ExecHandleBuilder();
builder.standardOutput(outStream);
builder.errorOutput(errStream);
builder.environment("GRADLE_HOME", "");
builder.environment("JAVA_HOME", System.getProperty("java.home"));
builder.environment("GRADLE_OPTS", "-ea");
builder.environment(environmentVars);
builder.execDirectory(getWorkingDir());
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
builder.execCommand("cmd");
builder.arguments("/c", windowsCommandSnippet);
builder.environment("Path", String.format("%s\\bin;%s", gradleHome, System.getenv("Path")));
builder.environment("GRADLE_EXIT_CONSOLE", "true");
} else {
builder.execCommand(unixCommandSnippet);
}
builder.arguments(getAllArgs());
LOG.info(String.format("Execute in %s with: %s %s", builder.getExecDirectory(), builder.getExecCommand(),
builder.getArguments()));
ExecHandle proc = builder.getExecHandle();
proc.startAndWaitForFinish();
int exitValue = proc.getExitCode();
String output = outStream.toString();
String error = errStream.toString();
boolean failed = exitValue != 0;
LOG.info("OUTPUT: " + output);
LOG.info("ERROR: " + error);
if (failed != expectFailure) {
System.out.format("Gradle execution %s in %s with: %s %s", expectFailure ? "did not fail" : "failed",
builder.getExecDirectory(), builder.getExecCommand(), builder.getArguments());
System.out.format(String.format("Process Output:%n%s", output));
System.out.format(String.format("Process Error:%n%s", error));
throw new RuntimeException(String.format("Integrationtests failed with: %s %s", output, error));
}
return GUtil.map("output", output, "error", error);
}
private static class ForkedExecutionResult implements ExecutionResult {
private final Map result;
public ForkedExecutionResult(Map result) {
this.result = result;
}
public String getOutput() {
return result.get("output").toString();
}
public String getError() {
return result.get("error").toString();
}
public ExecutionResult assertTasksExecuted(String... taskPaths) {
throw new UnsupportedOperationException();
}
public ExecutionResult assertTasksSkipped(String... taskPaths) {
throw new UnsupportedOperationException();
}
}
private static class ForkedExecutionFailure extends ForkedExecutionResult implements ExecutionFailure {
private final Map result;
public ForkedExecutionFailure(Map result) {
super(result);
this.result = result;
}
public void assertHasLineNumber(int lineNumber) {
assertThat(getError(), containsString(String.format(" line: %d", lineNumber)));
}
public void assertHasFileName(String filename) {
assertThat(getError(), containsLine(startsWith(filename)));
}
public void assertHasCause(String description) {
assertThatCause(startsWith(description));
}
public void assertThatCause(final Matcher<String> matcher) {
assertThat(getError(), containsLine(new BaseMatcher<String>() {
public boolean matches(Object o) {
String str = (String) o;
String prefix = "Cause: ";
return str.startsWith(prefix) && matcher.matches(str.substring(prefix.length()));
}
public void describeTo(Description description) {
matcher.describeTo(description);
}
}));
}
public void assertHasDescription(String context) {
assertThatDescription(startsWith(context));
}
public void assertThatDescription(Matcher<String> matcher) {
assertThat(getError(), containsLine(matcher));
}
}
}