mirror of
https://github.com/comby-tools/comby.git
synced 2024-09-11 13:25:36 +03:00
Handle all cases of trailing newline patch compatible diffs (#140)
This commit is contained in:
parent
a4eaa0884a
commit
72b6a9b8c6
@ -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
|
||||
|
@ -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" =
|
||||
[0;32m++++++ [0m[0;1m/dev/null[0m
|
||||
[0;100;30m@|[0m[0;1m-1,1 +1,1[0m ============================================================
|
||||
[0;43;30m!|[0m[0;31mhello [0mworld
|
||||
\ No newline at end of file
|
||||
|}]
|
||||
|
||||
let%expect_test "with_match_rule" =
|
||||
@ -168,7 +167,6 @@ let%expect_test "with_match_rule" =
|
||||
[0;32m++++++ [0m[0;1m/dev/null[0m
|
||||
[0;100;30m@|[0m[0;1m-1,1 +1,1[0m ============================================================
|
||||
[0;43;30m!|[0m[0;31mhello [0mworld
|
||||
\ No newline at end of file
|
||||
|}];
|
||||
|
||||
let source = "hello world" in
|
||||
@ -201,7 +199,6 @@ let%expect_test "with_rewrite_rule" =
|
||||
[0;32m++++++ [0m[0;1m/dev/null[0m
|
||||
[0;100;30m@|[0m[0;1m-1,1 +1,1[0m ============================================================
|
||||
[0;43;30m!|[0mhello[0;31m world[0m
|
||||
\ 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" =
|
||||
[0;32m++++++ [0m[0;1m/dev/null[0m
|
||||
[0;100;30m@|[0m[0;1m-1,1 +1,1[0m ============================================================
|
||||
[0;43;30m!|[0mhello[0;31m world[0m
|
||||
\ 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 {|[0;31m------ [0m[0;1m/dev/null[0m
|
||||
[0;32m++++++ [0m[0;1m/dev/null[0m
|
||||
[0;100;30m@|[0m[0;1m-1,1 +1,1[0m ============================================================
|
||||
[0;41;30m-|[0m[0m[0;2m\footnote{[0m[0;31m\small[0m[0;2m \url{https://github.com}}[0m
|
||||
\ No newline at end of file[0m
|
||||
[0;42;30m+|[0m[0m\footnote{[0;32m\scriptsize[0m \url{https://github.com}}
|
||||
\ No newline at end of file[0m
|
||||
[0;41;30m-|[0m[0m[0;2m\footnote{[0m[0;31m\small[0m[0;2m \url{https://github.com}}[0m[0m
|
||||
[0;42;30m+|[0m[0m\footnote{[0;32m\scriptsize[0m \url{https://github.com}}[0m
|
||||
|
||||
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 {|[0;31m------ [0m[0;1m/dev/null[0m
|
||||
[0;32m++++++ [0m[0;1m/dev/null[0m
|
||||
[0;100;30m@|[0m[0;1m-1,1 +1,1[0m ============================================================
|
||||
[0;41;30m-|[0m[0m[0;31ma[0m[0;2m X [0m[0;31mc a[0m[0;2m Y [0m[0;31mc[0m
|
||||
\ No newline at end of file[0m
|
||||
[0;42;30m+|[0m[0m[0;32mc[0m X [0;32ma c[0m Y [0;32ma[0m
|
||||
\ No newline at end of file[0m
|
||||
[0;41;30m-|[0m[0m[0;31ma[0m[0;2m X [0m[0;31mc a[0m[0;2m Y [0m[0;31mc[0m[0m
|
||||
[0;42;30m+|[0m[0m[0;32mc[0m X [0;32ma c[0m Y [0;32ma[0m[0m
|
||||
|}]
|
||||
|
||||
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 {|[0;31m------ [0m[0;1m/dev/null[0m
|
||||
[0;32m++++++ [0m[0;1m/dev/null[0m
|
||||
[0;100;30m@|[0m[0;1m-1,1 +1,1[0m ============================================================
|
||||
[0;41;30m-|[0m[0m[0;31ma[0m[0;2m X [0m[0;31mc a[0m[0;2m Y [0m[0;31mc[0m
|
||||
\ No newline at end of file[0m
|
||||
[0;42;30m+|[0m[0m[0;32mc[0m X [0;32ma c[0m Y [0;32ma[0m
|
||||
\ No newline at end of file[0m
|
||||
[0;41;30m-|[0m[0m[0;31ma[0m[0;2m X [0m[0;31mc a[0m[0;2m Y [0m[0;31mc[0m[0m
|
||||
[0;42;30m+|[0m[0m[0;32mc[0m X [0;32ma c[0m Y [0;32ma[0m[0m
|
||||
|}]
|
||||
|
||||
let%expect_test "diff_explicit_color" =
|
||||
@ -472,10 +463,8 @@ let%expect_test "diff_explicit_color" =
|
||||
[%expect_exact {|[0;31m------ [0m[0;1m/dev/null[0m
|
||||
[0;32m++++++ [0m[0;1m/dev/null[0m
|
||||
[0;100;30m@|[0m[0;1m-1,1 +1,1[0m ============================================================
|
||||
[0;41;30m-|[0m[0m[0;31ma[0m[0;2m X [0m[0;31mc a[0m[0;2m Y [0m[0;31mc[0m
|
||||
\ No newline at end of file[0m
|
||||
[0;42;30m+|[0m[0m[0;32mc[0m X [0;32ma c[0m Y [0;32ma[0m
|
||||
\ No newline at end of file[0m
|
||||
[0;41;30m-|[0m[0m[0;31ma[0m[0;2m X [0m[0;31mc a[0m[0;2m Y [0m[0;31mc[0m[0m
|
||||
[0;42;30m+|[0m[0m[0;32mc[0m X [0;32ma c[0m Y [0;32ma[0m[0m
|
||||
|}]
|
||||
|
||||
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 {|[0;31m------ [0m[0;1m/dev/null[0m
|
||||
[0;32m++++++ [0m[0;1m/dev/null[0m
|
||||
[0;100;30m@|[0m[0;1m-1,1 +1,1[0m ============================================================
|
||||
[0;41;30m-|[0m[0m[0;31mdont care[0m
|
||||
\ No newline at end of file[0m
|
||||
[0;42;30m+|[0m[0m[0;32mblah[0m
|
||||
\ No newline at end of file[0m
|
||||
[0;41;30m-|[0m[0m[0;31mdont care[0m[0m
|
||||
[0;42;30m+|[0m[0m[0;32mblah[0m[0m
|
||||
|}]
|
||||
|
||||
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
|
||||
|}]
|
||||
|
1
test/example/diff-no-newlines/1/a
Normal file
1
test/example/diff-no-newlines/1/a
Normal file
@ -0,0 +1 @@
|
||||
1
|
1
test/example/diff-no-newlines/1/b
Normal file
1
test/example/diff-no-newlines/1/b
Normal file
@ -0,0 +1 @@
|
||||
2
|
7
test/example/diff-no-newlines/1/expect.patch
Normal file
7
test/example/diff-no-newlines/1/expect.patch
Normal 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
|
2
test/example/diff-no-newlines/2/a
Normal file
2
test/example/diff-no-newlines/2/a
Normal file
@ -0,0 +1,2 @@
|
||||
1
|
||||
1
|
2
test/example/diff-no-newlines/2/b
Normal file
2
test/example/diff-no-newlines/2/b
Normal file
@ -0,0 +1,2 @@
|
||||
2
|
||||
2
|
9
test/example/diff-no-newlines/2/expect.patch
Normal file
9
test/example/diff-no-newlines/2/expect.patch
Normal 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
|
2
test/example/diff-no-newlines/3/a
Normal file
2
test/example/diff-no-newlines/3/a
Normal file
@ -0,0 +1,2 @@
|
||||
1
|
||||
2
|
2
test/example/diff-no-newlines/3/b
Normal file
2
test/example/diff-no-newlines/3/b
Normal file
@ -0,0 +1,2 @@
|
||||
2
|
||||
2
|
7
test/example/diff-no-newlines/3/expect.patch
Normal file
7
test/example/diff-no-newlines/3/expect.patch
Normal 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
|
1
test/example/diff-no-newlines/4/a
Normal file
1
test/example/diff-no-newlines/4/a
Normal file
@ -0,0 +1 @@
|
||||
1
|
1
test/example/diff-no-newlines/4/b
Normal file
1
test/example/diff-no-newlines/4/b
Normal file
@ -0,0 +1 @@
|
||||
2
|
6
test/example/diff-no-newlines/4/expect.patch
Normal file
6
test/example/diff-no-newlines/4/expect.patch
Normal 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
|
1
test/example/diff-no-newlines/5/a
Normal file
1
test/example/diff-no-newlines/5/a
Normal file
@ -0,0 +1 @@
|
||||
1
|
1
test/example/diff-no-newlines/5/b
Normal file
1
test/example/diff-no-newlines/5/b
Normal file
@ -0,0 +1 @@
|
||||
2
|
6
test/example/diff-no-newlines/5/expect.patch
Normal file
6
test/example/diff-no-newlines/5/expect.patch
Normal 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
|
Loading…
Reference in New Issue
Block a user