Skip to content

Commit

Permalink
Add -exclude-dir in zip support
Browse files Browse the repository at this point in the history
  • Loading branch information
rvantonder committed Jun 19, 2019
1 parent b27f6ab commit 27d89d1
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/command_configuration.ml
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ type t =
{ sources : Command_input.t
; specifications : Specification.t list
; file_filters : string list option
; exclude_directory_prefix : string
; run_options : run_options
; output_printer : Printer.t
}
Expand Down Expand Up @@ -460,6 +461,7 @@ let create
let sources =
match input_source with
| Stdin -> `String (In_channel.input_all In_channel.stdin)
(* TODO(RVT): Unify exclude-dir handling. Currently exclude-dir option must be done while processing zip file in main.ml. *)
| Zip -> `Zip (Option.value_exn zip_file)
| Directory -> `Paths (parse_source_directories ?file_filters exclude_directory_prefix target_directory directory_depth)
in
Expand All @@ -484,6 +486,7 @@ let create
{ sources
; specifications
; file_filters
; exclude_directory_prefix
; run_options =
{ sequential
; verbose
Expand Down
1 change: 1 addition & 0 deletions src/command_configuration.mli
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type t =
{ sources : Command_input.t
; specifications : Specification.t list
; file_filters : string list option
; exclude_directory_prefix : string
; run_options : run_options
; output_printer : Printer.t
}
Expand Down
26 changes: 22 additions & 4 deletions src/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ let run
{ sources
; specifications
; file_filters
; exclude_directory_prefix
; run_options =
{ sequential
; verbose
Expand Down Expand Up @@ -237,12 +238,22 @@ let run
| `Zip zip_file ->
if sequential then
let zip_in = Zip.open_in zip_file in
let not_in_an_exclude_directory filename =
not (String.is_prefix ~prefix:exclude_directory_prefix filename)
in
let entries =
match file_filters with
| Some [] | None -> List.filter (Zip.entries zip_in) ~f:(fun { is_directory; _ } -> not is_directory)
| Some [] | None -> List.filter (Zip.entries zip_in) ~f:(fun { is_directory; filename; _ } ->
not is_directory && not_in_an_exclude_directory filename)
| Some suffixes ->
let has_acceptable_suffix filename =
List.exists suffixes ~f:(fun suffix -> String.is_suffix ~suffix filename)
in
let not_in_an_exclude_directory filename =
not (String.is_prefix ~prefix:exclude_directory_prefix filename)
in
List.filter (Zip.entries zip_in) ~f:(fun { is_directory; filename; _ } ->
not is_directory && List.exists suffixes ~f:(fun suffix -> String.is_suffix ~suffix filename))
not is_directory && not_in_an_exclude_directory filename && has_acceptable_suffix filename)
in
let number_of_matches =
List.fold ~init:0 entries ~f:(fun acc ({ filename; _ } as entry) ->
Expand All @@ -269,12 +280,19 @@ let run
in
let number_of_matches =
let zip_in = Zip.open_in zip_file in
let not_in_an_exclude_directory filename =
not (String.is_prefix ~prefix:exclude_directory_prefix filename)
in
let entries =
match file_filters with
| Some [] | None -> List.filter (Zip.entries zip_in) ~f:(fun { is_directory; _ } -> not is_directory)
| Some [] | None -> List.filter (Zip.entries zip_in) ~f:(fun { is_directory; filename; _ } ->
not is_directory && not_in_an_exclude_directory filename)
| Some suffixes ->
let has_acceptable_suffix filename =
List.exists suffixes ~f:(fun suffix -> String.is_suffix ~suffix filename)
in
List.filter (Zip.entries zip_in) ~f:(fun { is_directory; filename; _ } ->
not is_directory && List.exists suffixes ~f:(fun suffix -> String.is_suffix ~suffix filename))
not is_directory && not_in_an_exclude_directory filename && has_acceptable_suffix filename)
in
Zip.close_in zip_in;
try Scheduler.map_reduce scheduler ~init:0 ~map ~reduce:(+) entries
Expand Down
Binary file added test/example/zip-test/sample-repo.zip
Binary file not shown.
2 changes: 2 additions & 0 deletions test/example/zip-test/sample-repo/src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// src
func main() {}
2 changes: 2 additions & 0 deletions test/example/zip-test/sample-repo/vendor/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// vendor
func main() {}
34 changes: 33 additions & 1 deletion test/test_cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,36 @@ let%expect_test "diff_only" =
let result = read_source_from_stdin command source in
print_string result;
[%expect{|
-json-only-diff can only be supplied with -json-lines or -json-pretty. |}];
-json-only-diff can only be supplied with -json-lines or -json-pretty. |}]

let%expect_test "zip_exclude_dir_with_extension" =
let source = "doesn't matter" in
let zip = "example" ^/ "zip-test" ^/ "sample-repo.zip" in
let exclude_dir = "sample-repo/vendor" in
let command_args = Format.sprintf "'main' 'pain' .go -zip %s -sequential -diff -exclude-dir %s" zip exclude_dir in
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_source_from_stdin command source in
print_string result;
[%expect{|
--- sample-repo/src/main.go
+++ sample-repo/src/main.go
@@ -1,2 +1,2 @@
// src
-func main() {}
+func pain() {} |}]

let%expect_test "zip_exclude_dir_no_extension" =
let source = "doesn't matter" in
let zip = "example" ^/ "zip-test" ^/ "sample-repo.zip" in
let exclude_dir = "sample-repo/vendor" in
let command_args = Format.sprintf "'main' 'pain' -zip %s -sequential -diff -exclude-dir %s" zip exclude_dir in
let command = Format.sprintf "%s %s" binary_path command_args in
let result = read_source_from_stdin command source in
print_string result;
[%expect{|
--- sample-repo/src/main.go
+++ sample-repo/src/main.go
@@ -1,2 +1,2 @@
// src
-func main() {}
+func pain() {} |}];

0 comments on commit 27d89d1

Please sign in to comment.