#[path = "support/os.rs"]
mod os;
#[path = "support/process.rs"]
mod process;
fn call(root: &std::path::Path, args: &[&str]) -> Output {
os::call(env!("CARGO_BIN_EXE_artifactize-tools"), root, args)
}
use std::{fs, process::Output};
fn text(output: Output) -> String {
assert!(os::succeeded(&output.status), "{output:?}");
artifactize_tools::text::output(&output.stdout).into_owned()
}
#[test]
fn subcommands_share_scoped_files_and_exit_codes() {
let root = os::tempdir();
fs::write(
root.path().join("notes.md"),
"# First\nalpha\n## Child\nchild\n# Next\nomega\n",
)
.unwrap();
assert_eq!(
text(call(root.path(), &["read", "notes.md"])),
"# First\nalpha\n## Child\nchild\n# Next\nomega\n"
);
assert_eq!(
text(call(root.path(), &["section", "notes.md", "First"])),
"# First\nalpha\n## Child\nchild\n"
);
for (args, expected) in [
(
vec!["list", "."],
serde_json::json!({
"artifactId":"cwd", "path":"",
"entries":[{"kind":"file", "name":"notes.md", "path":"notes.md"}],
"totalEntries":1, "truncated":false, "nextOffset":null,
}),
),
(
vec!["glob", "*.md"],
serde_json::json!({"path":"", "files":["notes.md"], "truncated":false}),
),
(
vec!["grep", "alpha"],
serde_json::json!({
"path":"", "matches":[{"path":"notes.md", "line":2, "text":"alpha"}],
"truncated":false,
}),
),
] {
let output = text(call(root.path(), &args));
assert_eq!(
serde_json::from_str::<serde_json::Value>(&output).unwrap(),
expected,
"{args:?}"
);
}
let missing = call(root.path(), &["section", "notes.md", "Absent"]);
assert_eq!(os::exit_code(&missing), 1);
let error = artifactize_tools::text::output(&missing.stderr).into_owned();
assert!(error.contains("First\nChild\nNext"), "{error}");
for sub in ["read", "list", "glob", "grep", "section"] {
let mut args = match sub {
"glob" => vec![sub, "*", "../outside"],
"grep" => vec![sub, "alpha", "../outside"],
"section" => vec![sub, "../outside", "First"],
_ => vec![sub, "../outside"],
};
assert_eq!(os::exit_code(&call(root.path(), &args)), 1, "{sub}");
let index = if matches!(sub, "glob" | "grep") { 2 } else { 1 };
let absolute = root.path().join("notes.md");
args[index] = absolute.to_str().unwrap();
assert_eq!(os::exit_code(&call(root.path(), &args)), 1, "{sub}");
}
let help = text(call(root.path(), &["--help"]));
assert!(help.contains("symlinks are refused"));
assert_eq!(os::exit_code(&call(root.path(), &["unknown"])), 2);
}
#[test]
fn searches_validate_cli_patterns_and_keep_the_declared_flags() {
let root = os::tempdir();
fs::write(root.path().join("notes.md"), "ALPHA\nbeta\n").unwrap();
for args in [["grep", "", "."], ["glob", "", "."], ["grep", "[", "."]] {
let output = call(root.path(), &args);
assert_eq!(os::exit_code(&output), 1, "{args:?}");
assert!(!output.stderr.is_empty(), "{args:?}");
}
let output = text(call(
root.path(),
&["grep", "alpha", "--case-insensitive", "--glob", "*.md"],
));
let result: serde_json::Value = serde_json::from_str(&output).unwrap();
assert_eq!(
result["matches"],
serde_json::json!([{"path":"notes.md","line":1,"text":"ALPHA"}])
);
}
#[test]
fn symlink_inputs_are_not_followed() {
let root = os::tempdir();
fs::write(root.path().join("real.md"), "# First\nbody\n").unwrap();
let link = root.path().join("link.md");
os::symlink_file(root.path().join("real.md"), &link);
for args in [vec!["read", "link.md"], vec!["section", "link.md", "First"]] {
let output = call(root.path(), &args);
assert_eq!(os::exit_code(&output), 1);
}
}
#[test]
fn help_resolves_path_and_enforces_output_bounds() {
let root = os::tempdir();
os::help_program(root.path());
let run = |arg| {
os::help(
env!("CARGO_BIN_EXE_artifactize-tools"),
root.path(),
"stub",
&[arg],
)
};
assert_eq!(text(run("sub")), "stub help: sub|--help\n");
let large = run("large");
assert_eq!(os::exit_code(&large), 1);
assert!(artifactize_tools::text::output(&large.stderr).contains("64 KiB"));
}
#[tokio::test]
async fn standalone_refuses_an_output_limit_without_room_for_the_probe() {
use artifactize_tools::launch::{Launch, LaunchError, Launcher, Standalone};
let root = os::tempdir();
let launch = Launch {
program: root.path().join("not-started"),
args: Vec::new(),
cwd: root.path().to_owned(),
timeout: std::time::Duration::ZERO,
output_limit: usize::MAX,
};
let error = Standalone
.run(launch, &tokio_util::sync::CancellationToken::new())
.await
.unwrap_err();
assert!(matches!(error, LaunchError::Failed(message) if message.contains("Output limit")));
}
#[tokio::test]
async fn help_timeout_ends_the_program_and_the_children_it_starts_at_once() {
let root = os::tempdir();
let result = process::timeout_tree(root.path()).await;
assert!(
matches!(
result,
Err(artifactize_tools::launch::LaunchError::TimedOut)
),
"{result:?}"
);
}
#[tokio::test]
async fn normal_leader_exit_cleans_up_a_descendant_that_inherited_output() {
let root = os::tempdir();
let finished = process::completed_tree(root.path()).await.unwrap();
assert!(finished.status.success(), "{finished:?}");
}
#[cfg(unix)]
#[tokio::test]
async fn a_terminal_hang_up_stops_the_help_and_open_launches() {
use std::process::Stdio;
use std::time::Duration;
use tokio::{io::AsyncReadExt, net::TcpListener};
const WATCHDOG: Duration = Duration::from_secs(30);
const FIXTURE: &str = r#"
use std::{io::{Read, Write}, net::TcpStream};
fn main() {
let mut connection = TcpStream::connect(std::env::var("TOOL_FIXTURE_ADDRESS").unwrap()).unwrap();
connection.write_all(b"ready").unwrap();
// Independently exit if the test drops its connection on failure.
let _ = connection.read(&mut [0]);
}
"#;
let root = os::tempdir();
os::compile(root.path(), "stub", FIXTURE);
os::compile(root.path(), os::opener_program(), FIXTURE);
let file = os::opener_input(root.path());
for (args, diagnostic) in [
(vec!["help", "stub", "sub"], "cancelled"),
(
vec!["open", file.file_name().unwrap().to_str().unwrap()],
"interrupted",
),
] {
let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0))
.await
.unwrap();
let mut command =
tokio::process::Command::from(os::tools(env!("CARGO_BIN_EXE_artifactize-tools")));
let cli = command
.current_dir(root.path())
.env("PATH", root.path())
.env(
"TOOL_FIXTURE_ADDRESS",
listener.local_addr().unwrap().to_string(),
)
.args(&args)
.stdout(Stdio::null())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()
.unwrap();
let mut fixture = tokio::time::timeout(WATCHDOG, async {
let (mut fixture, _) = listener.accept().await.unwrap();
let mut ready = [0; 5];
fixture.read_exact(&mut ready).await.unwrap();
assert_eq!(&ready, b"ready");
fixture
})
.await
.expect("tool started before the interrupt");
os::hang_up(cli.id().unwrap()).unwrap();
let output = tokio::time::timeout(WATCHDOG, cli.wait_with_output())
.await
.unwrap()
.unwrap();
assert_eq!(os::exit_code(&output), 1);
assert!(
artifactize_tools::text::output(&output.stderr)
.to_lowercase()
.contains(diagnostic),
"{args:?}: {output:?}"
);
assert_eq!(
tokio::time::timeout(WATCHDOG, fixture.read(&mut [0]))
.await
.unwrap()
.unwrap(),
0,
"opener or help program still owns its connection after the CLI returned"
);
}
}
#[tokio::test]
async fn help_tree_fixture_cleans_up_when_its_caller_unwinds() {
let root = os::tempdir();
process::unwind_tree(root.path()).await;
}
#[cfg(unix)]
#[test]
fn open_passes_one_absolute_target_to_the_recording_opener() {
let root = os::tempdir();
os::recording_program(root.path(), os::opener_program());
let file = os::opener_input(root.path());
let record = root.path().join("record");
let output = os::open_recording(
env!("CARGO_BIN_EXE_artifactize-tools"),
root.path(),
file.file_name().unwrap().to_str().unwrap(),
);
assert!(os::succeeded(&output.status), "{output:?}");
assert_eq!(
fs::read_to_string(record).unwrap(),
format!("1\n{}\n", file.display())
);
}
#[test]
fn version_matches_the_shared_workspace_version() {
let output = os::tools_output(env!("CARGO_BIN_EXE_artifactize-tools"), &["--version"]);
assert!(os::succeeded(&output.status));
assert_eq!(
artifactize_tools::text::output(&output.stdout),
format!("artifactize-tools {}\n", env!("CARGO_PKG_VERSION"))
);
assert!(output.stderr.is_empty());
}
#[cfg(unix)]
#[test]
fn list_and_glob_omit_native_names_that_cannot_be_reopened_logically() {
let root = os::tempdir();
os::write_unrepresentable_input(root.path());
fs::write(root.path().join("visible.md"), "visible").unwrap();
let list: serde_json::Value =
serde_json::from_str(&text(call(root.path(), &["list"]))).unwrap();
assert_eq!(
list["entries"],
serde_json::json!([{"kind":"file", "name":"visible.md", "path":"visible.md"}])
);
let glob: serde_json::Value =
serde_json::from_str(&text(call(root.path(), &["glob", "*.md"]))).unwrap();
assert_eq!(glob["files"], serde_json::json!(["visible.md"]));
}