From 020492f7e6a6e267a8ddfdc5859e328d0e9354ac Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Thu, 18 Aug 2022 10:15:27 +0200 Subject: tests/tester: Do not build as tests mod tester does not contains any tests, so do not build the module as containing tests. Instead use the mod.rs approach described in https://doc.rust-lang.org/book/ch11-03-test-organization.html#submodules-in-integration-tests. --- tests/tester.rs | 102 ---------------------------------------------------- tests/tester/mod.rs | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 102 deletions(-) delete mode 100644 tests/tester.rs create mode 100644 tests/tester/mod.rs diff --git a/tests/tester.rs b/tests/tester.rs deleted file mode 100644 index 8ddea11f..00000000 --- a/tests/tester.rs +++ /dev/null @@ -1,102 +0,0 @@ -use std::env; -use std::fs::{self, File}; -use std::io::Read; -use std::path::{Path, PathBuf}; -use std::process::Command; - -use tempfile::TempDir; - -use git2::build::CheckoutBuilder; -use git2::Repository; -use git2::Signature; - -pub struct BatTester { - /// Temporary working directory - temp_dir: TempDir, - - /// Path to the *bat* executable - exe: PathBuf, -} - -impl BatTester { - pub fn test_snapshot(&self, name: &str, style: &str) { - let output = Command::new(&self.exe) - .current_dir(self.temp_dir.path()) - .args(&[ - "sample.rs", - "--no-config", - "--paging=never", - "--color=never", - "--decorations=always", - "--terminal-width=80", - &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() - .replace("tests/snapshots/", ""); - - let mut expected = String::new(); - let mut file = File::open(format!("tests/snapshots/output/{}.snapshot.txt", name)) - .expect("snapshot file missing"); - file.read_to_string(&mut expected) - .expect("could not read snapshot file"); - - assert_eq!(expected, actual); - } -} - -impl Default for BatTester { - fn default() -> Self { - let temp_dir = create_sample_directory().expect("sample directory"); - - let root = env::current_exe() - .expect("tests executable") - .parent() - .expect("tests executable directory") - .parent() - .expect("bat executable directory") - .to_path_buf(); - - let exe_name = if cfg!(windows) { "bat.exe" } else { "bat" }; - let exe = root.join(exe_name); - - BatTester { temp_dir, exe } - } -} - -fn create_sample_directory() -> Result { - // Create temp directory and initialize repository - let temp_dir = TempDir::new().expect("Temp directory"); - let repo = Repository::init(&temp_dir)?; - - // 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"); - - // Commit - let mut index = repo.index()?; - index.add_path(Path::new("sample.rs"))?; - - let oid = index.write_tree()?; - let signature = Signature::now("bat test runner", "bat@test.runner")?; - let tree = repo.find_tree(oid)?; - let _ = 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) -} diff --git a/tests/tester/mod.rs b/tests/tester/mod.rs new file mode 100644 index 00000000..8ddea11f --- /dev/null +++ b/tests/tester/mod.rs @@ -0,0 +1,102 @@ +use std::env; +use std::fs::{self, File}; +use std::io::Read; +use std::path::{Path, PathBuf}; +use std::process::Command; + +use tempfile::TempDir; + +use git2::build::CheckoutBuilder; +use git2::Repository; +use git2::Signature; + +pub struct BatTester { + /// Temporary working directory + temp_dir: TempDir, + + /// Path to the *bat* executable + exe: PathBuf, +} + +impl BatTester { + pub fn test_snapshot(&self, name: &str, style: &str) { + let output = Command::new(&self.exe) + .current_dir(self.temp_dir.path()) + .args(&[ + "sample.rs", + "--no-config", + "--paging=never", + "--color=never", + "--decorations=always", + "--terminal-width=80", + &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() + .replace("tests/snapshots/", ""); + + let mut expected = String::new(); + let mut file = File::open(format!("tests/snapshots/output/{}.snapshot.txt", name)) + .expect("snapshot file missing"); + file.read_to_string(&mut expected) + .expect("could not read snapshot file"); + + assert_eq!(expected, actual); + } +} + +impl Default for BatTester { + fn default() -> Self { + let temp_dir = create_sample_directory().expect("sample directory"); + + let root = env::current_exe() + .expect("tests executable") + .parent() + .expect("tests executable directory") + .parent() + .expect("bat executable directory") + .to_path_buf(); + + let exe_name = if cfg!(windows) { "bat.exe" } else { "bat" }; + let exe = root.join(exe_name); + + BatTester { temp_dir, exe } + } +} + +fn create_sample_directory() -> Result { + // Create temp directory and initialize repository + let temp_dir = TempDir::new().expect("Temp directory"); + let repo = Repository::init(&temp_dir)?; + + // 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"); + + // Commit + let mut index = repo.index()?; + index.add_path(Path::new("sample.rs"))?; + + let oid = index.write_tree()?; + let signature = Signature::now("bat test runner", "bat@test.runner")?; + let tree = repo.find_tree(oid)?; + let _ = 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) +} -- cgit v1.2.3