Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
pbielicki committed Nov 2, 2021
1 parent 3b3ecaa commit 0f85578
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
2 changes: 0 additions & 2 deletions docs/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ plugins {

dependencies {
implementation(project(":samples-check"))
implementation(Libraries.JUNIT)
}

tasks.test {
useJUnit()
inputs.file("README.adoc").withPathSensitivity(PathSensitivity.RELATIVE)
}
4 changes: 3 additions & 1 deletion docs/src/test/java/org/gradle/exemplar/ReadmeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import org.gradle.exemplar.test.Samples;
import org.gradle.exemplar.test.engine.GradleSamplesExtension;

import static org.gradle.exemplar.test.Samples.SamplesType.EMBEDDED;

@Samples(root = ".",
samplesType = Samples.SamplesType.EMBEDDED,
samplesType = EMBEDDED,
implicitRootDirSupplier = GradleSamplesExtension.ImplicitSamplesRootDirSupplier.class,
commandExecutorFunction = GradleSamplesExtension.GradleCommandExecutorFunction.class
)
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
spock = "2.0-groovy-3.0"
junit-platform = "1.8.1"

[libraries]
asciidoctorj = "org.asciidoctor:asciidoctorj:1.5.8.1"
Expand All @@ -9,6 +10,8 @@ commons-lang3 = "org.apache.commons:commons-lang3:3.12.0"
groovy = "org.codehaus.groovy:groovy:3.0.8"
junit = "junit:junit:4.13.2"
junit-vintage-engine = "org.junit.vintage:junit-vintage-engine:5.8.1"
junit-platform-engine = { module="org.junit.platform:junit-platform-engine", version.ref="junit-platform" }
junit-platform-launcher = { module="org.junit.platform:junit-platform-launcher", version.ref="junit-platform" }
jsr305 = "com.google.code.findbugs:jsr305:3.0.2"
objenesis = "org.objenesis:objenesis:3.2"
spock-core = { module="org.spockframework:spock-core", version.ref="spock" }
Expand Down
3 changes: 2 additions & 1 deletion samples-check/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ dependencies {
compileOnly(libs.jsr305)
implementation(libs.commons.io)
implementation(libs.commons.lang3)
implementation("org.junit.platform:junit-platform-engine:1.8.1")
implementation(libs.junit.platform.engine)
implementation(libs.junit.platform.launcher)
implementation(gradleTestKit())
testImplementation(libs.cglib)
testImplementation(libs.groovy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.gradle.exemplar.executor.CliCommandExecutor;
import org.gradle.exemplar.executor.CommandExecutionResult;
import org.gradle.exemplar.executor.CommandExecutor;
import org.gradle.exemplar.executor.ExecutionMetadata;
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.SampleModifier;
import org.gradle.exemplar.test.Samples;
import org.gradle.exemplar.test.normalizer.OutputNormalizer;
import org.gradle.exemplar.test.verifier.AnyOrderLineSegmentedOutputVerifier;
import org.gradle.exemplar.test.verifier.StrictOrderLineSegmentedOutputVerifier;
import org.junit.experimental.categories.Category;
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.TestTag;
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.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.PostDiscoveryFilter;
import org.opentest4j.AssertionFailedError;

import java.io.File;
Expand All @@ -34,12 +37,16 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;

public class SamplesRunnerJUnitEngine implements TestEngine {

Expand All @@ -60,16 +67,26 @@ public String getId() {
@Override
public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) {
EngineDescriptor engineDescriptor = new EngineDescriptor(uniqueId, "Samples Runner JUnit Engine");
List<PostDiscoveryFilter> postDiscoveryFilters = new ArrayList<>();
if (discoveryRequest instanceof LauncherDiscoveryRequest) {
postDiscoveryFilters.addAll(((LauncherDiscoveryRequest) discoveryRequest).getPostDiscoveryFilters());
}

discoveryRequest.getSelectorsByType(ClassSelector.class)
.stream()
.map(ClassSelector::getJavaClass).forEach(javaClass -> {
filterClass(uniqueId, engineDescriptor, javaClass);
filterClass(uniqueId, postDiscoveryFilters, engineDescriptor, javaClass);
});

return engineDescriptor;
}

private void filterClass(UniqueId uniqueId, EngineDescriptor engineDescriptor, Class<?> javaClass) {
private void filterClass(
UniqueId uniqueId,
List<PostDiscoveryFilter> postDiscoveryFilters,
EngineDescriptor engineDescriptor,
Class<?> javaClass
) {
Samples samplesDef = javaClass.getAnnotation(Samples.class);
if (samplesDef != null) {
UniqueId classUniqueId = uniqueId.append("className", javaClass.getSimpleName());
Expand All @@ -78,6 +95,10 @@ private void filterClass(UniqueId uniqueId, EngineDescriptor engineDescriptor, C
javaClass
);

if (postDiscoveryFilters.stream().anyMatch(filter -> filter.apply(samplesTestDescriptor).excluded())) {
return;
}

List<Sample> samples = samplesDef.samplesType() == Samples.SamplesType.DEFAULT ?
SamplesDiscovery.externalSamples(getSamplesRootDir(
samplesDef.root(),
Expand Down Expand Up @@ -301,8 +322,26 @@ public Type getType() {

private static class SamplesTestDescriptor extends AbstractTestDescriptor {

private final Set<TestTag> tags;

private SamplesTestDescriptor(UniqueId uniqueId, Class<?> testClass) {
super(uniqueId, testClass.getSimpleName(), ClassSource.from(testClass));
Category category = testClass.getAnnotation(Category.class);
if (category != null) {
Set<TestTag> tmpTags = new HashSet<>();
for (Class<?> tag : category.value()) {
tmpTags.add(TestTag.create(tag.getName()));
}

tags = unmodifiableSet(tmpTags);
} else {
tags = emptySet();
}
}

@Override
public Set<TestTag> getTags() {
return tags;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package org.gradle.exemplar.test.runner
import org.gradle.exemplar.test.Samples
import org.junit.experimental.categories.Category
import org.junit.runner.Request
import spock.lang.Ignore
import spock.lang.Specification

@Ignore("Find a way to test the JUnit5 engine instead of JUnit4 runner")
class SamplesRunnerIntegrationTest extends Specification {
def "runs samples-check CLI samples"() {
def notifier = new CollectingNotifier()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import org.junit.Rule
import org.junit.experimental.categories.Category
import org.junit.rules.TemporaryFolder
import org.junit.runner.Request
import org.junit.runner.RunWith
import spock.lang.Ignore
import spock.lang.Specification

@Ignore("Find a way to test the JUnit5 engine instead of JUnit4 runner")
class SamplesRunnerSadDayIntegrationTest extends Specification {
@Rule
TemporaryFolder tmpDir = new TemporaryFolder()
Expand Down

0 comments on commit 0f85578

Please sign in to comment.