mod common;
use assert_cmd::prelude::*;
use common::dircat_cmd;
use predicates::prelude::*;
use std::fs;
use std::io::Write;
use tempfile::tempdir;
#[test]
fn test_error_invalid_input_path() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
dircat_cmd()
.arg("non_existent_path_hopefully")
.current_dir(temp.path())
.assert()
.failure()
.stderr(predicate::str::contains("Failed to resolve input path"));
temp.close()?;
Ok(())
}
#[test]
fn test_error_output_and_paste_conflict() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
fs::write(temp.path().join("a.txt"), "A")?;
dircat_cmd()
.arg("-o")
.arg("output.txt")
.arg("-p") .current_dir(temp.path())
.assert()
.failure()
.stderr(predicate::str::contains(
"Cannot use --output <FILE> (-o) and --paste (-p) simultaneously",
));
temp.close()?;
Ok(())
}
#[test]
fn test_error_invalid_max_size_format() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
fs::write(temp.path().join("a.txt"), "A")?;
dircat_cmd()
.arg("-m")
.arg("invalid_size") .current_dir(temp.path())
.assert()
.failure()
.stderr(predicate::str::contains("Invalid size format"));
temp.close()?;
Ok(())
}
#[test]
fn test_error_invalid_regex_pattern() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
fs::write(temp.path().join("a.txt"), "A")?;
dircat_cmd()
.arg("-r")
.arg("[invalid") .current_dir(temp.path())
.assert()
.failure()
.stderr(predicate::str::contains("Invalid path regex"));
temp.close()?;
Ok(())
}
#[test]
fn test_error_no_files_found() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
let sub = temp.path().join("sub");
fs::create_dir(&sub)?;
fs::write(sub.join(".hidden"), "content")?;
dircat_cmd()
.current_dir(temp.path()) .assert()
.success() .stdout("") .stderr(predicate::str::contains(
"dircat: No files found matching the specified criteria.",
));
temp.close()?;
Ok(())
}
#[test]
fn test_include_invalid_utf8_lossy() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
let file_path = temp.path().join("invalid_utf8.bin");
let invalid_bytes = &[0x48, 0x65, 0x6c, 0x6c, 0x80, 0x6f]; fs::File::create(&file_path)?.write_all(invalid_bytes)?;
let expected_lossy_content = "Hell\u{FFFD}o";
dircat_cmd()
.arg(file_path.to_str().unwrap())
.arg("--include-binary") .current_dir(temp.path())
.assert()
.success() .stdout(predicate::str::contains("## File: invalid_utf8.bin")) .stdout(predicate::str::contains(expected_lossy_content));
temp.close()?;
Ok(())
}