Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions crates/lest-core/src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ impl<'e> Runner<'e> {
if let Some(until) = retry.and_then(|r| r.until.as_ref()) {
if !outcome.passed && !outcome.cancelled {
// The attempt itself failed (exit code, expect): retry.
retry_reason = Some(outcome.error.clone().unwrap_or_else(|| "attempt failed".into()));
retry_reason = Some(attempt_reason(step.kind(), &outcome));
} else if !outcome.cancelled {
match expr::eval_bool(until, &self_scope) {
Ok(true) => {
Expand All @@ -1291,7 +1291,7 @@ impl<'e> Runner<'e> {
}
}
} else if !outcome.passed && !outcome.cancelled {
retry_reason = Some(outcome.error.clone().unwrap_or_else(|| "attempt failed".into()));
retry_reason = Some(attempt_reason(step.kind(), &outcome));
}

let done = outcome.passed || outcome.cancelled || attempt == max || retry_reason.is_none();
Expand Down Expand Up @@ -1566,3 +1566,19 @@ impl<'a> LineSink<'a> {
self.ctx.emitter.emit(EventBody::StepOutput { step_id: self.step_id.clone(), stream, line });
}
}

/// Why a failed attempt failed, in the words the step's headline would use.
fn attempt_reason(kind: StepKind, o: &steps::AttemptOutcome) -> String {
let probe = StepReport {
kind: Some(kind),
status: Some(if o.errored { StepStatus::Errored } else { StepStatus::Failed }),
error: o.error.clone(),
exit_code: o.exit_code,
http_status: o.http_status,
resolved: o.resolved.clone(),
stdout: o.stdout.clone(),
stderr: o.stderr.clone(),
..Default::default()
};
headline(&probe).unwrap_or_else(|| "failed".into())
}
25 changes: 25 additions & 0 deletions crates/lest-core/tests/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,31 @@ steps:
assert_eq!(ex.attempts, 2);
}

#[tokio::test]
async fn a_failed_attempt_says_why_it_is_retried() {
let f = Fixture::new(&[(
"a.lest.yaml",
r#"
apiVersion: lest/v1
id: retry-why
name: Retry why
steps:
- id: poll
run: 'echo "no match yet" >&2; exit 4'
retry: { attempts: 2, delay: 10ms }
"#,
)]);
let (_, events) = run(&f.engine(&[]), "retry-why", &[]).await;
let reason = events
.iter()
.find_map(|e| match &e.body {
EventBody::StepRetrying { reason, .. } => Some(reason.clone()),
_ => None,
})
.unwrap();
assert_eq!(reason, "exited 4: no match yet");
}

#[tokio::test]
async fn http_step_with_expect_and_outputs() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
Expand Down
Loading