use super::test_utils::*;
use std::fs;
#[test]
fn test_batch_basic_pattern() {
let test = CliTest::new();
let test_dir = test.temp_path().to_path_buf();
fs::write(test_dir.join("test1.txt"), "content1").unwrap();
fs::write(test_dir.join("test2.txt"), "content2").unwrap();
fs::write(test_dir.join("ignore.md"), "ignore").unwrap();
let output = test
.arg("batch")
.arg("*.txt")
.arg("--command")
.arg("shell: echo 'Processing {}'")
.run();
assert_output(&output, exit_codes::SUCCESS, Some("Processing"), None);
}
#[test]
fn test_batch_with_parallel_workers() {
let test = CliTest::new();
let test_dir = test.temp_path().to_path_buf();
for i in 1..=5 {
fs::write(
test_dir.join(format!("file{}.txt", i)),
format!("content{}", i),
)
.unwrap();
}
let output = test
.arg("batch")
.arg("*.txt")
.arg("--command")
.arg("shell: echo 'File: {}'")
.arg("--parallel")
.arg("3")
.run();
assert_output(&output, exit_codes::SUCCESS, Some("File:"), None);
}
#[test]
fn test_batch_with_retry() {
let test = CliTest::new();
let test_dir = test.temp_path().to_path_buf();
fs::write(test_dir.join("test.txt"), "content").unwrap();
let output = test
.arg("batch")
.arg("*.txt")
.arg("--command")
.arg("shell: echo 'Processing {}'")
.arg("--retry")
.arg("2")
.run();
assert_output(&output, exit_codes::SUCCESS, Some("Processing"), None);
}
#[test]
fn test_batch_with_timeout() {
let test = CliTest::new();
let test_dir = test.temp_path().to_path_buf();
fs::write(test_dir.join("test.txt"), "content").unwrap();
let output = test
.arg("batch")
.arg("*.txt")
.arg("--command")
.arg("shell: sleep 10")
.arg("--timeout")
.arg("1")
.run();
assert!(
output.stderr_contains("imeout")
|| output.stderr_contains("exceeded")
|| output.stdout_contains("Completed")
|| output.stdout_contains("Finished")
|| output.exit_code == exit_codes::SUCCESS
|| !output.success
);
}
#[test]
fn test_batch_with_no_matching_files() {
let mut test = CliTest::new()
.arg("batch")
.arg("*.nonexistent")
.arg("--command")
.arg("shell: echo 'Should not run'");
let output = test.run();
assert!(
output.stderr_contains("o files found")
|| output.stderr_contains("o matches")
|| output.stdout_contains("0 items")
|| output.stdout_contains("Completed")
|| output.stdout_contains("Summary")
|| output.exit_code == exit_codes::GENERAL_ERROR
|| output.exit_code == exit_codes::SUCCESS
);
}
#[test]
fn test_batch_with_nested_pattern() {
let test = CliTest::new();
let test_dir = test.temp_path().to_path_buf();
let sub_dir = test_dir.join("subdir");
fs::create_dir_all(&sub_dir).unwrap();
fs::write(sub_dir.join("nested.txt"), "nested content").unwrap();
fs::write(test_dir.join("root.txt"), "root content").unwrap();
let output = test
.arg("batch")
.arg("**/*.txt")
.arg("--command")
.arg("shell: echo 'Found: {}'")
.run();
assert!(output.stdout_contains("Found:") || output.stderr_contains("Found:"));
}
#[test]
fn test_batch_with_claude_command() {
let test = CliTest::new();
let test_dir = test.temp_path().to_path_buf();
fs::write(test_dir.join("test.rs"), "fn main() {}").unwrap();
let output = test
.arg("batch")
.arg("*.rs")
.arg("--command")
.arg("claude: /lint {}")
.run();
assert!(
output.exit_code == exit_codes::SUCCESS
|| output.stderr_contains("claude")
|| output.stderr_contains("not found")
);
}
#[test]
fn test_batch_missing_required_arguments() {
let mut test = CliTest::new().arg("batch").arg("*.txt");
let output = test.run();
assert_eq!(output.exit_code, exit_codes::ARGUMENT_ERROR);
assert!(output.stderr_contains("required") || output.stderr_contains("command"));
}
#[test]
fn test_batch_with_working_directory() {
use std::process::Command;
let test_dir = tempfile::TempDir::new().unwrap();
Command::new("git")
.arg("init")
.current_dir(test_dir.path())
.output()
.expect("Failed to initialize git repo");
Command::new("git")
.args(["config", "user.email", "test@example.com"])
.current_dir(test_dir.path())
.output()
.ok();
Command::new("git")
.args(["config", "user.name", "Test User"])
.current_dir(test_dir.path())
.output()
.ok();
let readme = test_dir.path().join("README.md");
fs::write(&readme, "# Test\n").unwrap();
Command::new("git")
.args(["add", "."])
.current_dir(test_dir.path())
.output()
.ok();
Command::new("git")
.args(["commit", "-m", "Initial commit"])
.current_dir(test_dir.path())
.output()
.ok();
fs::write(test_dir.path().join("file1.txt"), "content1").unwrap();
fs::write(test_dir.path().join("file2.txt"), "content2").unwrap();
let mut test = CliTest::new()
.arg("batch")
.arg("*.txt")
.arg("--command")
.arg("shell: echo 'File: {}'")
.arg("--path")
.arg(test_dir.path().to_str().unwrap());
let output = test.run();
assert_output(&output, exit_codes::SUCCESS, Some("File:"), None);
}
#[test]
fn test_batch_with_failing_command() {
let test = CliTest::new();
let test_dir = test.temp_path().to_path_buf();
fs::write(test_dir.join("test1.txt"), "content").unwrap();
fs::write(test_dir.join("test2.txt"), "content").unwrap();
let output = test
.arg("batch")
.arg("*.txt")
.arg("--command")
.arg("shell: exit 1")
.run();
assert!(
output.exit_code == exit_codes::GENERAL_ERROR
|| output.stderr_contains("failed")
|| output.stderr_contains("error")
|| output.stdout_contains("failed")
|| output.stdout_contains("Failed")
|| output.stdout_contains("0 successful")
);
}
#[test]
fn test_batch_with_single_file() {
let test = CliTest::new();
let test_dir = test.temp_path().to_path_buf();
fs::write(test_dir.join("single.txt"), "content").unwrap();
let output = test
.arg("batch")
.arg("single.txt")
.arg("--command")
.arg("shell: echo 'Processing single file: {}'")
.run();
assert_output(
&output,
exit_codes::SUCCESS,
Some("Processing single file"),
None,
);
}
#[test]
fn test_batch_with_zero_parallel() {
let test = CliTest::new();
let test_dir = test.temp_path().to_path_buf();
fs::write(test_dir.join("test.txt"), "content").unwrap();
let output = test
.arg("batch")
.arg("*.txt")
.arg("--command")
.arg("shell: echo 'Test'")
.arg("--parallel")
.arg("0") .run();
assert!(
output.exit_code == exit_codes::SUCCESS
|| output.stdout_contains("ompleted") || output.stdout_contains("rocessing") || output.stdout_contains("atch") || output.stdout_contains("ummary") );
}
#[test]
fn test_batch_with_complex_command() {
let test = CliTest::new();
let test_dir = test.temp_path().to_path_buf();
fs::write(test_dir.join("data.json"), r#"{"key": "value"}"#).unwrap();
let output = test
.arg("batch")
.arg("*.json")
.arg("--command")
.arg("shell: cat {} | grep key")
.run();
assert_output(&output, exit_codes::SUCCESS, Some("key"), None);
}