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_only_overrides_gitignore() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
fs::write(temp.path().join(".gitignore"), "*.log")?;
fs::write(temp.path().join("important.log"), "IMPORTANT LOG")?;
fs::write(temp.path().join("main.rs"), "fn main() {}")?;
dircat_cmd()
.arg("-O") .arg("*.log")
.current_dir(temp.path())
.assert()
.success()
.stdout(predicate::str::contains("## File: important.log"))
.stdout(predicate::str::contains("IMPORTANT LOG"))
.stdout(predicate::str::contains("## File: main.rs").not());
Ok(())
}
#[test]
fn test_last_with_no_gitignore_flag() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
fs::write(temp.path().join(".gitignore"), "*.log")?;
fs::write(temp.path().join("app.log"), "LOG")?;
fs::write(temp.path().join("main.rs"), "fn main() {}")?;
dircat_cmd()
.arg("-t") .arg("-z") .arg("*.log")
.current_dir(temp.path())
.assert()
.success()
.stdout(predicate::function(|output: &str| {
let pos_main = output.find("## File: main.rs").unwrap();
let pos_log = output.find("## File: app.log").unwrap();
pos_main < pos_log
}));
Ok(())
}
#[test]
fn test_last_with_invalid_glob_pattern() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
fs::write(temp.path().join("a.txt"), "A")?;
fs::write(temp.path().join("b.rs"), "B")?;
fs::write(temp.path().join("c.md"), "C")?;
dircat_cmd()
.arg("-z")
.arg("[invalid-glob") .arg("-z")
.arg("*.md") .current_dir(temp.path())
.assert()
.success()
.stdout(predicate::function(|output: &str| {
let pos_a = output.find("## File: a.txt").unwrap();
let pos_b = output.find("## File: b.rs").unwrap();
let pos_c = output.find("## File: c.md").unwrap();
pos_a < pos_c && pos_b < pos_c
}));
Ok(())
}
#[test]
fn test_last_with_filename_containing_spaces() -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
fs::write(temp.path().join("a.txt"), "A")?;
fs::write(temp.path().join("file with spaces.md"), "SPACES")?;
dircat_cmd()
.arg("-z")
.arg("file with spaces.md")
.current_dir(temp.path())
.assert()
.success()
.stdout(predicate::function(|output: &str| {
let pos_a = output.find("## File: a.txt").unwrap();
let pos_spaces = output.find("## File: file with spaces.md").unwrap();
pos_a < pos_spaces
}));
Ok(())
}
#[test]
fn test_last_overrides_root_gitignore_and_includes_all_matches(
) -> Result<(), Box<dyn std::error::Error>> {
let temp = tempdir()?;
let sub = temp.path().join("sub");
fs::create_dir(&sub)?;
fs::write(temp.path().join(".gitignore"), "*.log")?;
let mut sub_gitignore = fs::File::create(sub.join(".gitignore"))?;
writeln!(sub_gitignore, "!important.log")?;
drop(sub_gitignore);
fs::write(temp.path().join("a.log"), "A LOG")?;
fs::write(sub.join("b.log"), "B LOG")?;
fs::write(sub.join("important.log"), "IMPORTANT LOG")?;
fs::write(temp.path().join("main.rs"), "fn main() {}")?;
dircat_cmd()
.arg("-z")
.arg("*.log")
.current_dir(temp.path())
.assert()
.success()
.stdout(predicate::str::contains("## File: main.rs"))
.stdout(predicate::str::contains("## File: a.log"))
.stdout(predicate::str::contains("## File: sub/b.log"))
.stdout(predicate::str::contains("## File: sub/important.log"))
.stdout(predicate::function(|output: &str| {
let pos_main = output.find("## File: main.rs").unwrap();
let pos_a_log = output.find("## File: a.log").unwrap();
let pos_b_log = output.find("## File: sub/b.log").unwrap();
let pos_imp_log = output.find("## File: sub/important.log").unwrap();
pos_main < pos_a_log && pos_main < pos_b_log && pos_main < pos_imp_log
}));
Ok(())
}