diff --git a/Cargo.lock b/Cargo.lock index 1e42fd424..e3184c596 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2481,6 +2481,7 @@ dependencies = [ "insta", "lazy_static", "num-format", + "onefetch", "onefetch-ascii", "onefetch-image", "onefetch-manifest", diff --git a/Cargo.toml b/Cargo.toml index 40555c4e5..7d562969f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ gix-features-for-configuration-only = { package = "gix-features", version = "0.2 gix = { version = "0.44.1", default-features = false, features = [ "max-performance-safe", ] } +gix-testtools = { version = "0.11.0", optional = true } git2 = { version = "0.17.1", default-features = false } human-panic = "1.1.4" image = "0.24.6" @@ -52,13 +53,18 @@ yaml-rust = "0.4.5" [dev-dependencies] criterion = "0.4.0" -gix-testtools = "0.11.0" insta = { version = "1.29.0", features = ["json", "redactions"] } +onefetch = { path = ".", features = ["test-utils"] } pretty_assertions = "1.3.0" [[bench]] name = "repo" harness = false +required-features = ["test-utils"] + +[[test]] +name = "repo" +required-features = ["test-utils"] [build-dependencies] lazy_static = "1" @@ -75,3 +81,4 @@ enable-ansi-support = "0.2.1" [features] fail-on-deprecated = [] +test-utils = ["gix-testtools"] diff --git a/benches/repo.rs b/benches/repo.rs index d1cc06960..b7158b2bc 100644 --- a/benches/repo.rs +++ b/benches/repo.rs @@ -1,11 +1,9 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use gix::{open, ThreadSafeRepository}; +use onefetch::utils::repo; use onefetch::{cli::CliOptions, info::Info}; fn bench_repo_info(c: &mut Criterion) { - let name = "repo.sh".to_string(); - let repo_path = gix_testtools::scripted_fixture_read_only(name).unwrap(); - let repo = ThreadSafeRepository::open_opts(repo_path, open::Options::isolated()).unwrap(); + let repo = repo("repo.sh").unwrap(); let config: CliOptions = CliOptions { input: repo.path().to_path_buf(), ..Default::default() diff --git a/src/info/mod.rs b/src/info/mod.rs index d8f09f31e..b2691d229 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -309,7 +309,10 @@ fn get_style(is_bold: bool, color: DynColors) -> Style { #[cfg(test)] mod tests { + #[cfg(feature = "test-utils")] use crate::cli::TextForamttingCliOptions; + #[cfg(feature = "test-utils")] + use crate::utils::repo; use super::*; use owo_colors::AnsiColors; @@ -349,8 +352,11 @@ mod tests { } #[test] + #[cfg(feature = "test-utils")] fn test_info_style_info() -> Result<()> { + let repo = repo("basic_repo.sh")?; let config: CliOptions = CliOptions { + input: repo.path().to_path_buf(), text_formatting: TextForamttingCliOptions { text_colors: vec![0, 0, 0, 0, 0, 0], ..Default::default() @@ -370,8 +376,11 @@ mod tests { } #[test] + #[cfg(feature = "test-utils")] fn test_info_style_subtitle() -> Result<()> { + let repo = repo("basic_repo.sh")?; let config: CliOptions = CliOptions { + input: repo.path().to_path_buf(), text_formatting: TextForamttingCliOptions { text_colors: vec![0, 0, 0, 0, 15, 0], no_bold: false, diff --git a/src/info/title.rs b/src/info/title.rs index a6b23b6ff..3469f43e3 100644 --- a/src/info/title.rs +++ b/src/info/title.rs @@ -84,19 +84,13 @@ impl std::fmt::Display for Title { } } #[cfg(test)] +#[cfg(feature = "test-utils")] mod tests { use super::*; + use crate::utils::repo; use anyhow::Result; - use gix::{open, Repository, ThreadSafeRepository}; use owo_colors::AnsiColors; - fn repo(name: &str) -> Result { - let name = name.to_string(); - let repo_path = gix_testtools::scripted_fixture_read_only(name).unwrap(); - let safe_repo = ThreadSafeRepository::open_opts(repo_path, open::Options::isolated())?; - Ok(safe_repo.to_thread_local()) - } - #[test] fn test_get_git_username() -> Result<()> { let repo = repo("basic_repo.sh")?; diff --git a/src/lib.rs b/src/lib.rs index 24ff2a9fc..7cb21c91b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,3 +2,7 @@ pub mod cli; pub mod info; pub mod ui; +// Provide the git repo setup function for benchmarks, integration tests and +// some unit tests via a library module to avoid code duplication. +#[cfg(feature = "test-utils")] +pub mod utils; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 000000000..b199b5ceb --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,9 @@ +use anyhow::Result; +use gix::{open, Repository, ThreadSafeRepository}; + +pub fn repo(name: &str) -> Result { + let name = name.to_string(); + let repo_path = gix_testtools::scripted_fixture_read_only(name).unwrap(); + let safe_repo = ThreadSafeRepository::open_opts(repo_path, open::Options::isolated())?; + Ok(safe_repo.to_thread_local()) +} diff --git a/tests/repo.rs b/tests/repo.rs index c6750505b..8e1c6814c 100644 --- a/tests/repo.rs +++ b/tests/repo.rs @@ -1,14 +1,7 @@ use anyhow::Result; -use gix::{open, Repository, ThreadSafeRepository}; use onefetch::cli::{CliOptions, TextForamttingCliOptions}; use onefetch::info::{get_work_dir, Info}; - -fn repo(name: &str) -> Result { - let name = name.to_string(); - let repo_path = gix_testtools::scripted_fixture_read_only(name).unwrap(); - let safe_repo = ThreadSafeRepository::open_opts(repo_path, open::Options::isolated())?; - Ok(safe_repo.to_thread_local()) -} +use onefetch::utils::repo; #[test] fn test_bare_repo() -> Result<()> {