Skip to content

Commit

Permalink
reduce diff
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Aug 21, 2019
1 parent 729c8dc commit 3c0380a
Showing 1 changed file with 39 additions and 49 deletions.
88 changes: 39 additions & 49 deletions test/passing/dune
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
(* -*- tuareg -*- *)

type out = {ref: string option; opts: string option}
type setup =
{ mutable has_ref: bool
; mutable has_opts: bool
; mutable base_file: string option
; mutable extra_deps: string list }

type setup = {mutable outputs: out list; mutable extra_deps: string list}
let default_setup () =
{has_ref= false; has_opts= false; base_file= None; extra_deps= []}

let read_lines fn =
let rec aux acc ic =
Expand All @@ -23,46 +28,34 @@ let register_file tests fname =
(* foo_file-some_variant.ml should derive from foo_file.ml *)
match String.index_opt test_name '-' with
| None ->
let s = {outputs= []; extra_deps= []} in
let s =
{ has_ref= false
; has_opts= false
; base_file= None
; extra_deps= [] }
in
Hashtbl.add tests src_test_name s ;
s
| Some i -> (
let src_test_name = String.sub test_name 0 i ^ "." ^ ext in
match Hashtbl.find tests src_test_name with
let base_src_test_name =
String.sub test_name 0 i ^ "." ^ ext
in
match Hashtbl.find tests base_src_test_name with
| setup -> setup
| exception Not_found ->
let s = {outputs= []; extra_deps= []} in
let s =
{ has_ref= false
; has_opts= false
; base_file= Some base_src_test_name
; extra_deps= [] }
in
Hashtbl.add tests src_test_name s ;
s ) )
in
match rest with
| [] -> ()
| ["opts"] ->
let ref_name = test_name ^ "." ^ ext ^ ".ref" in
let ref_exists {ref; _} =
Option.equal String.equal ref (Some ref_name)
in
let outputs =
if List.exists ref_exists setup.outputs then
let opts = Some fname in
let update o = if ref_exists o then {o with opts} else o in
List.map update setup.outputs
else {ref= None; opts= Some fname} :: setup.outputs
in
setup.outputs <- outputs
| ["ref"] ->
let opts_name = test_name ^ "." ^ ext ^ ".opts" in
let opts_exists {opts; _} =
Option.equal String.equal opts (Some opts_name)
in
let outputs =
if List.exists opts_exists setup.outputs then
let ref = Some fname in
let update o = if opts_exists o then {o with ref} else o in
List.map update setup.outputs
else {ref= Some fname; opts= None} :: setup.outputs
in
setup.outputs <- outputs
| ["opts"] -> setup.has_opts <- true
| ["ref"] -> setup.has_ref <- true
| ["deps"] -> setup.extra_deps <- read_lines fname
| _ -> invalid_arg fname )
| _ -> ()
Expand All @@ -71,20 +64,18 @@ let register_file tests fname =

let emit_test buffer test_name setup =
let open Printf in
let opts =
if setup.has_opts then sprintf " %%{read-lines:%s.opts}" test_name
else ""
in
let ref_name = if setup.has_ref then test_name ^ ".ref" else test_name in
let base_test_name = Option.value setup.base_file ~default:test_name in
let extra_deps = String.concat " " setup.extra_deps in
List.iteri
(fun i output ->
let opts =
match output.opts with
| Some o -> sprintf " %%{read-lines:%s}" o
| None -> ""
in
let ref_name = Option.value output.ref ~default:test_name in
Printf.ksprintf
(Buffer.add_string buffer)
{|
Printf.ksprintf
(Buffer.add_string buffer)
{|
(rule
(targets %s.%i.output)
(targets %s.output)
(deps (:input %s) .ocamlformat %s)
(action
(setenv OCAMLFORMAT "max-iters=2"
Expand All @@ -93,12 +84,11 @@ let emit_test buffer test_name setup =

(alias
(name runtest)
(deps %s %s.%i.output)
(action (diff %s %s.%i.output)))
(deps %s %s.output)
(action (diff %s %s.output)))
|}
test_name i test_name extra_deps opts ref_name test_name i ref_name
test_name i)
setup.outputs
test_name base_test_name extra_deps opts ref_name test_name ref_name
test_name

let tests = Hashtbl.create 64

Expand Down

0 comments on commit 3c0380a

Please sign in to comment.