Skip to content

Commit e91fb58

Browse files
authored
ignore junit platform spock engine reporting when spock 2 is configured (via #842)
1 parent 0ae1c29 commit e91fb58

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@
8787
"ClassFanOutComplexity",
8888
"MultipleStringLiterals",
8989
"ClassDataAbstractionCoupling",
90-
"PMD.GodClass"
90+
"PMD.GodClass",
91+
"PMD.TooManyMethods"
9192
})
9293
public class AllureJunitPlatform implements TestExecutionListener {
9394

@@ -113,6 +114,11 @@ public class AllureJunitPlatform implements TestExecutionListener {
113114
private static final String TEXT_PLAIN = "text/plain";
114115
private static final String TXT_EXTENSION = ".txt";
115116

117+
private static final boolean HAS_SPOCK2_IN_CLASSPATH
118+
= isClassAvailableOnClasspath("io.qameta.allure.spock2.AllureSpock2");
119+
120+
private static final String ENGINE_SPOCK2 = "spock";
121+
116122
private final ThreadLocal<TestPlan> testPlanStorage = new InheritableThreadLocal<>();
117123

118124
private final ThreadLocal<Uuids> tests = new InheritableThreadLocal<Uuids>() {
@@ -142,6 +148,26 @@ public AllureLifecycle getLifecycle() {
142148
return lifecycle;
143149
}
144150

151+
private boolean shouldSkipReportingFor(final TestIdentifier testIdentifier) {
152+
return !testIdentifier.getParentId().isPresent()
153+
|| HAS_SPOCK2_IN_CLASSPATH && engineIs(testIdentifier, ENGINE_SPOCK2);
154+
}
155+
156+
private boolean engineIs(final TestIdentifier testIdentifier, final String engineId) {
157+
return testIdentifier.getUniqueIdObject().getEngineId()
158+
.filter(v -> Objects.equals(engineId, v))
159+
.isPresent();
160+
}
161+
162+
private static boolean isClassAvailableOnClasspath(final String clazz) {
163+
try {
164+
AllureJunitPlatform.class.getClassLoader().loadClass(clazz);
165+
return true;
166+
} catch (Exception ignored) {
167+
return false;
168+
}
169+
}
170+
145171
@Override
146172
public void testPlanExecutionStarted(final TestPlan testPlan) {
147173
testPlanStorage.set(testPlan);
@@ -158,8 +184,7 @@ public void testPlanExecutionFinished(final TestPlan testPlan) {
158184

159185
@Override
160186
public void executionStarted(final TestIdentifier testIdentifier) {
161-
// skip root
162-
if (!testIdentifier.getParentId().isPresent()) {
187+
if (shouldSkipReportingFor(testIdentifier)) {
163188
return;
164189
}
165190
// create container for every TestIdentifier. We need containers for tests in order
@@ -174,8 +199,7 @@ public void executionStarted(final TestIdentifier testIdentifier) {
174199
@Override
175200
public void executionFinished(final TestIdentifier testIdentifier,
176201
final TestExecutionResult testExecutionResult) {
177-
// skip root
178-
if (!testIdentifier.getParentId().isPresent()) {
202+
if (shouldSkipReportingFor(testIdentifier)) {
179203
return;
180204
}
181205
final Status status = extractStatus(testExecutionResult);
@@ -196,8 +220,7 @@ public void executionFinished(final TestIdentifier testIdentifier,
196220
@Override
197221
public void executionSkipped(final TestIdentifier testIdentifier,
198222
final String reason) {
199-
// skip root
200-
if (!testIdentifier.getParentId().isPresent()) {
223+
if (shouldSkipReportingFor(testIdentifier)) {
201224
return;
202225
}
203226
final TestPlan testPlan = testPlanStorage.get();
@@ -217,6 +240,10 @@ public void executionSkipped(final TestIdentifier testIdentifier,
217240
@Override
218241
public void reportingEntryPublished(final TestIdentifier testIdentifier,
219242
final ReportEntry entry) {
243+
if (shouldSkipReportingFor(testIdentifier)) {
244+
return;
245+
}
246+
220247
final Map<String, String> keyValuePairs = unwrap(entry.getKeyValuePairs());
221248
if (keyValuePairs.containsKey(ALLURE_FIXTURE)) {
222249
processFixtureEvent(testIdentifier, keyValuePairs);
@@ -322,7 +349,7 @@ private void resetContext(final TestIdentifier testIdentifier) {
322349
// test case uuid to allure thread local storage
323350
Optional.of(testIdentifier)
324351
.filter(TestIdentifier::isTest)
325-
.flatMap((TestIdentifier t) -> getTest(t))
352+
.flatMap(this::getTest)
326353
.ifPresent(Allure.getLifecycle()::setCurrentTestCase);
327354
}
328355

0 commit comments

Comments
 (0)