diff options
Diffstat (limited to 'src/tests/test_example_diffs.rs')
-rw-r--r-- | src/tests/test_example_diffs.rs | 223 |
1 files changed, 213 insertions, 10 deletions
diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index a3df1e36..c686924f 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -96,6 +96,80 @@ mod tests { } #[test] + fn test_default_language_is_used_for_syntax_highlighting() { + // Note: default-language will be used for files with no extension, but also + // for files with an extension, but for which the language was not detected. + // Use color-only so that we can refer to the line numbers from the input diff. + let config = integration_test_utils::make_config_from_args(&[ + "--color-only", + "--default-language", + "bash", + ]); + let output = integration_test_utils::run_delta(MODIFIED_BASH_AND_CSHARP_FILES, &config); + ansi_test_utils::assert_line_has_syntax_highlighted_substring( + &output, + 12, + 1, + " rsync -avu --delete $src/ $dst", + "abc.bash", + State::HunkZero(DiffType::Unified, None), + &config, + ); + } + + #[test] + fn test_default_language_is_not_used_when_other_language_is_detected() { + // Use color-only so that we can refer to the line numbers from the input diff. + let config = integration_test_utils::make_config_from_args(&[ + "--color-only", + "--default-language", + "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, + 1, + " static void Main(string[] args)", + "abc.cs", + State::HunkZero(DiffType::Unified, None), + &config, + ); + } + + #[test] + fn test_full_filename_used_to_detect_language() { + let config = integration_test_utils::make_config_from_args(&[ + "--color-only", + "--default-language", + "txt", + ]); + let output = integration_test_utils::run_delta(MODIFIED_DOCKER_AND_RS_FILES, &config); + let ansi = ansi::explain_ansi(&output, false); + + // Ensure presence and absence of highlighting. Do not use `assert_line_has_syntax_highlighted_substring` + // because it uses the same code path as the one to be tested here. + let expected = r"(normal)diff --git a/Dockerfile b/Dockerfile +index 0123456..1234567 100644 +--- a/Dockerfile ++++ b/Dockerfile +@@ -0,0 +2 @@ +(normal 22)+(203)FROM(231) foo(normal) +(normal 22)+(203)COPY(231) bar baz(normal) +diff --git a/rs b/rs +index 0123456..1234567 100644 +--- a/rs ++++ b/rs +@@ -0,0 +2 @@ +(normal 22)+(231)fn foobar() -> i8 {(normal) +(normal 22)+(231) 8(normal) +(normal 22)+(231)}(normal) +"; + assert_eq!(expected, ansi); + } + + #[test] fn test_diff_unified_two_files() { let config = integration_test_utils::make_config_from_args(&["--file-modified-label", "comparing:"]); @@ -154,7 +228,7 @@ mod tests { #[test] fn test_certain_bugs_are_not_present() { - for input in vec![ + for input in [ DIFF_EXHIBITING_PARSE_FILE_NAME_BUG, DIFF_EXHIBITING_STATE_MACHINE_PARSER_BUG, DIFF_EXHIBITING_TRUNCATION_BUG, @@ -168,7 +242,7 @@ mod tests { #[test] fn test_delta_paints_diff_when_there_is_unrecognized_initial_content() { - for input in vec![ + for input in [ DIFF_WITH_UNRECOGNIZED_PRECEDING_MATERIAL_1, DIFF_WITH_UNRECOGNIZED_PRECEDING_MATERIAL_2, ] { @@ -237,10 +311,11 @@ mod tests { #[test] fn test_binary_files_differ() { - let config = integration_test_utils::make_config_from_args(&[]); + let config = + integration_test_utils::make_config_from_args(&["--file-modified-label", "modified:"]); let output = integration_test_utils::run_delta(BINARY_FILES_DIFFER, &config); let output = strip_ansi_codes(&output); - assert!(output.contains("Binary files a/foo and b/foo differ\n")); + assert!(output.contains("\nmodified: foo (binary file)\n")); } #[test] @@ -248,7 +323,7 @@ mod tests { let config = integration_test_utils::make_config_from_args(&[]); let output = integration_test_utils::run_delta(BINARY_FILE_ADDED, &config); let output = strip_ansi_codes(&output); - assert!(output.contains("added: foo (binary file)\n")); + assert!(output.contains("\nadded: foo (binary file)\n")); } #[test] @@ -256,7 +331,17 @@ mod tests { let config = integration_test_utils::make_config_from_args(&[]); let output = integration_test_utils::run_delta(BINARY_FILE_REMOVED, &config); let output = strip_ansi_codes(&output); - assert!(output.contains("removed: foo (binary file)\n")); + assert!(output.contains("\nremoved: foo (binary file)\n")); + } + + #[test] + fn test_binary_files_differ_after_other() { + let config = + integration_test_utils::make_config_from_args(&["--file-modified-label", "modified:"]); + let output = integration_test_utils::run_delta(BINARY_FILES_DIFFER_AFTER_OTHER, &config); + let output = strip_ansi_codes(&output); + assert!(output.contains("\nrenamed: foo ⟶ bar\n")); + assert!(output.contains("\nmodified: qux (binary file)\n")); } #[test] @@ -269,6 +354,32 @@ mod tests { } #[test] + fn test_standalone_diff_files_are_identical() { + let diff = "Files foo and bar are identical\n"; + let config = integration_test_utils::make_config_from_args(&[]); + let output = integration_test_utils::run_delta(diff, &config); + assert_eq!(strip_ansi_codes(&output), diff); + } + + #[test] + fn test_standalone_diff_binary_files_differ() { + let diff = "Binary files foo and bar differ\n"; + let config = integration_test_utils::make_config_from_args(&[]); + let output = integration_test_utils::run_delta(diff, &config); + assert_eq!(strip_ansi_codes(&output), diff); + } + + #[test] + fn test_diff_no_index_binary_files_differ() { + let config = integration_test_utils::make_config_from_args(&[]); + let output = integration_test_utils::run_delta(DIFF_NO_INDEX_BINARY_FILES_DIFFER, &config); + assert_eq!( + strip_ansi_codes(&output), + "Binary files foo bar and sub dir/foo bar baz differ\n" + ); + } + + #[test] fn test_commit_style_raw_no_decoration() { let config = integration_test_utils::make_config_from_args(&[ "--commit-style", @@ -594,7 +705,7 @@ commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e "omit", ]); let output = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, &config); - for (i, line) in vec![ + for (i, line) in [ "diff --git a/src/align.rs b/src/align.rs", "index 8e37a9e..6ce4863 100644", "--- a/src/align.rs", @@ -642,7 +753,7 @@ index 8e37a9e..6ce4863 100644 GIT_DIFF_SINGLE_HUNK_WITH_ANSI_ESCAPE_SEQUENCES, &config, ); - for (i, line) in vec![ + for (i, line) in [ "diff --git a/src/align.rs b/src/align.rs", "index 8e37a9e..6ce4863 100644", "--- a/src/align.rs", @@ -1382,7 +1493,7 @@ src/align.rs:71: impl<'a> Alignment<'a> { │ 11, 4, "impl<'a> Alignment<'a> { ", - "rs", + "a.rs", State::HunkHeader( DiffType::Unified, ParsedHunkHeader::default(), @@ -1718,7 +1829,7 @@ src/align.rs:71: impl<'a> Alignment<'a> { │ 12, 1, " for (i, x_i) in self.x.iter().enumerate() {", - "rs", + "align.rs", State::HunkZero(DiffType::Unified, None), &config, ); @@ -1743,6 +1854,15 @@ src/align.rs:71: impl<'a> Alignment<'a> { │ assert_eq!(output, input); } + #[test] + fn test_git_diff_binary_is_unchanged_under_color_only() { + let config = integration_test_utils::make_config_from_args(&["--color-only"]); + let input = BINARY_FILES_DIFFER_BETWEEN_OTHER; + let output = integration_test_utils::run_delta(input, &config); + let output = strip_ansi_codes(&output); + assert_eq!(output, input); + } + // See https://github.com/dandavison/delta/issues/371#issuecomment-720173435 #[test] fn test_keep_plus_minus_markers_under_inspect_raw_lines() { @@ -2050,6 +2170,53 @@ index 0000000..84d55c5 +file1 contents "; + const MODIFIED_BASH_AND_CSHARP_FILES: &str = "\ +diff --git a/a b/a +index 8c4ae06..0a37de7 100644 +--- a/a ++++ b/a +@@ -9,7 +9,7 @@ foobar() + dst=$(winpath $2) + + # List the directory. +- ls -l $src ++ ls -la $src + + echo $src '->' $dst + rsync -avu --delete $src/ $dst +diff --git a/b.cs b/b.cs +index 2e73468..8d8b89d 100644 +--- a/b.cs ++++ b/b.cs +@@ -6,7 +6,7 @@ class Program + { + static void Main(string[] args) + { +- int message = 123; ++ int message = 456; + + Console.WriteLine(message); + } +"; + + const MODIFIED_DOCKER_AND_RS_FILES: &str = "\ +diff --git a/Dockerfile b/Dockerfile +index 0123456..1234567 100644 +--- a/Dockerfile ++++ b/Dockerfile +@@ -0,0 +2 @@ ++FROM foo ++COPY bar baz +diff --git a/rs b/rs +index 0123456..1234567 100644 +--- a/rs ++++ b/rs +@@ -0,0 +2 @@ ++fn foobar() -> i8 { ++ 8 ++} +"; + const RENAMED_FILE_INPUT: &str = "\ commit 1281650789680f1009dfff2497d5ccfbe7b96526 Author: Dan Davison <dandavison7@gmail.com> @@ -2309,6 +2476,42 @@ index c9bbb35..5fc172d 100644 Binary files a/foo and /dev/null differ "; + const BINARY_FILES_DIFFER_AFTER_OTHER: &str = " +diff --git a/foo b/bar +similarity index 100% +rename from foo +rename to bar +diff --git a/qux b/qux +index 00de669..d47cd84 100644 +Binary files a/qux and b/qux differ +"; + + const BINARY_FILES_DIFFER_BETWEEN_OTHER: &str = "\ +diff --git a/foo b/foo +index 7b57bd29ea8a..4d3b8c11a4a2 100644 +--- a/foo ++++ b/foo +@@ -1 +1 @@ +-abc ++def +diff --git a/qux b/qux +index 00de669..d47cd84 100644 +Binary files a/qux and b/qux differ +diff --git a/bar b/bar +index 7b57bd29ea8a..4d3b8c11a4a2 100644 +--- a/bar ++++ b/bar +@@ -1 +1 @@ +-123 ++456 +"; + + const DIFF_NO_INDEX_BINARY_FILES_DIFFER: &str = "\ +diff --git foo bar sub dir/foo bar baz +index 329fbf5..481817c 100644 +Binary files foo bar and sub dir/foo bar baz differ +"; + const GIT_DIFF_WITH_COPIED_FILE: &str = " commit f600ed5ced4d98295ffa97571ed240cd86c34ac6 (HEAD -> master) Author: Dan Davison <dandavison7@gmail.com> |