use fastshell::sdk::types::Config;
use fastshell::sdk::Fastshell;
use std::fs;
use std::sync::atomic::{AtomicUsize, Ordering};
static COUNTER: AtomicUsize = AtomicUsize::new(0);
fn setup() -> Fastshell {
let n = COUNTER.fetch_add(1, Ordering::SeqCst);
let dir = std::env::temp_dir().join(format!("fs_robust_{}_{}", std::process::id(), n));
let _ = fs::remove_dir_all(&dir);
let mut sdk = Fastshell::new();
sdk.init(Config {
sandbox_path: dir.to_string_lossy().to_string(),
python_enabled: false,
allow_subprocess: true,
network_ask_permission: false,
command_timeout_ms: 5_000,
..Default::default()
})
.unwrap();
sdk
}
const MALFORMED: &[&str] = &[
"",
" ",
"\n",
"\t",
"for",
"for x in",
"for x in a; do",
"for x in a; do echo",
"for x in a; do echo x",
"if",
"if true",
"if true; then",
"if true; then echo",
"elif",
"else",
"fi",
"while",
"while true; do",
"until",
"done",
"case",
"case x",
"case x in",
"case x in a)",
"esac",
"echo \"unterminated",
"echo 'unterminated",
"echo \"a\"'b",
"echo `unterminated",
"echo $(",
"echo $(",
"echo ${",
"echo ${x",
"echo $((1+",
"echo $(((",
"echo ))",
"echo $(())",
"echo ${x:-",
"echo ${x%%",
"|",
"||",
"&&",
";",
"echo |",
"| cat",
"echo >",
"echo >>",
"echo 2>&",
"echo > >",
"cat <",
"<<",
"echo | | cat",
"echo {a,",
"echo {a,b",
"echo }",
"echo {",
"echo {,}",
"echo [a-",
"sed 's/",
"sed -e",
"grep -E '(",
"grep -",
"cut -",
"sort -",
"head -n",
"tail -n",
"printf",
"printf %",
"test",
"[",
"[ ]",
"[ 1 -eq ]",
"exit",
"return",
"break",
"continue",
"cd",
"cd /nonexistent/xyz && echo ok",
"for a in 1; do for b in 2; do echo $a$b; done; done",
"echo $(echo $(echo $(echo deep)))",
"x=$((1+2)); echo $x",
"echo $((1/0))",
"echo $((1%0))",
"echo $((0/0))",
"echo $((-1/0))",
"echo $((2**0))",
"echo $(( ))",
"x=abc; echo ${x:1}",
"x=abc; echo ${x:1:2}",
"x=abc; echo ${x//a/X}",
"echo ${x:-d}",
"echo ${x:=d}",
"echo ${x%%b*}",
"printf '%d' abc",
"printf '%*d' -5 1",
"printf '%s'",
"echo {a..e}",
"echo {1..0}",
"echo {5..1}",
"head -c -1 a.txt",
"tail -c 0 a.txt",
"sed 's//x/' a.txt",
"grep '' a.txt",
"cut -d'' -f1 a.txt",
];
#[test]
fn malformed_inputs_do_not_panic() {
let s = setup();
for cmd in MALFORMED {
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| s.execute(cmd)));
assert!(r.is_ok(), "panic on input: {cmd:?}");
}
}