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]
#[cfg(not(feature = "clipboard"))] fn test_error_paste_feature_disabled() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
fs::write(temp.path().join("a.txt"), "A")?;
dircat_cmd()
.arg("-p") .current_dir(temp.path())
.assert()
.failure()
.stderr(predicate::str::contains(
"Clipboard feature is not enabled, cannot use --paste.",
));
temp.close()?;
Ok(())
}
#[test]
fn test_error_non_utf8_content() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
let file_path = temp.path().join("invalid_utf8.bin");
fs::File::create(&file_path)?.write_all(&[0x48, 0x65, 0x6c, 0x6c, 0x80, 0x6f])?;
dircat_cmd()
.arg(file_path.to_str().unwrap())
.current_dir(temp.path())
.assert()
.failure()
.stderr(predicate::str::contains("Failed to read file content"))
.stderr(predicate::str::contains("invalid_utf8.bin"))
.stderr(predicate::str::contains(
"stream did not contain valid UTF-8",
));
temp.close()?;
Ok(())
}