|
| 1 | +package dev.archunitjava.integration; |
| 2 | + |
| 3 | +import dev.archunitjava.cli.CliConfiguration; |
| 4 | +import dev.archunitjava.cli.CliConfigurationLoader; |
| 5 | +import dev.archunitjava.cli.CliGraphFormat; |
| 6 | +import dev.archunitjava.cli.CliResultFormat; |
| 7 | +import dev.archunitjava.cli.CliRunner; |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.LinkOption; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.TreeSet; |
| 16 | + |
| 17 | +/** |
| 18 | + * Executes the shared contract against already-compiled outputs. |
| 19 | + * |
| 20 | + * <p>This class has no process-launching API and never invokes Maven, Gradle, compilers, target |
| 21 | + * classes, plugins, or annotation processors. |
| 22 | + */ |
| 23 | +public final class BuildIntegrationRunner { |
| 24 | + public BuildIntegrationResult run( |
| 25 | + BuildIntegrationRequest request, Appendable standardOut, Appendable standardError) { |
| 26 | + BuildIntegrationRequest value = Objects.requireNonNull(request, "request"); |
| 27 | + try { |
| 28 | + Path root = value.approvedRoot().toRealPath(); |
| 29 | + if (!Files.isDirectory(root, LinkOption.NOFOLLOW_LINKS)) { |
| 30 | + throw new BuildIntegrationException("Approved build root must be a directory"); |
| 31 | + } |
| 32 | + List<Path> outputs = outputs(root, value.compiledOutputs(), value.tool()); |
| 33 | + CliConfiguration configuration = CliConfigurationLoader.load( |
| 34 | + value.configuration(), root); |
| 35 | + if (!configuration.inputs().equals(outputs)) { |
| 36 | + throw new BuildIntegrationException( |
| 37 | + "Compiled outputs do not exactly match the validated CLI inputs"); |
| 38 | + } |
| 39 | + List<String> arguments = new ArrayList<>(List.of( |
| 40 | + value.command().cliName(), |
| 41 | + "--config", configuration.source().toString(), |
| 42 | + "--root", root.toString())); |
| 43 | + value.resultFormat().ifPresent(format -> { |
| 44 | + arguments.add("--result-format"); |
| 45 | + arguments.add(resultFormat(format)); |
| 46 | + }); |
| 47 | + value.graphFormat().ifPresent(format -> { |
| 48 | + arguments.add("--graph-format"); |
| 49 | + arguments.add(format.name().toLowerCase(java.util.Locale.ROOT)); |
| 50 | + }); |
| 51 | + int exit = new CliRunner().run( |
| 52 | + arguments.toArray(String[]::new), standardOut, standardError); |
| 53 | + return new BuildIntegrationResult(value.tool(), value.lifecycle(), exit, outputs); |
| 54 | + } catch (BuildIntegrationException error) { |
| 55 | + throw error; |
| 56 | + } catch (IOException | RuntimeException error) { |
| 57 | + throw new BuildIntegrationException( |
| 58 | + value.tool() + " integration could not validate compiled outputs: " |
| 59 | + + safeMessage(error), error); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static List<Path> outputs( |
| 64 | + Path root, List<Path> configured, BuildTool tool) throws IOException { |
| 65 | + TreeSet<Path> sorted = new TreeSet<>(); |
| 66 | + for (Path output : configured) { |
| 67 | + Path candidate = (output.isAbsolute() ? output : root.resolve(output)).normalize(); |
| 68 | + if (!candidate.startsWith(root)) { |
| 69 | + throw new BuildIntegrationException("Compiled output escapes the approved root"); |
| 70 | + } |
| 71 | + Path resolved = candidate.toRealPath(); |
| 72 | + if (!resolved.startsWith(root) || !Files.isDirectory(resolved, LinkOption.NOFOLLOW_LINKS)) { |
| 73 | + throw new BuildIntegrationException( |
| 74 | + "Compiled output must be a directory inside the approved root: " + output); |
| 75 | + } |
| 76 | + if (!containsClass(resolved)) { |
| 77 | + String ordering = tool == BuildTool.MAVEN |
| 78 | + ? "Bind the architecture check to verify after compile/test-compile." |
| 79 | + : "Make the architecture check task depend on classes/testClasses."; |
| 80 | + throw new BuildIntegrationException( |
| 81 | + "Compiled output contains no .class files: " + output + ". " + ordering); |
| 82 | + } |
| 83 | + if (!sorted.add(resolved)) { |
| 84 | + throw new BuildIntegrationException( |
| 85 | + "Compiled outputs resolve to the same directory more than once: " + output); |
| 86 | + } |
| 87 | + } |
| 88 | + return List.copyOf(sorted); |
| 89 | + } |
| 90 | + |
| 91 | + private static boolean containsClass(Path directory) throws IOException { |
| 92 | + try (var paths = Files.walk(directory)) { |
| 93 | + return paths.limit(100_001) |
| 94 | + .anyMatch(path -> Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS) |
| 95 | + && path.getFileName().toString().endsWith(".class")); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static String resultFormat(CliResultFormat format) { |
| 100 | + return format == CliResultFormat.JUNIT_XML |
| 101 | + ? "junit-xml" : format.name().toLowerCase(java.util.Locale.ROOT); |
| 102 | + } |
| 103 | + |
| 104 | + private static String safeMessage(Exception error) { |
| 105 | + String message = error.getMessage(); |
| 106 | + return (message == null || message.isBlank() ? error.getClass().getSimpleName() : message) |
| 107 | + .replace("\r", "\\r").replace("\n", "\\n"); |
| 108 | + } |
| 109 | +} |
0 commit comments