Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
osnap.ml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322(*****************************************************************************) (* Open Source License *) (* Copyright (c) 2021 Valentin Chaboche *) (* Copyright (c) 2021 Nomadic Labs, <contact@nomadic-labs.com> *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) (* to deal in the Software without restriction, including without limitation *) (* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) (* and/or sell copies of the Software, and to permit persons to whom the *) (* Software is furnished to do so, subject to the following conditions: *) (* *) (* The above copyright notice and this permission notice shall be included *) (* in all copies or substantial portions of the Software. *) (* *) (* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) (* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) (* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) (* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) (* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) (* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) module Spec = Spec module Test = struct type ('a, 'b, 'c) cell = { path : string; name : string; spec : ('a -> 'b, 'c) Spec.t; f : 'a -> 'b; count : int; rand : Random.State.t; } type t = Test : ('a, 'b, 'c) cell -> t let make ?(count = 10) ?(rand = Random.State.make_self_init ()) ?path ~spec ~name f = let path = Option.value ~default:(Common.opt_path name) path in Test { path; spec; f; count; name; rand } end module Color = struct type color = [ `Red | `Green ] let int_of_color = function `Red -> 1 | `Green -> 2 let pp_str ?(bold = false) fmt (color : color) s = let open Format in let n = int_of_color color in let () = if bold then fprintf fmt "\x1b[3%d;1m" n else fprintf fmt "\x1b[3%dm" n in let () = pp_print_string fmt s in fprintf fmt "\x1b[0m" end module Runner = struct type mode = Interactive | Promote | Error type encoding = Snapshot.mode = Marshal | Data_encoding let mode_from_string s = let s = String.lowercase_ascii s in match s with | "interactive" -> Interactive | "promote" -> Promote | "error" | _ -> Error let encoding_from_string s = let s = String.lowercase_ascii s in match s with "data_encoding" -> Data_encoding | "marshal" | _ -> Marshal type res = [ `Passed of string | `Promoted of string | `Ignored of string | `Error of string * string ] list let get_passed xs = List.filter (function `Passed _ -> true | _ -> false) xs let get_promoted xs = List.filter (function `Promoted _ -> true | _ -> false) xs let get_ignored xs = List.filter (function `Ignored _ -> true | _ -> false) xs let get_errors xs = List.filter (function `Error _ -> true | _ -> false) xs let sep = String.make 68 '-' let pp_failure fmt color = let s = "failure" in if color then Color.(pp_str ~bold:true fmt `Red s) else Format.pp_print_string fmt s let pp_success fmt color = let s = "success" in if color then Color.(pp_str fmt `Green "success") else Format.pp_print_string fmt s let pp_error fmt ~color = function | `Error (_, msg) -> Format.fprintf fmt "@.--- %a %s@.@.%s@.@." pp_failure color sep msg | _ -> () let pp_recap fmt ~color passed promoted ignored errors = let open Format in let n = List.(length passed + length promoted + length ignored + length errors) in let strs = [ (let n = List.length passed in if n > 0 then Option.some @@ Printf.sprintf "%d passed" n else None); (let n = List.length errors in if n > 0 then Option.some @@ Printf.sprintf "%d error(s)" n else None); (let n = List.length promoted in if n > 0 then Option.some @@ Printf.sprintf "%d promoted" n else None); (let n = List.length ignored in if n > 0 then Option.some @@ Printf.sprintf "%d ignored" n else None); ] |> List.filter_map (fun x -> x) in let pp_aux fmt strs = pp_print_list ~pp_sep:(fun fmt () -> pp_print_string fmt ", ") pp_print_string fmt strs in let pp_res fmt errors = if List.length errors > 0 then pp_failure fmt color else pp_success fmt color in fprintf fmt "-----------%s@.%a: ran %d test%s (%a)@." sep pp_res errors n (if n = 1 then "" else "s") pp_aux strs let pp_res fmt ~color xs = let passed = get_passed xs in let promoted = get_promoted xs in let ignored = get_ignored xs in let errors = get_errors xs in let () = match errors with x :: _ -> pp_error fmt ~color x | _ -> () in pp_recap fmt ~color passed promoted ignored errors let input_msg fmt () = let open Format in fprintf fmt "@.@.%a@." pp_print_string "Do you want to promote this new snapshot? [Y\\n]" let rec take_input fmt () = let () = input_msg fmt () in match read_line () with | "Y" | "" -> true | "n" -> false | _ -> take_input fmt () let interactive ~path ~diff ~name fmt encoding spec (snapshot, snapshot_str) = match diff with | Diff.Same -> `Passed name | _ -> let () = Format.pp_print_string fmt snapshot_str in if take_input fmt () then let () = Snapshot.encode ~spec ~mode:encoding ~path snapshot in `Promoted name else `Ignored name let error ~path ~diff ~name = match diff with | Diff.Same -> `Passed name | Diff.(New _) -> let msg = Printf.sprintf "Error: no previous snapshot at %s" (Common.full_path path) in `Error (name, msg) | Diff.Diff diff -> `Error (name, diff) let promote ~path ~diff ~name encoding spec snapshot = match diff with | Diff.Same -> `Passed name | Diff.(New _) | Diff.Diff _ -> let () = Snapshot.encode ~spec ~mode:encoding ~path snapshot in `Promoted name let map o f = Option.map f o let ( >>| ) = map type ('fn, 'r) intermediate_snapshot = { snap : ('fn, 'r) Snapshot.t; text : string; path : string; } let prev spec mode path : ('a, 'b) intermediate_snapshot option = Snapshot.decode_opt ~spec ~mode ~path () >>| fun snap -> Snapshot.to_string spec snap |> fun text -> path ^ ".prev" |> fun path -> { snap; text; path } let next ~f ~rand ~name ~spec ~count ~path prev = let snap = match prev with | Some { snap; _ } -> Snapshot.create_from_snapshot snap f | None -> Snapshot.create ~rand ~name ~spec ~f count in let text = Snapshot.to_string spec snap in let path = path ^ ".next" in { snap; text; path } let diff path prev next = let diff_path = path ^ ".diff" in ( Diff.diff ~path:diff_path ~prev:(prev >>| fun prev -> prev.path) ~next:next.path, diff_path ) let prepare_run prev next = Option.iter (fun prev -> Common.write prev.path prev.text) prev ; Common.write next.path next.text let clean_run prev next diff_path = let clean fd = if Sys.file_exists fd then Sys.remove fd in Option.iter (fun prev -> clean prev.path) prev ; clean next.path ; clean diff_path let run encoding mode fmt test = let Test.(Test { spec; path; name; rand; count; f; _ }) = test in let prev = prev spec encoding path in let next = next ~f ~rand ~name ~spec ~count ~path prev in let () = prepare_run prev next in let (diff, diff_path) = diff path prev next in let res = match mode with | Error -> error ~path ~diff ~name | Promote -> promote ~path ~diff ~name encoding spec next.snap | Interactive -> interactive ~path ~diff ~name fmt encoding spec (next.snap, next.text) in let () = clean_run prev next diff_path in res let run_tests_with_res encoding mode out tests : res * int = let res = List.map (run encoding mode out) tests in let status = if List.exists (function `Error _ -> true | _ -> false) res then 1 else 0 in (res, status) let run_tests ?(encoding = Marshal) ?(mode = Error) ?(out = Format.std_formatter) ?(color = true) tests = let (res, status) = run_tests_with_res encoding mode out tests in let () = pp_res out ~color res in status module Cli = struct type args = { mode : mode; color : bool; encoding : encoding } let parse argv = let mode = ref "" in let color = ref false in let encoding = ref "" in let options = Arg.align [ ("--mode", Arg.Set_string mode, " set runner mode"); ("-m", Arg.Set_string mode, " set runner mode"); ("--color", Arg.Set color, " set color output"); ("-c", Arg.Set color, " set color output"); ("-encoding", Arg.Set_string encoding, " set encoding mode"); ("-e", Arg.Set_string encoding, " set encoding mode"); ] in let () = Arg.parse_argv argv options (fun _ -> ()) "run osnap suite" in let mode = mode_from_string !mode in let encoding = encoding_from_string !encoding in { mode; color = !color; encoding } end let run_tests_main ?(argv = Sys.argv) tests = try let cli_args = Cli.parse argv in exit (run_tests ~mode:cli_args.mode ~color:cli_args.color tests) with | Arg.Bad msg -> print_endline msg ; exit 1 | Arg.Help msg -> print_endline msg ; exit 0 end