#![allow(clippy::unwrap_used, clippy::expect_used)]
use std::fs;
use std::process::Command;
use tempfile::TempDir;
fn tokf() -> Command {
Command::new(env!("CARGO_BIN_EXE_tokf"))
}
fn write_filter(content: &str) -> (TempDir, std::path::PathBuf) {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("filter.toml");
fs::write(&path, content).unwrap();
(tmp, path)
}
fn write_fixture(content: &str) -> (TempDir, std::path::PathBuf) {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("fixture.txt");
fs::write(&path, content).unwrap();
(tmp, path)
}
#[test]
fn lua_filter_inline_source_replaces_output() {
let (_ftmp, filter) = write_filter(
r#"
command = "test"
[lua_script]
lang = "luau"
source = 'return "ok"'
"#,
);
let (_xtmp, fixture) = write_fixture("anything here");
let output = tokf()
.args([
"apply",
filter.to_str().unwrap(),
fixture.to_str().unwrap(),
"--exit-code",
"0",
])
.output()
.unwrap();
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "ok");
}
#[test]
fn lua_filter_nil_passthrough() {
let (_ftmp, filter) = write_filter(
r#"
command = "test"
[lua_script]
lang = "luau"
source = "return nil"
[on_success]
output = "branch output"
"#,
);
let (_xtmp, fixture) = write_fixture("ignored");
let output = tokf()
.args([
"apply",
filter.to_str().unwrap(),
fixture.to_str().unwrap(),
"--exit-code",
"0",
])
.output()
.unwrap();
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(
String::from_utf8_lossy(&output.stdout).trim(),
"branch output"
);
}
#[test]
fn lua_filter_exit_code_branching() {
let (_ftmp, filter) = write_filter(
r#"
command = "test"
[lua_script]
lang = "luau"
source = '''
if exit_code == 0 then
return "success path"
else
return "failure path"
end
'''
"#,
);
let (_xtmp, fixture) = write_fixture("some output");
let success = tokf()
.args([
"apply",
filter.to_str().unwrap(),
fixture.to_str().unwrap(),
"--exit-code",
"0",
])
.output()
.unwrap();
assert_eq!(
String::from_utf8_lossy(&success.stdout).trim(),
"success path"
);
let failure = tokf()
.args([
"apply",
filter.to_str().unwrap(),
fixture.to_str().unwrap(),
"--exit-code",
"1",
])
.output()
.unwrap();
assert_eq!(
String::from_utf8_lossy(&failure.stdout).trim(),
"failure path"
);
}
#[test]
fn lua_filter_args_accessible() {
let tmp = TempDir::new().unwrap();
let filters_dir = tmp.path().join(".tokf/filters");
fs::create_dir_all(&filters_dir).unwrap();
fs::write(
filters_dir.join("tokf-lua-args-test.toml"),
r#"
command = "tokf-lua-args-test"
run = "true"
[lua_script]
lang = "luau"
source = "return args[1] or 'no-args'"
"#,
)
.unwrap();
let output = tokf()
.current_dir(tmp.path())
.env("TOKF_SHOW_INDICATOR", "false")
.args(["run", "--no-cache", "tokf-lua-args-test", "hello"])
.output()
.unwrap();
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "hello");
}
#[test]
fn lua_filter_file_script() {
let script_tmp = TempDir::new().unwrap();
let script_path = script_tmp.path().join("filter.luau");
fs::write(&script_path, r#"return "from file: " .. output"#).unwrap();
let filter_content = format!(
r#"
command = "test"
[lua_script]
lang = "luau"
file = "{}"
"#,
script_path.display()
);
let (_ftmp, filter) = write_filter(&filter_content);
let (_xtmp, fixture) = write_fixture("hello");
let output = tokf()
.args([
"apply",
filter.to_str().unwrap(),
fixture.to_str().unwrap(),
"--exit-code",
"0",
])
.output()
.unwrap();
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(
String::from_utf8_lossy(&output.stdout).trim(),
"from file: hello"
);
}