From 669bde7d6ce3534c74db0558b7d6c06050baba9e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 8 Jul 2024 15:49:52 -0500 Subject: [PATCH] test: Don't fail if troff is missing --- tests/testsuite/demo.rs | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/testsuite/demo.rs b/tests/testsuite/demo.rs index 00b2796..6ab53d7 100644 --- a/tests/testsuite/demo.rs +++ b/tests/testsuite/demo.rs @@ -5,6 +5,10 @@ use pretty_assertions::assert_eq; fn demo() { use roff::*; + if !has_command("troff") { + return; + } + let page = Roff::new() .control("TH", ["CORRUPT", "1"]) .control("SH", ["NAME"]) @@ -63,3 +67,52 @@ fn roff_to_ascii(input: &str) -> String { .read() .unwrap() } + +pub(crate) fn has_command(command: &str) -> bool { + let output = match std::process::Command::new(command) + .arg("--version") + .output() + { + Ok(output) => output, + Err(e) => { + // CI is expected to support all of the commands + if is_ci() && cfg!(target_os = "linux") { + panic!( + "expected command `{}` to be somewhere in PATH: {}", + command, e + ); + } + return false; + } + }; + if !output.status.success() { + panic!( + "expected command `{}` to be runnable, got error {}:\n\ + stderr:{}\n\ + stdout:{}\n", + command, + output.status, + String::from_utf8_lossy(&output.stderr), + String::from_utf8_lossy(&output.stdout) + ); + } + let stdout = String::from_utf8_lossy(&output.stdout); + println!( + "$ {command} --version +{}", + stdout + ); + if cfg!(target_os = "macos") && stdout.starts_with("GNU bash, version 3") { + return false; + } + + true +} + +/// Whether or not this running in a Continuous Integration environment. +fn is_ci() -> bool { + // Consider using `tracked_env` instead of option_env! when it is stabilized. + // `tracked_env` will handle changes, but not require rebuilding the macro + // itself like option_env does. + option_env!("CI").is_some() || option_env!("TF_BUILD").is_some() +}