Normalize included log output to remove CI-specific details

See gh-28208
pull/32234/head
Andy Wilkinson 2 years ago
parent 329fa8d37d
commit b905d7f341

@ -21,7 +21,12 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; 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 java.util.stream.Collectors;
import org.gradle.api.DefaultTask; 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> 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; private FileCollection classpath;
@OutputFile @OutputFile
@ -83,6 +93,25 @@ public class ApplicationRunner extends DefaultTask {
return this.expectedLogging; 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 @TaskAction
void runApplication() throws IOException { void runApplication() throws IOException {
List<String> command = new ArrayList<>(); List<String> command = new ArrayList<>();
@ -98,6 +127,7 @@ public class ApplicationRunner extends DefaultTask {
.start(); .start();
awaitLogging(process); awaitLogging(process);
process.destroy(); process.destroy();
normalizeLogging();
} }
private void awaitLogging(Process process) { 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());
}
} }

@ -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"] args = ["https://myapp.example.com", "--spring.devtools.remote.secret=secret", "--spring.devtools.livereload.port=0"]
output = file("$buildDir/example-output/remote-spring-application.txt") output = file("$buildDir/example-output/remote-spring-application.txt")
expectedLogging = "Started RemoteSpringApplication in " 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) { 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"] args = ["--server.port=0"]
output = file("$buildDir/example-output/spring-application.txt") output = file("$buildDir/example-output/spring-application.txt")
expectedLogging = "Started MyApplication in " expectedLogging = "Started MyApplication in "
normalizeTomcatPort()
} }
task runLoggingFormatExample(type: org.springframework.boot.build.docs.ApplicationRunner) { 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"] args = ["--spring.main.banner-mode=off", "--server.port=0"]
output = file("$buildDir/example-output/logging-format.txt") output = file("$buildDir/example-output/logging-format.txt")
expectedLogging = "Started MyApplication in " expectedLogging = "Started MyApplication in "
normalizeTomcatPort()
} }
tasks.withType(org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask) { tasks.withType(org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask) {

Loading…
Cancel
Save