Skip to content

Commit

Permalink
fix up tests and newline printing
Browse files Browse the repository at this point in the history
  • Loading branch information
rvantonder committed Jun 6, 2019
1 parent 7dfc562 commit fc5f949
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/replacement/replacement.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ let yojson_to_string kind json =
let pp_json_pretty ppf (source_path, replacements, replacement_content, diff) =
Format.fprintf ppf "%s" @@ yojson_to_string `Pretty @@ to_json replacements source_path diff replacement_content

let pp_json_lines ppf (source_path, replacements, replacement_content, diff) =
let pp_json_line ppf (source_path, replacements, replacement_content, diff) =
Format.fprintf ppf "%s" @@ Yojson.Safe.to_string @@ to_json replacements source_path diff replacement_content
2 changes: 1 addition & 1 deletion lib/replacement/replacement.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ val empty_result : result

val pp_json_pretty : Format.formatter -> string option * t list * string * string option -> unit

val pp_json_lines : Format.formatter -> string option * t list * string * string option -> unit
val pp_json_line : Format.formatter -> string option * t list * string * string option -> unit
6 changes: 4 additions & 2 deletions src/command_configuration.ml
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,12 @@ module Printer = struct
Format.fprintf ppf "%s" replacement_content
| Json_pretty ->
let diff = Diff_configuration.get_diff Plain path source_content replacement_content in
Format.fprintf ppf "%a" Replacement.pp_json_pretty (path, replacements, replacement_content, diff)
let print diff = Format.fprintf ppf "%a@." Replacement.pp_json_pretty (path, replacements, replacement_content, diff) in
Option.value_map diff ~default:() ~f:(fun diff -> print (Some diff))
| Json_lines ->
let diff = Diff_configuration.get_diff Plain path source_content replacement_content in
Format.fprintf ppf "%a" Replacement.pp_json_lines (path, replacements, replacement_content, diff)
let print diff = Format.fprintf ppf "%a@." Replacement.pp_json_line (path, replacements, replacement_content, diff) in
Option.value_map diff ~default:() ~f:(fun diff -> print (Some diff))
| Diff Plain ->
let diff = Diff_configuration.get_diff Plain path source_content replacement_content in
Option.value_map diff ~default:() ~f:(fun diff -> Format.fprintf ppf "%s@." diff)
Expand Down
58 changes: 48 additions & 10 deletions test/test_cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,42 @@ let read_output command =
let { stdout; stderr; _ } = Unix.open_process_full ~env:[||] command in
read_with_timeout [stdout; stderr]

let%expect_test "json_lines_separates_by_line" =
let source = "hello world" in
let match_template = "o" in
let rewrite_template = "i" in
let command_args =
Format.sprintf "-stdin -sequential '%s' '%s' -f .c -json-lines" match_template rewrite_template
in
let command = Format.sprintf "%s %s" binary_path command_args in
read_source_from_stdin command source
|> print_string;
[%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"}
|}]

let%expect_test "json_lines_and_json_pretty_do_not_output_when_diff_null" =
let source = "hello world" in
let match_template = "asdf" in
let rewrite_template = "asdf" in
let command_args =
Format.sprintf "-stdin -sequential '%s' '%s' -f .c -json-pretty" match_template rewrite_template
in
let command = Format.sprintf "%s %s" binary_path command_args in
read_source_from_stdin command source
|> print_string;
[%expect_exact {||}];

let source = "hello world" in
let match_template = "asdf" in
let rewrite_template = "asdf" in
let command_args =
Format.sprintf "-stdin -sequential '%s' '%s' -f .c -json-lines" match_template rewrite_template
in
let command = Format.sprintf "%s %s" binary_path command_args in
read_source_from_stdin command source
|> print_string;
[%expect_exact {||}]

let%expect_test "error_on_zip_and_stdin" =
let command_args = "-zip x -stdin" in
let command = Format.sprintf "%s %s" binary_path command_args in
Expand Down Expand Up @@ -81,7 +117,7 @@ let%expect_test "stdin_command" =
[%expect_exact {|------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
[0;43;30m!|[0m[0;31mhello[0m world
[0;43;30m!|[0m[0;31mhello [0mworld
|}]

let%expect_test "with_match_rule" =
Expand All @@ -99,7 +135,7 @@ let%expect_test "with_match_rule" =
[%expect_exact {|------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
[0;43;30m!|[0m[0;31mhello[0m world
[0;43;30m!|[0m[0;31mhello [0mworld
|}];

let source = "hello world" in
Expand Down Expand Up @@ -220,7 +256,8 @@ let%expect_test "json_output_option" =
],
"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"
}|}];
}
|}];

let source = "a X c a Y c" in
let match_template = "a :[1] c" in
Expand Down Expand Up @@ -316,7 +353,8 @@ let%expect_test "patdiff_and_zip" =
}
],
"diff": "--- main.ml\n+++ main.ml\n@@ -1,1 +1,1 @@\n-hello world\n+world"
}|}]
}
|}]
)

let%expect_test "template_parsing_no_match_template" =
Expand Down Expand Up @@ -361,8 +399,8 @@ let%expect_test "diff_is_default" =
[%expect_exact {|------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
[0;41;30m-|[0m[0m[0;31ma[0m[0;2m X[0m[0;2m c[0m[0;31m a[0m[0;2m Y[0m[0;31m c[0m[0m
[0;42;30m+|[0m[0m[0;32mc[0m X[0;32m a[0m c Y[0;32m a[0m[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" =
Expand Down Expand Up @@ -410,8 +448,8 @@ let%expect_test "only_color_prints_colored_diff" =
[%expect_exact {|------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
[0;41;30m-|[0m[0m[0;31ma[0m[0;2m X[0m[0;2m c[0m[0;31m a[0m[0;2m Y[0m[0;31m c[0m[0m
[0;42;30m+|[0m[0m[0;32mc[0m X[0;32m a[0m c Y[0;32m a[0m[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" =
Expand All @@ -428,8 +466,8 @@ let%expect_test "diff_explicit_color" =
[%expect_exact {|------ /dev/null
++++++ /dev/null
@|-1,1 +1,1 ============================================================
[0;41;30m-|[0m[0m[0;31ma[0m[0;2m X[0m[0;2m c[0m[0;31m a[0m[0;2m Y[0m[0;31m c[0m[0m
[0;42;30m+|[0m[0m[0;32mc[0m X[0;32m a[0m c Y[0;32m a[0m[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
|}]


Expand Down

0 comments on commit fc5f949

Please sign in to comment.