mirror of
https://github.com/comby-tools/comby.git
synced 2024-08-16 16:50:37 +03:00
support match only for config specs (#272)
This commit is contained in:
parent
a47a10a5dd
commit
5863b9208c
@ -769,6 +769,13 @@ let create
|
||||
{ spec with match_template =
|
||||
String.substr_replace_all match_template ~pattern:"..." ~with_:":[_]" })
|
||||
in
|
||||
let specifications =
|
||||
if match_only then
|
||||
List.map specifications ~f:(fun {match_template; rule; _ } ->
|
||||
Specification.create ~match_template ?rule ())
|
||||
else
|
||||
specifications
|
||||
in
|
||||
if regex_pattern then (Format.printf "%s@." (regex_of_specifications specifications); exit 0);
|
||||
let file_filters_from_anonymous_args =
|
||||
match anonymous_arguments with
|
||||
|
@ -141,21 +141,35 @@ let output_result output_printer source_path source_content result =
|
||||
output_printer (Printer.Replacements { source_path; replacements; result; source_content })
|
||||
|
||||
let run_on_specifications specifications output_printer process (input : single_source) output_file =
|
||||
let result, count =
|
||||
List.fold specifications ~init:(Nothing, 0) ~f:(fun (result, count) specification ->
|
||||
let result =
|
||||
List.fold specifications ~init:Nothing ~f:(fun result specification ->
|
||||
let input =
|
||||
match result with
|
||||
| Nothing | Matches _ -> input
|
||||
| Nothing
|
||||
| Matches _ -> input
|
||||
| Replacement (_, content, _) -> String content
|
||||
in
|
||||
process input specification
|
||||
|> function
|
||||
| Nothing -> Nothing, count
|
||||
| Matches (l, number_of_matches) ->
|
||||
Matches (l, number_of_matches), count + number_of_matches
|
||||
| Replacement (l, content, number_of_matches) ->
|
||||
Replacement (l, content, number_of_matches),
|
||||
count + number_of_matches)
|
||||
match result, process input specification with
|
||||
| any, Nothing
|
||||
| Nothing, any -> any
|
||||
|
||||
| Matches (l, n), Matches (l', n') ->
|
||||
Matches (l@l', n+n')
|
||||
|
||||
| Replacement (l, _, n), Replacement (l', content, n') ->
|
||||
Replacement (l@l', content, n+n')
|
||||
|
||||
| Matches _, Replacement (l, content, n)
|
||||
| Replacement (l, content, n), Matches _ ->
|
||||
Format.eprintf "WARNING: input configuration specifies both rewrite and match templates. I am choosing to only process the configurations with both a 'match' and 'rewrite' part. If you only want to see matches, add -match-only to suppress this warning@.";
|
||||
Replacement (l, content, n)
|
||||
)
|
||||
in
|
||||
let count =
|
||||
match result with
|
||||
| Nothing -> 0
|
||||
| Matches (_, n)
|
||||
| Replacement (_, _, n) -> n
|
||||
in
|
||||
output_result output_printer output_file input result;
|
||||
count
|
||||
|
@ -2,11 +2,13 @@
|
||||
(name common_test_integration)
|
||||
(package comby)
|
||||
(modules
|
||||
test_cli_helper
|
||||
test_helpers
|
||||
test_extract_regex
|
||||
test_alpha
|
||||
test_omega
|
||||
test_cli
|
||||
test_cli_list
|
||||
; test_server
|
||||
test_statistics
|
||||
test_offset_conversion
|
||||
|
@ -290,63 +290,6 @@ let with_zip f =
|
||||
f file;
|
||||
Unix.remove file
|
||||
|
||||
let%expect_test "list_languages" =
|
||||
let command_args = "-list" in
|
||||
let command = Format.sprintf "%s %s" binary_path command_args in
|
||||
let result = read_output command in
|
||||
print_string result;
|
||||
[%expect_exact {|Option Language
|
||||
-matcher .s Assembly
|
||||
-matcher .sh Bash
|
||||
-matcher .c C
|
||||
-matcher .cs C#
|
||||
-matcher .css CSS
|
||||
-matcher .dart Dart
|
||||
-matcher .dyck Dyck
|
||||
-matcher .clj Clojure
|
||||
-matcher .v Coq
|
||||
-matcher .elm Elm
|
||||
-matcher .erl Erlang
|
||||
-matcher .ex Elixir
|
||||
-matcher .f Fortran
|
||||
-matcher .fsx F#
|
||||
-matcher .go Go
|
||||
-matcher .html HTML
|
||||
-matcher .hs Haskell
|
||||
-matcher .java Java
|
||||
-matcher .js JavaScript
|
||||
-matcher .jsx JSX
|
||||
-matcher .json JSON
|
||||
-matcher .jsonc JSONC
|
||||
-matcher .gql GraphQL
|
||||
-matcher .dhall Dhall
|
||||
-matcher .jl Julia
|
||||
-matcher .kt Kotlin
|
||||
-matcher .tex LaTeX
|
||||
-matcher .lisp Lisp
|
||||
-matcher .move Move
|
||||
-matcher .nim Nim
|
||||
-matcher .ml OCaml
|
||||
-matcher .paren Paren
|
||||
-matcher .pas Pascal
|
||||
-matcher .php PHP
|
||||
-matcher .py Python
|
||||
-matcher .re Reason
|
||||
-matcher .rb Ruby
|
||||
-matcher .rs Rust
|
||||
-matcher .scala Scala
|
||||
-matcher .sol Solidity
|
||||
-matcher .sql SQL
|
||||
-matcher .swift Swift
|
||||
-matcher .txt Text
|
||||
-matcher .ts TypeScript
|
||||
-matcher .tsx TSX
|
||||
-matcher .xml XML
|
||||
-matcher .zig Zig
|
||||
-matcher .generic Generic
|
||||
|}]
|
||||
|
||||
|
||||
let%expect_test "patdiff_and_zip" =
|
||||
with_zip (fun file ->
|
||||
let match_template = ":[[2]] :[[1]]" in
|
||||
@ -1217,11 +1160,46 @@ let%expect_test "test_toml_rule" =
|
||||
print_string result;
|
||||
[%expect{|main(rewrite)|}]
|
||||
|
||||
let%expect_test "test_toml_match_only_multi" =
|
||||
let source = "foo\nbar\nbaz\n" in
|
||||
let config = "example" ^/ "toml" ^/ "match-only-multi" ^/ "config.toml" in
|
||||
let command_args =
|
||||
Format.sprintf "-stdin -sequential -config %s -stdout -matcher .c -match-only" config
|
||||
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{|
|
||||
1:foo
|
||||
2:bar
|
||||
3:baz|}]
|
||||
|
||||
let%expect_test "test_toml_multi_rewrite_with_match_only_in_config" =
|
||||
let source = "foo\nbar\nbaz" in
|
||||
let config = "example" ^/ "toml" ^/ "match-only-multi" ^/ "config.toml" in
|
||||
let command_args =
|
||||
Format.sprintf "-stdin -sequential -config %s -matcher .c" config
|
||||
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{|
|
||||
[0;31m------ [0m[0;1m/dev/null[0m
|
||||
[0;32m++++++ [0m[0;1m/dev/null[0m
|
||||
[0;100;30m@|[0m[0;1m-1,3 +1,3[0m ============================================================
|
||||
[0;100;30m |[0mfoo
|
||||
[0;41;30m-|[0m[0m[0;31mbar[0m[0m
|
||||
[0;42;30m+|[0m[0m[0;32madsf[0m[0m
|
||||
[0;100;30m |[0mbaz
|
||||
WARNING: input configuration specifies both rewrite and match templates. I am choosing to only process the configurations with both a 'match' and 'rewrite' part. If you only want to see matches, add -match-only to suppress this warning
|
||||
WARNING: input configuration specifies both rewrite and match templates. I am choosing to only process the configurations with both a 'match' and 'rewrite' part. If you only want to see matches, add -match-only to suppress this warning|}]
|
||||
|
||||
let%expect_test "dot_comby_with_flags" =
|
||||
let source = "main(void)\n" in
|
||||
Sys.chdir ("example" ^/ "dot-comby");
|
||||
let command = Format.sprintf "../../%s %s" binary_path "" in
|
||||
let result = read_expect_stdin_and_stdout command source in
|
||||
Sys.chdir "../..";
|
||||
print_string result;
|
||||
[%expect{|
|
||||
[0;31m------ [0m[0;1m/dev/null[0m
|
||||
|
54
test/common/test_cli_helper.ml
Normal file
54
test/common/test_cli_helper.ml
Normal file
@ -0,0 +1,54 @@
|
||||
open Core
|
||||
|
||||
module Time = Core_kernel.Time_ns.Span
|
||||
|
||||
let binary_path = "../../../../comby"
|
||||
|
||||
let read_with_timeout read_from_channels =
|
||||
let read_from_fds = List.map ~f:Unix.descr_of_in_channel read_from_channels in
|
||||
let read_from_channels =
|
||||
Unix.select
|
||||
~restart:true
|
||||
~read:read_from_fds
|
||||
~write:[]
|
||||
~except:[]
|
||||
~timeout:(`After (Time.of_int_sec 1))
|
||||
()
|
||||
|> (fun { Unix.Select_fds.read; _ } -> read)
|
||||
|> List.map ~f:Unix.in_channel_of_descr
|
||||
in
|
||||
List.map read_from_channels ~f:In_channel.input_all
|
||||
|> String.concat ~sep:"\n"
|
||||
|
||||
let read_output command =
|
||||
let open Unix.Process_channels in
|
||||
let { stdout; stderr; _ } =
|
||||
Unix.open_process_full ~env:(Array.of_list ["COMBY_TEST=1"]) command
|
||||
in
|
||||
let stdout_result = In_channel.input_all stdout in
|
||||
let stderr_result = In_channel.input_all stderr in
|
||||
stdout_result ^ stderr_result
|
||||
|
||||
let read_expect_stdin_and_stdout command source =
|
||||
let open Unix.Process_channels in
|
||||
let { stdin; stdout; stderr } =
|
||||
Unix.open_process_full ~env:(Array.of_list ["COMBY_TEST=1"]) command
|
||||
in
|
||||
Out_channel.output_string stdin source;
|
||||
Out_channel.flush stdin;
|
||||
Out_channel.close stdin;
|
||||
let stdout_result = In_channel.input_all stdout in
|
||||
let stderr_result = In_channel.input_all stderr in
|
||||
stdout_result ^ stderr_result
|
||||
|
||||
let read_expect_stderr command source =
|
||||
let open Unix.Process_channels in
|
||||
let { stdin; stdout; stderr } =
|
||||
Unix.open_process_full ~env:(Array.of_list ["COMBY_TEST=1"]) command
|
||||
in
|
||||
Out_channel.output_string stdin source;
|
||||
Out_channel.flush stdin;
|
||||
Out_channel.close stdin;
|
||||
let _ = In_channel.input_all stdout in
|
||||
let stderr_result = In_channel.input_all stderr in
|
||||
stderr_result
|
57
test/common/test_cli_list.ml
Normal file
57
test/common/test_cli_list.ml
Normal file
@ -0,0 +1,57 @@
|
||||
open Test_cli_helper
|
||||
|
||||
let%expect_test "list_languages" =
|
||||
let command_args = "-list" in
|
||||
let command = Format.sprintf "%s %s" binary_path command_args in
|
||||
let result = read_output command in
|
||||
print_string result;
|
||||
[%expect_exact {|Option Language
|
||||
-matcher .s Assembly
|
||||
-matcher .sh Bash
|
||||
-matcher .c C
|
||||
-matcher .cs C#
|
||||
-matcher .css CSS
|
||||
-matcher .dart Dart
|
||||
-matcher .dyck Dyck
|
||||
-matcher .clj Clojure
|
||||
-matcher .v Coq
|
||||
-matcher .elm Elm
|
||||
-matcher .erl Erlang
|
||||
-matcher .ex Elixir
|
||||
-matcher .f Fortran
|
||||
-matcher .fsx F#
|
||||
-matcher .go Go
|
||||
-matcher .html HTML
|
||||
-matcher .hs Haskell
|
||||
-matcher .java Java
|
||||
-matcher .js JavaScript
|
||||
-matcher .jsx JSX
|
||||
-matcher .json JSON
|
||||
-matcher .jsonc JSONC
|
||||
-matcher .gql GraphQL
|
||||
-matcher .dhall Dhall
|
||||
-matcher .jl Julia
|
||||
-matcher .kt Kotlin
|
||||
-matcher .tex LaTeX
|
||||
-matcher .lisp Lisp
|
||||
-matcher .move Move
|
||||
-matcher .nim Nim
|
||||
-matcher .ml OCaml
|
||||
-matcher .paren Paren
|
||||
-matcher .pas Pascal
|
||||
-matcher .php PHP
|
||||
-matcher .py Python
|
||||
-matcher .re Reason
|
||||
-matcher .rb Ruby
|
||||
-matcher .rs Rust
|
||||
-matcher .scala Scala
|
||||
-matcher .sol Solidity
|
||||
-matcher .sql SQL
|
||||
-matcher .swift Swift
|
||||
-matcher .txt Text
|
||||
-matcher .ts TypeScript
|
||||
-matcher .tsx TSX
|
||||
-matcher .xml XML
|
||||
-matcher .zig Zig
|
||||
-matcher .generic Generic
|
||||
|}]
|
10
test/example/toml/match-only-multi/config.toml
Normal file
10
test/example/toml/match-only-multi/config.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[a]
|
||||
match='foo'
|
||||
|
||||
[b]
|
||||
match='bar'
|
||||
rewrite='adsf'
|
||||
|
||||
[c]
|
||||
match='baz'
|
||||
|
Loading…
Reference in New Issue
Block a user