|
| 1 | +/* |
| 2 | + * Copyright and related rights waived via CC0 |
| 3 | + * |
| 4 | + * You should have received a copy of the CC0 legalcode along with this |
| 5 | + * work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. |
| 6 | + */ |
| 7 | +package org.graalvm.internal.tck.harness.tasks |
| 8 | + |
| 9 | +import org.gradle.api.GradleException |
| 10 | +import org.gradle.api.tasks.TaskAction |
| 11 | +import org.gradle.process.ExecOperations |
| 12 | +import org.gradle.process.ExecSpec |
| 13 | +import org.graalvm.internal.tck.harness.TckExtension |
| 14 | + |
| 15 | +import javax.inject.Inject |
| 16 | +import java.nio.charset.StandardCharsets |
| 17 | +import java.nio.file.Path |
| 18 | + |
| 19 | +import static org.graalvm.internal.tck.Utils.coordinatesMatch |
| 20 | +import static org.graalvm.internal.tck.Utils.readIndexFile |
| 21 | +import static org.graalvm.internal.tck.Utils.splitCoordinates |
| 22 | + |
| 23 | +/** |
| 24 | + * Base task that resolves coordinates (via CoordinatesAwareTask) and executes a command for each coordinate. |
| 25 | + * Subclasses implement commandFor(String coordinates) and may override hooks for logging. |
| 26 | + */ |
| 27 | +abstract class AllCoordinatesExecTask extends CoordinatesAwareTask { |
| 28 | + |
| 29 | + @Inject |
| 30 | + abstract ExecOperations getExecOperations() |
| 31 | + |
| 32 | + |
| 33 | + /** |
| 34 | + * Subclasses must return the command line to run for the given coordinates. |
| 35 | + */ |
| 36 | + abstract List<String> commandFor(String coordinates) |
| 37 | + |
| 38 | + /** |
| 39 | + * Customize error message. |
| 40 | + */ |
| 41 | + protected String errorMessageFor(String coordinates, int exitCode) { |
| 42 | + return "Execution failed for ${coordinates} with exit code ${exitCode}" |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Hook invoked before executing each coordinate. |
| 47 | + */ |
| 48 | + protected void beforeEach(String coordinates, List<String> command) { |
| 49 | + // no-op |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Hook invoked after executing each coordinate on success. |
| 54 | + */ |
| 55 | + protected void afterEach(String coordinates) { |
| 56 | + // no-op |
| 57 | + } |
| 58 | + |
| 59 | + @TaskAction |
| 60 | + final void runAll() { |
| 61 | + List<String> coords = resolveCoordinates() |
| 62 | + if (coords.isEmpty()) { |
| 63 | + getLogger().lifecycle("No matching coordinates found. Nothing to do.") |
| 64 | + return |
| 65 | + } |
| 66 | + for (String c : coords) { |
| 67 | + runSingle(c) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private void runSingle(String coordinates) { |
| 72 | + List<String> command = commandFor(coordinates) |
| 73 | + beforeEach(coordinates, command) |
| 74 | + |
| 75 | + def out = new ByteArrayOutputStream() |
| 76 | + def err = new ByteArrayOutputStream() |
| 77 | + |
| 78 | + def execResult = getExecOperations().exec { ExecSpec spec -> |
| 79 | + this.configureSpec(spec, coordinates, command) |
| 80 | + spec.standardOutput = new TeeOutputStream(out, System.out) |
| 81 | + spec.errorOutput = new TeeOutputStream(err, System.err) |
| 82 | + } |
| 83 | + |
| 84 | + // write output file like AbstractSubprojectTask |
| 85 | + String hash = command.join(",").md5() |
| 86 | + File outputFile = project.layout.buildDirectory.file("tests/${coordinates}/${hash}.out").get().asFile |
| 87 | + outputFile.parentFile.mkdirs() |
| 88 | + outputFile.text = """Standard out |
| 89 | +----- |
| 90 | +${out.toString(StandardCharsets.UTF_8)} |
| 91 | +----- |
| 92 | +Standard err |
| 93 | +---- |
| 94 | +${err.toString(StandardCharsets.UTF_8)} |
| 95 | +---- |
| 96 | +""" |
| 97 | + |
| 98 | + int exitCode = execResult.exitValue |
| 99 | + if (exitCode != 0) { |
| 100 | + throw new GradleException(errorMessageFor(coordinates, exitCode)) |
| 101 | + } |
| 102 | + afterEach(coordinates) |
| 103 | + } |
| 104 | + |
| 105 | + protected void configureSpec(ExecSpec spec, String coordinates, List<String> command) { |
| 106 | + def (String groupId, String artifactId, String version) = splitCoordinates(coordinates) |
| 107 | + Path metadataDir = tckExtension.getMetadataDir(coordinates) |
| 108 | + boolean override = false |
| 109 | + |
| 110 | + def metadataIndex = readIndexFile(metadataDir.parent) |
| 111 | + for (def entry in metadataIndex) { |
| 112 | + if (coordinatesMatch((String) entry["module"], groupId, artifactId) && ((List<String>) entry["tested-versions"]).contains(version)) { |
| 113 | + if (entry.containsKey("override")) { |
| 114 | + override |= entry["override"] as boolean |
| 115 | + } |
| 116 | + break |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + Path testDir = tckExtension.getTestDir(coordinates) |
| 121 | + |
| 122 | + Map<String, String> env = new HashMap<>(System.getenv()) |
| 123 | + env.put("GVM_TCK_LC", coordinates) |
| 124 | + env.put("GVM_TCK_EXCLUDE", override.toString()) |
| 125 | + if (System.getenv("GVM_TCK_LV") == null) { |
| 126 | + env.put("GVM_TCK_LV", version) |
| 127 | + } |
| 128 | + env.put("GVM_TCK_MD", metadataDir.toAbsolutePath().toString()) |
| 129 | + env.put("GVM_TCK_TCKDIR", tckExtension.getTckRoot().get().getAsFile().toPath().toAbsolutePath().toString()) |
| 130 | + |
| 131 | + spec.environment(env) |
| 132 | + spec.commandLine(command) |
| 133 | + spec.workingDir(testDir.toAbsolutePath().toFile()) |
| 134 | + spec.setIgnoreExitValue(true) |
| 135 | + spec.standardOutput = System.out |
| 136 | + spec.errorOutput = System.err |
| 137 | + } |
| 138 | +} |
0 commit comments