use agshell::{Options, Session};
use proptest::prelude::*;
mod common;
use common::{agshell, bash_program, kinds, run_bash};
fn session() -> (tokio::runtime::Runtime, Session) {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("runtime");
let session = runtime
.block_on(Session::new(Options::default()))
.expect("session");
(runtime, session)
}
fn body() -> impl Strategy<Value = String> {
prop::sample::select(vec![
"$_.Name",
"$env:PATH",
"$(Get-Date)",
"`git rev-parse HEAD`",
"Get-Item .",
"Write-Output plain",
"Get-ChildItem -Path .",
"-NoProfile",
])
.prop_map(str::to_string)
}
fn powershell_script(quote: char, body: &str) -> String {
format!("pwsh -NoProfile -Command {quote}{body}{quote}")
}
#[test]
fn quoting_decides_the_scanner() {
proptest!(|(body in body(), double in any::<bool>())| {
let quote = if double { '"' } else { '\'' };
let source = powershell_script(quote, &body);
let flagged = agshell::scan::foreign_script_expansion(&source).is_some();
let expandable = body.contains('$') || body.contains('`');
prop_assert_eq!(
flagged,
double && expandable,
"scanner disagreed with the rule for {}",
source
);
});
}
#[test]
fn the_linter_agrees_with_the_scanner() {
let (_runtime, session) = session();
proptest!(|(body in body(), double in any::<bool>())| {
let quote = if double { '"' } else { '\'' };
let source = powershell_script(quote, &body);
let scanner = agshell::scan::foreign_script_expansion(&source).is_some();
let linter = kinds(&session.check(&source)).contains(&"expanded_before_interpreter");
prop_assert_eq!(linter, scanner, "layers disagreed for {}", source);
});
}
#[test]
fn the_linter_never_panics_on_arbitrary_text() {
let (_runtime, session) = session();
let text = proptest::string::string_regex("(?s).{0,200}").expect("regex strategy");
proptest!(|(source in text)| {
let first = session.check(&source);
let second = session.check(&source);
prop_assert_eq!(kinds(&first), kinds(&second), "not deterministic for {:?}", source);
for finding in &first.findings {
prop_assert!(!finding.detail.is_empty(), "{:?}: {:?}", source, finding);
if finding.kind == "expanded_before_interpreter" {
prop_assert!(finding.text.starts_with('"'), "{:?}: {:?}", source, finding);
prop_assert!(
finding.text.contains('$') || finding.text.contains('`'),
"{:?}: {:?}",
source,
finding
);
}
}
});
}
fn word() -> impl Strategy<Value = String> {
prop_oneof![
prop::sample::select(vec!["a", "abc", "$x", "${x}", "$((1+2))", "x=y"]).prop_map(str::to_string),
prop::sample::select(vec![r#"'a b'"#, r#"'$x'"#, r#"'bt `printf c`'"#, r#"'it'\''s'"#])
.prop_map(str::to_string),
prop::sample::select(vec![
r#""a b""#,
r#""$x""#,
r#""${x} tail""#,
r#""a $(printf b) c""#,
r#""bt `printf c`""#,
r#""esc \$x""#,
r#""q \"q\"""#,
])
.prop_map(str::to_string),
]
}
fn script() -> impl Strategy<Value = String> {
(word(), word(), word(), any::<bool>()).prop_map(|(a, b, c, tail)| {
let mut source = format!("x=7; printf '[%s][%s][%s]\\n' {a} {b} {c}; ");
source.push_str(if tail {
"true && printf 'and\\n'"
} else {
"false || printf 'or\\n'"
});
source
})
}
#[test]
fn generated_scripts_agree_with_bash() {
let Some(program) = bash_program() else {
eprintln!("no working `bash` on this machine: skipping the generated scripts");
return;
};
let config = ProptestConfig {
cases: 48,
..ProptestConfig::default()
};
proptest!(config, |(source in script())| {
let (expected_stdout, expected_code) = run_bash(&program, &source);
let (report, code) = agshell(&source);
prop_assert_eq!(
report["stdout"].as_str().unwrap_or(""),
expected_stdout.as_str(),
"printed differently: {}",
source
);
prop_assert_eq!(code, expected_code, "exited differently: {}", source);
});
}