diff options
author | sharkdp <davidpeter@web.de> | 2018-06-02 18:06:40 +0200 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2018-06-02 19:00:05 +0200 |
commit | c9f7a0c1268781676d21cd430ef4a8e2bd106b9e (patch) | |
tree | d049d07b374c6371eb59a04e27151814f2fa6629 /tests | |
parent | 69c798eafd93d174d0340158794b7c1d5f4e598d (diff) |
Use separate Git repository for snapshot testing
closes #161
Diffstat (limited to 'tests')
-rw-r--r-- | tests/tester.rs | 75 |
1 files changed, 49 insertions, 26 deletions
diff --git a/tests/tester.rs b/tests/tester.rs index 404e8342..5f32113e 100644 --- a/tests/tester.rs +++ b/tests/tester.rs @@ -1,16 +1,29 @@ use std::env; use std::fs::{self, File}; -use std::io::Read; -use std::path::PathBuf; +use std::io::{self, Read}; +use std::path::{Path, PathBuf}; 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 { + /// Temporary working directory + temp_dir: TempDir, + + /// Path to the *bat* executable exe: PathBuf, } impl BatTester { pub fn new() -> Self { - modify_sample_file(); + let temp_dir = create_sample_directory().expect("sample directory"); let root = env::current_exe() .expect("tests executable") @@ -21,17 +34,18 @@ impl BatTester { .to_path_buf(); let exe_name = if cfg!(windows) { "bat.exe" } else { "bat" }; + let exe = root.join(exe_name); - BatTester { - exe: root.join(exe_name), - } + BatTester { temp_dir, exe } } pub fn test_snapshot(&self, style: &str) { 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() .expect("bat failed"); + // 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() @@ -47,26 +61,35 @@ impl BatTester { } } -impl Drop for BatTester { - fn drop(&mut self) { - undo_sample_file_modification(); - } -} +fn create_sample_directory() -> Result<TempDir, git2::Error> { + // Create temp directory and initialize repository + let temp_dir = TempDir::new("bat-tests").expect("Temp directory"); + let repo = Repository::init(&temp_dir)?; -fn modify_sample_file() { - fs::copy( - "tests/snapshots/sample.modified.rs", - "tests/snapshots/sample.rs", - ).expect("generating modified sample file failed"); -} + // 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"); -fn undo_sample_file_modification() { - let output = Command::new("git") - .args(&["checkout", "--", "tests/snapshots/sample.rs"]) - .output() - .expect("git checkout failed"); + // Commit + let mut index = repo.index()?; + index.add_path(Path::new("sample.rs"))?; - if !output.status.success() { - panic!("undoing modified sample changes failed") - } + let oid = index.write_tree()?; + 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) } |