Skip to content

Commit 8d87365

Browse files
authored
Merge pull request #80 from giurim/fix-tagged-init
fix: tagged classes failing during initialization now marked as failed
2 parents a1e2b7f + a26414d commit 8d87365

File tree

7 files changed

+73
-6
lines changed

7 files changed

+73
-6
lines changed

src/main/java/co/helmethair/scalatest/descriptor/ScalatestDescriptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public abstract class ScalatestDescriptor implements TestDescriptor {
1616
private final UniqueId id;
1717
private TestDescriptor parentDescriptor = null;
1818
private Set<TestDescriptor> childDescriptors = new HashSet<TestDescriptor>();
19+
protected Set<TestTag> tags = new HashSet<>();
1920

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

7677
@Override
7778
public Set<TestTag> getTags() {
78-
return new HashSet<>();
79+
return tags;
7980
}
8081

8182
@Override

src/main/java/co/helmethair/scalatest/descriptor/ScalatestFailedInitDescriptor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package co.helmethair.scalatest.descriptor;
22

33
import org.junit.platform.engine.TestSource;
4+
import org.junit.platform.engine.TestTag;
45
import org.junit.platform.engine.support.descriptor.MethodSource;
56

67
import java.util.Optional;
8+
import java.util.Set;
79

810
public class ScalatestFailedInitDescriptor extends ScalatestDescriptor {
911
private final Throwable cause;
1012
private final String suiteId;
1113

12-
public ScalatestFailedInitDescriptor(Throwable cause, String name) {
14+
public ScalatestFailedInitDescriptor(Throwable cause, String name, Set<TestTag> tags) {
1315
super(ENGINE_ID.append("failed", name));
1416
this.cause = cause;
1517
this.suiteId = name;
18+
this.tags = tags;
1619
}
1720

1821
@Override

src/main/java/co/helmethair/scalatest/runtime/Discovery.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import org.junit.platform.engine.TestDescriptor;
99
import org.junit.platform.engine.TestTag;
1010
import org.scalatest.Suite;
11+
import org.scalatest.TagAnnotation;
1112
import scala.Option;
1213

14+
import java.util.Arrays;
1315
import java.util.Collections;
1416
import java.util.List;
1517
import java.util.Set;
@@ -23,15 +25,16 @@ public ScalatestEngineDescriptor discover(ScalatestEngineDescriptor engineDescri
2325
Suite suite = ((Suite) classLoader.loadClass(c.getName()).newInstance());
2426
addSuite(suite, engineDescriptor);
2527
} catch (Throwable e) {
26-
addFailedInit(e, c.getName(), engineDescriptor);
28+
addFailedInit(e, c, engineDescriptor);
2729
}
2830
});
2931

3032
return engineDescriptor;
3133
}
3234

33-
private void addFailedInit(Throwable cause, String className, TestDescriptor parent) {
34-
ScalatestFailedInitDescriptor failed = new ScalatestFailedInitDescriptor(cause, className);
35+
private void addFailedInit(Throwable cause, Class<? extends Suite> clazz, TestDescriptor parent) {
36+
String className = clazz.getName();
37+
ScalatestFailedInitDescriptor failed = new ScalatestFailedInitDescriptor(cause, className, extractTags(clazz));
3538
linkChild(parent, failed);
3639
}
3740

@@ -43,7 +46,7 @@ private void addSuite(Suite suite, TestDescriptor parent) {
4346
try {
4447
addSuite(scalatestNestedSuite, scalatestSuiteDescriptor);
4548
} catch (Throwable e) {
46-
addFailedInit(e, scalatestNestedSuite.getClass().getName(), scalatestSuiteDescriptor);
49+
addFailedInit(e, scalatestNestedSuite.getClass(), scalatestSuiteDescriptor);
4750
}
4851
});
4952
}
@@ -58,6 +61,13 @@ private void addTests(ScalatestSuiteDescriptor suite, Set<String> testNames) {
5861
linkChild(suite, new ScalatestTestDescriptor(suite, testName, getTags(suite.getScalasuite(), testName))));
5962
}
6063

64+
private Set<TestTag> extractTags(Class<? extends Suite> clazz) {
65+
return Arrays.stream(clazz.getAnnotations()).filter(a ->
66+
a.annotationType().isAnnotationPresent(TagAnnotation.class)
67+
).map(a -> TestTag.create(a.annotationType().getName()))
68+
.collect(Collectors.toSet());
69+
}
70+
6171
private Set<TestTag> getTags(Suite scalasuite, String testName) {
6272
Option<scala.collection.immutable.Set<String>> tagSetOption = scalasuite.tags().get(testName);
6373
if (tagSetOption.isDefined()) {

src/test/java/co/helmethair/scalatest/ClassInitializationErrorTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.junit.platform.engine.EngineDiscoveryRequest;
77
import org.junit.platform.engine.ExecutionRequest;
88
import org.junit.platform.engine.TestDescriptor;
9+
import org.junit.platform.launcher.TestExecutionListener;
10+
11+
import java.util.Arrays;
912

1013
import static org.mockito.Mockito.spy;
1114

@@ -24,4 +27,16 @@ void failedToInitializeClass() {
2427
verifyTestStartReported(testId, listener);
2528
verifyTestFailReported(testId, listener);
2629
}
30+
31+
@Test
32+
void failedToInitializeTaggedClass() {
33+
TestExecutionListener listener = spy(new TestExecutionListener() {
34+
});
35+
36+
launch(Arrays.asList("tests.InitErrorTaggedTest"), Arrays.asList("co.helmethair.scalatest.SuitLevelTag"), Arrays.asList(), listener);
37+
38+
String testId = "[engine:scalatest]/[failed:tests.InitErrorTaggedTest]";
39+
verifyTestStartReported(testId, listener);
40+
verifyTestFailReported(testId, listener);
41+
}
2742
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package co.helmethair.scalatest;
2+
3+
import org.scalatest.TagAnnotation;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@TagAnnotation
11+
@Retention(RetentionPolicy.RUNTIME)
12+
@Target({ElementType.METHOD, ElementType.TYPE})
13+
public @interface SuitLevelTag { }

src/test/java/co/helmethair/scalatest/helper/TestHelpers.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ default void verifyTestStartReported(String testIdSuffix, TestExecutionListener
128128
verifyTestStartReported(testIdSuffix, listener, atLeastOnce());
129129
}
130130

131+
default void verifyTestFailReported(String testIdSuffix, TestExecutionListener listener) {
132+
verifyTestFailReported(testIdSuffix, listener, atLeastOnce());
133+
}
134+
131135
default void verifyTestStartNotReported(String testIdSuffix, TestExecutionListener listener) {
132136
verifyTestStartReported(testIdSuffix, listener, never());
133137
}
@@ -138,6 +142,11 @@ default void verifyTestStartReported(String testIdSuffix, TestExecutionListener
138142
);
139143
}
140144

145+
default void verifyTestFailReported(String testIdSuffix, TestExecutionListener listener, VerificationMode mode) {
146+
verify(listener, mode).executionFinished(
147+
argThat(a -> a.getUniqueId().endsWith(testIdSuffix)), argThat(a -> a.getStatus() == TestExecutionResult.Status.FAILED));
148+
}
149+
141150
default void verifyTestFailReported(String testIdsuffix, TestEngineExecutionListener listener) {
142151
verifyTestFailReportedWith(testIdsuffix, listener, null);
143152
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package tests
2+
3+
import co.helmethair.scalatest.SuitLevelTag
4+
import co.helmethair.scalatest.helper.RegisterCall
5+
import org.scalatest.funspec.AnyFunSpec
6+
7+
@SuitLevelTag
8+
class InitErrorTaggedTest extends AnyFunSpec with RegisterCall {
9+
//throw some serious stuff during instantiation
10+
throw new InternalError()
11+
describe("InitErrorTaggedTest") {
12+
it("never runs") {
13+
register()
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)