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;
|
2018-06-02 18:06:40 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-05-12 16:48:45 +02:00
|
|
|
use std::process::Command;
|
|
|
|
|
2021-02-16 08:50:41 +01:00
|
|
|
use tempfile::TempDir;
|
2018-06-02 18:06:40 +02:00
|
|
|
|
2020-03-30 18:43:13 +02:00
|
|
|
use git2::build::CheckoutBuilder;
|
|
|
|
use git2::Repository;
|
|
|
|
use git2::Signature;
|
2018-06-02 18:06:40 +02:00
|
|
|
|
2018-05-12 16:48:45 +02:00
|
|
|
pub struct BatTester {
|
2018-06-02 18:06:40 +02:00
|
|
|
/// 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)
|
2018-06-02 18:06:40 +02:00
|
|
|
.current_dir(self.temp_dir.path())
|
2024-02-20 19:39:22 +01: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",
|
2018-08-31 22:52:35 +02:00
|
|
|
"--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-06-02 18:06:40 +02:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 19:15:39 +01:00
|
|
|
impl Default for BatTester {
|
|
|
|
fn default() -> Self {
|
|
|
|
let temp_dir = create_sample_directory().expect("sample directory");
|
|
|
|
|
|
|
|
let root = env::current_exe()
|
|
|
|
.expect("tests executable")
|
|
|
|
.parent()
|
|
|
|
.expect("tests executable directory")
|
|
|
|
.parent()
|
|
|
|
.expect("bat executable directory")
|
|
|
|
.to_path_buf();
|
|
|
|
|
|
|
|
let exe_name = if cfg!(windows) { "bat.exe" } else { "bat" };
|
|
|
|
let exe = root.join(exe_name);
|
|
|
|
|
|
|
|
BatTester { temp_dir, exe }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 18:06:40 +02:00
|
|
|
fn create_sample_directory() -> Result<TempDir, git2::Error> {
|
|
|
|
// Create temp directory and initialize repository
|
2021-02-16 08:50:41 +01:00
|
|
|
let temp_dir = TempDir::new().expect("Temp directory");
|
2018-06-02 18:06:40 +02:00
|
|
|
let repo = Repository::init(&temp_dir)?;
|
2018-05-12 16:48:45 +02:00
|
|
|
|
2018-06-02 18:06:40 +02:00
|
|
|
// Copy over `sample.rs`
|
|
|
|
let sample_path = temp_dir.path().join("sample.rs");
|
|
|
|
println!("{:?}", &sample_path);
|
|
|
|
fs::copy("tests/snapshots/sample.rs", &sample_path).expect("successful copy");
|
2018-05-12 16:48:45 +02:00
|
|
|
|
2018-06-02 18:06:40 +02:00
|
|
|
// Commit
|
|
|
|
let mut index = repo.index()?;
|
|
|
|
index.add_path(Path::new("sample.rs"))?;
|
2018-05-12 16:48:45 +02:00
|
|
|
|
2018-06-02 18:06:40 +02:00
|
|
|
let oid = index.write_tree()?;
|
|
|
|
let signature = Signature::now("bat test runner", "bat@test.runner")?;
|
|
|
|
let tree = repo.find_tree(oid)?;
|
2018-08-18 20:44:25 +02:00
|
|
|
let _ = repo.commit(
|
2018-06-02 18:06:40 +02:00
|
|
|
Some("HEAD"), // point HEAD to our new commit
|
|
|
|
&signature, // author
|
|
|
|
&signature, // committer
|
|
|
|
"initial commit",
|
|
|
|
&tree,
|
|
|
|
&[],
|
|
|
|
);
|
|
|
|
let mut opts = CheckoutBuilder::new();
|
|
|
|
repo.checkout_head(Some(opts.force()))?;
|
|
|
|
|
|
|
|
fs::copy("tests/snapshots/sample.modified.rs", &sample_path).expect("successful copy");
|
|
|
|
|
|
|
|
Ok(temp_dir)
|
2018-05-12 16:48:45 +02:00
|
|
|
}
|