Skip to content

Commit

Permalink
Change Discovery to allow extensions to include excluded tests again...
Browse files Browse the repository at this point in the history
Call RunContext.stop() on SpockEngineDescriptor.after()

Co-authored-by: Marc Philipp <mail@marcphilipp.de>
  • Loading branch information
leonard84 and marcphilipp committed Oct 31, 2019
1 parent 884bb9a commit 5a35ab1
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
package org.spockframework.runtime;

import org.junit.platform.commons.support.ReflectionSupport;
import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.discovery.ClassSelector;
import org.junit.platform.engine.discovery.UniqueIdSelector;
import org.junit.platform.engine.support.discovery.SelectorResolver;
import org.spockframework.runtime.model.SpecInfo;

import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.*;
import java.util.function.*;

import static java.util.stream.Collectors.toCollection;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
import org.junit.platform.commons.support.ReflectionSupport;
import org.junit.platform.engine.*;
import org.junit.platform.engine.discovery.*;
import org.junit.platform.engine.support.discovery.SelectorResolver;

class ClassSelectorResolver implements SelectorResolver {

private final Predicate<String> classNameFilter;
private final RunContext runContext;

ClassSelectorResolver(Predicate<String> classNameFilter, RunContext runContext) {
ClassSelectorResolver(Predicate<String> classNameFilter) {
this.classNameFilter = classNameFilter;
this.runContext = runContext;
}

@Override
public Resolution resolve(ClassSelector selector, Context context) {
return resolveClass(((ClassSelector) selector).getJavaClass(), context);
return resolveClass(selector.getJavaClass(), context);
}

@Override
Expand All @@ -48,13 +39,8 @@ private Resolution resolveClass(Class<?> specClass, Context context) {
SpecInfo specInfo = new SpecInfoBuilder(specClass).build();
return context
.addToParent(parent -> {
specInfo.getAllFeatures().forEach(featureInfo -> featureInfo.setExcluded(true));
UniqueId uniqueId = parent.getUniqueId().append("spec", specInfo.getReflection().getName());
try {
runContext.createExtensionRunner(specInfo).run();
} catch (Exception e) {
// TODO revisit, this should be handled on the platform level
return Optional.of(new ErrorSpecNode(uniqueId, specInfo, e));
}
return Optional.of(new SpecNode(uniqueId, specInfo));
})
.map(specNode -> toResolution(specInfo, specNode))
Expand All @@ -68,9 +54,9 @@ private Resolution toResolution(SpecInfo specInfo, SpecNode specNode) {
}

private Supplier<Set<? extends DiscoverySelector>> features(SpecInfo specInfo) {
return () -> specInfo.getAllFeaturesInExecutionOrder().stream()
.map(feature -> selectMethod(specInfo.getReflection(), feature.getFeatureMethod().getReflection()))
.collect(toCollection(LinkedHashSet::new));
return () -> {
specInfo.getAllFeatures().forEach(featureInfo -> featureInfo.setExcluded(false));
return Collections.emptySet();
};
}

}
Original file line number Diff line number Diff line change
@@ -1,110 +1,65 @@
package org.spockframework.runtime;

import org.spockframework.runtime.model.*;
import org.spockframework.runtime.model.FeatureInfo;

import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.junit.platform.engine.*;
import org.junit.platform.engine.discovery.MethodSelector;
import org.junit.platform.engine.discovery.UniqueIdSelector;
import org.junit.platform.engine.discovery.*;
import org.junit.platform.engine.support.discovery.SelectorResolver;

import static java.util.Collections.*;
import static org.junit.platform.engine.discovery.DiscoverySelectors.*;

public class MethodSelectorResolver implements SelectorResolver {

private static final Object[] EMPTY_ARGS = new Object[0];

@Override
public Resolution resolve(MethodSelector selector, Context context) {
return context
.addToParent(() -> selectClass(selector.getJavaClass()), parent -> {
if (parent instanceof SpecNode) {
String methodName = selector.getMethodName();
Optional<SpockNode> node = toNode((SpecNode) parent, feature -> feature.getFeatureMethod().getReflection().getName().equals(methodName));
if (!node.isPresent()) {
node = toNode((SpecNode) parent, feature -> methodName.equals(feature.getName()));
}
return node;
}
return Optional.empty();
})
.map(node -> Resolution.match(Match.exact(node, expansionCallback(node))))
.orElse(Resolution.unresolved());

String methodName = selector.getMethodName();
Predicate<FeatureInfo> filter = feature ->
feature.getFeatureMethod().getReflection().getName().equals(methodName)
|| methodName.equals(feature.getName());

DiscoverySelector parentSelector = selectClass(selector.getJavaClass());
return resolve(context, parentSelector, filter);
}

Resolution resolve(Context context, DiscoverySelector parentSelector, Predicate<FeatureInfo> filter) {
return context.resolve(parentSelector).map(testDescriptor -> handle(testDescriptor, filter))
.map(descriptor -> Resolution.match(Match.partial(descriptor)))
.orElseGet(Resolution::unresolved);
}

private TestDescriptor handle(TestDescriptor testDescriptor, Predicate<FeatureInfo> filter) {
if (testDescriptor instanceof SpecNode) {
SpecNode specNode = (SpecNode) testDescriptor;
long count = specNode.getSpecInfo().getAllFeaturesInExecutionOrder().stream()
.filter(filter)
.peek(featureInfo -> featureInfo.setExcluded(false))
.count();
return count == 0 ? null : testDescriptor;
}
return null;
}

@Override
public Resolution resolve(UniqueIdSelector selector, Context context) {
UniqueId uniqueId = selector.getUniqueId();
UniqueId.Segment lastSegment = uniqueId.getLastSegment();
if ("feature".equals(lastSegment.getType())) {
return context
.addToParent(() -> selectUniqueId(uniqueId.removeLastSegment()), parent -> {
if (parent instanceof SpecNode) {
String methodName = lastSegment.getValue();
return toNode((SpecNode) parent, feature -> methodName.equals(feature.getFeatureMethod().getReflection().getName()));
}
return Optional.empty();
})
.map(node -> Resolution.match(Match.exact(node, expansionCallback(node))))
.orElse(Resolution.unresolved());
String methodName = lastSegment.getValue();
return resolve(context,
selectUniqueId(uniqueId.removeLastSegment()),
feature -> methodName.equals(feature.getFeatureMethod().getReflection().getName()));
}
if ("iteration".equals(lastSegment.getType())) {
return context
.addToParent(() -> selectUniqueId(uniqueId.removeLastSegment()), parent -> {
if (parent instanceof ParameterizedFeatureNode) {
FeatureNode featureNode = (FeatureNode) parent;
int iterationIndex = Integer.parseInt(lastSegment.getValue());
// TODO Add iterationIndex as allowed index to featureNode
}
return Optional.empty();
})
.map(node -> Resolution.match(Match.partial(node, expansionCallback(node))))
.orElse(Resolution.unresolved());
return resolve(selectUniqueId(uniqueId.removeLastSegment()), context);
// if (parent instanceof ParameterizedFeatureNode) {
// FeatureNode featureNode = (FeatureNode) parent;
// int iterationIndex = Integer.parseInt(lastSegment.getValue());
// // TODO Add iterationIndex as allowed index to featureNode
// }
}
return Resolution.unresolved();
}

private Optional<SpockNode> toNode(SpecNode specNode, Predicate<FeatureInfo> predicate) {
return specNode.getSpecInfo().getAllFeaturesInExecutionOrder().stream()
.filter(predicate)
.filter(feature -> !feature.isExcluded())
.findAny()
.map(feature -> createNode(specNode, feature));
}

private SpockNode createNode(SpecNode specNode, FeatureInfo feature) {
if (feature.isParameterized()) {
return describeParameterizedFeature(specNode.getUniqueId(), feature);
} else {
return describeSimpleFeature(specNode.getUniqueId(), feature);
}
}

private FeatureNode describeParameterizedFeature(UniqueId parentId, FeatureInfo feature) {
return new ParameterizedFeatureNode(toUniqueId(parentId, feature), feature);
}

private SpockNode describeSimpleFeature(UniqueId parentId, FeatureInfo feature) {
IterationInfo iterationInfo = new IterationInfo(feature, EMPTY_ARGS, 1);
iterationInfo.setName(feature.getName());
UniqueId uniqueId = toUniqueId(parentId, feature);
IterationNode iterationNode = new IterationNode(toUniqueId(uniqueId, feature), iterationInfo);
return new SimpleFeatureNode(uniqueId, feature, iterationNode);
}

private UniqueId toUniqueId(UniqueId parentId, FeatureInfo feature) {
return parentId.append("feature", feature.getFeatureMethod().getReflection().getName());
}

private Supplier<Set<? extends DiscoverySelector>> expansionCallback(TestDescriptor node) {
return () -> {
// TODO allow all iteration indexes
return emptySet();
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@
import org.junit.platform.engine.support.hierarchical.EngineExecutionContext;

public class RunContext implements EngineExecutionContext {
private static final ThreadLocal<LinkedList<RunContext>> contextStacks =
new ThreadLocal<LinkedList<RunContext>>() {
@Override
protected LinkedList<RunContext> initialValue() {
return new LinkedList<>();
}
};
private static final ThreadLocal<LinkedList<RunContext>> contextStacks = ThreadLocal.withInitial(() -> new LinkedList<>());

private final String name;
private final File spockUserHome;
Expand Down Expand Up @@ -63,7 +57,7 @@ private void start() {
globalExtensionRegistry.startGlobalExtensions();
}

private void stop() {
void stop() {
globalExtensionRegistry.stopGlobalExtensions();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId
SpockEngineDescriptor engineDescriptor = new SpockEngineDescriptor(uniqueId, runContext);
EngineDiscoveryRequestResolver.builder()
.addClassContainerSelectorResolver(SpecUtil::isRunnableSpec)
.addSelectorResolver(context -> new ClassSelectorResolver(context.getClassNameFilter(), runContext))
.addSelectorResolver(context -> new ClassSelectorResolver(context.getClassNameFilter()))
.addSelectorResolver(new MethodSelectorResolver())
.build()
.resolve(discoveryRequest, engineDescriptor);
return engineDescriptor;

return new SpockEngineDiscoveryPostProcessor()
.postProcessEngineDescriptor(uniqueId, runContext, engineDescriptor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public SpockEngineDescriptor(UniqueId uniqueId, RunContext runContext) {
public SpockExecutionContext prepare(SpockExecutionContext context) throws Exception {
return context.withRunContext(runContext);
}

@Override
public void after(SpockExecutionContext context) throws Exception {
runContext.stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.spockframework.runtime;

import org.spockframework.runtime.model.*;

import org.junit.platform.engine.*;

class SpockEngineDiscoveryPostProcessor {

private static final Object[] EMPTY_ARGS = new Object[0];

SpockEngineDescriptor postProcessEngineDescriptor(UniqueId uniqueId, RunContext runContext,
SpockEngineDescriptor engineDescriptor) {
SpockEngineDescriptor processedEngineDescriptor = new SpockEngineDescriptor(uniqueId, runContext);
engineDescriptor.getChildren().stream()
.map(child -> processSpecNode(child, runContext))
.forEach(processedEngineDescriptor::addChild);
return processedEngineDescriptor;
}

private SpockNode createNode(SpecNode specNode, FeatureInfo feature) {
if (feature.isParameterized()) {
return describeParameterizedFeature(specNode.getUniqueId(), feature);
} else {
return describeSimpleFeature(specNode.getUniqueId(), feature);
}
}

private FeatureNode describeParameterizedFeature(UniqueId parentId, FeatureInfo feature) {
return new ParameterizedFeatureNode(toUniqueId(parentId, feature), feature);
}

private SpockNode describeSimpleFeature(UniqueId parentId, FeatureInfo feature) {
IterationInfo iterationInfo = new IterationInfo(feature, EMPTY_ARGS, 1);
iterationInfo.setName(feature.getName());
UniqueId uniqueId = toUniqueId(parentId, feature);
IterationNode iterationNode = new IterationNode(toUniqueId(uniqueId, feature), iterationInfo);
return new SimpleFeatureNode(uniqueId, feature, iterationNode);
}

private UniqueId toUniqueId(UniqueId parentId, FeatureInfo feature) {
return parentId.append("feature", feature.getFeatureMethod().getReflection().getName());
}

private TestDescriptor processSpecNode(TestDescriptor child, RunContext runContext) {
if (child instanceof SpecNode) {
SpecNode specNode = (SpecNode) child;
try {
runContext.createExtensionRunner(specNode.getSpecInfo()).run();
} catch (Exception e) {
return new ErrorSpecNode(specNode.getUniqueId(), specNode.getSpecInfo(), e);
}
specNode.getSpecInfo().getAllFeaturesInExecutionOrder().stream()
.filter(featureInfo -> !featureInfo.isExcluded())
.map(featureInfo -> createNode(specNode, featureInfo))
.forEach(specNode::addChild);
}
return child;
}
}

0 comments on commit 5a35ab1

Please sign in to comment.