use assert_cmd::Command;
#[test]
fn test_main_help_output() {
Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg("--help")
.assert()
.success();
}
#[test]
fn test_main_version_output() {
Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg("--version")
.assert()
.success();
}
#[test]
fn test_main_with_directory_input() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.assert();
cmd.success();
}
#[test]
fn test_main_with_file_input() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
let file_path = temp_dir.path().join("test.txt");
fs::write(&file_path, "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(file_path)
.assert();
cmd.success();
}
#[test]
fn test_main_with_json_output() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--json")
.assert();
cmd.success();
}
#[test]
fn test_main_with_tree_header() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--tree-header")
.assert();
cmd.success();
}
#[test]
fn test_main_with_line_numbers() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "line1\nline2").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--line-numbers")
.assert();
cmd.success();
}
#[test]
fn test_main_with_yaml_boolean_config() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "line1\nline2").unwrap();
fs::write(
temp_dir.path().join("yek.yaml"),
"line_numbers: true\ntree_header: true\n",
)
.unwrap();
let output_name = temp_dir.path().join("output.txt");
Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.current_dir(temp_dir.path())
.arg(".")
.arg("--output-name")
.arg(&output_name)
.env("FORCE_TTY", "1")
.assert()
.success();
let output = fs::read_to_string(output_name).unwrap();
assert!(
output.contains("Directory structure:"),
"expected tree_header from YAML config, got:\n{}",
output
);
assert!(
output.contains(" 1 | line1") && output.contains(" 2 | line2"),
"expected line_numbers from YAML config, got:\n{}",
output
);
}
#[test]
fn test_main_with_yaml_json_boolean_config() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
fs::write(temp_dir.path().join("yek.yaml"), "json: true\n").unwrap();
let output = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.current_dir(temp_dir.path())
.arg(".")
.output()
.expect("Failed to execute command");
assert!(output.status.success());
let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8");
assert!(
stdout.trim_start().starts_with('[') && stdout.contains("test.txt"),
"expected JSON output from YAML config, got:\n{}",
stdout
);
}
#[test]
fn test_main_with_yaml_tree_only_boolean_config() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
fs::write(temp_dir.path().join("yek.yaml"), "tree-only: true\n").unwrap();
let output = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.current_dir(temp_dir.path())
.arg(".")
.output()
.expect("Failed to execute command");
assert!(output.status.success());
let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8");
assert!(
stdout.contains("└── test.txt") && !stdout.contains(">>>>"),
"expected tree-only output from YAML config, got:\n{}",
stdout
);
}
#[test]
fn test_main_with_yaml_debug_boolean_config() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
fs::write(temp_dir.path().join("yek.yaml"), "debug: true\n").unwrap();
Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.current_dir(temp_dir.path())
.arg(".")
.assert()
.success();
}
#[test]
fn test_main_with_output_name() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let output_name = temp_dir.path().join("output.txt");
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--output-name")
.arg(&output_name)
.assert();
cmd.success();
assert!(output_name.exists());
}
#[test]
fn test_main_with_debug_flag() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--debug")
.assert();
cmd.success();
}
#[test]
fn test_main_non_streaming_mode() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--output-dir")
.arg(temp_dir.path())
.assert();
cmd.success();
}
#[test]
fn test_main_with_token_mode() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--tokens")
.arg("1000")
.assert();
cmd.success();
}
#[test]
fn test_main_with_force_tty() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.env("FORCE_TTY", "1")
.assert();
cmd.success();
}
#[test]
fn test_main_with_invalid_output_template() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--output-template")
.arg("INVALID_TEMPLATE")
.assert();
cmd.failure();
}
#[test]
fn test_main_with_zero_max_size() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--max-size")
.arg("0")
.assert();
cmd.failure();
}
#[test]
fn test_main_with_invalid_ignore_pattern() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--ignore-patterns")
.arg("[invalid")
.assert();
cmd.failure();
}
#[test]
fn test_main_with_invalid_priority_rule() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--priority-rules")
.arg("*.rs:1001") .assert();
cmd.failure();
}
#[test]
fn test_main_streaming_mode_with_debug() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--debug")
.arg("--output-name")
.arg("output.txt")
.arg("--no-config") .assert();
cmd.success();
assert!(std::path::Path::new("output.txt").exists());
std::fs::remove_file("output.txt").ok();
}
#[test]
fn test_main_checksum_error_handling() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::create_dir(temp_dir.path().join("subdir")).unwrap();
fs::write(temp_dir.path().join("subdir").join("file.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--output-dir")
.arg(temp_dir.path().join("output"))
.assert();
cmd.success();
}
#[test]
fn test_main_file_write_failure_recovery() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let output_name = "a".repeat(255) + ".txt";
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--output-name")
.arg(&output_name)
.assert();
let _ = cmd.get_output();
}
#[test]
fn test_main_force_tty_environment() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--output-dir")
.arg(temp_dir.path())
.env("FORCE_TTY", "1")
.assert();
cmd.success();
}
#[test]
fn test_main_with_missing_output_dir_fallback() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--output-dir")
.arg("/nonexistent/deeply/nested/path/that/cannot/be/created")
.assert();
cmd.success();
}
#[test]
fn test_output_dir_and_output_name_combination() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let output_dir = temp_dir.path().join("output");
fs::create_dir_all(&output_dir).unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--output-dir")
.arg(&output_dir)
.arg("--output-name")
.arg("custom-output.txt")
.assert();
cmd.success();
let expected_file = output_dir.join("custom-output.txt");
assert!(
expected_file.exists(),
"Output file should be created at output_dir/output_name"
);
}
#[test]
fn test_output_name_only_no_output_dir() {
use std::fs;
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
let cmd = Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg(temp_dir.path())
.arg("--output-name")
.arg("standalone-output.txt")
.assert();
cmd.success();
}
#[test]
fn test_main_help_includes_update_flag() {
use predicates::prelude::*;
Command::cargo_bin("yek")
.expect("Binary 'yek' not found")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("--update"))
.stdout(predicate::str::contains("Update yek to the latest version"));
}