-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSystemTestUtils.java
More file actions
198 lines (170 loc) · 6.85 KB
/
SystemTestUtils.java
File metadata and controls
198 lines (170 loc) · 6.85 KB
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
package com.teamscale.test.commons;
import com.teamscale.report.testwise.model.TestInfo;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.apache.commons.lang3.SystemUtils;
import org.conqat.lib.commons.io.ProcessUtils;
import org.jetbrains.annotations.NotNull;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Utilities for running system tests.
*/
public class SystemTestUtils {
/**
* The port for the mock Teamscale server that was picked by the Gradle build script and is guaranteed to not
* conflict with other system tests.
*/
public static final Integer TEAMSCALE_PORT = Integer.getInteger("teamscalePort");
/**
* The port for the agent that was picked by the Gradle build script and is guaranteed to not conflict with other
* system tests.
*/
public static final Integer AGENT_PORT = Integer.getInteger("agentPort");
/**
* Turns the coverage of the given {@link TestInfo} into a string for simple assertions.
* <p>
* Example: {@code file1.java:1,7-12;file2.java:9-22,33}
*/
public static String getCoverageString(TestInfo info) {
return info.paths.stream().flatMap(path -> path.files.stream())
.map(file -> file.fileName + ":" + file.coveredLines).collect(
Collectors.joining(";"));
}
/**
* Runs the clean and verify goal of the Maven project at the given path.
*
* @throws IOException if running Maven fails.
*/
public static void runMavenTests(String mavenProjectPath) throws IOException {
runMavenTests(mavenProjectPath, new String[0]);
}
/**
* Runs the clean and verify goal of the Maven project at the given path with the provided arguments
*
* @throws IOException if running Maven fails.
*/
public static void runMavenTests(String mavenProjectPath, String... args) throws IOException {
String[] allArguments = new String[2 + args.length];
allArguments[0] = "clean";
allArguments[1] = "verify";
System.arraycopy(args, 0, allArguments, 2, args.length);
runMaven(mavenProjectPath, allArguments);
}
/**
* Runs Maven in the given Maven project path with the given arguments.
*
* @throws IOException if running Maven fails.
*/
public static void runMaven(String mavenProjectPath, String... mavenArguments) throws IOException {
ProcessUtils.ExecutionResult result;
try {
result = ProcessUtils.execute(buildMavenProcess(mavenProjectPath, mavenArguments));
} catch (IOException e) {
throw new IOException("Failed to run ./mvnw clean verify in directory " + mavenProjectPath, e);
}
// in case the process succeeded, we still log stdout and stderr in case later assertions fail. This helps
// debug test failures
System.out.println("Maven stdout: " + result.getStdout());
System.out.println("Maven stderr: " + result.getStderr());
if (result.terminatedByTimeoutOrInterruption()) {
throw new IOException("Running Maven failed: " + result.getStdout() + "\n" + result.getStderr());
}
}
/**
* Runs Gradle in the given Gradle project path with the given arguments.
*
* @throws IOException if running Gradle fails.
*/
public static ProcessUtils.ExecutionResult runGradle(String gradleProjectPath, String... gradleArguments) throws IOException {
ProcessUtils.ExecutionResult result;
try {
result = ProcessUtils.execute(buildGradleProcess(gradleProjectPath, gradleArguments));
} catch (IOException e) {
throw new IOException("Failed to run ./gradlew clean verify in directory " + gradleProjectPath, e);
}
// in case the process succeeded, we still log stdout and stderr in case later assertions fail. This helps
// debug test failures
System.out.println("Gradle stdout: " + result.getStdout());
System.out.println("Gradle stderr: " + result.getStderr());
if (result.terminatedByTimeoutOrInterruption()) {
throw new IOException("Running Gradle failed: " + result.getStdout() + "\n" + result.getStderr());
}
return result;
}
/**
* Creates the command-line arguments that can be passed to {@link ProcessBuilder} to invoke Maven with the given
* arguments.
*/
@NotNull
public static ProcessBuilder buildMavenProcess(String mavenProjectDirectory, String... mavenArguments) {
List<String> arguments = new ArrayList<>();
if (SystemUtils.IS_OS_WINDOWS) {
Collections.addAll(arguments, "cmd", "/c", "mvnw.cmd");
} else {
arguments.add("./mvnw");
}
arguments.addAll(Arrays.asList(mavenArguments));
return new ProcessBuilder(arguments).directory(new File(mavenProjectDirectory));
}
/**
* Creates the command-line arguments that can be passed to {@link ProcessBuilder} to invoke Gradle with the given
* arguments.
*/
@NotNull
public static ProcessBuilder buildGradleProcess(String gradleProjectDirectory, String... gradleArguments) {
List<String> arguments = new ArrayList<>();
if (SystemUtils.IS_OS_WINDOWS) {
Collections.addAll(arguments, "cmd", "/c", "gradlew.bat");
} else {
arguments.add("./gradlew");
}
arguments.addAll(Arrays.asList(gradleArguments));
return new ProcessBuilder(arguments).directory(new File(gradleProjectDirectory));
}
/** Retrieve all files in the `tia/reports` folder sorted by name. */
public static List<Path> getReportFileNames(String mavenProjectPath, String folderName) throws IOException {
try (Stream<Path> stream = Files.walk(Paths.get(mavenProjectPath, "target", folderName, "reports"))) {
return stream.filter(Files::isRegularFile).sorted().collect(Collectors.toList());
}
}
/**
* Interface for interacting with an agent service that enables coverage dumping and partition management.
*/
public interface AgentService {
/** Dumps coverage */
@POST("/dump")
Call<Void> dump();
/** Changes the current partition. */
@PUT("/partition")
Call<Void> changePartition(@Body RequestBody newPartition);
/** Changes the current partition. Convenience method to pass a plain string. */
default Call<Void> changePartition(String newPartition) {
return changePartition(RequestBody.create(MediaType.parse("text/plain"), newPartition));
}
}
/** Instructs the agent via HTTP to dump the currently collected coverage. */
public static void dumpCoverage(int agentPort) throws IOException {
new Retrofit.Builder().baseUrl("http://localhost:" + agentPort).build()
.create(AgentService.class).dump().execute();
}
/** Instructs the agent via HTTP to change the partition. */
public static void changePartition(int agentPort, String newPartition) throws IOException {
new Retrofit.Builder().baseUrl("http://localhost:" + agentPort).build()
.create(AgentService.class).changePartition(newPartition).execute();
}
}