diff options
-rw-r--r-- | src/config.rs | 5 | ||||
-rw-r--r-- | src/git.rs | 2 | ||||
-rw-r--r-- | src/git/commit.rs | 14 | ||||
-rw-r--r-- | src/git/commit_diff_loader.rs | 2 | ||||
-rw-r--r-- | src/git/reference.rs | 2 | ||||
-rw-r--r-- | src/git/reference_kind.rs | 2 | ||||
-rw-r--r-- | src/git/repository.rs | 5 | ||||
-rw-r--r-- | src/git/testutil.rs | 3 | ||||
-rw-r--r-- | src/git/testutil/with_temp_repository.rs | 57 | ||||
-rw-r--r-- | src/module/modules.rs | 2 | ||||
-rw-r--r-- | src/modules/show_commit/tests.rs | 7 | ||||
-rw-r--r-- | src/test_helpers.rs | 5 | ||||
-rw-r--r-- | src/test_helpers/shared.rs | 5 | ||||
-rw-r--r-- | src/test_helpers/shared/git2.rs | 13 | ||||
-rw-r--r-- | src/test_helpers/shared/with_temporary_path.rs | 11 | ||||
-rw-r--r-- | src/test_helpers/with_temp_bare_repository.rs | 18 | ||||
-rw-r--r-- | src/test_helpers/with_temp_repository.rs | 20 |
17 files changed, 89 insertions, 84 deletions
diff --git a/src/config.rs b/src/config.rs index d92b3dd..ef3fb5a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -135,10 +135,7 @@ mod tests { use rstest::rstest; use super::*; - use crate::{ - git::testutil::with_temp_bare_repository, - test_helpers::{invalid_utf, with_git_config}, - }; + use crate::test_helpers::{invalid_utf, with_git_config, with_temp_bare_repository}; #[test] fn new() { @@ -23,8 +23,6 @@ mod reference; mod reference_kind; mod repository; mod status; -#[cfg(test)] -pub(crate) mod testutil; mod user; pub(crate) use git2::{Config, ErrorCode}; diff --git a/src/git/commit.rs b/src/git/commit.rs index 195abae..fccce68 100644 --- a/src/git/commit.rs +++ b/src/git/commit.rs @@ -119,14 +119,12 @@ mod tests { use testutils::assert_err_eq; use super::*; - use crate::{ - git::testutil::with_temp_repository, - test_helpers::{ - builders::{CommitBuilder, ReferenceBuilder}, - create_commit, - CreateCommitOptions, - JAN_2021_EPOCH, - }, + use crate::test_helpers::{ + builders::{CommitBuilder, ReferenceBuilder}, + create_commit, + with_temp_repository, + CreateCommitOptions, + JAN_2021_EPOCH, }; #[test] diff --git a/src/git/commit_diff_loader.rs b/src/git/commit_diff_loader.rs index 60d48b4..3b3621b 100644 --- a/src/git/commit_diff_loader.rs +++ b/src/git/commit_diff_loader.rs @@ -165,7 +165,7 @@ mod tests { }; use super::*; - use crate::git::{testutil::with_temp_repository, Origin}; + use crate::{git::Origin, test_helpers::with_temp_repository}; fn _format_status(status: &FileStatus) -> String { let s = match status.status() { diff --git a/src/git/reference.rs b/src/git/reference.rs index ac1789b..dd44cbb 100644 --- a/src/git/reference.rs +++ b/src/git/reference.rs @@ -59,7 +59,7 @@ impl Reference { #[cfg(test)] mod tests { use super::*; - use crate::git::testutil::with_temp_repository; + use crate::test_helpers::with_temp_repository; #[test] fn test() { diff --git a/src/git/reference_kind.rs b/src/git/reference_kind.rs index d9eaa64..db29536 100644 --- a/src/git/reference_kind.rs +++ b/src/git/reference_kind.rs @@ -37,7 +37,7 @@ impl ReferenceKind { #[cfg(test)] mod tests { use super::*; - use crate::{git::testutil::with_temp_repository, test_helpers::JAN_2021_EPOCH}; + use crate::test_helpers::{with_temp_repository, JAN_2021_EPOCH}; #[test] fn from_git2_reference_branch() { diff --git a/src/git/repository.rs b/src/git/repository.rs index 3239d74..b57f1a8 100644 --- a/src/git/repository.rs +++ b/src/git/repository.rs @@ -193,10 +193,7 @@ mod tests { use testutils::assert_err_eq; use super::*; - use crate::{ - git::testutil::{with_temp_bare_repository, with_temp_repository}, - test_helpers::create_commit, - }; + use crate::test_helpers::{create_commit, with_temp_bare_repository, with_temp_repository}; #[test] #[serial_test::serial] diff --git a/src/git/testutil.rs b/src/git/testutil.rs deleted file mode 100644 index 85de3ee..0000000 --- a/src/git/testutil.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod with_temp_repository; - -pub(crate) use self::with_temp_repository::{with_temp_bare_repository, with_temp_repository}; diff --git a/src/git/testutil/with_temp_repository.rs b/src/git/testutil/with_temp_repository.rs deleted file mode 100644 index 33084c8..0000000 --- a/src/git/testutil/with_temp_repository.rs +++ /dev/null @@ -1,57 +0,0 @@ -#![cfg(not(tarpaulin_include))] - -use std::path::Path; - -use tempfile::Builder; - -use crate::{git::Repository, test_helpers::JAN_2021_EPOCH}; - -fn with_temporary_path<F>(callback: F) -where F: FnOnce(&Path) { - let temp_repository_directory = Builder::new().prefix("interactive-rebase-tool").tempdir().unwrap(); - let path = temp_repository_directory.path(); - callback(path); - temp_repository_directory.close().unwrap(); -} - -fn create_repository_from_git2_repo(repo: git2::Repository) -> Repository { - { - let id = repo.index().unwrap().write_tree().unwrap(); - let tree = repo.find_tree(id).unwrap(); - let sig = git2::Signature::new("name", "name@example.com", &git2::Time::new(JAN_2021_EPOCH, 0)).unwrap(); - _ = repo - .commit(Some("HEAD"), &sig, &sig, "initial commit", &tree, &[]) - .unwrap(); - }; - Repository::from(repo) -} - -/// Provides a new repository instance in a temporary directory for testing that contains an initial -/// empty commit. -/// -/// # Panics -/// -/// If the repository cannot be created for any reason, this function will panic. -pub(crate) fn with_temp_repository<F>(callback: F) -where F: FnOnce(Repository) { - with_temporary_path(|path| { - let mut opts = git2::RepositoryInitOptions::new(); - _ = opts.initial_head("main"); - let repo = create_repository_from_git2_repo(git2::Repository::init_opts(path, &opts).unwrap()); - callback(repo); - }); -} - -/// Provide a bare repository for testing in a temporary directory. -/// -/// # Panics -/// -/// If the repository cannot be created for any reason, this function will panic. -#[allow(clippy::panic)] -pub(crate) fn with_temp_bare_repository<F>(callback: F) -where F: FnOnce(Repository) { - with_temporary_path(|path| { - let repo = create_repository_from_git2_repo(git2::Repository::init_bare(path).unwrap()); - callback(repo); - }); -} diff --git a/src/module/modules.rs b/src/module/modules.rs index 4a7cab4..012d292 100644 --- a/src/module/modules.rs +++ b/src/module/modules.rs @@ -69,7 +69,7 @@ impl ModuleProvider for Modules { #[cfg(test)] mod tests { use super::*; - use crate::{git::testutil::with_temp_repository, todo_file::testutil::with_todo_file}; + use crate::{test_helpers::with_temp_repository, todo_file::testutil::with_todo_file}; pub(crate) fn modules_test<C>(callback: C) where C: FnOnce(Modules) { diff --git a/src/modules/show_commit/tests.rs b/src/modules/show_commit/tests.rs index fa10d39..fa48e0f 100644 --- a/src/modules/show_commit/tests.rs +++ b/src/modules/show_commit/tests.rs @@ -5,11 +5,14 @@ use super::*; use crate::{ assert_rendered_output, assert_results, - git::{testutil::with_temp_repository, Delta, DiffLine, FileMode, Origin, Status, User}, + git::{Delta, DiffLine, FileMode, Origin, Status, User}, input::StandardEvent, process::Artifact, render_line, - test_helpers::builders::{CommitBuilder, CommitDiffBuilder, FileStatusBuilder}, + test_helpers::{ + builders::{CommitBuilder, CommitDiffBuilder, FileStatusBuilder}, + with_temp_repository, + }, testutil::module_test, view::{testutil::AssertRenderOptions, ViewLine}, }; diff --git a/src/test_helpers.rs b/src/test_helpers.rs index 68d8e0b..6850d6e 100644 --- a/src/test_helpers.rs +++ b/src/test_helpers.rs @@ -2,7 +2,10 @@ pub(crate) mod builders; mod create_commit; mod create_invalid_utf; pub(crate) mod mocks; +mod shared; mod with_git_config; +mod with_temp_bare_repository; +mod with_temp_repository; pub(crate) static JAN_2021_EPOCH: i64 = 1_609_459_200; @@ -10,4 +13,6 @@ pub(crate) use self::{ create_commit::{create_commit, CreateCommitOptions}, create_invalid_utf::invalid_utf, with_git_config::with_git_config, + with_temp_bare_repository::with_temp_bare_repository, + with_temp_repository::with_temp_repository, }; diff --git a/src/test_helpers/shared.rs b/src/test_helpers/shared.rs new file mode 100644 index 0000000..43cd677 --- /dev/null +++ b/src/test_helpers/shared.rs @@ -0,0 +1,5 @@ +mod with_temporary_path; + +mod git2; + +pub(crate) use self::{git2::create_repository, with_temporary_path::with_temporary_path}; diff --git a/src/test_helpers/shared/git2.rs b/src/test_helpers/shared/git2.rs new file mode 100644 index 0000000..abeee23 --- /dev/null +++ b/src/test_helpers/shared/git2.rs @@ -0,0 +1,13 @@ +use crate::{git::Repository, test_helpers::JAN_2021_EPOCH}; + +pub(crate) fn create_repository(repo: git2::Repository) -> Repository { + { + let id = repo.index().unwrap().write_tree().unwrap(); + let tree = repo.find_tree(id).unwrap(); + let sig = git2::Signature::new("name", "name@example.com", &git2::Time::new(JAN_2021_EPOCH, 0)).unwrap(); + _ = repo + .commit(Some("HEAD"), &sig, &sig, "initial commit", &tree, &[]) + .unwrap(); + }; + Repository::from(repo) +} diff --git a/src/test_helpers/shared/with_temporary_path.rs b/src/test_helpers/shared/with_temporary_path.rs new file mode 100644 index 0000000..f0a7131 --- /dev/null +++ b/src/test_helpers/shared/with_temporary_path.rs @@ -0,0 +1,11 @@ +use std::path::Path; + +use tempfile::Builder; + +pub(crate) fn with_temporary_path<F>(callback: F) +where F: FnOnce(&Path) { + let temp_repository_directory = Builder::new().prefix("interactive-rebase-tool").tempdir().unwrap(); + let path = temp_repository_directory.path(); + callback(path); + temp_repository_directory.close().unwrap(); +} diff --git a/src/test_helpers/with_temp_bare_repository.rs b/src/test_helpers/with_temp_bare_repository.rs new file mode 100644 index 0000000..b84bc60 --- /dev/null +++ b/src/test_helpers/with_temp_bare_repository.rs @@ -0,0 +1,18 @@ +use crate::{ + git::Repository, + test_helpers::shared::{create_repository, with_temporary_path}, +}; + +/// Provide a bare repository for testing in a temporary directory. +/// +/// # Panics +/// +/// If the repository cannot be created for any reason, this function will panic. +#[allow(clippy::panic)] +pub(crate) fn with_temp_bare_repository<F>(callback: F) +where F: FnOnce(Repository) { + with_temporary_path(|path| { + let repo = create_repository(git2::Repository::init_bare(path).unwrap()); + callback(repo); + }); +} diff --git a/src/test_helpers/with_temp_repository.rs b/src/test_helpers/with_temp_repository.rs new file mode 100644 index 0000000..4e5140d --- /dev/null +++ b/src/test_helpers/with_temp_repository.rs @@ -0,0 +1,20 @@ +use crate::{ + git::Repository, + test_helpers::shared::{create_repository, with_temporary_path}, +}; + +/// Provides a new repository instance in a temporary directory for testing that contains an initial +/// empty commit. +/// +/// # Panics +/// +/// If the repository cannot be created for any reason, this function will panic. +pub(crate) fn with_temp_repository<F>(callback: F) +where F: FnOnce(Repository) { + with_temporary_path(|path| { + let mut opts = git2::RepositoryInitOptions::new(); + _ = opts.initial_head("main"); + let repo = create_repository(git2::Repository::init_opts(path, &opts).unwrap()); + callback(repo); + }); +} |