Handle all cases of trailing newline patch compatible diffs (#140)

This commit is contained in:
Rijnard van Tonder 2019-11-20 01:56:30 -07:00 committed by GitHub
parent a4eaa0884a
commit 72b6a9b8c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 148 additions and 29 deletions

View File

@ -94,6 +94,7 @@ let plain_configuration =
(
(context 3)
;; unrefined: output every changed line. unrefined false will merge + and - lines if they are similar.
(unrefined true)
(line_same
@ -152,7 +153,7 @@ let get_diff kind source_path source_content result =
let prev = Patdiff_core.{ name = source_path; text = source_content } in
let next = Patdiff_core.{ name = source_path; text = result } in
Compare_core.diff_strings ~print_global_header:true ~patch_compatible_hunks:true configuration ~prev ~next
Compare_core.diff_strings ~print_global_header:true configuration ~prev ~next
|> function
| `Different diff -> Some diff
| `Same -> None

View File

@ -63,7 +63,7 @@ let%expect_test "json_lines_separates_by_line" =
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect_exact {|{"uri":null,"rewritten_source":"helli wirld","in_place_substitutions":[{"range":{"start":{"offset":7,"line":-1,"column":-1},"end":{"offset":8,"line":-1,"column":-1}},"replacement_content":"i","environment":[]},{"range":{"start":{"offset":4,"line":-1,"column":-1},"end":{"offset":5,"line":-1,"column":-1}},"replacement_content":"i","environment":[]}],"diff":"--- /dev/null\n+++ /dev/null\n@@ -1,1 +1,1 @@\n-hello world\n+helli wirld\n\\ No newline at end of file"}
[%expect_exact {|{"uri":null,"rewritten_source":"helli wirld","in_place_substitutions":[{"range":{"start":{"offset":7,"line":-1,"column":-1},"end":{"offset":8,"line":-1,"column":-1}},"replacement_content":"i","environment":[]},{"range":{"start":{"offset":4,"line":-1,"column":-1},"end":{"offset":5,"line":-1,"column":-1}},"replacement_content":"i","environment":[]}],"diff":"--- /dev/null\n+++ /dev/null\n@@ -1,1 +1,1 @@\n-hello world\n\\ No newline at end of file\n+helli wirld\n\\ No newline at end of file"}
|}]
let%expect_test "json_lines_json_pretty_do_not_output_when_diff_null" =
@ -149,7 +149,6 @@ let%expect_test "stdin_command" =
++++++ /dev/null
@|-1,1 +1,1 ============================================================
!|hello world
\ No newline at end of file
|}]
let%expect_test "with_match_rule" =
@ -168,7 +167,6 @@ let%expect_test "with_match_rule" =
++++++ /dev/null
@|-1,1 +1,1 ============================================================
!|hello world
\ No newline at end of file
|}];
let source = "hello world" in
@ -201,7 +199,6 @@ let%expect_test "with_rewrite_rule" =
++++++ /dev/null
@|-1,1 +1,1 ============================================================
!|hello world
\ No newline at end of file
|}]
let%expect_test "with_rewrite_rule_stdin_default_no_extension" =
@ -219,7 +216,6 @@ let%expect_test "with_rewrite_rule_stdin_default_no_extension" =
++++++ /dev/null
@|-1,1 +1,1 ============================================================
!|hello world
\ No newline at end of file
WARNING: the GENERIC matcher was used, because a language could not be inferred from the file extension(s). The GENERIC matcher may miss matches. See '-list' to set a matcher for a specific language and to remove this warning.
|}]
@ -237,10 +233,8 @@ let%expect_test "generic_matcher" =
[%expect_exact {|------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
-|\footnote{\small \url{https://github.com}}
\ No newline at end of file
+|\footnote{\scriptsize \url{https://github.com}}
\ No newline at end of file
-|\footnote{\small \url{https://github.com}}
+|\footnote{\scriptsize \url{https://github.com}}
WARNING: the GENERIC matcher was used, because a language could not be inferred from the file extension(s). The GENERIC matcher may miss matches. See '-list' to set a matcher for a specific language and to remove this warning.
|}]
@ -257,7 +251,7 @@ let%expect_test "json_output_option" =
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect_exact {|{"uri":null,"rewritten_source":"c X a c Y a","in_place_substitutions":[{"range":{"start":{"offset":6,"line":-1,"column":-1},"end":{"offset":11,"line":-1,"column":-1}},"replacement_content":"c Y a","environment":[{"variable":"1","value":"Y","range":{"start":{"offset":2,"line":-1,"column":-1},"end":{"offset":3,"line":-1,"column":-1}}}]},{"range":{"start":{"offset":0,"line":-1,"column":-1},"end":{"offset":5,"line":-1,"column":-1}},"replacement_content":"c X a","environment":[{"variable":"1","value":"X","range":{"start":{"offset":2,"line":-1,"column":-1},"end":{"offset":3,"line":-1,"column":-1}}}]}],"diff":"--- /dev/null\n+++ /dev/null\n@@ -1,1 +1,1 @@\n-a X c a Y c\n+c X a c Y a\n\\ No newline at end of file"}
[%expect_exact {|{"uri":null,"rewritten_source":"c X a c Y a","in_place_substitutions":[{"range":{"start":{"offset":6,"line":-1,"column":-1},"end":{"offset":11,"line":-1,"column":-1}},"replacement_content":"c Y a","environment":[{"variable":"1","value":"Y","range":{"start":{"offset":2,"line":-1,"column":-1},"end":{"offset":3,"line":-1,"column":-1}}}]},{"range":{"start":{"offset":0,"line":-1,"column":-1},"end":{"offset":5,"line":-1,"column":-1}},"replacement_content":"c X a","environment":[{"variable":"1","value":"X","range":{"start":{"offset":2,"line":-1,"column":-1},"end":{"offset":3,"line":-1,"column":-1}}}]}],"diff":"--- /dev/null\n+++ /dev/null\n@@ -1,1 +1,1 @@\n-a X c a Y c\n\\ No newline at end of file\n+c X a c Y a\n\\ No newline at end of file"}
|}];
let source = "a X c a Y c" in
@ -342,7 +336,7 @@ let%expect_test "patdiff_and_zip" =
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_output command in
print_string result;
[%expect_exact {|{"uri":"main.ml","rewritten_source":"world","in_place_substitutions":[{"range":{"start":{"offset":0,"line":-1,"column":-1},"end":{"offset":5,"line":-1,"column":-1}},"replacement_content":"world","environment":[{"variable":"1","value":"world","range":{"start":{"offset":0,"line":-1,"column":-1},"end":{"offset":5,"line":-1,"column":-1}}}]}],"diff":"--- main.ml\n+++ main.ml\n@@ -1,1 +1,1 @@\n-hello world\n+world\n\\ No newline at end of file"}
[%expect_exact {|{"uri":"main.ml","rewritten_source":"world","in_place_substitutions":[{"range":{"start":{"offset":0,"line":-1,"column":-1},"end":{"offset":5,"line":-1,"column":-1}},"replacement_content":"world","environment":[{"variable":"1","value":"world","range":{"start":{"offset":0,"line":-1,"column":-1},"end":{"offset":5,"line":-1,"column":-1}}}]}],"diff":"--- main.ml\n+++ main.ml\n@@ -1,1 +1,1 @@\n-hello world\n\\ No newline at end of file\n+world\n\\ No newline at end of file"}
|}]
)
@ -400,10 +394,8 @@ let%expect_test "diff_is_default" =
[%expect_exact {|------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
-|a X c a Y c
\ No newline at end of file
+|c X a c Y a
\ No newline at end of file
-|a X c a Y c
+|c X a c Y a
|}]
let%expect_test "diff_option" =
@ -421,6 +413,7 @@ let%expect_test "diff_option" =
+++ /dev/null
@@ -1,1 +1,1 @@
-a X c a Y c
\ No newline at end of file
+c X a c Y a
\ No newline at end of file
|}]
@ -452,10 +445,8 @@ let%expect_test "only_color_prints_colored_diff" =
[%expect_exact {|------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
-|a X c a Y c
\ No newline at end of file
+|c X a c Y a
\ No newline at end of file
-|a X c a Y c
+|c X a c Y a
|}]
let%expect_test "diff_explicit_color" =
@ -472,10 +463,8 @@ let%expect_test "diff_explicit_color" =
[%expect_exact {|------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
-|a X c a Y c
\ No newline at end of file
+|c X a c Y a
\ No newline at end of file
-|a X c a Y c
+|c X a c Y a
|}]
let%expect_test "is_real_directory" =
@ -715,7 +704,7 @@ let%expect_test "diff_only" =
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect{|
{"uri":null,"diff":"--- /dev/null\n+++ /dev/null\n@@ -1,1 +1,1 @@\n-hello world\n+world world\n\\ No newline at end of file"}
{"uri":null,"diff":"--- /dev/null\n+++ /dev/null\n@@ -1,1 +1,1 @@\n-hello world\n\\ No newline at end of file\n+world world\n\\ No newline at end of file"}
WARNING: the GENERIC matcher was used, because a language could not be inferred from the file extension(s). The GENERIC matcher may miss matches. See '-list' to set a matcher for a specific language and to remove this warning. |}];
@ -907,10 +896,8 @@ let%expect_test "generic_matcher_ok" =
[%expect_exact {|------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
-|dont care
\ No newline at end of file
+|blah
\ No newline at end of file
-|dont care
+|blah
|}]
let%expect_test "warn_on_anonymous_and_templates_flag" =
@ -964,3 +951,85 @@ let%expect_test "substitute_ok" =
|> print_string;
[%expect_exact {|hole_1 hole_2
|}]
let%expect_test "diff_patches_with_trailing_newlines" =
let source = "dont care" in
let src_dir = "example" ^/ "diff-no-newlines" ^/ "1" in
let match_template = "1" in
let rewrite_template = "2" in
let command_args = Format.sprintf "'%s' '%s' -diff -f a -d %s -matcher .txt " match_template rewrite_template src_dir in
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect_exact {|--- example/diff-no-newlines/1/a
+++ example/diff-no-newlines/1/a
@@ -1,1 +1,1 @@
-1
\ No newline at end of file
+2
\ No newline at end of file
|}];
let src_dir = "example" ^/ "diff-no-newlines" ^/ "2" in
let command_args = Format.sprintf "'%s' '%s' -diff -f a -d %s -matcher .txt " match_template rewrite_template src_dir in
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect_exact {|--- example/diff-no-newlines/2/a
+++ example/diff-no-newlines/2/a
@@ -1,2 +1,2 @@
-1
-1
\ No newline at end of file
+2
+2
\ No newline at end of file
|}];
let src_dir = "example" ^/ "diff-no-newlines" ^/ "3" in
let command_args = Format.sprintf "'%s' '%s' -diff -f a -d %s -matcher .txt " match_template rewrite_template src_dir in
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect_exact {|--- example/diff-no-newlines/3/a
+++ example/diff-no-newlines/3/a
@@ -1,2 +1,2 @@
-1
+2
2
\ No newline at end of file
|}];
(* Introduce newline *)
let match_template = "1" in
let rewrite_template = "2\n" in
let src_dir = "example" ^/ "diff-no-newlines" ^/ "4" in
let command_args = Format.sprintf "'%s' '%s' -diff -f a -d %s -matcher .txt " match_template rewrite_template src_dir in
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect_exact {|--- example/diff-no-newlines/4/a
+++ example/diff-no-newlines/4/a
@@ -1,1 +1,1 @@
-1
\ No newline at end of file
+2
|}];
(* Delete newline *)
let match_template = "1\n" in
let rewrite_template = "2" in
let src_dir = "example" ^/ "diff-no-newlines" ^/ "5" in
let command_args = Format.sprintf "'%s' '%s' -diff -f a -d %s -matcher .txt " match_template rewrite_template src_dir in
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_expect_stdin_and_stdout command source in
print_string result;
[%expect_exact {|--- example/diff-no-newlines/5/a
+++ example/diff-no-newlines/5/a
@@ -1,1 +1,1 @@
-1
+2
\ No newline at end of file
|}]

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
2

View File

@ -0,0 +1,7 @@
--- a 2019-11-18 21:13:06.000000000 -0700
+++ b 2019-11-18 21:13:06.000000000 -0700
@@ -1 +1 @@
-1
\ No newline at end of file
+2
\ No newline at end of file

View File

@ -0,0 +1,2 @@
1
1

View File

@ -0,0 +1,2 @@
2
2

View File

@ -0,0 +1,9 @@
--- a 2019-11-20 00:12:09.000000000 -0700
+++ b 2019-11-20 00:12:09.000000000 -0700
@@ -1,2 +1,2 @@
-1
-1
\ No newline at end of file
+2
+2
\ No newline at end of file

View File

@ -0,0 +1,2 @@
1
2

View File

@ -0,0 +1,2 @@
2
2

View File

@ -0,0 +1,7 @@
--- a 2019-11-20 00:15:07.000000000 -0700
+++ b 2019-11-20 00:15:07.000000000 -0700
@@ -1,2 +1,2 @@
-1
+2
2
\ No newline at end of file

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
2

View File

@ -0,0 +1,6 @@
--- a 2019-11-20 00:20:35.000000000 -0700
+++ b 2019-11-20 00:20:35.000000000 -0700
@@ -1 +1 @@
-1
\ No newline at end of file
+2

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
2

View File

@ -0,0 +1,6 @@
--- a 2019-11-20 00:20:54.000000000 -0700
+++ b 2019-11-20 00:20:54.000000000 -0700
@@ -1 +1 @@
-1
+2
\ No newline at end of file