Skip to content

Commit 3572474

Browse files
committed
feat(testkit): add ExecOutput::success and Sandbox::sh
Give `ExecOutput` the same `success()` check that `CliOutput` has, and record the command so the failure message names it. Add `Sandbox::sh` to run a shell script with `sh -c`. Suites hand-write the exit-code check and the `sh -c` wrapper after every `exec`; these let them drop that boilerplate. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent a2afbcd commit 3572474

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎tests/testkit/testkit/src/context.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,27 @@ pub struct Sandbox {
422422
/// The buffered output of an exec.
423423
#[derive(Debug)]
424424
pub struct ExecOutput {
425+
/// The command as run, for failure messages.
426+
pub command: String,
425427
pub exit_code: i32,
426428
pub stdout: String,
427429
pub stderr: String,
428430
}
429431

432+
impl ExecOutput {
433+
/// Fail unless the command exited 0.
434+
pub fn success(self) -> Result<Self> {
435+
crate::ensure!(
436+
self.exit_code == 0,
437+
"`{}` exited with {}; stderr: {}",
438+
self.command,
439+
self.exit_code,
440+
self.stderr.trim()
441+
);
442+
Ok(self)
443+
}
444+
}
445+
430446
impl Sandbox {
431447
pub fn name(&self) -> &str {
432448
&self.name
@@ -587,12 +603,18 @@ impl Sandbox {
587603
.await
588604
.context(format!("exec {command:?} in sandbox {}", self.name))?;
589605
Ok(ExecOutput {
606+
command: command.join(" "),
590607
exit_code: result.exit_code,
591608
stdout: String::from_utf8_lossy(&result.stdout).into_owned(),
592609
stderr: String::from_utf8_lossy(&result.stderr).into_owned(),
593610
})
594611
}
595612

613+
/// Run a shell script with `sh -c` and buffer its output.
614+
pub async fn sh(&self, script: &str) -> Result<ExecOutput> {
615+
self.exec(["sh", "-c", script]).await
616+
}
617+
596618
/// Delete the sandbox and wait until it is gone.
597619
pub async fn delete(&self) -> Result {
598620
self.client

0 commit comments

Comments
 (0)