Files
sized/tests/cli.rs

100 lines
3.0 KiB
Rust

use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::fs::File;
use std::io::Write;
use std::process::Command;
use tempfile::TempDir;
#[test]
fn test_basic_cli_run() {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_sized"));
cmd.arg("--version")
.assert()
.success()
.stdout(predicate::str::contains("sized 1.0.0"));
}
#[test]
fn test_run_on_directory() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("test_file.txt");
File::create(&file_path)
.unwrap()
.write_all(b"Hello")
.unwrap();
let mut cmd = Command::new(env!("CARGO_BIN_EXE_sized"));
cmd.arg(temp_dir.path())
.assert()
.success()
.stdout(predicate::str::contains("test_file.txt")); // Basic check
}
#[test]
fn test_json_output() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("data.json");
File::create(&file_path).unwrap().write_all(b"{}").unwrap();
let mut cmd = Command::new(env!("CARGO_BIN_EXE_sized"));
cmd.arg(temp_dir.path())
.arg("--format")
.arg("json")
.assert()
.success()
.stdout(predicate::str::contains("\"entry_type\":\"File\""))
.stdout(predicate::str::contains("\"path\":"));
}
#[test]
fn missing_path_fails_without_a_success_report() {
let dir = TempDir::new().unwrap();
Command::new(env!("CARGO_BIN_EXE_sized"))
.arg(dir.path().join("missing"))
.args(["--format", "json"])
.assert()
.failure()
.stdout(predicate::str::is_empty())
.stderr(predicate::str::contains("cannot scan"));
}
#[cfg(unix)]
#[test]
fn partial_scan_has_nonzero_status_and_explicit_json_flag() {
use std::{fs, os::unix::fs::PermissionsExt};
let dir = TempDir::new().unwrap();
let blocked = dir.path().join("blocked");
fs::create_dir(&blocked).unwrap();
fs::set_permissions(&blocked, fs::Permissions::from_mode(0o000)).unwrap();
let result = std::panic::catch_unwind(|| {
assert!(
fs::read_dir(&blocked).is_err(),
"permission tests require an unprivileged user"
);
Command::new(env!("CARGO_BIN_EXE_sized"))
.arg(dir.path())
.args(["--format", "json"])
.assert()
.failure()
.stdout(predicate::str::contains("\"complete\":false"))
.stderr(predicate::str::contains("incomplete scan"));
});
fs::set_permissions(blocked, fs::Permissions::from_mode(0o700)).unwrap();
result.unwrap();
}
#[cfg(unix)]
#[test]
fn dangling_symlink_is_a_valid_scan_target() {
use std::os::unix::fs::symlink;
let dir = TempDir::new().unwrap();
let path = dir.path().join("dangling");
symlink(dir.path().join("missing"), &path).unwrap();
Command::new(env!("CARGO_BIN_EXE_sized"))
.arg(path)
.args(["--format", "json"])
.assert()
.success()
.stdout(predicate::str::contains("\"complete\":true"));
}