Skip to content

Commit

Permalink
Make relative-paths work with binary files
Browse files Browse the repository at this point in the history
`relativize_path_maybe()` was not called in this case. Added test.
  • Loading branch information
th1000s committed Jul 6, 2024
1 parent bf0f771 commit fd7d03b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/handlers/diff_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ pub fn get_file_change_description_from_file_paths(

#[cfg(test)]
mod tests {
use insta::assert_snapshot;
use crate::tests::integration_test_utils::{make_config_from_args, DeltaTest};
use super::*;

#[test]
Expand Down Expand Up @@ -637,4 +639,42 @@ mod tests {
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(&["--width=40", "--relative-paths"]);
cfg.cwd_relative_to_repo_root = Some("src/".into());
let result =
DeltaTest::with_config(&cfg)
.with_input(BIN_AND_TXT_FILE_ADDED)
.output;
assert_snapshot!(result, @r###"
added: ../BIN (binary file)
────────────────────────────────────────
added: ../TXT
────────────────────────────────────────
───┐
1: │
───┘
plain text
"###
);
}
}
3 changes: 3 additions & 0 deletions src/handlers/diff_header_misc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::delta::{DiffType, Source, State, StateMachine};
use crate::utils::path::relativize_path_maybe;

impl<'a> StateMachine<'a> {
#[inline]
Expand Down Expand Up @@ -29,9 +30,11 @@ impl<'a> StateMachine<'a> {
}

if self.minus_file != "/dev/null" {
relativize_path_maybe(&self.config, &mut self.minus_file);
self.minus_file.push_str(" (binary file)");
}
if self.plus_file != "/dev/null" {
relativize_path_maybe(&self.config, &mut self.plus_file);
self.plus_file.push_str(" (binary file)");
}
return Ok(true);
Expand Down
1 change: 0 additions & 1 deletion src/tests/test_example_diffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ mod tests {
"bash",
]);
let output = integration_test_utils::run_delta(MODIFIED_BASH_AND_CSHARP_FILES, &config);
eprintln!("{}", &output);
ansi_test_utils::assert_line_has_syntax_highlighted_substring(
&output,
19,
Expand Down

0 comments on commit fd7d03b

Please sign in to comment.