diff options
author | Dan Davison <dandavison7@gmail.com> | 2022-01-17 15:28:13 -0500 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2022-01-17 18:30:32 -0500 |
commit | fb38eddd9a8b6faa079ff8bdac2affc3b7c3f2b3 (patch) | |
tree | 619a9bba35461c7f1684b51e74c12058bfecdaae | |
parent | 1f80503a56eab8b287f9eaf5263dd7256574ccaf (diff) |
Add failing test of file hyperlink in git repo
-rw-r--r-- | src/features/hyperlinks.rs | 57 | ||||
-rw-r--r-- | src/handlers/hunk_header.rs | 1 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/features/hyperlinks.rs b/src/features/hyperlinks.rs index 6637734c..ef85211e 100644 --- a/src/features/hyperlinks.rs +++ b/src/features/hyperlinks.rs @@ -110,3 +110,60 @@ fn format_commit_line_captures_with_osc8_commit_hyperlink( fn format_github_commit_url(commit: &str, github_repo: &str) -> String { format!("https://github.com/{}/commit/{}", github_repo, commit) } + +#[cfg(test)] +pub mod tests { + use std::path::PathBuf; + + use super::*; + use crate::tests::integration_test_utils; + + fn assert_file_hyperlink_matches( + relative_path: &str, + expected_hyperlink_path: &str, + config: &Config, + ) { + let link_text = "link text"; + assert_eq!( + format_osc8_hyperlink( + &PathBuf::from(expected_hyperlink_path).to_string_lossy(), + link_text + ), + format_osc8_file_hyperlink(relative_path, None, link_text, config) + ) + } + + #[test] + #[cfg(not(target_os = "windows"))] + fn test_relative_path_file_hyperlink_when_not_child_process_of_git() { + // The current process is not a child process of git. + // Delta receives a file path 'a'. + // The hyperlink should be $cwd/a. + let mut config = integration_test_utils::make_config_from_args(&[ + "--hyperlinks", + "--hyperlinks-file-link-format", + "{path}", + ]); + config.cwd_relative_to_repo_root = None; // Not set because we were not invoked by git + config.cwd = Some(PathBuf::from("/some/cwd")); + assert_file_hyperlink_matches("a", "/some/cwd/a", &config) + } + + #[test] + #[cfg(not(target_os = "windows"))] + fn test_relative_path_file_hyperlink_when_child_process_of_git() { + // The current process is a child process of git. + // Delta receives a file path 'a'. + // We are in directory b/ relative to the repo root. + // The hyperlink should be $repo_root/b/a. + let mut config = integration_test_utils::make_config_from_args(&[ + "--hyperlinks", + "--hyperlinks-file-link-format", + "{path}", + ]); + config.cwd_relative_to_repo_root = None; // Not set because we were not invoked by git + config.cwd = Some("/some/repo-root".into()); // Git invokes delta from the repo root + config.cwd_relative_to_repo_root = Some("b".into()); // Git preserves the user's directory in the GIT_PREFIX env var + assert_file_hyperlink_matches("a", "/some/repo-root/b/a", &config) + } +} diff --git a/src/handlers/hunk_header.rs b/src/handlers/hunk_header.rs index bb6ad8dd..f1d90900 100644 --- a/src/handlers/hunk_header.rs +++ b/src/handlers/hunk_header.rs @@ -400,6 +400,7 @@ pub mod tests { } #[test] + #[cfg(not(target_os = "windows"))] fn test_paint_file_path_with_line_number_hyperlinks() { // hunk-header-style (by default) includes 'line-number' but not 'file'. // Normally, `paint_file_path_with_line_number` would return a painted line number. |