Use separate Git repository for snapshot testing

closes #161
This commit is contained in:
sharkdp 2018-06-02 18:06:40 +02:00 committed by David Peter
parent 69c798eafd
commit c9f7a0c126
3 changed files with 72 additions and 26 deletions

20
Cargo.lock generated
View File

@ -51,6 +51,7 @@ dependencies = [
"git2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntect 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntect 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -505,6 +506,14 @@ dependencies = [
"ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "remove_dir_all"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "safemem" name = "safemem"
version = "0.2.0" version = "0.2.0"
@ -598,6 +607,15 @@ dependencies = [
"yaml-rust 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "yaml-rust 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "tempdir"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "term_size" name = "term_size"
version = "0.3.1" version = "0.3.1"
@ -831,6 +849,7 @@ dependencies = [
"checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384" "checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384"
"checksum regex-syntax 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8e931c58b93d86f080c734bfd2bce7dd0079ae2331235818133c8be7f422e20e" "checksum regex-syntax 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8e931c58b93d86f080c734bfd2bce7dd0079ae2331235818133c8be7f422e20e"
"checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" "checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7"
"checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5"
"checksum safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e27a8b19b835f7aea908818e871f5cc3a5a186550c30773be987e155e8163d8f" "checksum safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e27a8b19b835f7aea908818e871f5cc3a5a186550c30773be987e155e8163d8f"
"checksum same-file 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "cfb6eded0b06a0b512c8ddbcf04089138c9b4362c2f696f3c3d76039d68f3637" "checksum same-file 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "cfb6eded0b06a0b512c8ddbcf04089138c9b4362c2f696f3c3d76039d68f3637"
"checksum serde 1.0.51 (registry+https://github.com/rust-lang/crates.io-index)" = "21924cc18e5281f232a17c040355fac97732b42cf019c24996a1642bcb169cdb" "checksum serde 1.0.51 (registry+https://github.com/rust-lang/crates.io-index)" = "21924cc18e5281f232a17c040355fac97732b42cf019c24996a1642bcb169cdb"
@ -842,6 +861,7 @@ dependencies = [
"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
"checksum syn 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)" = "61b8f1b737f929c6516ba46a3133fd6d5215ad8a62f66760f851f7048aebedfb" "checksum syn 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)" = "61b8f1b737f929c6516ba46a3133fd6d5215ad8a62f66760f851f7048aebedfb"
"checksum syntect 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dc8a6f0db88d4afc340522c20d260411e746b2225b257c6b238a75de9d7cec78" "checksum syntect 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dc8a6f0db88d4afc340522c20d260411e746b2225b257c6b238a75de9d7cec78"
"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
"checksum term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9e5b9a66db815dcfd2da92db471106457082577c3c278d4138ab3e3b4e189327" "checksum term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9e5b9a66db815dcfd2da92db471106457082577c3c278d4138ab3e3b4e189327"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum termios 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d9cf598a6d7ce700a4e6a9199da127e6819a61e64b68609683cc9a01b5683a" "checksum termios 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d9cf598a6d7ce700a4e6a9199da127e6819a61e64b68609683cc9a01b5683a"

View File

@ -39,3 +39,6 @@ features = ["suggestions", "color", "wrap_help"]
version = "0.11" version = "0.11"
default-features = false default-features = false
features = [] features = []
[dev-dependencies]
tempdir = "0.3"

View File

@ -1,16 +1,29 @@
use std::env; use std::env;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::Read; use std::io::{self, Read};
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::process::Command; use std::process::Command;
extern crate tempdir;
use self::tempdir::TempDir;
extern crate git2;
use self::git2::build::CheckoutBuilder;
use self::git2::Error;
use self::git2::Repository;
use self::git2::Signature;
pub struct BatTester { pub struct BatTester {
/// Temporary working directory
temp_dir: TempDir,
/// Path to the *bat* executable
exe: PathBuf, exe: PathBuf,
} }
impl BatTester { impl BatTester {
pub fn new() -> Self { pub fn new() -> Self {
modify_sample_file(); let temp_dir = create_sample_directory().expect("sample directory");
let root = env::current_exe() let root = env::current_exe()
.expect("tests executable") .expect("tests executable")
@ -21,17 +34,18 @@ impl BatTester {
.to_path_buf(); .to_path_buf();
let exe_name = if cfg!(windows) { "bat.exe" } else { "bat" }; let exe_name = if cfg!(windows) { "bat.exe" } else { "bat" };
let exe = root.join(exe_name);
BatTester { BatTester { temp_dir, exe }
exe: root.join(exe_name),
}
} }
pub fn test_snapshot(&self, style: &str) { pub fn test_snapshot(&self, style: &str) {
let output = Command::new(&self.exe) let output = Command::new(&self.exe)
.args(&["tests/snapshots/sample.rs", &format!("--style={}", style)]) .current_dir(self.temp_dir.path())
.args(&["sample.rs", &format!("--style={}", style)])
.output() .output()
.expect("bat failed"); .expect("bat failed");
// have to do the replace because the filename in the header changes based on the current working directory // 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) let actual = String::from_utf8_lossy(&output.stdout)
.as_ref() .as_ref()
@ -47,26 +61,35 @@ impl BatTester {
} }
} }
impl Drop for BatTester { fn create_sample_directory() -> Result<TempDir, git2::Error> {
fn drop(&mut self) { // Create temp directory and initialize repository
undo_sample_file_modification(); let temp_dir = TempDir::new("bat-tests").expect("Temp directory");
} let repo = Repository::init(&temp_dir)?;
}
fn modify_sample_file() { // Copy over `sample.rs`
fs::copy( let sample_path = temp_dir.path().join("sample.rs");
"tests/snapshots/sample.modified.rs", println!("{:?}", &sample_path);
"tests/snapshots/sample.rs", fs::copy("tests/snapshots/sample.rs", &sample_path).expect("successful copy");
).expect("generating modified sample file failed");
}
fn undo_sample_file_modification() { // Commit
let output = Command::new("git") let mut index = repo.index()?;
.args(&["checkout", "--", "tests/snapshots/sample.rs"]) index.add_path(Path::new("sample.rs"))?;
.output()
.expect("git checkout failed");
if !output.status.success() { let oid = index.write_tree()?;
panic!("undoing modified sample changes failed") let signature = Signature::now("bat test runner", "bat@test.runner")?;
} let tree = repo.find_tree(oid)?;
repo.commit(
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)
} }