Normalize included log output to remove CI-specific details

See gh-28208
This commit is contained in:
Andy Wilkinson 2022-08-04 21:55:17 +01:00
parent 329fa8d37d
commit b905d7f341
2 changed files with 87 additions and 0 deletions

View File

@ -21,7 +21,12 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.gradle.api.DefaultTask;
@ -52,6 +57,11 @@ public class ApplicationRunner extends DefaultTask {
private final Property<String> expectedLogging = getProject().getObjects().property(String.class);
private final Property<String> applicationJar = getProject().getObjects().property(String.class)
.convention("/opt/apps/myapp.jar");
private final Map<String, String> normalizations = new HashMap<>();
private FileCollection classpath;
@OutputFile
@ -83,6 +93,25 @@ public class ApplicationRunner extends DefaultTask {
return this.expectedLogging;
}
@Input
Map<String, String> getNormalizations() {
return this.normalizations;
}
@Input
public Property<String> getApplicationJar() {
return this.applicationJar;
}
public void normalizeTomcatPort() {
this.normalizations.put("(Tomcat started on port\\(s\\): )[\\d]+( \\(http\\))", "$18080$2");
this.normalizations.put("(Tomcat initialized with port\\(s\\): )[\\d]+( \\(http\\))", "$18080$2");
}
public void normalizeLiveReloadPort() {
this.normalizations.put("(LiveReload server is running on port )[\\d]+", "$135729");
}
@TaskAction
void runApplication() throws IOException {
List<String> command = new ArrayList<>();
@ -98,6 +127,7 @@ public class ApplicationRunner extends DefaultTask {
.start();
awaitLogging(process);
process.destroy();
normalizeLogging();
}
private void awaitLogging(Process process) {
@ -126,4 +156,57 @@ public class ApplicationRunner extends DefaultTask {
}
}
private void normalizeLogging() {
List<String> outputLines = outputLines();
List<String> normalizedLines = normalize(outputLines);
Path outputPath = this.output.get().getAsFile().toPath();
try {
Files.write(outputPath, normalizedLines);
}
catch (IOException ex) {
throw new RuntimeException("Failed to write normalized lines of output to '" + outputPath + "'", ex);
}
}
private List<String> normalize(List<String> lines) {
List<String> normalizedLines = lines;
Map<String, String> normalizations = new HashMap<>(this.normalizations);
normalizations.put("(Starting .* using Java .* on ).*( with PID [\\d]+ \\().*( started by ).*( in ).*(\\))",
"$1myhost$2" + this.applicationJar.get() + "$3myuser$4/opt/apps/$5");
for (Entry<String, String> normalization : normalizations.entrySet()) {
Pattern pattern = Pattern.compile(normalization.getKey());
normalizedLines = normalize(normalizedLines, pattern, normalization.getValue());
}
return normalizedLines;
}
private List<String> normalize(List<String> lines, Pattern pattern, String replacement) {
boolean matched = false;
List<String> normalizedLines = new ArrayList<>();
for (String line : lines) {
Matcher matcher = pattern.matcher(line);
StringBuffer transformed = new StringBuffer();
while (matcher.find()) {
matched = true;
matcher.appendReplacement(transformed, replacement);
}
matcher.appendTail(transformed);
normalizedLines.add(transformed.toString());
}
if (!matched) {
reportUnmatchedNormalization(lines, pattern);
}
return normalizedLines;
}
private void reportUnmatchedNormalization(List<String> lines, Pattern pattern) {
StringBuilder message = new StringBuilder(
"'" + pattern + "' did not match any of the following lines of output:");
message.append(String.format("%n"));
for (String line : lines) {
message.append(String.format("%s%n", line));
}
throw new IllegalStateException(message.toString());
}
}

View File

@ -265,6 +265,8 @@ task runRemoteSpringApplicationExample(type: org.springframework.boot.build.docs
args = ["https://myapp.example.com", "--spring.devtools.remote.secret=secret", "--spring.devtools.livereload.port=0"]
output = file("$buildDir/example-output/remote-spring-application.txt")
expectedLogging = "Started RemoteSpringApplication in "
applicationJar = "/Users/myuser/.m2/repository/org/springframework/boot/spring-boot-devtools/${project.version}/spring-boot-devtools-${project.version}.jar"
normalizeLiveReloadPort()
}
task runSpringApplicationExample(type: org.springframework.boot.build.docs.ApplicationRunner) {
@ -273,6 +275,7 @@ task runSpringApplicationExample(type: org.springframework.boot.build.docs.Appli
args = ["--server.port=0"]
output = file("$buildDir/example-output/spring-application.txt")
expectedLogging = "Started MyApplication in "
normalizeTomcatPort()
}
task runLoggingFormatExample(type: org.springframework.boot.build.docs.ApplicationRunner) {
@ -281,6 +284,7 @@ task runLoggingFormatExample(type: org.springframework.boot.build.docs.Applicati
args = ["--spring.main.banner-mode=off", "--server.port=0"]
output = file("$buildDir/example-output/logging-format.txt")
expectedLogging = "Started MyApplication in "
normalizeTomcatPort()
}
tasks.withType(org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask) {