-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathAllureCommandline.java
251 lines (207 loc) · 9.34 KB
/
AllureCommandline.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright 2016-2024 Qameta Software Inc
*
* 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 io.qameta.allure.maven;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.settings.Proxy;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import static io.qameta.allure.maven.VersionUtils.versionCompare;
@SuppressWarnings({"PMD.GodClass", "ClassDataAbstractionCoupling", "ClassFanOutComplexity",
"MultipleStringLiterals"})
public class AllureCommandline {
public static final String ALLURE_DEFAULT_VERSION = "2.30.0";
private static final int DEFAULT_TIMEOUT = 3600;
private final String version;
private final int timeout;
private final Path installationDirectory;
public AllureCommandline(final Path installationDirectory, final String version) {
this(installationDirectory, version, DEFAULT_TIMEOUT);
}
public AllureCommandline(final Path installationDirectory, final String version,
final int timeout) {
this.installationDirectory = installationDirectory;
this.version = StringUtils.isBlank(version) || versionCompare(version, "2.8.0") < 0
? ALLURE_DEFAULT_VERSION
: version;
this.timeout = timeout;
}
public int generateReport(final List<Path> resultsPaths, final Path reportPath,
final boolean singleFile) throws IOException {
this.checkAllureExists();
FileUtils.deleteQuietly(reportPath.toFile());
final CommandLine commandLine =
new CommandLine(getAllureExecutablePath().toAbsolutePath().toFile());
commandLine.addArgument("generate");
commandLine.addArgument("--clean");
if (singleFile) {
commandLine.addArgument("--single-file");
}
for (Path resultsPath : resultsPaths) {
commandLine.addArgument(resultsPath.toAbsolutePath().toString(), true);
}
commandLine.addArgument("-o");
commandLine.addArgument(reportPath.toAbsolutePath().toString(), true);
return execute(commandLine, timeout);
}
public int serve(final List<Path> resultsPaths, final Path reportPath, final String serveHost,
final Integer servePort) throws IOException {
this.checkAllureExists();
FileUtils.deleteQuietly(reportPath.toFile());
final CommandLine commandLine =
new CommandLine(getAllureExecutablePath().toAbsolutePath().toFile());
commandLine.addArgument("serve");
if (serveHost != null && serveHost.matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
commandLine.addArgument("--host");
commandLine.addArgument(serveHost);
}
if (servePort > 0) {
commandLine.addArgument("--port");
commandLine.addArgument(Objects.toString(servePort));
}
for (Path resultsPath : resultsPaths) {
commandLine.addArgument(resultsPath.toAbsolutePath().toString(), true);
}
return execute(commandLine, timeout);
}
private void checkAllureExists() throws FileNotFoundException {
if (allureNotExists()) {
throw new FileNotFoundException("There is no valid allure installation."
+ " Make sure you're using allure version not less then 2.x.");
}
}
private int execute(final CommandLine commandLine, final int timeout) throws IOException {
final DefaultExecutor executor = DefaultExecutor.builder().get();
final ExecuteWatchdog watchdog = ExecuteWatchdog.builder()
.setTimeout(Duration.ofMillis(TimeUnit.SECONDS.toMillis(timeout))).get();
executor.setWatchdog(watchdog);
executor.setExitValue(0);
return executor.execute(commandLine);
}
private Path getAllureExecutablePath() {
final String allureExecutable = isWindows() ? "allure.bat" : "allure";
return getAllureHome().resolve("bin").resolve(allureExecutable);
}
private Path getAllureHome() {
return installationDirectory.resolve(String.format("allure-%s", version));
}
public boolean allureExists() {
final Path allureExecutablePath = getAllureExecutablePath();
return Files.exists(allureExecutablePath) && Files.isExecutable(allureExecutablePath);
}
public boolean allureNotExists() {
return !allureExists();
}
public void downloadWithMaven(final MavenSession session,
final DependencyResolver dependencyResolver) throws IOException {
final ProjectBuildingRequest buildingRequest =
new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setResolveDependencies(false);
final Dependency cliDep = new Dependency();
cliDep.setGroupId("io.qameta.allure");
cliDep.setArtifactId("allure-commandline");
cliDep.setVersion(version);
cliDep.setType("zip");
try {
final Iterator<ArtifactResult> resolved =
dependencyResolver.resolveDependencies(buildingRequest,
Collections.singletonList(cliDep), null, null).iterator();
if (resolved.hasNext()) {
unpack(resolved.next().getArtifact().getFile());
} else {
throw new IOException("No allure commandline artifact found.");
}
} catch (DependencyResolverException e) {
throw new IOException("Cannot resolve allure commandline dependencies.", e);
}
}
public void download(final String allureDownloadUrl, final Proxy mavenProxy)
throws IOException {
if (allureExists()) {
return;
}
final Path allureZip = Files.createTempFile("allure", version);
final String allureUrl = String.format(allureDownloadUrl, version, version);
final URL url = new URL(allureUrl);
if (mavenProxy != null && version != null) {
final InetSocketAddress proxyAddress =
new InetSocketAddress(mavenProxy.getHost(), mavenProxy.getPort());
if (mavenProxy.getUsername() != null && mavenProxy.getPassword() != null) {
final String proxyUser = mavenProxy.getUsername();
final String proxyPassword = mavenProxy.getPassword();
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray());
}
});
}
final java.net.Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, proxyAddress);
final InputStream inputStream = url.openConnection(proxy).getInputStream();
Files.copy(inputStream, allureZip, StandardCopyOption.REPLACE_EXISTING);
} else {
FileUtils.copyURLToFile(url, allureZip.toFile());
}
unpack(allureZip.toFile());
}
private void unpack(final File file) throws IOException {
try (ZipFile zipFile = new ZipFile(file)) {
zipFile.extractAll(getInstallationDirectory().toAbsolutePath().toString());
} catch (ZipException e) {
throw new IOException(e);
}
final Path allureExecutable = getAllureExecutablePath();
if (Files.exists(allureExecutable)) {
allureExecutable.toFile().setExecutable(true);
}
}
public Path getInstallationDirectory() {
return installationDirectory;
}
public String getVersion() {
return version;
}
private boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
}