commit 1c7964fda8eef1697372109a29eedd1e4ba050ee
parent 7a92ec2027c0d172e402efbc48357cd264ee7c82
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date: Thu, 27 Jun 2024 13:30:30 -0700
Extract all test utils to tests/common module
Diffstat:
3 files changed, 53 insertions(+), 77 deletions(-)
diff --git a/src/utils.rs b/src/utils.rs
@@ -77,58 +77,17 @@ pub fn run_script(path: &str, prefix: &str, postfix: &str) -> Result<(), String>
}
#[cfg(test)]
+#[path = "../tests/common/mod.rs"]
+mod common;
+
+#[cfg(test)]
mod tests {
use super::*;
-
- use std::env;
- use std::io::Write;
- use std::path::Path;
-
- /// Stores the path to a temporary directory that is automatically deleted
- /// when the value is dropped.
- ///
- /// Adapted from ripgrep's tests (crates/ignore/src/lib.rs)
- struct TempDir {
- dir: PathBuf
- }
- impl Drop for TempDir {
- fn drop(&mut self) {
- fs::remove_dir_all(&self.dir).unwrap();
- }
- }
- impl TempDir {
- fn new(name: &str) -> TempDir {
- let dir = env::temp_dir().join("coliru-tests").join(name);
- assert_eq!(dir.exists(), false);
- fs::create_dir_all(&dir).unwrap();
- TempDir { dir }
- }
- }
-
- /// Creates a temporary directory with a certain name and sets $HOME and the
- /// CWD to the parent directory.
- ///
- /// All tests in this module use the same values for $HOME and the CWD,
- /// which prevents issues when tests are run in multiple threads.
- fn setup(name: &str) -> TempDir {
- let dir = TempDir::new(name);
- let root = dir.dir.parent().unwrap();
- env::set_current_dir(root).unwrap();
- if cfg!(target_family = "unix") {
- env::set_var("HOME", root);
- }
- dir
- }
-
- /// Writes a string to a file, overwriting it if it already exists.
- fn write_file(path: &Path, contents: &str) {
- let mut file = fs::File::create(path).unwrap();
- file.write_all(contents.as_bytes()).unwrap();
- }
+ use common::{setup_integration, write_file};
#[test]
fn test_copy_file_create_dirs() {
- let tmp = setup("test_copy_file_create_dirs");
+ let tmp = setup_integration("test_copy_file_create_dirs");
let src = &tmp.dir.join("foo");
let dst = &tmp.dir.join("dir1").join("dir2").join("bar");
@@ -144,7 +103,7 @@ mod tests {
#[test]
fn test_copy_file_existing_file() {
- let tmp = setup("test_copy_file_existing_file");
+ let tmp = setup_integration("test_copy_file_existing_file");
let src = &tmp.dir.join("foo");
let dst = &tmp.dir.join("bar");
@@ -162,7 +121,7 @@ mod tests {
#[test]
#[cfg(target_family = "unix")]
fn test_copy_file_existing_broken_symlink() {
- let tmp = setup("test_copy_file_existing_broken_symlink");
+ let tmp = setup_integration("test_copy_file_existing_broken_symlink");
let src = &tmp.dir.join("foo");
let dst = &tmp.dir.join("bar");
@@ -180,7 +139,7 @@ mod tests {
#[test]
#[cfg(target_family = "unix")]
fn test_copy_file_tilde_expansion() {
- let tmp = setup("test_copy_file_tilde_expansion");
+ let tmp = setup_integration("test_copy_file_tilde_expansion");
let src = &tmp.dir.join("foo");
let dst = &tmp.dir.join("dir").join("bar");
@@ -197,7 +156,7 @@ mod tests {
#[test]
fn test_link_file_create_dirs() {
- let tmp = setup("test_link_file_create_dirs");
+ let tmp = setup_integration("test_link_file_create_dirs");
let src = &tmp.dir.join("foo");
let dst = &tmp.dir.join("dir1").join("dir2").join("bar");
@@ -213,7 +172,7 @@ mod tests {
#[test]
fn test_link_file_existing_file() {
- let tmp = setup("test_link_file_existing_file");
+ let tmp = setup_integration("test_link_file_existing_file");
let src = &tmp.dir.join("foo");
let dst = &tmp.dir.join("bar");
@@ -231,7 +190,7 @@ mod tests {
#[test]
#[cfg(target_family = "unix")]
fn test_link_file_existing_broken_symlink() {
- let tmp = setup("test_link_file_existing_broken_symlink");
+ let tmp = setup_integration("test_link_file_existing_broken_symlink");
let src = &tmp.dir.join("foo");
let dst = &tmp.dir.join("bar");
@@ -249,7 +208,7 @@ mod tests {
#[test]
#[cfg(target_family = "unix")]
fn test_link_file_tilde_expansion() {
- let tmp = setup("test_link_file_tilde_expansion");
+ let tmp = setup_integration("test_link_file_tilde_expansion");
let src = &tmp.dir.join("foo");
let dst = &tmp.dir.join("dir").join("bar");
@@ -267,7 +226,7 @@ mod tests {
#[test]
#[cfg(target_family = "unix")]
fn test_link_file_relative_source() {
- let tmp = setup("test_link_file_relative_source");
+ let tmp = setup_integration("test_link_file_relative_source");
let src = &tmp.dir.join("foo");
let src_rel = "test_link_file_relative_source/foo";
@@ -287,7 +246,7 @@ mod tests {
#[test]
#[cfg(target_family = "unix")]
fn test_run_script_successful() {
- let tmp = setup("test_run_script_successful");
+ let tmp = setup_integration("test_run_script_successful");
let src = &tmp.dir.join("foo");
write_file(src, "exit 0");
@@ -300,7 +259,7 @@ mod tests {
#[test]
#[cfg(target_family = "windows")]
fn test_run_script_successful() {
- let tmp = setup("test_run_script_successful");
+ let tmp = setup_integration("test_run_script_successful");
let src = &tmp.dir.join("foo.bat");
write_file(src, "exit 0");
@@ -313,7 +272,7 @@ mod tests {
#[test]
#[cfg(target_family = "unix")]
fn test_run_script_failure() {
- let tmp = setup("test_run_script_failure");
+ let tmp = setup_integration("test_run_script_failure");
let src = &tmp.dir.join("foo");
write_file(src, "exit 2");
@@ -327,7 +286,7 @@ mod tests {
#[test]
#[cfg(target_family = "windows")]
fn test_run_script_failure() {
- let tmp = setup("test_run_script_failure");
+ let tmp = setup_integration("test_run_script_failure");
let src = &tmp.dir.join("foo.bat");
write_file(src, "exit 1");
@@ -341,7 +300,7 @@ mod tests {
#[test]
#[cfg(target_family = "unix")]
fn test_run_script_postfix() {
- let tmp = setup("test_run_script_postfix");
+ let tmp = setup_integration("test_run_script_postfix");
let src = &tmp.dir.join("foo");
let dst = &tmp.dir.join("bar");
@@ -357,7 +316,7 @@ mod tests {
#[test]
#[cfg(target_family = "windows")]
fn test_run_script_postfix() {
- let tmp = setup("test_run_script_postfix");
+ let tmp = setup_integration("test_run_script_postfix");
let src = &tmp.dir.join("foo.bat");
let dst = &tmp.dir.join("bar");
diff --git a/tests/basic.rs b/tests/basic.rs
@@ -26,7 +26,7 @@ fn manifest_1(dir: &Path) {
#[test]
fn test_help() {
- let (_dir, mut cmd) = setup("test_help");
+ let (_dir, mut cmd) = setup_e2e("test_help");
cmd.arg("--help");
let expected = format!("\
A minimal, flexible, dotfile installer
@@ -50,7 +50,7 @@ Options:
#[test]
#[cfg(target_family = "unix")]
fn test_standard() {
- let (dir, mut cmd) = setup("test_standard");
+ let (dir, mut cmd) = setup_e2e("test_standard");
cmd.args(["manifest.yml", "-t", "linux"]);
manifest_1(&dir.dir);
@@ -79,7 +79,7 @@ script.sh called with arg1 linux
#[test]
#[cfg(target_family = "windows")]
fn test_standard() {
- let (dir, mut cmd) = setup("test_standard");
+ let (dir, mut cmd) = setup_e2e("test_standard");
cmd.args(["manifest-windows-test.yml", "-t", "windows"]);
manifest_1(&dir.dir);
@@ -108,7 +108,7 @@ script.bat called with arg1 windows\r
#[test]
#[cfg(target_family = "unix")]
fn test_run_alternate_tag_rules_1() {
- let (dir, mut cmd) = setup("test_run_alternate_tag_rules_1");
+ let (dir, mut cmd) = setup_e2e("test_run_alternate_tag_rules_1");
cmd.args(["manifest.yml", "-t", "linux", "^windows"]);
manifest_1(&dir.dir);
@@ -135,7 +135,7 @@ script.sh called with arg1 linux ^windows
#[test]
#[cfg(target_family = "unix")]
fn test_run_alternate_tag_rules_2() {
- let (dir, mut cmd) = setup("test_run_alternate_tag_rules_2");
+ let (dir, mut cmd) = setup_e2e("test_run_alternate_tag_rules_2");
cmd.args(["manifest.yml", "-t", "macos"]);
manifest_1(&dir.dir);
@@ -163,7 +163,7 @@ script.sh called with arg1 macos
#[test]
fn test_dry_run() {
- let (dir, mut cmd) = setup("test_dry_run");
+ let (dir, mut cmd) = setup_e2e("test_dry_run");
cmd.args(["manifest.yml", "--dry-run", "-t", "linux"]);
manifest_1(&dir.dir);
@@ -189,7 +189,7 @@ fn test_dry_run() {
#[test]
#[cfg(target_family = "unix")]
fn test_copy() {
- let (dir, mut cmd) = setup("test_copy");
+ let (dir, mut cmd) = setup_e2e("test_copy");
cmd.args(["manifest.yml", "--copy", "-t", "linux"]);
manifest_1(&dir.dir);
@@ -218,7 +218,7 @@ script.sh called with arg1 linux
#[test]
#[cfg(target_family = "windows")]
fn test_copy() {
- let (dir, mut cmd) = setup("test_copy");
+ let (dir, mut cmd) = setup_e2e("test_copy");
cmd.args(["manifest-windows-test.yml", "--copy", "-t", "windows"]);
manifest_1(&dir.dir);
@@ -247,7 +247,7 @@ script.bat called with arg1 windows\r
#[test]
#[cfg(target_family = "unix")]
fn test_run_failure() {
- let (dir, mut cmd) = setup("test_run_failure");
+ let (dir, mut cmd) = setup_e2e("test_run_failure");
cmd.args(["manifest.yml", "-t", "linux"]);
manifest_1(&dir.dir);
write_file(&dir.dir.join("script.sh"), "exit 1");
@@ -275,7 +275,7 @@ fn test_run_failure() {
#[test]
#[cfg(target_family = "windows")]
fn test_run_failure() {
- let (dir, mut cmd) = setup("test_run_failure");
+ let (dir, mut cmd) = setup_e2e("test_run_failure");
cmd.args(["manifest-windows-test.yml", "-t", "windows"]);
manifest_1(&dir.dir);
write_file(&dir.dir.join("script.bat"), "@echo off\r\nexit 1");
@@ -303,7 +303,7 @@ fn test_run_failure() {
#[test]
#[cfg(target_family = "unix")]
fn test_missing_file() {
- let (dir, mut cmd) = setup("test_missing_file");
+ let (dir, mut cmd) = setup_e2e("test_missing_file");
cmd.args(["manifest.yml", "-t", "linux"]);
manifest_1(&dir.dir);
remove_file(&dir.dir.join("vimrc")).unwrap();
@@ -330,7 +330,7 @@ script.sh called with arg1 linux
#[test]
#[cfg(target_family = "windows")]
fn test_missing_file() {
- let (dir, mut cmd) = setup("test_missing_file");
+ let (dir, mut cmd) = setup_e2e("test_missing_file");
cmd.args(["manifest-windows-test.yml", "-t", "windows"]);
manifest_1(&dir.dir);
remove_file(&dir.dir.join("vimrc")).unwrap();
@@ -356,7 +356,7 @@ script.bat called with arg1 windows\r
#[test]
fn test_empty_manifest() {
- let (dir, mut cmd) = setup("test_empty_manifest");
+ let (dir, mut cmd) = setup_e2e("test_empty_manifest");
cmd.args(["manifest.yml"]);
write_file(&dir.dir.join("manifest.yml"), "");
@@ -368,7 +368,7 @@ fn test_empty_manifest() {
#[test]
#[cfg(target_family = "unix")]
fn test_missing_manifest() {
- let (_dir, mut cmd) = setup("test_missing_manifest");
+ let (_dir, mut cmd) = setup_e2e("test_missing_manifest");
cmd.args(["missing.yml"]);
let expected = "Error: No such file or directory (os error 2)\n";
@@ -379,7 +379,7 @@ fn test_missing_manifest() {
#[test]
#[cfg(target_family = "windows")]
fn test_missing_manifest() {
- let (_dir, mut cmd) = setup("test_missing_manifest");
+ let (_dir, mut cmd) = setup_e2e("test_missing_manifest");
cmd.args(["missing-windows-test.yml"]);
let expected = "Error: The system cannot find the file specified. \
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
@@ -1,3 +1,5 @@
+#![allow(dead_code)]
+
use std::env;
use std::fs;
use std::io::Write;
@@ -25,11 +27,26 @@ impl TempDir {
}
}
+/// Creates a temporary directory with a certain name and sets $HOME and the
+/// CWD to the parent directory.
+///
+/// All tests in this module use the same values for $HOME and the CWD,
+/// which prevents issues when tests are run in multiple threads.
+pub fn setup_integration(name: &str) -> TempDir {
+ let dir = TempDir::new(name);
+ let root = dir.dir.parent().unwrap();
+ env::set_current_dir(root).unwrap();
+ if cfg!(target_family = "unix") {
+ env::set_var("HOME", root);
+ }
+ dir
+}
+
/// Creates a temporary directory with a certain name and create a new coliru
/// Command with $HOME and the CWD set the the temporary directory.
///
/// Adapted from ripgrep's tests (tests/utils.rs)
-pub fn setup(name: &str) -> (TempDir, Command) {
+pub fn setup_e2e(name: &str) -> (TempDir, Command) {
let dir = TempDir::new(name);
let exe = env::current_exe().unwrap().parent().unwrap().to_path_buf()