Make relative-paths work with binary files

`relativize_path_maybe()` was not called in this case. Added
test and converted a few existing ones to insta.
This commit is contained in:
Thomas Otto 2024-07-06 08:48:52 +02:00 committed by Dan Davison
parent 995dc41525
commit 5d538b2303
5 changed files with 84 additions and 21 deletions

1
Cargo.lock generated
View File

@ -652,6 +652,7 @@ dependencies = [
"console", "console",
"lazy_static", "lazy_static",
"linked-hash-map", "linked-hash-map",
"regex",
"similar", "similar",
] ]

View File

@ -68,7 +68,7 @@ default-features = false
features = [] features = []
[dev-dependencies] [dev-dependencies]
insta = { version = "1.*", features = ["colors"] } insta = { version = "1.*", features = ["colors", "filters"] }
[profile.test] [profile.test]
opt-level = 2 opt-level = 2

View File

@ -468,6 +468,8 @@ pub fn get_file_change_description_from_file_paths(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::tests::integration_test_utils::{make_config_from_args, DeltaTest};
use insta::assert_snapshot;
#[test] #[test]
fn test_get_filename_from_marker_line() { fn test_get_filename_from_marker_line() {
@ -637,4 +639,43 @@ mod tests {
Some(".config/Code - Insiders/User/settings.json".to_string()) Some(".config/Code - Insiders/User/settings.json".to_string())
); );
} }
pub const BIN_AND_TXT_FILE_ADDED: &str = "\
diff --git a/BIN b/BIN
new file mode 100644
index 0000000..a5d0c46
Binary files /dev/null and b/BIN differ
diff --git a/TXT b/TXT
new file mode 100644
index 0000000..323fae0
--- /dev/null
+++ b/TXT
@@ -0,0 +1 @@
+plain text";
#[test]
fn test_diff_header_relative_paths() {
// rustfmt ignores the assert macro arguments, so do the setup outside
let mut cfg = make_config_from_args(&["--relative-paths", "-s"]);
cfg.cwd_relative_to_repo_root = Some("src/utils/".into());
let result = DeltaTest::with_config(&cfg)
.with_input(BIN_AND_TXT_FILE_ADDED)
.output;
// convert windows '..\' to unix '../' paths
insta::with_settings!({filters => vec![(r"\.\.\\", "../")]}, {
assert_snapshot!(result, @r###"
added: ../../BIN (binary file)
added: ../../TXT
1:
1 plain text
"###)
});
}
} }

View File

@ -1,4 +1,5 @@
use crate::delta::{DiffType, Source, State, StateMachine}; use crate::delta::{DiffType, Source, State, StateMachine};
use crate::utils::path::relativize_path_maybe;
impl<'a> StateMachine<'a> { impl<'a> StateMachine<'a> {
#[inline] #[inline]
@ -29,9 +30,11 @@ impl<'a> StateMachine<'a> {
} }
if self.minus_file != "/dev/null" { if self.minus_file != "/dev/null" {
relativize_path_maybe(&mut self.minus_file, self.config);
self.minus_file.push_str(" (binary file)"); self.minus_file.push_str(" (binary file)");
} }
if self.plus_file != "/dev/null" { if self.plus_file != "/dev/null" {
relativize_path_maybe(&mut self.plus_file, self.config);
self.plus_file.push_str(" (binary file)"); self.plus_file.push_str(" (binary file)");
} }
return Ok(true); return Ok(true);

View File

@ -8,6 +8,7 @@ mod tests {
use crate::tests::ansi_test_utils::ansi_test_utils; use crate::tests::ansi_test_utils::ansi_test_utils;
use crate::tests::integration_test_utils; use crate::tests::integration_test_utils;
use crate::tests::integration_test_utils::DeltaTest; use crate::tests::integration_test_utils::DeltaTest;
use insta::assert_snapshot;
#[test] #[test]
fn test_added_file() { fn test_added_file() {
@ -126,7 +127,6 @@ mod tests {
"bash", "bash",
]); ]);
let output = integration_test_utils::run_delta(MODIFIED_BASH_AND_CSHARP_FILES, &config); let output = integration_test_utils::run_delta(MODIFIED_BASH_AND_CSHARP_FILES, &config);
eprintln!("{}", &output);
ansi_test_utils::assert_line_has_syntax_highlighted_substring( ansi_test_utils::assert_line_has_syntax_highlighted_substring(
&output, &output,
19, 19,
@ -311,37 +311,55 @@ index 0123456..1234567 100644
#[test] #[test]
fn test_binary_files_differ() { fn test_binary_files_differ() {
let config = let output = DeltaTest::with_args(&["--file-modified-label", "modified:"])
integration_test_utils::make_config_from_args(&["--file-modified-label", "modified:"]); .with_input(BINARY_FILES_DIFFER)
let output = integration_test_utils::run_delta(BINARY_FILES_DIFFER, &config); .skip_header();
let output = strip_ansi_codes(&output);
assert!(output.contains("\nmodified: foo (binary file)\n")); assert_snapshot!(output, @r###"
modified: foo (binary file)
"###);
} }
#[test] #[test]
fn test_binary_file_added() { fn test_binary_file_added() {
let config = integration_test_utils::make_config_from_args(&[]); let output = DeltaTest::with_args(&[])
let output = integration_test_utils::run_delta(BINARY_FILE_ADDED, &config); .with_input(BINARY_FILE_ADDED)
let output = strip_ansi_codes(&output); .skip_header();
assert!(output.contains("\nadded: foo (binary file)\n")); assert_snapshot!(output, @r###"
added: foo (binary file)
"###);
} }
#[test] #[test]
fn test_binary_file_removed() { fn test_binary_file_removed() {
let config = integration_test_utils::make_config_from_args(&[]); let output = DeltaTest::with_args(&[])
let output = integration_test_utils::run_delta(BINARY_FILE_REMOVED, &config); .with_input(BINARY_FILE_REMOVED)
let output = strip_ansi_codes(&output); .skip_header();
assert!(output.contains("\nremoved: foo (binary file)\n")); assert_snapshot!(output, @r###"
removed: foo (binary file)
"###);
} }
#[test] #[test]
fn test_binary_files_differ_after_other() { fn test_binary_files_differ_after_other() {
let config = let output = DeltaTest::with_args(&["--file-modified-label", "modified:"])
integration_test_utils::make_config_from_args(&["--file-modified-label", "modified:"]); .with_input(BINARY_FILES_DIFFER_AFTER_OTHER)
let output = integration_test_utils::run_delta(BINARY_FILES_DIFFER_AFTER_OTHER, &config); .output;
let output = strip_ansi_codes(&output); assert_snapshot!(output, @r###"
assert!(output.contains("\nrenamed: foo ⟶ bar\n"));
assert!(output.contains("\nmodified: qux (binary file)\n"));
renamed: foo bar
modified: qux (binary file)
"###);
} }
#[test] #[test]