on master help sign in
Add snapshot testing
commit0886a24685dbab13bfcf186d9c5d0891d8100344 authorEzinwa Okpoechi <brainmaestro@outlook.com> dateSat 12 May 2018, 16:48 (8 years ago) parent53eb9c5
3 files changed+109−1
tests/snapshots/sample.modified.rs+1−1 tests/tester.rs+75 tests/tests.rs+33
tests/snapshots/sample.modified.rs+1−1
@@ -18,4 +18,4 @@ fn area(rectangle: &Rectangle) -> u32 {
1818
1919fn perimeter(rectangle: &Rectangle) -> u32 {
2020 (rectangle.width + rectangle.height) * 2
21−}
21+}
tests/tester.rsnew file+75
@@ -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+}
tests/tests.rsnew file+33
@@ -0,0 +1,33 @@
1+mod tester;
2+
3+use tester::BatTester;
4+
5+static STYLES: &'static [&'static str] = &[
6+ "changes",
7+ "grid",
8+ "header",
9+ "numbers",
10+ "changes,grid",
11+ "changes,header",
12+ "changes,numbers",
13+ "grid,header",
14+ "grid,numbers",
15+ "header,numbers",
16+ "changes,grid,header",
17+ "changes,grid,numbers",
18+ "changes,header,numbers",
19+ "grid,header,numbers",
20+ "changes,grid,header,numbers",
21+ "full",
22+ "plain",
23+];
24+
25+#[test]
26+fn test_snapshots() {
27+ let bat_tester = BatTester::new();
28+
29+ for style in STYLES {
30+ println!("testing {}", style);
31+ bat_tester.test_snapshot(&*style);
32+ }
33+}