Skip to content

Commit 0d07510

Browse files
committed
promote attachment and log steps
1 parent cb9ff6f commit 0d07510

4 files changed

Lines changed: 266 additions & 84 deletions

File tree

allure-awaitility/src/main/java/io/qameta/allure/awaitility/AllureAwaitilityListener.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,13 @@ public void onTimeout(final TimeoutEvent timeoutEvent) {
164164
if (currentConditionStepKey == null) {
165165
return;
166166
}
167-
final AllureExternalKey timeoutStepKey = AllureExternalKey.random(AllureAwaitilityListener.class);
168-
getLifecycle().startStep(
167+
getLifecycle().logStep(
169168
currentConditionStepKey,
170-
timeoutStepKey,
171169
new StepResult()
172170
.setName(String.format(onTimeoutStepTextPattern, timeoutEvent.getDescription()))
173171
.setDescription("Awaitility condition timeout")
174172
.setStatus(Status.BROKEN)
175173
);
176-
getLifecycle().stopStep(timeoutStepKey);
177174
getLifecycle().stopStep(currentConditionStepKey);
178175
}
179176

@@ -202,16 +199,13 @@ public void conditionEvaluated(final EvaluatedCondition<Object> condition) {
202199
if (currentConditionStepKey == null) {
203200
return;
204201
}
205-
final AllureExternalKey lastAwaitStepKey = AllureExternalKey.random(AllureAwaitilityListener.class);
206-
getLifecycle().startStep(
202+
getLifecycle().logStep(
207203
currentConditionStepKey,
208-
lastAwaitStepKey,
209204
new StepResult()
210205
.setName(message)
211206
.setDescription("Awaitility condition satisfied or not, but awaiting still in progress")
212207
.setStatus(Status.PASSED)
213208
);
214-
getLifecycle().stopStep(lastAwaitStepKey);
215209
if (condition.isSatisfied()) {
216210
getLifecycle().updateStep(
217211
currentConditionStepKey, awaitilityCondition -> awaitilityCondition.setStatus(Status.PASSED)

allure-java-commons/src/main/java/io/qameta/allure/Allure.java

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import io.qameta.allure.http.HttpExchange;
1919
import io.qameta.allure.http.HttpExchangeSerializer;
20-
import io.qameta.allure.listener.LifecycleNotifier;
2120
import io.qameta.allure.model.Label;
2221
import io.qameta.allure.model.Link;
2322
import io.qameta.allure.model.Parameter;
@@ -47,7 +46,7 @@
4746
/**
4847
* The class contains some useful methods to work with {@link AllureLifecycle}.
4948
*/
50-
@SuppressWarnings({"PMD.TooManyMethods", "PMD.GodClass"})
49+
@SuppressWarnings("PMD.TooManyMethods")
5150
public final class Allure {
5251

5352
private static final String TEXT_PLAIN = "text/plain";
@@ -98,8 +97,7 @@ public static void step(final String name) {
9897
* @param status the step status.
9998
*/
10099
public static void step(final String name, final Status status) {
101-
getLifecycle().startStep(new StepResult().setName(name).setStatus(status));
102-
getLifecycle().stopStep();
100+
getLifecycle().logStep(new StepResult().setName(name).setStatus(status));
103101
}
104102

105103
/**
@@ -479,31 +477,7 @@ public static CompletableFuture<Void> attachmentAsync(
479477
final String name, final String type,
480478
final CompletionStage<? extends InputStream> body,
481479
final AttachmentOptions options) {
482-
final AllureLifecycle lifecycle = getLifecycle();
483-
if (isDirectAttachmentWrite()) {
484-
return lifecycle.addAttachmentAsync(name, type, body, options);
485-
}
486-
487-
final StepResult step = new StepResult()
488-
.setName(attachmentStepName(name))
489-
.setStatus(Status.PASSED);
490-
lifecycle.startStep(step);
491-
try {
492-
// the status update runs inside the tracked future, so a failed async attachment is
493-
// guaranteed to mark the step before the owning test result is written
494-
return lifecycle.addAttachmentAsync(name, type, body, options, (result, throwable) -> {
495-
if (Objects.nonNull(throwable)) {
496-
step.setStatus(getStatus(throwable).orElse(Status.BROKEN))
497-
.setStatusDetails(getStatusDetails(throwable).orElse(null));
498-
}
499-
});
500-
} catch (Throwable throwable) {
501-
step.setStatus(getStatus(throwable).orElse(Status.BROKEN))
502-
.setStatusDetails(getStatusDetails(throwable).orElse(null));
503-
throw ExceptionUtils.sneakyThrow(throwable);
504-
} finally {
505-
lifecycle.stopStep();
506-
}
480+
return getLifecycle().addAttachmentStepAsync(name, type, body, options);
507481
}
508482

509483
/**
@@ -538,41 +512,7 @@ private static void addAttachmentAsStep(final String name, final String type,
538512

539513
private static void addAttachmentAsStep(final String name, final String type,
540514
final InputStream content, final AttachmentOptions options) {
541-
final AllureLifecycle lifecycle = getLifecycle();
542-
if (isDirectAttachmentWrite()) {
543-
addAttachmentDirect(lifecycle, name, type, content, options);
544-
return;
545-
}
546-
547-
lifecycle.startStep(new StepResult().setName(attachmentStepName(name)));
548-
try {
549-
lifecycle.addAttachment(name, type, content, options);
550-
lifecycle.updateStep(step -> step.setStatus(Status.PASSED));
551-
} catch (Throwable throwable) {
552-
lifecycle.updateStep(
553-
step -> step
554-
.setStatus(getStatus(throwable).orElse(Status.BROKEN))
555-
.setStatusDetails(getStatusDetails(throwable).orElse(null))
556-
);
557-
throw ExceptionUtils.sneakyThrow(throwable);
558-
} finally {
559-
lifecycle.stopStep();
560-
}
561-
}
562-
563-
private static void addAttachmentDirect(final AllureLifecycle lifecycle,
564-
final String name, final String type,
565-
final InputStream content, final AttachmentOptions options) {
566-
lifecycle.addAttachment(name, type, content, options);
567-
}
568-
569-
private static boolean isDirectAttachmentWrite() {
570-
return LifecycleNotifier.isListenerCallbackRunning()
571-
|| getLifecycle().getCurrentExecutableKey().isEmpty();
572-
}
573-
574-
private static String attachmentStepName(final String name) {
575-
return Objects.isNull(name) || name.isEmpty() ? "Attachment" : name;
515+
getLifecycle().addAttachmentStep(name, type, content, options);
576516
}
577517

578518
/**

0 commit comments

Comments
 (0)