diff options
| author | Thomas Otto <th1000s@posteo.net> | 2024-11-14 23:22:24 +0100 |
|---|---|---|
| committer | Thomas Otto <th1000s@posteo.net> | 2024-11-20 23:45:55 +0100 |
| commit | 4ea8f9ab6038b934a03c6370e5c3e45da0466fe5 (patch) | |
| tree | 3768eed6dd0e0b866280780fb4a5735da14582aa | |
| parent | 1dd28e7b51a913fc28eefc59d01f3b78f1146850 (diff) | |
testability: add pretty_assertions, git mocks
| -rw-r--r-- | Cargo.lock | 23 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/features/hyperlinks.rs | 14 | ||||
| -rw-r--r-- | src/git_config/mod.rs | 14 |
4 files changed, 50 insertions, 2 deletions
@@ -384,6 +384,12 @@ dependencies = [ ] [[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + +[[package]] name = "dirs" version = "5.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -615,6 +621,7 @@ dependencies = [ "lazy_static", "palette", "pathdiff", + "pretty_assertions", "regex", "rstest", "serde", @@ -1078,6 +1085,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" [[package]] +name = "pretty_assertions" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d" +dependencies = [ + "diff", + "yansi", +] + +[[package]] name = "proc-macro-crate" version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1932,3 +1949,9 @@ checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" dependencies = [ "linked-hash-map", ] + +[[package]] +name = "yansi" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" @@ -64,6 +64,7 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tarpaulin_include)'] } [dev-dependencies] insta = { version = "1.*", features = ["colors", "filters"] } rstest = "0.21.0" +pretty_assertions = "1.4" [profile.test] opt-level = 2 diff --git a/src/features/hyperlinks.rs b/src/features/hyperlinks.rs index e0a354b..ca21eb3 100644 --- a/src/features/hyperlinks.rs +++ b/src/features/hyperlinks.rs @@ -19,6 +19,18 @@ pub fn make_feature() -> Vec<(String, OptionValueFunction)> { ]) } +#[cfg(test)] +pub fn remote_from_config(_: &Option<&GitConfig>) -> Option<GitRemoteRepo> { + Some(GitRemoteRepo::GitHub { + slug: "dandavison/delta".to_string(), + }) +} + +#[cfg(not(test))] +pub fn remote_from_config(cfg: &Option<&GitConfig>) -> Option<GitRemoteRepo> { + cfg.and_then(GitConfig::get_remote_url) +} + pub fn format_commit_line_with_osc8_commit_hyperlink<'a>( line: &'a str, config: &Config, @@ -32,7 +44,7 @@ pub fn format_commit_line_with_osc8_commit_hyperlink<'a>( format_osc8_hyperlink(&commit_link_format.replace("{commit}", commit), commit); format!("{prefix}{formatted_commit}{suffix}") }) - } else if let Some(repo) = config.git_config().and_then(GitConfig::get_remote_url) { + } else if let Some(repo) = remote_from_config(&config.git_config()) { COMMIT_LINE_REGEX.replace(line, |captures: &Captures| { format_commit_line_captures_with_osc8_commit_hyperlink(captures, &repo) }) diff --git a/src/git_config/mod.rs b/src/git_config/mod.rs index 22fc604..fc59a84 100644 --- a/src/git_config/mod.rs +++ b/src/git_config/mod.rs @@ -6,7 +6,6 @@ use crate::env::DeltaEnv; use regex::Regex; use std::collections::HashMap; use std::path::Path; -use std::str::FromStr; use lazy_static::lazy_static; @@ -70,6 +69,17 @@ impl GitConfig { None } + #[cfg(test)] + pub fn for_testing() -> Option<Self> { + Some(GitConfig { + config: git2::Config::new().unwrap(), + config_from_env_var: HashMap::new(), + enabled: true, + repo: None, + path: std::path::PathBuf::from("/invalid_null.git"), + }) + } + pub fn from_path(env: &DeltaEnv, path: &Path, honor_env_var: bool) -> Self { use crate::fatal; @@ -109,7 +119,9 @@ impl GitConfig { } } + #[cfg(not(test))] pub fn get_remote_url(&self) -> Option<GitRemoteRepo> { + use std::str::FromStr; self.repo .as_ref()? .find_remote("origin") |
