Files
bat/tests/tester/mod.rs
T

102 lines
2.9 KiB
Rust
Raw Normal View History

2026-04-26 22:00:50 +02:00
use gix::actor::SignatureRef;
use gix::bstr::BString;
use gix::bstr::ByteSlice;
use gix::date::time::Format;
use gix::date::Time;
use gix::objs::tree;
2018-05-12 16:48:45 +02:00
use std::env;
use std::fs::{self, File};
2018-08-18 20:44:25 +02:00
use std::io::Read;
2026-04-26 22:00:50 +02:00
use std::path::PathBuf;
2018-05-12 16:48:45 +02:00
use std::process::Command;
use tempfile::TempDir;
2018-05-12 16:48:45 +02:00
pub struct BatTester {
/// Temporary working directory
temp_dir: TempDir,
/// Path to the *bat* executable
2018-05-12 16:48:45 +02:00
exe: PathBuf,
}
impl BatTester {
2018-10-19 21:10:32 +02:00
pub fn test_snapshot(&self, name: &str, style: &str) {
2018-05-12 16:48:45 +02:00
let output = Command::new(&self.exe)
.current_dir(self.temp_dir.path())
2024-02-21 02:39:22 +08:00
.args([
2018-08-23 23:13:24 +02:00
"sample.rs",
2018-10-11 21:40:14 +02:00
"--no-config",
2018-09-12 20:32:49 +02:00
"--paging=never",
"--color=never",
2018-08-23 23:13:24 +02:00
"--decorations=always",
"--terminal-width=80",
2024-02-24 22:36:14 +01:00
&format!("--style={style}"),
2018-09-26 19:16:03 +02:00
])
.output()
2018-05-12 16:48:45 +02:00
.expect("bat failed");
2018-05-12 16:48:45 +02:00
// have to do the replace because the filename in the header changes based on the current working directory
let actual = String::from_utf8_lossy(&output.stdout)
.as_ref()
.replace("tests/snapshots/", "");
let mut expected = String::new();
2024-02-24 22:36:14 +01:00
let mut file = File::open(format!("tests/snapshots/output/{name}.snapshot.txt"))
2018-05-12 16:48:45 +02:00
.expect("snapshot file missing");
file.read_to_string(&mut expected)
.expect("could not read snapshot file");
assert_eq!(expected, actual);
}
}
impl Default for BatTester {
fn default() -> Self {
let temp_dir = create_sample_directory().expect("sample directory");
2026-01-08 09:42:42 -05:00
let exe = assert_cmd::cargo::cargo_bin!("bat").to_path_buf();
BatTester { temp_dir, exe }
}
}
2026-04-26 22:00:50 +02:00
fn create_sample_directory() -> Result<TempDir, Box<dyn std::error::Error>> {
// Create temp directory and initialize repository
let temp_dir = TempDir::new().expect("Temp directory");
2026-04-26 22:00:50 +02:00
let repo = gix::init(&temp_dir)?;
let mut tree = gix::objs::Tree::empty();
2018-05-12 16:48:45 +02:00
2026-04-26 22:00:50 +02:00
// Create sample.rs from snapshot file
let blob_id = repo.write_blob_stream(File::open("tests/snapshots/sample.rs")?)?;
let entry = tree::Entry {
mode: tree::EntryMode::from(tree::EntryKind::Blob),
oid: blob_id.object()?.id,
filename: BString::from("sample.rs"),
};
tree.entries.push(entry);
let tree_id = repo.write_object(tree)?;
2018-05-12 16:48:45 +02:00
2026-04-26 22:00:50 +02:00
let author = SignatureRef {
name: "test".as_bytes().as_bstr(),
email: "test@test.test".as_bytes().as_bstr(),
time: &Time::now_local_or_utc().format_or_unix(Format::Raw),
};
let commit_id = repo.commit_as(
author,
author,
"HEAD",
"initial commit",
2026-04-26 22:00:50 +02:00
tree_id,
gix::commit::NO_PARENT_IDS,
)?;
assert_eq!(commit_id, repo.head_id()?);
2026-04-26 22:00:50 +02:00
fs::copy(
"tests/snapshots/sample.modified.rs",
temp_dir.path().join("sample.rs"),
)
.expect("successful copy");
Ok(temp_dir)
2018-05-12 16:48:45 +02:00
}