Skip to content

Commit

Permalink
Merge pull request #80 from giurim/fix-tagged-init
Browse files Browse the repository at this point in the history
fix: tagged classes failing during initialization now marked as failed
  • Loading branch information
giurim authored May 12, 2022
2 parents a1e2b7f + a26414d commit 8d87365
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public abstract class ScalatestDescriptor implements TestDescriptor {
private final UniqueId id;
private TestDescriptor parentDescriptor = null;
private Set<TestDescriptor> childDescriptors = new HashSet<TestDescriptor>();
protected Set<TestTag> tags = new HashSet<>();

protected ScalatestDescriptor(UniqueId id) {
this.id = id;
Expand Down Expand Up @@ -75,7 +76,7 @@ public Set<TestDescriptor> getChildren() {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package co.helmethair.scalatest.descriptor;

import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.TestTag;
import org.junit.platform.engine.support.descriptor.MethodSource;

import java.util.Optional;
import java.util.Set;

public class ScalatestFailedInitDescriptor extends ScalatestDescriptor {
private final Throwable cause;
private final String suiteId;

public ScalatestFailedInitDescriptor(Throwable cause, String name) {
public ScalatestFailedInitDescriptor(Throwable cause, String name, Set<TestTag> tags) {
super(ENGINE_ID.append("failed", name));
this.cause = cause;
this.suiteId = name;
this.tags = tags;
}

@Override
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/co/helmethair/scalatest/runtime/Discovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestTag;
import org.scalatest.Suite;
import org.scalatest.TagAnnotation;
import scala.Option;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand All @@ -23,15 +25,16 @@ public ScalatestEngineDescriptor discover(ScalatestEngineDescriptor engineDescri
Suite suite = ((Suite) classLoader.loadClass(c.getName()).newInstance());
addSuite(suite, engineDescriptor);
} catch (Throwable e) {
addFailedInit(e, c.getName(), engineDescriptor);
addFailedInit(e, c, engineDescriptor);
}
});

return engineDescriptor;
}

private void addFailedInit(Throwable cause, String className, TestDescriptor parent) {
ScalatestFailedInitDescriptor failed = new ScalatestFailedInitDescriptor(cause, className);
private void addFailedInit(Throwable cause, Class<? extends Suite> clazz, TestDescriptor parent) {
String className = clazz.getName();
ScalatestFailedInitDescriptor failed = new ScalatestFailedInitDescriptor(cause, className, extractTags(clazz));
linkChild(parent, failed);
}

Expand All @@ -43,7 +46,7 @@ private void addSuite(Suite suite, TestDescriptor parent) {
try {
addSuite(scalatestNestedSuite, scalatestSuiteDescriptor);
} catch (Throwable e) {
addFailedInit(e, scalatestNestedSuite.getClass().getName(), scalatestSuiteDescriptor);
addFailedInit(e, scalatestNestedSuite.getClass(), scalatestSuiteDescriptor);
}
});
}
Expand All @@ -58,6 +61,13 @@ private void addTests(ScalatestSuiteDescriptor suite, Set<String> testNames) {
linkChild(suite, new ScalatestTestDescriptor(suite, testName, getTags(suite.getScalasuite(), testName))));
}

private Set<TestTag> extractTags(Class<? extends Suite> clazz) {
return Arrays.stream(clazz.getAnnotations()).filter(a ->
a.annotationType().isAnnotationPresent(TagAnnotation.class)
).map(a -> TestTag.create(a.annotationType().getName()))
.collect(Collectors.toSet());
}

private Set<TestTag> getTags(Suite scalasuite, String testName) {
Option<scala.collection.immutable.Set<String>> tagSetOption = scalasuite.tags().get(testName);
if (tagSetOption.isDefined()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.junit.platform.engine.EngineDiscoveryRequest;
import org.junit.platform.engine.ExecutionRequest;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.launcher.TestExecutionListener;

import java.util.Arrays;

import static org.mockito.Mockito.spy;

Expand All @@ -24,4 +27,16 @@ void failedToInitializeClass() {
verifyTestStartReported(testId, listener);
verifyTestFailReported(testId, listener);
}

@Test
void failedToInitializeTaggedClass() {
TestExecutionListener listener = spy(new TestExecutionListener() {
});

launch(Arrays.asList("tests.InitErrorTaggedTest"), Arrays.asList("co.helmethair.scalatest.SuitLevelTag"), Arrays.asList(), listener);

String testId = "[engine:scalatest]/[failed:tests.InitErrorTaggedTest]";
verifyTestStartReported(testId, listener);
verifyTestFailReported(testId, listener);
}
}
13 changes: 13 additions & 0 deletions src/test/java/co/helmethair/scalatest/SuitLevelTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package co.helmethair.scalatest;

import org.scalatest.TagAnnotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@TagAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface SuitLevelTag { }
9 changes: 9 additions & 0 deletions src/test/java/co/helmethair/scalatest/helper/TestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ default void verifyTestStartReported(String testIdSuffix, TestExecutionListener
verifyTestStartReported(testIdSuffix, listener, atLeastOnce());
}

default void verifyTestFailReported(String testIdSuffix, TestExecutionListener listener) {
verifyTestFailReported(testIdSuffix, listener, atLeastOnce());
}

default void verifyTestStartNotReported(String testIdSuffix, TestExecutionListener listener) {
verifyTestStartReported(testIdSuffix, listener, never());
}
Expand All @@ -138,6 +142,11 @@ default void verifyTestStartReported(String testIdSuffix, TestExecutionListener
);
}

default void verifyTestFailReported(String testIdSuffix, TestExecutionListener listener, VerificationMode mode) {
verify(listener, mode).executionFinished(
argThat(a -> a.getUniqueId().endsWith(testIdSuffix)), argThat(a -> a.getStatus() == TestExecutionResult.Status.FAILED));
}

default void verifyTestFailReported(String testIdsuffix, TestEngineExecutionListener listener) {
verifyTestFailReportedWith(testIdsuffix, listener, null);
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/scala/tests/InitErrorTaggedTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tests

import co.helmethair.scalatest.SuitLevelTag
import co.helmethair.scalatest.helper.RegisterCall
import org.scalatest.funspec.AnyFunSpec

@SuitLevelTag
class InitErrorTaggedTest extends AnyFunSpec with RegisterCall {
//throw some serious stuff during instantiation
throw new InternalError()
describe("InitErrorTaggedTest") {
it("never runs") {
register()
}
}
}

0 comments on commit 8d87365

Please sign in to comment.