|
1 | 1 | use camino::Utf8Path;
|
| 2 | +use std::process::Output; |
2 | 3 |
|
3 | 4 | use color_eyre::eyre::{eyre, Context};
|
4 | 5 | use color_eyre::Result;
|
@@ -63,6 +64,58 @@ pub(crate) fn get_alternative_test_image() -> String {
|
63 | 64 | }
|
64 | 65 | }
|
65 | 66 |
|
| 67 | +/// Captured output from a command with decoded stdout/stderr strings |
| 68 | +pub(crate) struct CapturedOutput { |
| 69 | + pub output: Output, |
| 70 | + pub stdout: String, |
| 71 | + pub stderr: String, |
| 72 | +} |
| 73 | + |
| 74 | +impl CapturedOutput { |
| 75 | + /// Create from a raw Output |
| 76 | + pub fn new(output: Output) -> Self { |
| 77 | + let stdout = String::from_utf8_lossy(&output.stdout).into_owned(); |
| 78 | + let stderr = String::from_utf8_lossy(&output.stderr).into_owned(); |
| 79 | + Self { |
| 80 | + output, |
| 81 | + stdout, |
| 82 | + stderr, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// Assert that the command succeeded, printing debug info on failure |
| 87 | + pub fn assert_success(&self, context: &str) { |
| 88 | + assert!( |
| 89 | + self.output.status.success(), |
| 90 | + "{} failed: {}", |
| 91 | + context, |
| 92 | + self.stderr |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /// Get the exit code |
| 97 | + pub fn exit_code(&self) -> Option<i32> { |
| 98 | + self.output.status.code() |
| 99 | + } |
| 100 | + |
| 101 | + /// Check if the command succeeded |
| 102 | + pub fn success(&self) -> bool { |
| 103 | + self.output.status.success() |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/// Run a command, capturing output |
| 108 | +pub(crate) fn run_command(program: &str, args: &[&str]) -> std::io::Result<CapturedOutput> { |
| 109 | + let output = std::process::Command::new(program).args(args).output()?; |
| 110 | + Ok(CapturedOutput::new(output)) |
| 111 | +} |
| 112 | + |
| 113 | +/// Run the bcvk command, capturing output |
| 114 | +pub(crate) fn run_bcvk(args: &[&str]) -> std::io::Result<CapturedOutput> { |
| 115 | + let bck = get_bck_command().expect("Failed to get bcvk command"); |
| 116 | + run_command(&bck, args) |
| 117 | +} |
| 118 | + |
66 | 119 | fn test_images_list() -> Result<()> {
|
67 | 120 | println!("Running test: bcvk images list --json");
|
68 | 121 |
|
|
0 commit comments