jj/cli/tests/test_config_schema.rs
Jonas Greitemann 7bb8e17e88 tests: factor out utility function is_external_tool_installed
A pattern has emerged where a integration tests check for the
availability of an external tool (`git`, `taplo`, `gpg`, ...) and skip
the test (by simply passing it) when it is not available. To check this,
the program is run with the `--version` flag.

Some tests require that the program be available at least when running
in CI, by calling `ensure_running_outside_ci` conditionally on the
outcome. The decision is up to each test, though, the utility merely
returns a `bool`.
2025-04-24 15:48:08 +00:00

52 lines
1.6 KiB
Rust

use std::path::Path;
use std::process::Command;
use std::process::Output;
use std::process::Stdio;
use testutils::ensure_running_outside_ci;
use testutils::is_external_tool_installed;
fn taplo_check_config(file: &Path) -> datatest_stable::Result<Option<Output>> {
if !is_external_tool_installed("taplo") {
ensure_running_outside_ci("`taplo` must be in the PATH");
eprintln!("Skipping test because taplo is not installed on the system");
return Ok(None);
}
// Taplo requires an absolute URL to the schema :/
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Ok(Some(
Command::new("taplo")
.args([
"check",
"--schema",
&format!("file://{}/src/config-schema.json", root.display()),
])
.arg(file.as_os_str())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?
.wait_with_output()?,
))
}
pub(crate) fn taplo_check_config_valid(file: &Path) -> datatest_stable::Result<()> {
if let Some(taplo_res) = taplo_check_config(file)? {
if !taplo_res.status.success() {
eprintln!("Failed to validate {}:", file.display());
eprintln!("{}", String::from_utf8_lossy(&taplo_res.stderr));
return Err("Validation failed".into());
}
}
Ok(())
}
pub(crate) fn taplo_check_config_invalid(file: &Path) -> datatest_stable::Result<()> {
if let Some(taplo_res) = taplo_check_config(file)? {
if taplo_res.status.success() {
return Err("Validation unexpectedly passed".into());
}
}
Ok(())
}