Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f1caad0

Browse files
committedMay 11, 2021
Initial config-sensitive filter
1 parent f02ef86 commit f1caad0

File tree

12 files changed

+81
-51
lines changed

12 files changed

+81
-51
lines changed
 

‎src/alcotest-engine/alcotest_engine.ml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Unstable = struct
1010
module Cli = Cli.Unstable
1111
module Config = Config
1212
module Core = Core.Unstable
13+
module Filter = Filter
1314
module Source_code_position = Source_code_position
1415
module Tag = Tag
1516
module Test = Test

‎src/alcotest-engine/alcotest_engine.mli

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module Unstable : sig
5252
module Core = Core.Unstable
5353
module Cli = Cli.Unstable
5454
module Tag = Tag
55+
module Filter = Filter
5556
module Config = Config
5657
module Source_code_position = Source_code_position
5758
end

‎src/alcotest-engine/config.ml

+20-3
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ module Key = struct
205205
module V2 = struct
206206
type t = filter_v2
207207

208-
let of_v1 ~quick_only position_filter tags =
208+
let of_v1 ~quick_only position_filter _ tags =
209209
let speed_level =
210210
match Tag.Set.find Tag.Speed_level.tag tags with
211211
| Some `Slow -> if quick_only then `Skip else `Run
@@ -223,7 +223,24 @@ module Key = struct
223223

224224
type t = [ `V1 of V1.t | `V2 of V2.t ]
225225

226-
let default = Tag.Filter.default
226+
let default =
227+
(* TODO: duplicated *)
228+
let only_if _ s =
229+
match Tag.Set.find Tag.Predicate.tag s with
230+
| Some p -> p ()
231+
| None -> `Run
232+
in
233+
234+
let quick_only_config c s =
235+
match Tag.Set.find Tag.Speed_level.tag s with
236+
| Some `Slow when c#quick_only -> `Skip
237+
| _ -> `Run
238+
in
239+
240+
let ( ++ ) f g a b =
241+
match (f a b, g a b) with `Run, `Run -> `Run | _, _ -> `Skip
242+
in
243+
only_if ++ quick_only_config
227244
end
228245
end
229246

@@ -313,8 +330,8 @@ let apply_defaults ~default_log_dir : User.t -> t =
313330
verbose;
314331
compact;
315332
tail_errors;
316-
quick_only;
317333
show_errors;
334+
quick_only;
318335
json;
319336
filter;
320337
log_dir;

‎src/alcotest-engine/config_intf.ml

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
module Types = struct
22
type bound = [ `Unlimited | `Limit of int ]
3-
type filter_v1 = name:string -> index:int -> [ `Run | `Skip ]
4-
type filter_v2 = Tag.Filter.t
5-
type filter = [ `V1 of filter_v1 | `V2 of filter_v2 ]
3+
type filter_result = [ `Run | `Skip ]
64

7-
type t =
5+
type filter_v1 = name:string -> index:int -> filter_result
6+
7+
and filter_v2 = t -> Tag.Set.t -> filter_result
8+
9+
and filter = [ `V1 of filter_v1 | `V2 of filter_v2 ]
10+
11+
and t =
812
< and_exit : bool
913
; verbose : bool
1014
; compact : bool

‎src/alcotest-engine/core.ml

+9-9
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,10 @@ module Make (P : Platform.MAKER) (M : Monad.S) = struct
198198
let+ (result : Run_result.t) = with_captured_logs t index test args in
199199
(* Store errors *)
200200
let errored : bool =
201-
let error, errored =
202-
if Run_result.is_failure result then
203-
([ Fmt.const (pp_error t) result ], true)
204-
else ([], false)
205-
in
206-
t.errors <- error @ t.errors;
207-
errored
201+
if Run_result.is_failure result then (
202+
t.errors <- Fmt.const (pp_error t) result :: t.errors;
203+
true)
204+
else false
208205
in
209206
(* Show any remaining test output before the event *)
210207
Fmt.(flush stdout ());
@@ -238,7 +235,8 @@ module Make (P : Platform.MAKER) (M : Monad.S) = struct
238235
();
239236
let start_time = P.time () in
240237
let+ results =
241-
Suite.foldi_until t.suite ~filter:t.config#filter
238+
Suite.foldi_until t.suite
239+
~filter:(Filter.apply t.config#filter t.config)
242240
~init:{ tests_so_far = 0; first_error = None; failures = 0 }
243241
~finish:(fun x -> x)
244242
~group:(fun _ctx acc _ ->
@@ -286,7 +284,9 @@ module Make (P : Platform.MAKER) (M : Monad.S) = struct
286284
let config = Config.apply_defaults ~default_log_dir config in
287285
let suite = Suite.of_tests_exn ~name ~loc:here tests in
288286
let* at_least_one_test =
289-
Suite.foldi_until suite ~filter:config#filter ~init:()
287+
Suite.foldi_until suite
288+
~filter:(Filter.apply config#filter config)
289+
~init:()
290290
~finish:(fun () -> false)
291291
~test:
292292
(fun _ () -> function

‎src/alcotest-engine/filter.ml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type t = Config.filter_v2
2+
3+
let v f _ = f
4+
5+
let ( ++ ) f g a b =
6+
match (f a b, g a b) with `Run, `Run -> `Run | _, _ -> `Skip
7+
8+
let only_if _ s =
9+
match Tag.Set.find Tag.Predicate.tag s with Some p -> p () | None -> `Run
10+
11+
let quick_only_config c s =
12+
match Tag.Set.find Tag.Speed_level.tag s with
13+
| Some `Slow when c#quick_only -> `Skip
14+
| _ -> `Run
15+
16+
let default = only_if ++ quick_only_config
17+
let apply f = f

‎src/alcotest-engine/filter.mli

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type t = Config.filter_v2
2+
3+
val v : (Tag.Set.t -> [ `Run | `Skip ]) -> t
4+
5+
val ( ++ ) : t -> t -> t
6+
(** [f ++ g] is the filter that runs only tests that are run by both [f] {i and}
7+
[g]. *)
8+
9+
val default : t
10+
11+
(** Internal: *)
12+
13+
val apply : t -> Config.t -> Tag.Set.t -> [ `Run | `Skip ]
14+
val quick_only_config : t
15+
val only_if : t

‎src/alcotest-engine/model.mli

+6-7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ end
3131

3232
module Suite (M : Monad.S) : sig
3333
type 'a test
34+
type filter_result := [ `Run | `Skip ]
35+
type filter := Tag.Set.t -> filter_result
3436

3537
val test :
3638
name:string ->
@@ -49,12 +51,9 @@ module Suite (M : Monad.S) : sig
4951
type 'a t
5052

5153
val foldi_until :
52-
filter:Tag.Filter.t ->
54+
filter:filter ->
5355
?group:
54-
(Index.t ->
55-
'acc ->
56-
[ `Run | `Skip ] ->
57-
('acc, 'final) continue_or_stop M.t) ->
56+
(Index.t -> 'acc -> filter_result -> ('acc, 'final) continue_or_stop M.t) ->
5857
?test:
5958
(Index.t ->
6059
'acc ->
@@ -68,8 +67,8 @@ module Suite (M : Monad.S) : sig
6867
[filter] predicate defined over node {!Tag}s. *)
6968

7069
val fold :
71-
filter:Tag.Filter.t ->
72-
group:('acc -> [ `Run | `Skip ] -> 'acc) ->
70+
filter:filter ->
71+
group:('acc -> filter_result -> 'acc) ->
7372
test:('acc -> [ `Run of 'a -> unit M.t | `Skip ] -> 'acc) ->
7473
init:'acc ->
7574
'a t ->

‎src/alcotest-engine/tag.ml

-12
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ module Speed_level = struct
9797

9898
let quick = V (tag, `Quick)
9999
let slow = V (tag, `Slow)
100-
101-
let without_slow s =
102-
match Set.find tag s with Some `Slow -> `Skip | _ -> `Run
103100
end
104101

105102
module Predicate = struct
@@ -108,17 +105,8 @@ module Predicate = struct
108105
let tag =
109106
Key.create ~name:"Predicate" ~pp_data:(fun ppf _ ->
110107
Fmt.pf ppf "Predicate <...>")
111-
112-
let only_if s = match Set.find tag s with Some p -> p () | None -> `Run
113108
end
114109

115110
module Position = struct
116111
let tag = Key.create ~name:"index" ~pp_data:Fmt.(using snd int)
117112
end
118-
119-
module Filter = struct
120-
type t = Set.t -> [ `Run | `Skip ]
121-
122-
let ( ++ ) f g x = match (f x, g x) with `Run, `Run -> `Run | _, _ -> `Skip
123-
let default = Predicate.only_if (* TODO: be sensitive to `quick_only` *)
124-
end

‎src/alcotest-engine/tag.mli

-12
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,13 @@ module Set : sig
3131
end
3232
with type tag := t
3333

34-
module Filter : sig
35-
type t = Set.t -> [ `Run | `Skip ]
36-
37-
val ( ++ ) : t -> t -> t
38-
(** [f ++ g] is the filter that runs only tests that are run by both [f]
39-
{i and} [g]. *)
40-
41-
val default : t
42-
end
43-
4434
module Speed_level : sig
4535
type tag
4636
type t = [ `Quick | `Slow ]
4737

4838
val tag : t Key.t
4939
val quick : tag
5040
val slow : tag
51-
val without_slow : Filter.t
5241
end
5342
with type tag := t
5443

@@ -60,5 +49,4 @@ module Predicate : sig
6049
type t = unit -> [ `Run | `Skip ]
6150

6251
val tag : t Key.t
63-
val only_if : Filter.t
6452
end

‎src/alcotest/alcotest.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ module Unstable : sig
8484
?quick_only:bool ->
8585
?show_errors:bool ->
8686
?json:bool ->
87-
?filter:Tag.Filter.t ->
87+
?filter:Filter.t ->
8888
?bail:bool ->
8989
?log_dir:string ->
9090
unit ->

‎test/e2e/alcotest/passing/quick_only.expected

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ Testing `test/e2e/alcotest/passing/quick_only.ml'.
22
This run has ID `<uuid>'.
33

44
[PASS] test-a › Quick
5-
[PASS] test-a › Slow
6-
[PASS] test-b › Slow
5+
[SKIP] test-a › Slow
6+
[SKIP] test-b › Slow
77
[PASS] test-b › Quick
88

99
Full test results in `<build-context>/_build/_tests/<test-dir>'.
10-
Test Successful in <test-duration>s. 4 tests run.
10+
Test Successful in <test-duration>s. 2 tests run.

0 commit comments

Comments
 (0)
Please sign in to comment.