From db87e144abd7d83330f02e7577adef4da9893d10 Mon Sep 17 00:00:00 2001 From: Lucio Sauer Date: Mon, 1 May 2023 14:57:03 +0200 Subject: [PATCH 1/2] Fix 050568f1: run unit tests in generated git repo test_info_style_info() and test_info_style_subtitle() fail with "Error: Could not find a git repository in '.' or in any of its parents" if the current working directory is not a git repository. * Provide the git repository setup function as a opt-in library utility. Deduplicate the repo setup for benchmarking, integration tests and unit tests. --- Cargo.lock | 1 + Cargo.toml | 9 ++++++++- benches/repo.rs | 6 ++---- src/info/mod.rs | 9 +++++++++ src/info/title.rs | 12 +++--------- src/lib.rs | 4 ++++ src/utils.rs | 9 +++++++++ tests/repo.rs | 9 +-------- 8 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 src/utils.rs 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..fa751fd6d 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::{cli::CliOptions, info::Info}; +use onefetch::utils::repo; 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..51d52344b 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..fcb01a483 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 anyhow::Result; - use gix::{open, Repository, ThreadSafeRepository}; + use crate::utils::repo; + use super::*; 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<()> { From de8a90396251516cc6952c7ea06b908e47ed4d42 Mon Sep 17 00:00:00 2001 From: Lucio Sauer Date: Mon, 1 May 2023 22:38:00 +0200 Subject: [PATCH 2/2] Chore: Fix formatting via `cargo fmt` --- benches/repo.rs | 2 +- src/info/mod.rs | 8 ++++---- src/info/title.rs | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/benches/repo.rs b/benches/repo.rs index fa751fd6d..b7158b2bc 100644 --- a/benches/repo.rs +++ b/benches/repo.rs @@ -1,6 +1,6 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use onefetch::{cli::CliOptions, info::Info}; use onefetch::utils::repo; +use onefetch::{cli::CliOptions, info::Info}; fn bench_repo_info(c: &mut Criterion) { let repo = repo("repo.sh").unwrap(); diff --git a/src/info/mod.rs b/src/info/mod.rs index 51d52344b..b2691d229 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -354,9 +354,9 @@ mod tests { #[test] #[cfg(feature = "test-utils")] fn test_info_style_info() -> Result<()> { - let repo = repo("basic_repo.sh")?; + let repo = repo("basic_repo.sh")?; let config: CliOptions = CliOptions { - input: repo.path().to_path_buf(), + input: repo.path().to_path_buf(), text_formatting: TextForamttingCliOptions { text_colors: vec![0, 0, 0, 0, 0, 0], ..Default::default() @@ -378,9 +378,9 @@ mod tests { #[test] #[cfg(feature = "test-utils")] fn test_info_style_subtitle() -> Result<()> { - let repo = repo("basic_repo.sh")?; + let repo = repo("basic_repo.sh")?; let config: CliOptions = CliOptions { - input: repo.path().to_path_buf(), + 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 fcb01a483..3469f43e3 100644 --- a/src/info/title.rs +++ b/src/info/title.rs @@ -86,9 +86,9 @@ impl std::fmt::Display for Title { #[cfg(test)] #[cfg(feature = "test-utils")] mod tests { - use anyhow::Result; - use crate::utils::repo; use super::*; + use crate::utils::repo; + use anyhow::Result; use owo_colors::AnsiColors; #[test]