-
Notifications
You must be signed in to change notification settings - Fork 13
Dev prod/junit platform engine #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
zielezin
wants to merge
5
commits into
main
Choose a base branch
from
dev-prod/junit-platform-engine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b235a4
Use JUnit5 platform engine
pbielicki e3a81bb
Test custom PluginUnderTestAwareGradleRunnerCommandExecutor
pbielicki 183e18e
Remove PluginUnderTestAwareGradleRunnerCommandExecutor
pbielicki ca3e2e0
Set test class name correctly
pbielicki 8257daf
proposal of introducing custom executors
zielezin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
samples-check/src/main/java/org/gradle/exemplar/executor/CommandExecutorExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package org.gradle.exemplar.executor; | ||
|
|
||
| import org.gradle.exemplar.model.Command; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| public interface CommandExecutorExtension { | ||
|
|
||
| CommandExecutionResult execute(final Command command, final ExecutionMetadata executionMetadata, File workingDir); | ||
|
|
||
| } |
247 changes: 247 additions & 0 deletions
247
samples-check/src/main/java/org/gradle/exemplar/test/engine/SamplesRunnerJUnitEngine.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| package org.gradle.exemplar.test.engine; | ||
|
|
||
| import org.apache.commons.io.FileUtils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.gradle.exemplar.executor.*; | ||
| import org.gradle.exemplar.loader.SamplesDiscovery; | ||
| import org.gradle.exemplar.model.Command; | ||
| import org.gradle.exemplar.model.Sample; | ||
| import org.gradle.exemplar.test.normalizer.OutputNormalizer; | ||
| import org.gradle.exemplar.test.runner.SampleModifier; | ||
| import org.gradle.exemplar.test.runner.SamplesRoot; | ||
| import org.gradle.exemplar.test.runner.SamplesRunner; | ||
| import org.gradle.exemplar.test.verifier.AnyOrderLineSegmentedOutputVerifier; | ||
| import org.gradle.exemplar.test.verifier.StrictOrderLineSegmentedOutputVerifier; | ||
| import org.junit.platform.engine.EngineDiscoveryRequest; | ||
| import org.junit.platform.engine.EngineExecutionListener; | ||
| import org.junit.platform.engine.ExecutionRequest; | ||
| import org.junit.platform.engine.TestDescriptor; | ||
| import org.junit.platform.engine.TestEngine; | ||
| import org.junit.platform.engine.TestExecutionResult; | ||
| import org.junit.platform.engine.UniqueId; | ||
| import org.junit.platform.engine.discovery.ClassSelector; | ||
| import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor; | ||
| import org.junit.platform.engine.support.descriptor.ClassSource; | ||
| import org.junit.platform.engine.support.descriptor.EngineDescriptor; | ||
| import org.opentest4j.AssertionFailedError; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
|
|
||
| public class SamplesRunnerJUnitEngine implements TestEngine { | ||
|
|
||
| private final File tempDir; | ||
| private final List<? extends OutputNormalizer> normalizers = emptyList(); | ||
| private final List<SampleModifier> sampleModifiers = emptyList(); | ||
|
|
||
| public SamplesRunnerJUnitEngine() throws IOException { | ||
| tempDir = Files.createTempDirectory("samples-junit-engine").toFile(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getId() { | ||
| return "SamplesRunnerJUnitEngine"; | ||
| } | ||
|
|
||
| @Override | ||
| public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) { | ||
| EngineDescriptor engineDescriptor = new EngineDescriptor(uniqueId, "Samples Runner JUnit Engine"); | ||
| discoveryRequest.getSelectorsByType(ClassSelector.class) | ||
| .stream() | ||
| .map(ClassSelector::getJavaClass).forEach(javaClass -> { | ||
| SamplesRoot samplesRoot = javaClass.getAnnotation(SamplesRoot.class); | ||
| if (samplesRoot != null) { | ||
| SamplesTestDescriptor samplesTestDescriptor = new SamplesTestDescriptor( | ||
| uniqueId.append("className", javaClass.getSimpleName()), | ||
| javaClass | ||
| ); | ||
|
|
||
| for (Sample sample : SamplesDiscovery.externalSamples(getSamplesRootDir(samplesRoot))) { | ||
| samplesTestDescriptor.addChild(new SampleTestDescriptor( | ||
| uniqueId.append("sampleId", sample.getId()), | ||
| sample | ||
| )); | ||
| } | ||
|
|
||
| engineDescriptor.addChild(samplesTestDescriptor); | ||
| } | ||
| }); | ||
|
|
||
| return engineDescriptor; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(ExecutionRequest request) { | ||
| TestDescriptor engineDescriptor = request.getRootTestDescriptor(); | ||
| EngineExecutionListener listener = request.getEngineExecutionListener(); | ||
|
|
||
| listener.executionStarted(engineDescriptor); | ||
| for (TestDescriptor classDescriptor : engineDescriptor.getChildren()) { | ||
| listener.executionStarted(classDescriptor); | ||
| for (TestDescriptor testDescriptor : classDescriptor.getChildren()) { | ||
| SampleTestDescriptor descriptor = (SampleTestDescriptor) testDescriptor; | ||
| listener.executionStarted(testDescriptor); | ||
| try { | ||
| runSample(descriptor.sample); | ||
| listener.executionFinished(testDescriptor, TestExecutionResult.successful()); | ||
| } catch (Throwable t) { | ||
| listener.executionFinished(testDescriptor, TestExecutionResult.failed(t)); | ||
| } | ||
| } | ||
|
|
||
| listener.executionFinished(classDescriptor, TestExecutionResult.successful()); | ||
| } | ||
| listener.executionFinished(engineDescriptor, TestExecutionResult.successful()); | ||
| } | ||
|
|
||
| protected File getSamplesRootDir(SamplesRoot samplesRoot) { | ||
| File samplesRootDir = null; | ||
| try { | ||
| if (samplesRoot != null) { | ||
| samplesRootDir = new File(samplesRoot.value()); | ||
| } | ||
| if (samplesRootDir == null) { | ||
| throw new IllegalArgumentException("Samples root directory is not declared. Please annotate your test class with @SamplesRoot(\"path/to/samples\")"); | ||
| } | ||
| if (!samplesRootDir.exists()) { | ||
| throw new IllegalArgumentException("Samples root directory " + samplesRootDir.getAbsolutePath() + " does not exist"); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Could not initialize SamplesRunnerJUnitEngine", e); | ||
| } | ||
| return samplesRootDir; | ||
| } | ||
|
|
||
| private void runSample(Sample sample) throws Exception { | ||
| final Sample testSpecificSample = initSample(sample); | ||
| File baseWorkingDir = testSpecificSample.getProjectDir(); | ||
|
|
||
| // Execute and verify each command | ||
| for (Command command : testSpecificSample.getCommands()) { | ||
| File workingDir = baseWorkingDir; | ||
|
|
||
| if (command.getExecutionSubdirectory() != null) { | ||
| workingDir = new File(workingDir, command.getExecutionSubdirectory()); | ||
| } | ||
|
|
||
| // This should be some kind of plugable executor rather than hard-coded here | ||
| if (command.getExecutable().equals("cd")) { | ||
| baseWorkingDir = new File(baseWorkingDir, command.getArgs().get(0)).getCanonicalFile(); | ||
| continue; | ||
| } | ||
|
|
||
| CommandExecutionResult result = executeSample(getExecutionMetadata(testSpecificSample.getProjectDir()), workingDir, command); | ||
|
|
||
| if (result.getExitCode() != 0 && !command.isExpectFailure()) { | ||
| throw new AssertionFailedError(String.format( | ||
| "Expected sample invocation to succeed but it failed.%nCommand was: '%s %s'%nWorking directory: '%s'%n[BEGIN OUTPUT]%n%s%n[END OUTPUT]%n", | ||
| command.getExecutable(), | ||
| StringUtils.join(command.getArgs(), " "), | ||
| workingDir.getAbsolutePath(), | ||
| result.getOutput() | ||
| )); | ||
| } else if (result.getExitCode() == 0 && command.isExpectFailure()) { | ||
| throw new AssertionFailedError(String.format( | ||
| "Expected sample invocation to fail but it succeeded.%nCommand was: '%s %s'%nWorking directory: '%s'%n[BEGIN OUTPUT]%n%s%n[END OUTPUT]%n", | ||
| command.getExecutable(), | ||
| StringUtils.join(command.getArgs(), " "), | ||
| workingDir.getAbsolutePath(), | ||
| result.getOutput() | ||
| )); | ||
| } | ||
|
|
||
| verifyOutput(command, result); | ||
| } | ||
| } | ||
|
|
||
| private void verifyOutput(final Command command, final CommandExecutionResult executionResult) { | ||
| if (command.getExpectedOutput() == null) { | ||
| return; | ||
| } | ||
|
|
||
| String expectedOutput = command.getExpectedOutput(); | ||
| String actualOutput = executionResult.getOutput(); | ||
|
|
||
| for (OutputNormalizer normalizer : normalizers) { | ||
| actualOutput = normalizer.normalize(actualOutput, executionResult.getExecutionMetadata()); | ||
| } | ||
|
|
||
| if (command.isAllowDisorderedOutput()) { | ||
| new AnyOrderLineSegmentedOutputVerifier().verify(expectedOutput, actualOutput, command.isAllowAdditionalOutput()); | ||
| } else { | ||
| new StrictOrderLineSegmentedOutputVerifier().verify(expectedOutput, actualOutput, command.isAllowAdditionalOutput()); | ||
| } | ||
| } | ||
|
|
||
| private CommandExecutionResult executeSample(ExecutionMetadata executionMetadata, File workingDir, Command command) { | ||
| CommandExecutorExtension commandExecutor = selectExecutor(executionMetadata, workingDir, command); | ||
| return commandExecutor.execute(command, executionMetadata, workingDir); | ||
| } | ||
|
|
||
| protected CommandExecutorExtension selectExecutor(ExecutionMetadata executionMetadata, File workingDir, Command command) { | ||
| String executorName = command.getExecutorName(); | ||
| if("CLI".equals(executorName)){ | ||
| return new CliCommandExecutor(); | ||
| }else if(StringUtils.isNotBlank(executorName)){ | ||
| try | ||
| { | ||
| return (CommandExecutorExtension)(Class.forName(executorName).getDeclaredConstructor().newInstance()); | ||
| }catch (Exception e){ | ||
| throw new IllegalArgumentException("Could not init a custom executor: '"+executorName+"', caused by: "+e.getMessage(), e); | ||
| } | ||
| } | ||
| throw new IllegalArgumentException("Unknown executor: '"+ executorName +"' provided."); | ||
| } | ||
|
|
||
| private ExecutionMetadata getExecutionMetadata(final File tempSampleOutputDir) { | ||
| Map<String, String> systemProperties = new HashMap<>(); | ||
| for (String systemPropertyKey : SamplesRunner.SAFE_SYSTEM_PROPERTIES) { | ||
| systemProperties.put(systemPropertyKey, System.getProperty(systemPropertyKey)); | ||
| } | ||
|
|
||
| return new ExecutionMetadata(tempSampleOutputDir, systemProperties); | ||
| } | ||
|
|
||
| private Sample initSample(final Sample sampleIn) throws IOException { | ||
| File tmpProjectDir = new File(tempDir, sampleIn.getId()); | ||
| FileUtils.copyDirectory(sampleIn.getProjectDir(), tmpProjectDir); | ||
| Sample sample = new Sample(sampleIn.getId(), tmpProjectDir, sampleIn.getCommands()); | ||
| for (SampleModifier sampleModifier : sampleModifiers) { | ||
| sample = sampleModifier.modify(sample); | ||
| } | ||
| return sample; | ||
| } | ||
|
|
||
| private static class SampleTestDescriptor extends AbstractTestDescriptor { | ||
|
|
||
| private final Sample sample; | ||
|
|
||
| private SampleTestDescriptor(UniqueId uniqueId, Sample sample) { | ||
| super(uniqueId, sample.getId()); | ||
| this.sample = sample; | ||
| } | ||
|
|
||
| @Override | ||
| public Type getType() { | ||
| return Type.TEST; | ||
| } | ||
| } | ||
|
|
||
| private static class SamplesTestDescriptor extends AbstractTestDescriptor { | ||
|
|
||
| private SamplesTestDescriptor(UniqueId uniqueId, Class<?> testClass) { | ||
| super(uniqueId, testClass.getSimpleName(), ClassSource.from(testClass)); | ||
| } | ||
|
|
||
| @Override | ||
| public Type getType() { | ||
| return Type.CONTAINER; | ||
| } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
samples-check/src/main/java/org/gradle/exemplar/test/engine/ValidationExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package org.gradle.exemplar.test.engine; | ||
|
|
||
| import org.gradle.exemplar.model.Sample; | ||
|
|
||
| public interface ValidationExecutor { | ||
|
|
||
| void executeValidation(Sample testSpecificSample); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
samples-check/src/main/resources/META-INF/services/org.junit.platform.engine.TestEngine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| org.gradle.exemplar.test.engine.SamplesRunnerJUnitEngine |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs a proper const in the Libraries
we can also consider the new way of managing dependencies in Gradle using TOML file