@@ -0,0 +1,75 @@
2+use std::fs::{self, File};
4+use std::path::PathBuf;
5+use std::process::Command;
12+ pub fn new() -> Self {
13+ modify_sample_file();
15+ let root = env::current_exe()
16+ .expect("tests executable")
18+ .expect("tests executable directory")
20+ .expect("bat executable directory")
23+ let exe_name = if cfg!(windows) { "bat.exe" } else { "bat" };
26+ exe: root.join(exe_name),
30+ pub fn test_snapshot(&self, style: &str) {
31+ let output = Command::new(&self.exe)
33+ "tests/snapshots/sample.rs",
34+ &format!("--style={}", style),
37+ .expect("bat failed");
38+ // have to do the replace because the filename in the header changes based on the current working directory
39+ let actual = String::from_utf8_lossy(&output.stdout)
41+ .replace("tests/snapshots/", "");
43+ let mut expected = String::new();
44+ let mut file = File::open(format!("tests/snapshots/output/{}.snapshot.txt", style))
45+ .expect("snapshot file missing");
46+ file.read_to_string(&mut expected)
47+ .expect("could not read snapshot file");
49+ assert_eq!(expected, actual);
53+impl Drop for BatTester {
55+ undo_sample_file_modification();
59+fn modify_sample_file() {
61+ "tests/snapshots/sample.modified.rs",
62+ "tests/snapshots/sample.rs",
63+ ).expect("generating modified sample file failed");
66+fn undo_sample_file_modification() {
67+ let output = Command::new("git")
68+ .args(&["checkout", "--", "tests/snapshots/sample.rs"])
70+ .expect("git checkout failed");
72+ if !output.status.success() {
73+ panic!("undoing modified sample changes failed")