Skip to content

Commit 0e49279

Browse files
committed
fix missing context warnings
1 parent ade0442 commit 0e49279

16 files changed

Lines changed: 185 additions & 21 deletions

File tree

allure-cucumber7-jvm/src/main/java/io/qameta/allure/cucumber7jvm/AllureCucumber7Jvm.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import java.util.Optional;
6464
import java.util.UUID;
6565
import java.util.concurrent.ConcurrentHashMap;
66+
import java.util.function.Supplier;
6667
import java.util.stream.Collectors;
6768
import java.util.stream.IntStream;
6869
import java.util.stream.Stream;
@@ -342,7 +343,7 @@ private void handleWriteEvent(final WriteEvent event) {
342343
addAttachmentToCurrent(
343344
"Text output",
344345
TEXT_PLAIN,
345-
new ByteArrayInputStream(Objects.toString(event.getText()).getBytes(StandardCharsets.UTF_8)),
346+
() -> new ByteArrayInputStream(Objects.toString(event.getText()).getBytes(StandardCharsets.UTF_8)),
346347
AttachmentOptions.empty()
347348
);
348349
}
@@ -351,7 +352,7 @@ private void handleEmbedEvent(final EmbedEvent event) {
351352
addAttachmentToCurrent(
352353
event.name,
353354
event.getMediaType(),
354-
new ByteArrayInputStream(event.getData()),
355+
() -> new ByteArrayInputStream(event.getData()),
355356
AttachmentOptions.empty()
356357
);
357358
}
@@ -440,6 +441,15 @@ private List<Parameter> getExamplesAsParameters(
440441
}
441442

442443
private void createDataTableAttachment(final DataTableArgument dataTableArgument) {
444+
addAttachmentToCurrent(
445+
"Data table",
446+
"text/csv",
447+
() -> new ByteArrayInputStream(toCsv(dataTableArgument).getBytes(StandardCharsets.UTF_8)),
448+
AttachmentOptions.empty()
449+
);
450+
}
451+
452+
private static String toCsv(final DataTableArgument dataTableArgument) {
443453
final List<List<String>> rowsInTable = dataTableArgument.cells();
444454
final StringBuilder dataTableCsv = new StringBuilder();
445455
for (List<String> columns : rowsInTable) {
@@ -459,23 +469,22 @@ private void createDataTableAttachment(final DataTableArgument dataTableArgument
459469
dataTableCsv.append(rowValue);
460470
}
461471
}
462-
addAttachmentToCurrent(
463-
"Data table",
464-
"text/csv",
465-
new ByteArrayInputStream(dataTableCsv.toString().getBytes(StandardCharsets.UTF_8)),
466-
AttachmentOptions.empty()
467-
);
472+
return dataTableCsv.toString();
468473
}
469474

475+
/**
476+
* Attaches to the current executable, or silently skips — including the content computation — when no
477+
* executable is running (for example when Allure reporting is disabled).
478+
*/
470479
private void addAttachmentToCurrent(final String name, final String type,
471-
final InputStream stream, final AttachmentOptions options) {
480+
final Supplier<InputStream> content, final AttachmentOptions options) {
472481
lifecycle.getCurrentExecutableKey()
473482
.ifPresent(
474483
key -> lifecycle.addAttachment(
475484
key,
476485
name,
477486
type,
478-
stream,
487+
content.get(),
479488
options
480489
)
481490
);

allure-hamcrest/src/main/java/io/qameta/allure/hamcrest/AllureHamcrestAssert.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public void initAssertThat() {
9191
*/
9292
@Before("initAssertThat()")
9393
public void catchAndStartStep(final JoinPoint joinPoint) {
94+
// enrichment-only integration: silently skip when no executable is running,
95+
// so a disabled Allure reporter produces no warnings and no wasted work
96+
if (getLifecycle().getCurrentExecutableKey().isEmpty()) {
97+
return;
98+
}
9499
if (joinPoint.getArgs().length == 3) {
95100
final String reason = (String) joinPoint.getArgs()[0];
96101
final String actual = ObjectUtils.toString(joinPoint.getArgs()[1]);
@@ -121,6 +126,9 @@ public void catchAndStartStep(final JoinPoint joinPoint) {
121126
* @param e the e
122127
*/
123128
public void stepFailed(final Throwable e) {
129+
if (getLifecycle().getCurrentExecutableKey().isEmpty()) {
130+
return;
131+
}
124132
getLifecycle().updateStep(s -> s.setStatus(getStatus(e).orElse(Status.BROKEN)));
125133
getLifecycle().stopStep();
126134
}
@@ -130,6 +138,9 @@ public void stepFailed(final Throwable e) {
130138
*/
131139
@AfterReturning(pointcut = "initAssertThat()")
132140
public void stepStop() {
141+
if (getLifecycle().getCurrentExecutableKey().isEmpty()) {
142+
return;
143+
}
133144
getLifecycle().updateStep(s -> s.setStatus(Status.PASSED));
134145
getLifecycle().stopStep();
135146
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2016-2026 Qameta Software Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.qameta.allure.hamcrest;
17+
18+
import io.qameta.allure.test.AllureResults;
19+
import org.hamcrest.MatcherAssert;
20+
import org.hamcrest.Matchers;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static io.qameta.allure.test.RunUtils.runTests;
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Hamcrest asserts are enrichment-only: with no Allure executable running (for example when the reporter is
28+
* disabled), the aspect must skip silently — no step results, no warnings, and the assert itself still executes.
29+
*/
30+
class AllureHamcrestNoContextTest {
31+
32+
@Test
33+
void shouldSkipSilentlyWithoutTestContext() {
34+
final AllureResults results = runTests(lifecycle ->
35+
MatcherAssert.assertThat("the assert still runs", Matchers.notNullValue()));
36+
37+
assertThat(results.getTestResults()).isEmpty();
38+
assertThat(results.getTestResultContainers()).isEmpty();
39+
}
40+
}

allure-httpclient/src/main/java/io/qameta/allure/httpclient/AllureHttpClientRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.qameta.allure.httpclient;
1717

18+
import io.qameta.allure.Allure;
1819
import io.qameta.allure.http.HttpExchangeBody;
1920
import io.qameta.allure.http.HttpExchangeRequest;
2021
import org.apache.http.Header;
@@ -46,6 +47,11 @@ public class AllureHttpClientRequest implements HttpRequestInterceptor {
4647
public void process(final HttpRequest request,
4748
final HttpContext context)
4849
throws IOException {
50+
// enrichment-only integration: silently skip when no executable is running,
51+
// so a disabled Allure reporter produces no warnings and no body copying
52+
if (Allure.getLifecycle().getCurrentExecutableKey().isEmpty()) {
53+
return;
54+
}
4955

5056
final HttpExchangeRequest.Builder builder = HttpExchangeRequest
5157
.builder(request.getRequestLine().getMethod(), request.getRequestLine().getUri());

allure-httpclient/src/main/java/io/qameta/allure/httpclient/AllureHttpClientResponse.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public AllureHttpClientResponse configureHttpExchange(final Consumer<HttpExchang
6363
public void process(final HttpResponse response,
6464
final HttpContext context)
6565
throws IOException {
66+
// enrichment-only integration: silently skip when no executable is running,
67+
// so a disabled Allure reporter produces no warnings and no entity buffering
68+
if (Allure.getLifecycle().getCurrentExecutableKey().isEmpty()) {
69+
return;
70+
}
6671

6772
final HttpExchangeResponse.Builder builder = HttpExchangeResponse.builder()
6873
.setStatus(response.getStatusLine().getStatusCode())

allure-httpclient5/src/main/java/io/qameta/allure/httpclient5/AllureHttpClient5Request.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.qameta.allure.httpclient5;
1717

18+
import io.qameta.allure.Allure;
1819
import io.qameta.allure.http.HttpExchangeBody;
1920
import io.qameta.allure.http.HttpExchangeRequest;
2021
import org.apache.hc.core5.http.EntityDetails;
@@ -46,6 +47,11 @@ public class AllureHttpClient5Request implements HttpRequestInterceptor {
4647
public void process(final HttpRequest request,
4748
final EntityDetails entity,
4849
final HttpContext context) {
50+
// enrichment-only integration: silently skip when no executable is running,
51+
// so a disabled Allure reporter produces no warnings and no body copying
52+
if (Allure.getLifecycle().getCurrentExecutableKey().isEmpty()) {
53+
return;
54+
}
4955
final HttpExchangeRequest.Builder builder = HttpExchangeRequest
5056
.builder(request.getMethod(), request.getRequestUri());
5157

allure-httpclient5/src/main/java/io/qameta/allure/httpclient5/AllureHttpClient5Response.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public void process(final HttpResponse response,
7070
final EntityDetails entity,
7171
final HttpContext context)
7272
throws IOException {
73+
// enrichment-only integration: silently skip when no executable is running,
74+
// so a disabled Allure reporter produces no warnings and no entity buffering
75+
if (Allure.getLifecycle().getCurrentExecutableKey().isEmpty()) {
76+
return;
77+
}
7378
final HttpExchangeResponse.Builder builder = HttpExchangeResponse.builder()
7479
.setStatus(response.getCode())
7580
.setStatusText(response.getReasonPhrase());

allure-java-commons/src/main/java/io/qameta/allure/aspects/AttachmentsAspects.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ public void anyMethod() {
7474
returning = "result"
7575
)
7676
public void attachment(final JoinPoint joinPoint, final Object result) {
77+
// enrichment aspect: silently skip — including the content conversion — when no
78+
// executable is running, so a disabled Allure reporter produces no warnings
79+
final AllureLifecycle lifecycle = getLifecycle();
80+
if (lifecycle.getCurrentExecutableKey().isEmpty()) {
81+
return;
82+
}
7783
final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
7884
final Attachment attachment = methodSignature.getMethod()
7985
.getAnnotation(Attachment.class);
@@ -85,17 +91,14 @@ public void attachment(final JoinPoint joinPoint, final Object result) {
8591
final String name = attachment.value().isEmpty()
8692
? methodSignature.getName()
8793
: processNameTemplate(attachment.value(), getParametersMap(joinPoint));
88-
final AllureLifecycle lifecycle = getLifecycle();
89-
if (lifecycle.getCurrentExecutableKey().isPresent()) {
90-
lifecycle.addAttachment(
91-
name,
92-
attachment.type(),
93-
new ByteArrayInputStream(bytes),
94-
attachment.fileExtension().isEmpty()
95-
? AttachmentOptions.empty()
96-
: AttachmentOptions.withFileExtension(attachment.fileExtension())
97-
);
98-
}
94+
lifecycle.addAttachment(
95+
name,
96+
attachment.type(),
97+
new ByteArrayInputStream(bytes),
98+
attachment.fileExtension().isEmpty()
99+
? AttachmentOptions.empty()
100+
: AttachmentOptions.withFileExtension(attachment.fileExtension())
101+
);
99102
}
100103

101104
/**

allure-java-commons/src/main/java/io/qameta/allure/aspects/StepsAspects.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public void anyMethod() {
7474
*/
7575
@Before("anyMethod() && withStepAnnotation()")
7676
public void stepStart(final JoinPoint joinPoint) {
77+
// enrichment aspect: silently skip when no executable is running,
78+
// so a disabled Allure reporter produces no warnings and no wasted work
79+
if (getLifecycle().getCurrentExecutableKey().isEmpty()) {
80+
return;
81+
}
7782
final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
7883
final Step step = methodSignature.getMethod().getAnnotation(Step.class);
7984

@@ -97,6 +102,9 @@ public void stepStart(final JoinPoint joinPoint) {
97102
throwing = "e"
98103
)
99104
public void stepFailed(final Throwable e) {
105+
if (getLifecycle().getCurrentExecutableKey().isEmpty()) {
106+
return;
107+
}
100108
getLifecycle().updateStep(
101109
s -> s
102110
.setStatus(getStatus(e).orElse(Status.BROKEN))
@@ -110,6 +118,9 @@ public void stepFailed(final Throwable e) {
110118
*/
111119
@AfterReturning(pointcut = "anyMethod() && withStepAnnotation()")
112120
public void stepStop() {
121+
if (getLifecycle().getCurrentExecutableKey().isEmpty()) {
122+
return;
123+
}
113124
getLifecycle().updateStep(s -> s.setStatus(Status.PASSED));
114125
getLifecycle().stopStep();
115126
}

allure-jax-rs/src/main/java/io/qameta/allure/jaxrs/AllureJaxRs.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public AllureJaxRs configureHttpExchange(final Consumer<HttpExchange.Builder> ex
6464
*/
6565
@Override
6666
public void filter(final ClientRequestContext requestContext) {
67+
// enrichment-only integration: silently skip when no executable is running,
68+
// so a disabled Allure reporter produces no warnings and no wasted work
69+
if (Allure.getLifecycle().getCurrentExecutableKey().isEmpty()) {
70+
return;
71+
}
6772

6873
final String requestUrl = requestContext.getUri().toString();
6974
final Object requestBody = requestContext.getEntity();
@@ -87,6 +92,9 @@ public void filter(final ClientRequestContext requestContext) {
8792
public void filter(final ClientRequestContext requestContext,
8893
final ClientResponseContext responseContext)
8994
throws IOException {
95+
if (Allure.getLifecycle().getCurrentExecutableKey().isEmpty()) {
96+
return;
97+
}
9098

9199
final HttpExchangeResponse.Builder responseBuilder = HttpExchangeResponse.builder()
92100
.setStatus(responseContext.getStatus())

0 commit comments

Comments
 (0)