@@ -0,0 +1,75 @@
1+use std::env;
2+use std::fs::{self, File};
3+use std::io::Read;
4+use std::path::PathBuf;
5+use std::process::Command;
6+
7+pub struct BatTester {
8+ exe: PathBuf,
9+}
10+
11+impl BatTester {
12+ pub fn new() -> Self {
13+ modify_sample_file();
14+ // Lifted from fd :)
15+ let root = env::current_exe()
16+ .expect("tests executable")
17+ .parent()
18+ .expect("tests executable directory")
19+ .parent()
20+ .expect("bat executable directory")
21+ .to_path_buf();
22+
23+ let exe_name = if cfg!(windows) { "bat.exe" } else { "bat" };
24+
25+ BatTester {
26+ exe: root.join(exe_name),
27+ }
28+ }
29+
30+ pub fn test_snapshot(&self, style: &str) {
31+ let output = Command::new(&self.exe)
32+ .args(&[
33+ "tests/snapshots/sample.rs",
34+ &format!("--style={}", style),
35+ ])
36+ .output()
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)
40+ .as_ref()
41+ .replace("tests/snapshots/", "");
42+
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");
48+
49+ assert_eq!(expected, actual);
50+ }
51+}
52+
53+impl Drop for BatTester {
54+ fn drop(&mut self) {
55+ undo_sample_file_modification();
56+ }
57+}
58+
59+fn modify_sample_file() {
60+ fs::copy(
61+ "tests/snapshots/sample.modified.rs",
62+ "tests/snapshots/sample.rs",
63+ ).expect("generating modified sample file failed");
64+}
65+
66+fn undo_sample_file_modification() {
67+ let output = Command::new("git")
68+ .args(&["checkout", "--", "tests/snapshots/sample.rs"])
69+ .output()
70+ .expect("git checkout failed");
71+
72+ if !output.status.success() {
73+ panic!("undoing modified sample changes failed")
74+ }
75+}