diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/webgpu/tests/cts/vendor/src/process.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/webgpu/tests/cts/vendor/src/process.rs')
-rw-r--r-- | dom/webgpu/tests/cts/vendor/src/process.rs | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/dom/webgpu/tests/cts/vendor/src/process.rs b/dom/webgpu/tests/cts/vendor/src/process.rs new file mode 100644 index 0000000000..b36c3b953d --- /dev/null +++ b/dom/webgpu/tests/cts/vendor/src/process.rs @@ -0,0 +1,85 @@ +use std::{ + ffi::{OsStr, OsString}, + fmt::{self, Display}, + iter::once, + process::{Command, Output}, +}; + +use format::lazy_format; +use miette::{ensure, Context, IntoDiagnostic}; + +pub(crate) fn which(name: &'static str, desc: &str) -> miette::Result<OsString> { + let found = ::which::which(name) + .into_diagnostic() + .wrap_err(lazy_format!("failed to find `{name}` executable"))?; + log::debug!("using {desc} from {}", found.display()); + Ok(found.file_name().unwrap().to_owned()) +} + +pub(crate) struct EasyCommand { + inner: Command, +} + +impl EasyCommand { + pub(crate) fn new<C>(cmd: C, f: impl FnOnce(&mut Command) -> &mut Command) -> Self + where + C: AsRef<OsStr>, + { + let mut cmd = Command::new(cmd); + f(&mut cmd); + Self { inner: cmd } + } + + pub(crate) fn spawn(&mut self) -> miette::Result<()> { + log::debug!("spawning {self}…"); + let status = self + .inner + .spawn() + .into_diagnostic() + .wrap_err_with(|| format!("failed to spawn {self}"))? + .wait() + .into_diagnostic() + .wrap_err_with(|| format!("failed to wait for exit code from {self}"))?; + log::debug!("{self} returned {:?}", status.code()); + ensure!(status.success(), "{self} returned {:?}", status.code()); + Ok(()) + } + + fn just_stdout(&mut self) -> miette::Result<Vec<u8>> { + log::debug!("getting `stdout` output of {self}"); + let output = self + .inner + .output() + .into_diagnostic() + .wrap_err_with(|| format!("failed to execute `{self}`"))?; + let Output { + status, + stdout: _, + stderr, + } = &output; + log::debug!("{self} returned {:?}", status.code()); + ensure!( + status.success(), + "{self} returned {:?}; full output: {output:#?}", + status.code(), + ); + assert!(stderr.is_empty()); + Ok(output.stdout) + } + + pub(crate) fn just_stdout_utf8(&mut self) -> miette::Result<String> { + String::from_utf8(self.just_stdout()?) + .into_diagnostic() + .wrap_err_with(|| format!("output of {self} was not UTF-8 (!?)")) + } +} + +impl Display for EasyCommand { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let Self { inner } = self; + let prog = inner.get_program().to_string_lossy(); + let args = inner.get_args().map(|a| a.to_string_lossy()); + let shell_words = ::shell_words::join(once(prog).chain(args)); + write!(f, "`{shell_words}`") + } +} |