diff options
author | Tim Oram <dev@mitmaro.ca> | 2024-02-12 09:18:24 -0330 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2024-02-15 20:27:06 -0330 |
commit | fb22b8da99e1c8bf3b787844be8af3232d9c3aec (patch) | |
tree | 54cf163dbc8583286a059e443cd561297f60e2f8 | |
parent | f3784a9f671e4a21cd99b5a48caaefd3843f129c (diff) |
Move git builder test utils to test_helpers
-rw-r--r-- | src/git/commit.rs | 13 | ||||
-rw-r--r-- | src/git/commit_diff.rs | 12 | ||||
-rw-r--r-- | src/git/commit_diff_loader.rs | 6 | ||||
-rw-r--r-- | src/git/reference.rs | 4 | ||||
-rw-r--r-- | src/git/reference_kind.rs | 2 | ||||
-rw-r--r-- | src/git/repository.rs | 4 | ||||
-rw-r--r-- | src/git/testutil.rs | 44 | ||||
-rw-r--r-- | src/git/testutil/create_commit.rs | 18 | ||||
-rw-r--r-- | src/git/testutil/with_temp_repository.rs | 34 | ||||
-rw-r--r-- | src/modules/show_commit/tests.rs | 15 | ||||
-rw-r--r-- | src/test_helpers.rs | 3 | ||||
-rw-r--r-- | src/test_helpers/builders.rs | 11 | ||||
-rw-r--r-- | src/test_helpers/builders/commit.rs (renamed from src/git/testutil/build_commit.rs) | 5 | ||||
-rw-r--r-- | src/test_helpers/builders/commit_diff.rs (renamed from src/git/testutil/build_commit_diff.rs) | 0 | ||||
-rw-r--r-- | src/test_helpers/builders/file_status.rs (renamed from src/git/testutil/build_file_status.rs) | 0 | ||||
-rw-r--r-- | src/test_helpers/builders/reference.rs (renamed from src/git/testutil/build_reference.rs) | 0 | ||||
-rw-r--r-- | src/test_helpers/git.rs | 0 |
17 files changed, 59 insertions, 112 deletions
diff --git a/src/git/commit.rs b/src/git/commit.rs index 4fe45c6..0d5cea5 100644 --- a/src/git/commit.rs +++ b/src/git/commit.rs @@ -119,13 +119,12 @@ mod tests { use testutils::assert_err_eq; use super::*; - use crate::git::testutil::{ - create_commit, - with_temp_repository, - CommitBuilder, - CreateCommitOptions, - ReferenceBuilder, - JAN_2021_EPOCH, + use crate::{ + git::testutil::{create_commit, with_temp_repository, CreateCommitOptions}, + test_helpers::{ + builders::{CommitBuilder, ReferenceBuilder}, + JAN_2021_EPOCH, + }, }; #[test] diff --git a/src/git/commit_diff.rs b/src/git/commit_diff.rs index 243e582..46d0d8c 100644 --- a/src/git/commit_diff.rs +++ b/src/git/commit_diff.rs @@ -53,15 +53,9 @@ impl CommitDiff { mod tests { use claims::assert_some_eq; - use crate::git::{ - testutil::{CommitBuilder, CommitDiffBuilder}, - Delta, - DiffLine, - FileMode, - FileStatus, - FileStatusBuilder, - Origin, - Status, + use crate::{ + git::{Delta, DiffLine, FileMode, FileStatus, FileStatusBuilder, Origin, Status}, + test_helpers::builders::{CommitBuilder, CommitDiffBuilder}, }; #[test] diff --git a/src/git/commit_diff_loader.rs b/src/git/commit_diff_loader.rs index ff67d0f..a43472f 100644 --- a/src/git/commit_diff_loader.rs +++ b/src/git/commit_diff_loader.rs @@ -560,7 +560,7 @@ mod tests { let mut permissions = file.metadata().unwrap().permissions(); permissions.set_mode(0o755); file.set_permissions(permissions).unwrap(); - add_path_to_index(&repo, PathBuf::from("a").as_path()); + repo.add_path_to_index(PathBuf::from("a").as_path()).unwrap(); create_commit(&repo); let diff = diff_from_head(&repo, &CommitDiffLoaderOptions::new().renames(true, 100)); assert_eq!(diff.number_files_changed(), 1); @@ -581,8 +581,8 @@ mod tests { create_commit(&repo); remove_path(&repo, "a"); symlink(root.join("b"), root.join("a")).unwrap(); - add_path_to_index(&repo, PathBuf::from("a").as_path()); - add_path_to_index(&repo, PathBuf::from("b").as_path()); + repo.add_path_to_index(PathBuf::from("a").as_path()).unwrap(); + repo.add_path_to_index(PathBuf::from("b").as_path()).unwrap(); create_commit(&repo); let diff = diff_from_head(&repo, &CommitDiffLoaderOptions::new()); assert_eq!(diff.number_files_changed(), 1); diff --git a/src/git/reference.rs b/src/git/reference.rs index 804ab1b..ac1789b 100644 --- a/src/git/reference.rs +++ b/src/git/reference.rs @@ -59,12 +59,12 @@ impl Reference { #[cfg(test)] mod tests { use super::*; - use crate::git::testutil::{head_id, with_temp_repository}; + use crate::git::testutil::with_temp_repository; #[test] fn test() { with_temp_repository(|repository| { - let oid = head_id(&repository, "main"); + let oid = repository.head_id("main").unwrap(); let reference = repository.find_reference("refs/heads/main").unwrap(); assert_eq!(reference.hash(), format!("{oid}")); assert_eq!(reference.name(), "refs/heads/main"); diff --git a/src/git/reference_kind.rs b/src/git/reference_kind.rs index f3db0b6..d9eaa64 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, JAN_2021_EPOCH}; + use crate::{git::testutil::with_temp_repository, test_helpers::JAN_2021_EPOCH}; #[test] fn from_git2_reference_branch() { diff --git a/src/git/repository.rs b/src/git/repository.rs index b542d14..545390d 100644 --- a/src/git/repository.rs +++ b/src/git/repository.rs @@ -193,7 +193,7 @@ mod tests { use testutils::assert_err_eq; use super::*; - use crate::git::testutil::{commit_id_from_ref, create_commit, with_temp_bare_repository, with_temp_repository}; + use crate::git::testutil::{create_commit, with_temp_bare_repository, with_temp_repository}; #[test] #[serial_test::serial] @@ -268,7 +268,7 @@ mod tests { fn load_commit_diff() { with_temp_repository(|repository| { create_commit(&repository, None); - let id = commit_id_from_ref(&repository, "refs/heads/main"); + let id = repository.commit_id_from_ref("refs/heads/main").unwrap(); assert_ok!(repository.load_commit_diff(id.to_string().as_str(), &CommitDiffLoaderOptions::new())); }); } diff --git a/src/git/testutil.rs b/src/git/testutil.rs index dbb5842..0d869a5 100644 --- a/src/git/testutil.rs +++ b/src/git/testutil.rs @@ -1,49 +1,7 @@ -#![cfg(not(tarpaulin_include))] - -//! Utilities for writing tests that interact with Git. -mod build_commit; -mod build_commit_diff; -mod build_file_status; -mod build_reference; mod create_commit; mod with_temp_repository; -use std::path::PathBuf; - -use git2::Oid; - pub(crate) use self::{ - build_commit::CommitBuilder, - build_commit_diff::CommitDiffBuilder, - build_file_status::FileStatusBuilder, - build_reference::ReferenceBuilder, - create_commit::{add_path_to_index, create_commit, remove_path_from_index, CreateCommitOptions}, + create_commit::{create_commit, CreateCommitOptions}, with_temp_repository::{with_temp_bare_repository, with_temp_repository}, }; -use crate::git::Repository; - -pub(crate) static JAN_2021_EPOCH: i64 = 1_609_459_200; - -/// Get the the path to the repository. -#[must_use] -pub(crate) fn repo_path(repo: &Repository) -> PathBuf { - repo.repo_path() -} - -/// Get the Oid of provided head reference name. -/// -/// # Panics -/// If the head id cannot be queried. -#[must_use] -pub(crate) fn head_id(repo: &Repository, head_name: &str) -> Oid { - repo.head_id(head_name).unwrap() -} - -/// Get the Commit Oid from a reference name. -/// -/// # Panics -/// If the head id cannot be queried. -#[must_use] -pub(crate) fn commit_id_from_ref(repo: &Repository, reference: &str) -> Oid { - repo.commit_id_from_ref(reference).unwrap() -} diff --git a/src/git/testutil/create_commit.rs b/src/git/testutil/create_commit.rs index fc7644a..9b1062e 100644 --- a/src/git/testutil/create_commit.rs +++ b/src/git/testutil/create_commit.rs @@ -4,7 +4,7 @@ use std::path::Path; use lazy_static::lazy_static; -use crate::git::{testutil::JAN_2021_EPOCH, Repository}; +use crate::{git::Repository, test_helpers::JAN_2021_EPOCH}; lazy_static! { static ref DEFAULT_COMMIT_OPTIONS: CreateCommitOptions = CreateCommitOptions::new(); @@ -102,22 +102,6 @@ impl CreateCommitOptions { } } -/// Add a path to the working index. -/// -/// # Panics -/// If the path cannot be added to the index. -pub(crate) fn add_path_to_index(repo: &Repository, path: &Path) { - repo.add_path_to_index(path).unwrap(); -} - -/// Remove a path to the working index. -/// -/// # Panics -/// If the path cannot be removed from the index. -pub(crate) fn remove_path_from_index(repo: &Repository, path: &Path) { - repo.remove_path_from_index(path).unwrap(); -} - /// Create a commit based on the provided options. If `options` is not provided, will create a /// commit using the default options. This function does not add modified or new files to the stage /// before creating a commit. diff --git a/src/git/testutil/with_temp_repository.rs b/src/git/testutil/with_temp_repository.rs index b9c741b..33084c8 100644 --- a/src/git/testutil/with_temp_repository.rs +++ b/src/git/testutil/with_temp_repository.rs @@ -4,9 +4,9 @@ use std::path::Path; use tempfile::Builder; -use crate::git::{testutil::JAN_2021_EPOCH, Repository}; +use crate::{git::Repository, test_helpers::JAN_2021_EPOCH}; -pub(crate) fn with_temporary_path<F>(callback: F) +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(); @@ -14,6 +14,18 @@ where F: FnOnce(&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. /// @@ -25,17 +37,8 @@ where F: FnOnce(Repository) { with_temporary_path(|path| { let mut opts = git2::RepositoryInitOptions::new(); _ = opts.initial_head("main"); - let repo = git2::Repository::init_opts(path, &opts).unwrap(); - - { - 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(); - }; - callback(Repository::from(repo)); + let repo = create_repository_from_git2_repo(git2::Repository::init_opts(path, &opts).unwrap()); + callback(repo); }); } @@ -48,8 +51,7 @@ where F: FnOnce(Repository) { pub(crate) fn with_temp_bare_repository<F>(callback: F) where F: FnOnce(Repository) { with_temporary_path(|path| { - let git2_repository = git2::Repository::init_bare(path).unwrap(); - let repository = Repository::from(git2_repository); - callback(repository); + let repo = create_repository_from_git2_repo(git2::Repository::init_bare(path).unwrap()); + callback(repo); }); } diff --git a/src/modules/show_commit/tests.rs b/src/modules/show_commit/tests.rs index 18dbb5c..fa10d39 100644 --- a/src/modules/show_commit/tests.rs +++ b/src/modules/show_commit/tests.rs @@ -5,18 +5,11 @@ use super::*; use crate::{ assert_rendered_output, assert_results, - git::{ - testutil::{head_id, with_temp_repository, CommitBuilder, CommitDiffBuilder, FileStatusBuilder}, - Delta, - DiffLine, - FileMode, - Origin, - Status, - User, - }, + git::{testutil::with_temp_repository, Delta, DiffLine, FileMode, Origin, Status, User}, input::StandardEvent, process::Artifact, render_line, + test_helpers::builders::{CommitBuilder, CommitDiffBuilder, FileStatusBuilder}, testutil::module_test, view::{testutil::AssertRenderOptions, ViewLine}, }; @@ -32,7 +25,7 @@ fn render_options() -> AssertRenderOptions { #[test] fn load_commit_during_activate() { with_temp_repository(|repo| { - let oid = head_id(&repo, "main"); + let oid = repo.head_id("main").unwrap(); let line = format!("pick {oid} comment1"); module_test(&[line.as_str()], &[], |mut test_context| { let mut module = create_show_commit(&Config::new(), repo, test_context.take_todo_file()); @@ -45,7 +38,7 @@ fn load_commit_during_activate() { #[test] fn cached_commit_in_activate() { with_temp_repository(|repo| { - let oid = head_id(&repo, "main"); + let oid = repo.head_id("main").unwrap(); let line = format!("pick {oid} comment1"); module_test(&[line.as_str()], &[], |mut test_context| { let mut module = create_show_commit(&Config::new(), repo, test_context.take_todo_file()); diff --git a/src/test_helpers.rs b/src/test_helpers.rs index a353ef4..81f04a9 100644 --- a/src/test_helpers.rs +++ b/src/test_helpers.rs @@ -1,5 +1,8 @@ +pub(crate) mod builders; mod create_invalid_utf; pub(crate) mod mocks; mod with_git_config; +pub(crate) static JAN_2021_EPOCH: i64 = 1_609_459_200; + pub(crate) use self::{create_invalid_utf::invalid_utf, with_git_config::with_git_config}; diff --git a/src/test_helpers/builders.rs b/src/test_helpers/builders.rs new file mode 100644 index 0000000..6292a53 --- /dev/null +++ b/src/test_helpers/builders.rs @@ -0,0 +1,11 @@ +mod commit; +mod commit_diff; +mod file_status; +mod reference; + +pub(crate) use self::{ + commit::CommitBuilder, + commit_diff::CommitDiffBuilder, + file_status::FileStatusBuilder, + reference::ReferenceBuilder, +}; diff --git a/src/git/testutil/build_commit.rs b/src/test_helpers/builders/commit.rs index 2778b35..afa15d6 100644 --- a/src/git/testutil/build_commit.rs +++ b/src/test_helpers/builders/commit.rs @@ -1,6 +1,9 @@ use chrono::{Local, TimeZone}; -use crate::git::{testutil::JAN_2021_EPOCH, Commit, Reference, User}; +use crate::{ + git::{Commit, Reference, User}, + test_helpers::JAN_2021_EPOCH, +}; /// Builder for creating a new commit. #[derive(Debug)] diff --git a/src/git/testutil/build_commit_diff.rs b/src/test_helpers/builders/commit_diff.rs index 1c97815..1c97815 100644 --- a/src/git/testutil/build_commit_diff.rs +++ b/src/test_helpers/builders/commit_diff.rs diff --git a/src/git/testutil/build_file_status.rs b/src/test_helpers/builders/file_status.rs index 35cd434..35cd434 100644 --- a/src/git/testutil/build_file_status.rs +++ b/src/test_helpers/builders/file_status.rs diff --git a/src/git/testutil/build_reference.rs b/src/test_helpers/builders/reference.rs index 58ba2e9..58ba2e9 100644 --- a/src/git/testutil/build_reference.rs +++ b/src/test_helpers/builders/reference.rs diff --git a/src/test_helpers/git.rs b/src/test_helpers/git.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/test_helpers/git.rs |