Skip to content

Commit cb9ff6f

Browse files
committed
simplify
1 parent 0e49279 commit cb9ff6f

2 files changed

Lines changed: 48 additions & 56 deletions

File tree

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

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import io.qameta.allure.model.TestResult;
5252

5353
import java.io.ByteArrayInputStream;
54-
import java.io.InputStream;
5554
import java.nio.charset.StandardCharsets;
5655
import java.nio.file.Paths;
5756
import java.util.Collections;
@@ -63,7 +62,6 @@
6362
import java.util.Optional;
6463
import java.util.UUID;
6564
import java.util.concurrent.ConcurrentHashMap;
66-
import java.util.function.Supplier;
6765
import java.util.stream.Collectors;
6866
import java.util.stream.IntStream;
6967
import java.util.stream.Stream;
@@ -276,13 +274,14 @@ private void handleStartPickleStep(final TestCase testCase,
276274
.setName(step.getKeyword() + step.getText())
277275
.setStart(System.currentTimeMillis());
278276

277+
final AllureExternalKey stepKey = stepKey(pickleStep.getId().toString());
279278
lifecycle.setCurrent(testKey(uuid));
280-
lifecycle.startStep(stepKey(pickleStep.getId().toString()), stepResult);
279+
lifecycle.startStep(stepKey, stepResult);
281280

282281
final StepArgument stepArgument = step.getArgument();
283282
if (stepArgument instanceof DataTableArgument) {
284283
final DataTableArgument dataTableArgument = (DataTableArgument) stepArgument;
285-
createDataTableAttachment(dataTableArgument);
284+
createDataTableAttachment(stepKey, dataTableArgument);
286285
}
287286
}
288287

@@ -340,20 +339,28 @@ private static boolean isFixtureHook(final HookTestStep hook) {
340339
}
341340

342341
private void handleWriteEvent(final WriteEvent event) {
343-
addAttachmentToCurrent(
344-
"Text output",
345-
TEXT_PLAIN,
346-
() -> new ByteArrayInputStream(Objects.toString(event.getText()).getBytes(StandardCharsets.UTF_8)),
347-
AttachmentOptions.empty()
342+
// user output is genuinely ambient: it lands under whatever executable is current,
343+
// and is silently skipped (unsupported executables) — no key to address it by
344+
lifecycle.getCurrentExecutableKey().ifPresent(
345+
key -> lifecycle.addAttachment(
346+
key,
347+
"Text output",
348+
TEXT_PLAIN,
349+
new ByteArrayInputStream(Objects.toString(event.getText()).getBytes(StandardCharsets.UTF_8)),
350+
AttachmentOptions.empty()
351+
)
348352
);
349353
}
350354

351355
private void handleEmbedEvent(final EmbedEvent event) {
352-
addAttachmentToCurrent(
353-
event.name,
354-
event.getMediaType(),
355-
() -> new ByteArrayInputStream(event.getData()),
356-
AttachmentOptions.empty()
356+
lifecycle.getCurrentExecutableKey().ifPresent(
357+
key -> lifecycle.addAttachment(
358+
key,
359+
event.name,
360+
event.getMediaType(),
361+
new ByteArrayInputStream(event.getData()),
362+
AttachmentOptions.empty()
363+
)
357364
);
358365
}
359366

@@ -440,11 +447,14 @@ private List<Parameter> getExamplesAsParameters(
440447
.collect(Collectors.toList());
441448
}
442449

443-
private void createDataTableAttachment(final DataTableArgument dataTableArgument) {
444-
addAttachmentToCurrent(
450+
private void createDataTableAttachment(final AllureExternalKey stepKey,
451+
final DataTableArgument dataTableArgument) {
452+
// the data table belongs to the step this adapter just started — address it by key
453+
lifecycle.addAttachment(
454+
stepKey,
445455
"Data table",
446456
"text/csv",
447-
() -> new ByteArrayInputStream(toCsv(dataTableArgument).getBytes(StandardCharsets.UTF_8)),
457+
new ByteArrayInputStream(toCsv(dataTableArgument).getBytes(StandardCharsets.UTF_8)),
448458
AttachmentOptions.empty()
449459
);
450460
}
@@ -472,24 +482,6 @@ private static String toCsv(final DataTableArgument dataTableArgument) {
472482
return dataTableCsv.toString();
473483
}
474484

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-
*/
479-
private void addAttachmentToCurrent(final String name, final String type,
480-
final Supplier<InputStream> content, final AttachmentOptions options) {
481-
lifecycle.getCurrentExecutableKey()
482-
.ifPresent(
483-
key -> lifecycle.addAttachment(
484-
key,
485-
name,
486-
type,
487-
content.get(),
488-
options
489-
)
490-
);
491-
}
492-
493485
private void handleStopHookStep(final Result eventResult,
494486
final HookTestStep hook) {
495487
final String containerUuid = hookStepContainerUuid.get(hook.getId());

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -360,29 +360,29 @@ public void reportingEntryPublished(final TestIdentifier testIdentifier,
360360
return;
361361
}
362362

363-
if (keyValuePairs.containsKey(STDOUT)) {
364-
final String content = keyValuePairs.getOrDefault(STDOUT, "");
365-
addAttachmentToCurrent("Stdout", content);
366-
}
367-
if (keyValuePairs.containsKey(STDERR)) {
368-
final String content = keyValuePairs.getOrDefault(STDERR, "");
369-
addAttachmentToCurrent("Stderr", content);
370-
}
371-
372-
}
373-
374-
private void addAttachmentToCurrent(final String name, final String content) {
363+
// captured output is genuinely ambient: it lands under whatever executable is current,
364+
// and is silently skipped (unsupported executables) — no key to address it by
375365
final AllureLifecycle lifecycle = getLifecycle();
376-
lifecycle.getCurrentExecutableKey()
377-
.ifPresent(
378-
key -> lifecycle.addAttachment(
379-
key,
380-
name,
381-
TEXT_PLAIN,
382-
new ByteArrayInputStream(content.getBytes(UTF_8)),
383-
AttachmentOptions.empty()
384-
)
366+
lifecycle.getCurrentExecutableKey().ifPresent(key -> {
367+
if (keyValuePairs.containsKey(STDOUT)) {
368+
lifecycle.addAttachment(
369+
key,
370+
"Stdout",
371+
TEXT_PLAIN,
372+
new ByteArrayInputStream(keyValuePairs.getOrDefault(STDOUT, "").getBytes(UTF_8)),
373+
AttachmentOptions.empty()
385374
);
375+
}
376+
if (keyValuePairs.containsKey(STDERR)) {
377+
lifecycle.addAttachment(
378+
key,
379+
"Stderr",
380+
TEXT_PLAIN,
381+
new ByteArrayInputStream(keyValuePairs.getOrDefault(STDERR, "").getBytes(UTF_8)),
382+
AttachmentOptions.empty()
383+
);
384+
}
385+
});
386386
}
387387

388388
@SuppressWarnings("PMD.InefficientEmptyStringCheck")

0 commit comments

Comments
 (0)