use std::path::Path;
use std::process::Command;
use ctl_core::fixture::Suite;
use ctl_core::shell::Shell;
pub(crate) fn check(shell: Shell, script: &str, exclude: &[&str]) {
let suite = Suite::new("shell-script");
match shell {
Shell::Bash => {
let path = suite.root().file("script.bash", script);
run("bash", &["-n"], &path);
let excluded = format!("--exclude={}", exclude.join(","));
let mut args = vec!["--shell=bash", "--enable=all"];
if !exclude.is_empty() {
args.push(&excluded);
}
run("shellcheck", &args, &path);
}
Shell::Zsh => run(
"zsh",
&["-f", "-n"],
&suite.root().file("script.zsh", script),
),
Shell::Fish => run(
"fish",
&["--no-config", "-n"],
&suite.root().file("script.fish", script),
),
}
}
fn run(program: &str, args: &[&str], path: &Path) {
let output = Command::new(program)
.args(args)
.arg(path)
.output()
.unwrap_or_else(|error| panic!("start {program}, a tool mise.test.toml pins: {error}"));
assert!(
output.status.success(),
"{program} {args:?} rejected the script:\n{}{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
}