Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use separate Git repository for snapshot testing #165

Merged
merged 1 commit into from
Jun 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ features = ["suggestions", "color", "wrap_help"]
version = "0.11"
default-features = false
features = []

[dev-dependencies]
tempdir = "0.3"
75 changes: 49 additions & 26 deletions tests/tester.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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()
Expand All @@ -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)
}