2022-03-08 17:03:14 +03:00
|
|
|
(* This file is part of the Catala build system, a specification language for
|
|
|
|
tax and social benefits computation rules. Copyright (C) 2020 Inria,
|
|
|
|
contributors: Denis Merigoux <denis.merigoux@inria.fr>, Emile Rolley
|
|
|
|
<emile.rolley@tuta.io>
|
2022-02-25 20:00:10 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
the License at
|
2022-02-25 20:00:10 +03:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
License for the specific language governing permissions and limitations under
|
2022-02-25 20:00:10 +03:00
|
|
|
the License. *)
|
|
|
|
|
|
|
|
open Cmdliner
|
|
|
|
open Utils
|
|
|
|
open Ninja_utils
|
|
|
|
module Nj = Ninja_utils
|
|
|
|
|
|
|
|
(** {1 Command line interface} *)
|
|
|
|
|
|
|
|
let files_or_folders =
|
|
|
|
Arg.(
|
2022-03-08 17:03:14 +03:00
|
|
|
non_empty & pos_right 0 file []
|
|
|
|
& info [] ~docv:"FILE(S)" ~doc:"File(s) or folder(s) to process")
|
2022-02-25 20:00:10 +03:00
|
|
|
|
|
|
|
let command =
|
|
|
|
Arg.(
|
|
|
|
required
|
|
|
|
& pos 0 (some string) None
|
|
|
|
& info [] ~docv:"COMMAND" ~doc:"Command selection among: test, run")
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let debug =
|
|
|
|
Arg.(value & flag & info [ "debug"; "d" ] ~doc:"Prints debug information")
|
2022-02-25 20:00:10 +03:00
|
|
|
|
|
|
|
let reset_test_outputs =
|
|
|
|
Arg.(
|
|
|
|
value & flag
|
|
|
|
& info [ "r"; "reset" ]
|
|
|
|
~doc:
|
2022-03-08 17:03:14 +03:00
|
|
|
"Used with the `test` command, resets the test output to whatever is \
|
|
|
|
output by the Catala compiler.")
|
2022-02-25 20:00:10 +03:00
|
|
|
|
|
|
|
let catalac =
|
|
|
|
Arg.(
|
|
|
|
value
|
|
|
|
& opt (some string) None
|
2022-03-08 17:03:14 +03:00
|
|
|
& info [ "e"; "exe" ] ~docv:"EXE"
|
|
|
|
~doc:"Catala compiler executable, defaults to `catala`")
|
2022-02-25 20:00:10 +03:00
|
|
|
|
|
|
|
let ninja_output =
|
|
|
|
Arg.(
|
|
|
|
value
|
2022-02-25 22:03:58 +03:00
|
|
|
& opt (some string) None
|
2022-02-25 20:00:10 +03:00
|
|
|
& info [ "o"; "output" ] ~docv:"OUTPUT"
|
|
|
|
~doc:
|
2022-03-08 17:03:14 +03:00
|
|
|
"$(i, OUTPUT) is the file that will contain the build.ninja file \
|
|
|
|
output. If not specified, the build.ninja file will be outputed in \
|
|
|
|
the temporary directory of the system.")
|
2022-02-25 20:00:10 +03:00
|
|
|
|
|
|
|
let scope =
|
|
|
|
Arg.(
|
|
|
|
value
|
|
|
|
& opt (some string) None
|
|
|
|
& info [ "s"; "scope" ] ~docv:"SCOPE"
|
2022-03-08 17:03:14 +03:00
|
|
|
~doc:
|
|
|
|
"Used with the `run` command, selects which scope of a given Catala \
|
|
|
|
file to run.")
|
2022-02-25 20:00:10 +03:00
|
|
|
|
2022-04-14 12:47:18 +03:00
|
|
|
let makeflags =
|
|
|
|
Arg.(
|
|
|
|
value
|
|
|
|
& opt (some string) None
|
|
|
|
& info [ "makeflags" ] ~docv:"LANG"
|
|
|
|
~doc:
|
|
|
|
"Provides the contents of a $(i, MAKEFLAGS) variable to pass on to \
|
|
|
|
Ninja. Currently recognizes the -i and -j options.")
|
|
|
|
|
2022-02-25 20:00:10 +03:00
|
|
|
let catala_opts =
|
|
|
|
Arg.(
|
|
|
|
value
|
|
|
|
& opt (some string) None
|
2022-03-08 17:03:14 +03:00
|
|
|
& info [ "c"; "catala-opts" ] ~docv:"LANG"
|
|
|
|
~doc:"Options to pass to the Catala compiler")
|
2022-02-25 20:00:10 +03:00
|
|
|
|
|
|
|
let clerk_t f =
|
|
|
|
Term.(
|
2022-04-14 12:47:18 +03:00
|
|
|
const f $ files_or_folders $ command $ catalac $ catala_opts $ makeflags
|
|
|
|
$ debug $ scope $ reset_test_outputs $ ninja_output)
|
2022-02-25 20:00:10 +03:00
|
|
|
|
|
|
|
let version = "0.5.0"
|
|
|
|
|
|
|
|
let info =
|
|
|
|
let doc =
|
2022-03-08 17:03:14 +03:00
|
|
|
"Build system for Catala, a specification language for tax and social \
|
|
|
|
benefits computation rules."
|
2022-02-25 20:00:10 +03:00
|
|
|
in
|
|
|
|
let man =
|
|
|
|
[
|
|
|
|
`S Manpage.s_description;
|
|
|
|
`P
|
2022-03-08 17:03:14 +03:00
|
|
|
"$(b,clerk) is a build system for Catala, a specification language for \
|
|
|
|
tax and social benefits computation rules";
|
2022-02-25 20:00:10 +03:00
|
|
|
`S Manpage.s_commands;
|
|
|
|
`I
|
|
|
|
( "test",
|
2022-03-08 17:03:14 +03:00
|
|
|
"Tests a Catala source file given expected outputs provided in a \
|
|
|
|
directory called `output` at the same level that the tested file. \
|
|
|
|
If the tested file is `foo.catala_en`, then `output` should contain \
|
|
|
|
expected output files like `foo.catala_en.$(i,BACKEND)` where \
|
|
|
|
$(i,BACKEND) is chosen among: `Interpret`, `Dcalc`, `Scalc`, \
|
|
|
|
`Lcalc`, `Typecheck, `Scopelang`, `html`, `tex`, `py`, `ml` and `d` \
|
|
|
|
(for Makefile dependencies). For the `Interpret` backend, the scope \
|
|
|
|
to test is selected by naming the expected output file \
|
|
|
|
`foo.catala_en.$(i,SCOPE).interpret`. When the argument of \
|
|
|
|
$(b,clerk) is a folder, it recursively looks for Catala files \
|
|
|
|
coupled with `output` directories and matching expected output on \
|
|
|
|
which to perform tests." );
|
2022-02-25 20:00:10 +03:00
|
|
|
`I
|
2022-03-08 17:03:14 +03:00
|
|
|
( "run",
|
|
|
|
"Runs the Catala interpreter on a given scope of a given file. See \
|
|
|
|
the `-s` option." );
|
2022-02-25 20:00:10 +03:00
|
|
|
`S Manpage.s_authors;
|
|
|
|
`P "Denis Merigoux <denis.merigoux@inria.fr>";
|
|
|
|
`P "Emile Rolley <emile.rolley@tuta.io>";
|
|
|
|
`S Manpage.s_examples;
|
|
|
|
`P "Typical usage:";
|
|
|
|
`Pre "clerk test file.catala_en";
|
|
|
|
`S Manpage.s_bugs;
|
2022-03-08 17:03:14 +03:00
|
|
|
`P
|
|
|
|
"Please file bug reports at https://github.com/CatalaLang/catala/issues";
|
2022-02-25 20:00:10 +03:00
|
|
|
]
|
|
|
|
in
|
|
|
|
let exits = Term.default_exits @ [ Term.exit_info ~doc:"on error." 1 ] in
|
|
|
|
Term.info "clerk" ~version ~doc ~exits ~man
|
|
|
|
|
|
|
|
(**{1 Testing}*)
|
|
|
|
|
|
|
|
type expected_output_descr = {
|
|
|
|
base_filename : string;
|
|
|
|
output_dir : string;
|
|
|
|
complete_filename : string;
|
|
|
|
backend : Cli.backend_option;
|
|
|
|
scope : string option;
|
|
|
|
}
|
|
|
|
|
|
|
|
let catala_suffix_regex = Re.Pcre.regexp "\\.catala_(\\w){2}"
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let filename_to_expected_output_descr (output_dir : string) (filename : string)
|
|
|
|
: expected_output_descr option =
|
2022-02-25 20:00:10 +03:00
|
|
|
let complete_filename = filename in
|
|
|
|
let first_extension = Filename.extension filename in
|
|
|
|
let filename = Filename.remove_extension filename in
|
|
|
|
let backend =
|
|
|
|
match String.lowercase_ascii first_extension with
|
|
|
|
| ".dcalc" -> Some Cli.Dcalc
|
|
|
|
| ".d" -> Some Cli.Makefile
|
|
|
|
| ".html" -> Some Cli.Html
|
|
|
|
| ".interpret" -> Some Cli.Interpret
|
|
|
|
| ".lcalc" -> Some Cli.Lcalc
|
|
|
|
| ".ml" -> Some Cli.OCaml
|
|
|
|
| ".proof" -> Some Cli.Proof
|
|
|
|
| ".py" -> Some Cli.Python
|
|
|
|
| ".scalc" -> Some Cli.Scalc
|
|
|
|
| ".scopelang" -> Some Cli.Scopelang
|
|
|
|
| ".tex" -> Some Cli.Latex
|
|
|
|
| ".typecheck" -> Some Cli.Typecheck
|
|
|
|
| _ -> None
|
|
|
|
in
|
|
|
|
match backend with
|
|
|
|
| None -> None
|
|
|
|
| Some backend ->
|
|
|
|
let second_extension = Filename.extension filename in
|
|
|
|
let base_filename, scope =
|
2022-03-08 17:03:14 +03:00
|
|
|
if Re.Pcre.pmatch ~rex:catala_suffix_regex second_extension then
|
|
|
|
(filename, None)
|
2022-02-25 20:00:10 +03:00
|
|
|
else
|
|
|
|
let scope_name_regex = Re.Pcre.regexp "\\.(.+)" in
|
2022-03-08 17:03:14 +03:00
|
|
|
let scope_name =
|
|
|
|
(Re.Pcre.extract ~rex:scope_name_regex second_extension).(1)
|
|
|
|
in
|
2022-02-25 20:00:10 +03:00
|
|
|
(Filename.remove_extension filename, Some scope_name)
|
|
|
|
in
|
|
|
|
Some { output_dir; complete_filename; base_filename; backend; scope }
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [readdir_sort dirname] returns the sorted subdirectories of [dirname] in an
|
|
|
|
array or an empty array if the [dirname] doesn't exist. *)
|
2022-03-07 20:02:12 +03:00
|
|
|
let readdir_sort (dirname : string) : string array =
|
|
|
|
try
|
|
|
|
let dirs = Sys.readdir dirname in
|
|
|
|
Array.fast_sort String.compare dirs;
|
|
|
|
dirs
|
|
|
|
with Sys_error _ -> Array.make 0 ""
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** Given a file, looks in the relative [output] directory if there are files
|
|
|
|
with the same base name that contain expected outputs for different *)
|
2022-02-25 20:00:10 +03:00
|
|
|
let search_for_expected_outputs (file : string) : expected_output_descr list =
|
|
|
|
let output_dir = Filename.dirname file ^ Filename.dir_sep ^ "output/" in
|
2022-03-07 20:02:12 +03:00
|
|
|
let output_files = readdir_sort output_dir in
|
2022-02-25 20:00:10 +03:00
|
|
|
List.filter_map
|
|
|
|
(fun output_file ->
|
|
|
|
match filename_to_expected_output_descr output_dir output_file with
|
|
|
|
| None -> None
|
|
|
|
| Some expected_output ->
|
2022-03-08 17:03:14 +03:00
|
|
|
if expected_output.base_filename = Filename.basename file then
|
|
|
|
Some expected_output
|
2022-02-25 20:00:10 +03:00
|
|
|
else None)
|
|
|
|
(Array.to_list output_files)
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let add_reset_rules_aux
|
|
|
|
~(redirect : string)
|
|
|
|
~(with_scope_output_rule : string)
|
|
|
|
~(without_scope_output_rule : string)
|
|
|
|
(catala_exe_opts : string)
|
|
|
|
(rules : Rule.t Nj.RuleMap.t) : Rule.t Nj.RuleMap.t =
|
2022-02-25 20:00:10 +03:00
|
|
|
let reset_common_cmd_exprs =
|
|
|
|
Nj.Expr.
|
|
|
|
[
|
|
|
|
Var "catala_cmd";
|
|
|
|
Var "tested_file";
|
|
|
|
Var "extra_flags";
|
|
|
|
Lit "--unstyled";
|
|
|
|
Lit redirect;
|
|
|
|
Var "expected_output";
|
|
|
|
Lit "2>&1";
|
2022-04-04 16:56:45 +03:00
|
|
|
Lit "| true";
|
2022-02-25 20:00:10 +03:00
|
|
|
]
|
|
|
|
in
|
|
|
|
let reset_with_scope_rule =
|
|
|
|
Nj.Rule.make with_scope_output_rule
|
|
|
|
~command:
|
2022-03-08 17:03:14 +03:00
|
|
|
Nj.Expr.(
|
|
|
|
Seq
|
|
|
|
([ Lit catala_exe_opts; Lit "-s"; Var "scope" ]
|
|
|
|
@ reset_common_cmd_exprs))
|
2022-02-25 20:00:10 +03:00
|
|
|
~description:
|
|
|
|
Nj.Expr.(
|
|
|
|
Seq
|
|
|
|
[
|
|
|
|
Lit "RESET scope";
|
|
|
|
Var "scope";
|
|
|
|
Lit "of file";
|
|
|
|
Var "tested_file";
|
|
|
|
Lit "with the";
|
|
|
|
Var "catala_cmd";
|
|
|
|
Lit "command";
|
|
|
|
])
|
|
|
|
in
|
|
|
|
let reset_without_scope_rule =
|
|
|
|
Nj.Rule.make without_scope_output_rule
|
|
|
|
~command:Nj.Expr.(Seq (Lit catala_exe_opts :: reset_common_cmd_exprs))
|
|
|
|
~description:
|
|
|
|
Nj.Expr.(
|
|
|
|
Seq
|
|
|
|
[
|
|
|
|
Lit "RESET";
|
|
|
|
Lit "file";
|
|
|
|
Var "tested_file";
|
|
|
|
Lit "with the";
|
|
|
|
Var "catala_cmd";
|
|
|
|
Lit "command";
|
|
|
|
])
|
|
|
|
in
|
|
|
|
Nj.RuleMap.(
|
|
|
|
rules
|
|
|
|
|> add reset_with_scope_rule.name reset_with_scope_rule
|
|
|
|
|> add reset_without_scope_rule.name reset_without_scope_rule)
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let add_test_rules_aux
|
|
|
|
~(test_common_cmd_exprs : Expr.t list)
|
|
|
|
~(with_scope_output_rule : string)
|
|
|
|
~(without_scope_output_rule : string)
|
|
|
|
(catala_exe_opts : string)
|
|
|
|
(rules : Rule.t Nj.RuleMap.t) : Rule.t Nj.RuleMap.t =
|
2022-02-25 20:00:10 +03:00
|
|
|
let test_with_scope_rule =
|
|
|
|
Nj.Rule.make with_scope_output_rule
|
|
|
|
~command:
|
2022-03-08 17:03:14 +03:00
|
|
|
Nj.Expr.(
|
|
|
|
Seq
|
|
|
|
([ Lit catala_exe_opts; Lit "-s"; Var "scope" ]
|
|
|
|
@ test_common_cmd_exprs))
|
2022-02-25 20:00:10 +03:00
|
|
|
~description:
|
|
|
|
Nj.Expr.(
|
|
|
|
Seq
|
|
|
|
[
|
|
|
|
Lit "TEST scope";
|
|
|
|
Var "scope";
|
|
|
|
Lit "of file";
|
|
|
|
Var "tested_file";
|
|
|
|
Lit "with the";
|
|
|
|
Var "catala_cmd";
|
|
|
|
Lit "command";
|
|
|
|
])
|
|
|
|
in
|
|
|
|
let test_without_scope_rule =
|
|
|
|
Nj.Rule.make without_scope_output_rule
|
|
|
|
~command:Nj.Expr.(Seq (Lit catala_exe_opts :: test_common_cmd_exprs))
|
|
|
|
~description:
|
|
|
|
Nj.Expr.(
|
|
|
|
Seq
|
|
|
|
[
|
2022-03-08 17:03:14 +03:00
|
|
|
Lit "TEST on file";
|
|
|
|
Var "tested_file";
|
|
|
|
Lit "with the";
|
|
|
|
Var "catala_cmd";
|
|
|
|
Lit "command";
|
2022-02-25 20:00:10 +03:00
|
|
|
])
|
|
|
|
in
|
|
|
|
Nj.RuleMap.(
|
|
|
|
rules
|
|
|
|
|> add test_with_scope_rule.name test_with_scope_rule
|
|
|
|
|> add test_without_scope_rule.name test_without_scope_rule)
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [add_reset_rules catala_exe_opts rules] adds ninja rules used to reset test
|
|
|
|
files into [rules] and returns it.*)
|
|
|
|
let add_reset_rules (catala_exe_opts : string) (rules : Rule.t Nj.RuleMap.t) :
|
|
|
|
Rule.t Nj.RuleMap.t =
|
2022-02-25 20:00:10 +03:00
|
|
|
add_reset_rules_aux ~with_scope_output_rule:"reset_with_scope"
|
2022-03-08 17:03:14 +03:00
|
|
|
~without_scope_output_rule:"reset_without_scope" ~redirect:">"
|
|
|
|
catala_exe_opts rules
|
2022-02-25 20:00:10 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [add_test_rules catala_exe_opts rules] adds ninja rules used to test files
|
|
|
|
into [rules] and returns it.*)
|
|
|
|
let add_test_rules (catala_exe_opts : string) (rules : Rule.t Nj.RuleMap.t) :
|
|
|
|
Rule.t Nj.RuleMap.t =
|
2022-02-25 20:00:10 +03:00
|
|
|
let test_common_cmd_exprs =
|
|
|
|
Nj.Expr.
|
|
|
|
[
|
|
|
|
Var "catala_cmd";
|
|
|
|
Var "tested_file";
|
|
|
|
Var "extra_flags";
|
|
|
|
Lit "--unstyled";
|
|
|
|
Lit "2>&1 | colordiff -u -b";
|
|
|
|
Var "expected_output";
|
|
|
|
Lit "-";
|
|
|
|
]
|
|
|
|
in
|
2022-03-08 17:03:14 +03:00
|
|
|
add_test_rules_aux ~test_common_cmd_exprs
|
|
|
|
~with_scope_output_rule:"test_with_scope"
|
2022-02-25 20:00:10 +03:00
|
|
|
~without_scope_output_rule:"test_without_scope" catala_exe_opts rules
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [add_reset_with_ouput_rules catala_exe_opts rules] adds ninja rules used to
|
|
|
|
reset test files using an output flag into [rules] and returns it.*)
|
|
|
|
let add_reset_with_output_rules
|
|
|
|
(catala_exe_opts : string) (rules : Rule.t Nj.RuleMap.t) :
|
2022-02-25 20:00:10 +03:00
|
|
|
Rule.t Nj.RuleMap.t =
|
|
|
|
add_reset_rules_aux ~with_scope_output_rule:"reset_with_scope_and_output"
|
2022-03-08 17:03:14 +03:00
|
|
|
~without_scope_output_rule:"reset_without_scope_and_output" ~redirect:"-o"
|
|
|
|
catala_exe_opts rules
|
2022-02-25 20:00:10 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [add_test_with_output_rules catala_exe_opts rules] adds ninja rules used to
|
|
|
|
test files using an output flag into [rules] and returns it.*)
|
|
|
|
let add_test_with_output_rules
|
|
|
|
(catala_exe_opts : string) (rules : Rule.t Nj.RuleMap.t) :
|
2022-02-25 20:00:10 +03:00
|
|
|
Rule.t Nj.RuleMap.t =
|
|
|
|
let test_common_cmd_exprs =
|
|
|
|
Nj.Expr.
|
|
|
|
[
|
|
|
|
Var "catala_cmd";
|
|
|
|
Var "tested_file";
|
|
|
|
Var "extra_flags";
|
|
|
|
Lit "--unstyled";
|
|
|
|
Lit "-o";
|
|
|
|
Var "tmp_file";
|
|
|
|
Lit "; colordiff -u -b";
|
|
|
|
Var "expected_output";
|
|
|
|
Var "tmp_file";
|
|
|
|
]
|
|
|
|
in
|
2022-03-08 17:03:14 +03:00
|
|
|
add_test_rules_aux ~test_common_cmd_exprs
|
|
|
|
~with_scope_output_rule:"test_with_scope_and_output"
|
|
|
|
~without_scope_output_rule:"test_without_scope_and_output" catala_exe_opts
|
|
|
|
rules
|
2022-02-25 20:00:10 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [ninja_start catala_exe] returns the inital [ninja] data structure with
|
|
|
|
rules needed to reset and test files. *)
|
2022-02-25 20:00:10 +03:00
|
|
|
let ninja_start (catala_exe : string) (catala_opts : string) : ninja =
|
|
|
|
let catala_exe_opts = catala_exe ^ " " ^ catala_opts in
|
|
|
|
let run_and_display_final_message =
|
|
|
|
Nj.Rule.make "run_and_display_final_message"
|
|
|
|
~command:Nj.Expr.(Seq [ Lit ":" ])
|
2022-03-08 17:03:14 +03:00
|
|
|
~description:
|
|
|
|
Nj.Expr.(
|
|
|
|
Seq [ Lit "All tests"; Var "test_file_or_folder"; Lit "passed!" ])
|
2022-02-25 20:00:10 +03:00
|
|
|
in
|
|
|
|
{
|
|
|
|
rules =
|
|
|
|
Nj.RuleMap.(
|
2022-03-08 17:03:14 +03:00
|
|
|
empty
|
|
|
|
|> add_reset_rules catala_exe_opts
|
|
|
|
|> add_test_rules catala_exe_opts
|
2022-02-25 20:00:10 +03:00
|
|
|
|> add_test_with_output_rules catala_exe_opts
|
|
|
|
|> add_reset_with_output_rules catala_exe_opts
|
|
|
|
|> add run_and_display_final_message.name run_and_display_final_message);
|
|
|
|
builds = Nj.BuildMap.empty;
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [collect_all_ninja_build ninja tested_file catala_exe catala_opts reset_test_outputs]
|
|
|
|
creates and returns all ninja build statements needed to test the
|
|
|
|
[tested_file]. *)
|
|
|
|
let collect_all_ninja_build
|
|
|
|
(ninja : ninja) (tested_file : string) (reset_test_outputs : bool) :
|
2022-02-25 20:00:10 +03:00
|
|
|
(string * ninja) option =
|
|
|
|
let expected_outputs = search_for_expected_outputs tested_file in
|
|
|
|
if List.length expected_outputs = 0 then (
|
2022-03-08 17:03:14 +03:00
|
|
|
Cli.debug_print "No expected outputs were found for test file %s"
|
|
|
|
tested_file;
|
2022-02-25 20:00:10 +03:00
|
|
|
None)
|
|
|
|
else
|
|
|
|
let ninja, test_names =
|
|
|
|
List.fold_left
|
|
|
|
(fun (ninja, test_names) expected_output ->
|
|
|
|
let vars =
|
|
|
|
[
|
2022-03-08 17:03:14 +03:00
|
|
|
( "catala_cmd",
|
2022-03-08 17:52:26 +03:00
|
|
|
Nj.Expr.Lit
|
|
|
|
(Cli.catala_backend_option_to_string expected_output.backend)
|
2022-03-08 17:03:14 +03:00
|
|
|
);
|
2022-02-25 20:00:10 +03:00
|
|
|
("tested_file", Nj.Expr.Lit tested_file);
|
|
|
|
( "expected_output",
|
2022-03-08 17:03:14 +03:00
|
|
|
Nj.Expr.Lit
|
|
|
|
(expected_output.output_dir
|
|
|
|
^ expected_output.complete_filename) );
|
2022-02-25 20:00:10 +03:00
|
|
|
]
|
2022-03-08 17:52:26 +03:00
|
|
|
and output_build_kind = if reset_test_outputs then "reset" else "test"
|
|
|
|
and catala_backend =
|
|
|
|
Cli.catala_backend_option_to_string expected_output.backend
|
2022-03-08 17:03:14 +03:00
|
|
|
in
|
2022-02-25 20:00:10 +03:00
|
|
|
|
|
|
|
let get_rule_infos ?(rule_postfix = "") :
|
2022-03-08 17:03:14 +03:00
|
|
|
string option -> string * string * (string * Nj.Expr.t) list =
|
|
|
|
function
|
2022-02-25 20:00:10 +03:00
|
|
|
| Some scope ->
|
2022-03-08 17:03:14 +03:00
|
|
|
( Printf.sprintf "%s_%s_%s_%s" output_build_kind scope
|
|
|
|
catala_backend tested_file
|
2022-02-25 20:00:10 +03:00
|
|
|
|> Nj.Build.unpath,
|
|
|
|
output_build_kind ^ "_with_scope" ^ rule_postfix,
|
|
|
|
("scope", Nj.Expr.Lit scope) :: vars )
|
|
|
|
| None ->
|
2022-03-08 17:03:14 +03:00
|
|
|
( Printf.sprintf "%s_%s_%s" output_build_kind catala_backend
|
|
|
|
tested_file
|
2022-02-25 20:00:10 +03:00
|
|
|
|> Nj.Build.unpath,
|
|
|
|
output_build_kind ^ "_without_scope" ^ rule_postfix,
|
|
|
|
vars )
|
|
|
|
in
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let ninja_add_new_rule
|
|
|
|
(rule_output : string)
|
|
|
|
(rule : string)
|
|
|
|
(vars : (string * Nj.Expr.t) list)
|
|
|
|
(ninja : ninja) : ninja =
|
2022-02-25 20:00:10 +03:00
|
|
|
{
|
|
|
|
ninja with
|
|
|
|
builds =
|
|
|
|
Nj.BuildMap.add rule_output
|
2022-03-08 17:03:14 +03:00
|
|
|
(Nj.Build.make_with_vars
|
|
|
|
~outputs:[ Nj.Expr.Lit rule_output ]
|
|
|
|
~rule ~vars)
|
2022-02-25 20:00:10 +03:00
|
|
|
ninja.builds;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
|
|
|
|
match expected_output.backend with
|
2022-03-08 17:03:14 +03:00
|
|
|
| Cli.Interpret | Cli.Proof | Cli.Typecheck | Cli.Dcalc
|
|
|
|
| Cli.Scopelang | Cli.Scalc | Cli.Lcalc ->
|
|
|
|
let rule_output, rule_name, rule_vars =
|
|
|
|
get_rule_infos expected_output.scope
|
|
|
|
in
|
2022-02-25 20:00:10 +03:00
|
|
|
let rule_vars =
|
|
|
|
match expected_output.backend with
|
|
|
|
| Cli.Proof ->
|
2022-03-08 17:03:14 +03:00
|
|
|
("extra_flags", Nj.Expr.Lit "--disable_counterexamples")
|
|
|
|
:: rule_vars
|
|
|
|
(* Counterexamples can be different at each call because of
|
|
|
|
the randomness inside SMT solver, so we can't expect
|
|
|
|
their value to remain constant. Hence we disable the
|
|
|
|
counterexamples when testing the replication of failed
|
2022-02-25 20:00:10 +03:00
|
|
|
proofs. *)
|
|
|
|
| _ -> rule_vars
|
|
|
|
in
|
|
|
|
( ninja_add_new_rule rule_output rule_name rule_vars ninja,
|
|
|
|
test_names ^ " $\n " ^ rule_output )
|
|
|
|
| Cli.Python | Cli.OCaml | Cli.Latex | Cli.Html | Cli.Makefile ->
|
2022-03-08 17:03:14 +03:00
|
|
|
let tmp_file =
|
|
|
|
Filename.temp_file "clerk_" ("_" ^ catala_backend)
|
|
|
|
in
|
2022-02-25 20:00:10 +03:00
|
|
|
let rule_output, rule_name, rule_vars =
|
|
|
|
get_rule_infos ~rule_postfix:"_and_output" expected_output.scope
|
|
|
|
in
|
|
|
|
let rule_vars = ("tmp_file", Nj.Expr.Lit tmp_file) :: rule_vars in
|
|
|
|
( ninja_add_new_rule rule_output rule_name rule_vars ninja,
|
|
|
|
test_names ^ " $\n " ^ rule_output ))
|
|
|
|
(ninja, "") expected_outputs
|
|
|
|
in
|
|
|
|
let test_name =
|
|
|
|
tested_file
|
|
|
|
|> (if reset_test_outputs then Printf.sprintf "reset_file_%s"
|
|
|
|
else Printf.sprintf "test_file_%s")
|
|
|
|
|> Nj.Build.unpath
|
|
|
|
in
|
|
|
|
Some
|
|
|
|
( test_name,
|
|
|
|
{
|
|
|
|
ninja with
|
|
|
|
builds =
|
|
|
|
Nj.BuildMap.add test_name
|
2022-03-08 17:03:14 +03:00
|
|
|
(Nj.Build.make_with_inputs ~outputs:[ Nj.Expr.Lit test_name ]
|
|
|
|
~rule:"phony" ~inputs:[ Nj.Expr.Lit test_names ])
|
2022-02-25 20:00:10 +03:00
|
|
|
ninja.builds;
|
|
|
|
} )
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [add_root_test_build ninja all_file_names all_test_builds] add the 'test'
|
|
|
|
ninja build declaration calling the rule 'run_and_display_final_message' for
|
|
|
|
[all_test_builds] which correspond to [all_file_names]. *)
|
|
|
|
let add_root_test_build
|
|
|
|
(ninja : ninja) (all_file_names : string list) (all_test_builds : string) :
|
2022-02-25 20:00:10 +03:00
|
|
|
ninja =
|
|
|
|
let file_names_str =
|
|
|
|
List.hd all_file_names ^ ""
|
2022-03-08 17:03:14 +03:00
|
|
|
^ List.fold_left
|
|
|
|
(fun acc name -> acc ^ "; " ^ name)
|
|
|
|
"" (List.tl all_file_names)
|
2022-02-25 20:00:10 +03:00
|
|
|
in
|
|
|
|
{
|
|
|
|
ninja with
|
|
|
|
builds =
|
|
|
|
Nj.BuildMap.add "test"
|
|
|
|
(Nj.Build.make_with_vars_and_inputs ~outputs:[ Nj.Expr.Lit "test" ]
|
2022-03-08 17:03:14 +03:00
|
|
|
~rule:"run_and_display_final_message"
|
|
|
|
~inputs:[ Nj.Expr.Lit all_test_builds ]
|
|
|
|
~vars:
|
|
|
|
[
|
|
|
|
( "test_file_or_folder",
|
|
|
|
Nj.Expr.Lit ("in [ " ^ file_names_str ^ " ]") );
|
|
|
|
])
|
2022-02-25 20:00:10 +03:00
|
|
|
ninja.builds;
|
|
|
|
}
|
|
|
|
|
|
|
|
(**{1 Running}*)
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let run_file
|
|
|
|
(file : string)
|
|
|
|
(catala_exe : string)
|
|
|
|
(catala_opts : string)
|
|
|
|
(scope : string) : int =
|
2022-02-25 20:00:10 +03:00
|
|
|
let command =
|
|
|
|
String.concat " "
|
2022-03-08 17:03:14 +03:00
|
|
|
(List.filter
|
|
|
|
(fun s -> s <> "")
|
|
|
|
[ catala_exe; catala_opts; "-s " ^ scope; "Interpret"; file ])
|
2022-02-25 20:00:10 +03:00
|
|
|
in
|
2022-03-08 15:04:27 +03:00
|
|
|
Cli.debug_print "Running: %s" command;
|
2022-02-25 20:00:10 +03:00
|
|
|
Sys.command command
|
|
|
|
|
|
|
|
(** {1 Driver} *)
|
|
|
|
|
|
|
|
let get_catala_files_in_folder (dir : string) : string list =
|
|
|
|
let rec loop result = function
|
|
|
|
| f :: fs ->
|
|
|
|
let f_is_dir =
|
|
|
|
try Sys.is_directory f
|
|
|
|
with Sys_error e ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Cli.warning_print "skipping %s" e;
|
2022-02-25 20:00:10 +03:00
|
|
|
false
|
|
|
|
in
|
|
|
|
if f_is_dir then
|
2022-03-07 20:02:12 +03:00
|
|
|
readdir_sort f |> Array.to_list
|
2022-02-25 20:00:10 +03:00
|
|
|
|> List.map (Filename.concat f)
|
|
|
|
|> List.append fs |> loop result
|
|
|
|
else loop (f :: result) fs
|
|
|
|
| [] -> result
|
|
|
|
in
|
|
|
|
let all_files_in_folder = loop [] [ dir ] in
|
|
|
|
List.filter (Re.Pcre.pmatch ~rex:catala_suffix_regex) all_files_in_folder
|
|
|
|
|
|
|
|
type ninja_building_context = {
|
|
|
|
last_valid_ninja : ninja;
|
|
|
|
curr_ninja : ninja option;
|
|
|
|
all_file_names : string list;
|
|
|
|
all_test_builds : string;
|
|
|
|
all_failed_names : string list;
|
|
|
|
}
|
2022-03-08 17:03:14 +03:00
|
|
|
(** Record used to keep tracks of the current context while building the
|
|
|
|
[Ninja_utils.ninja].*)
|
2022-02-25 20:00:10 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [ninja_building_context_init ninja_init] returns the empty context
|
|
|
|
corresponding to [ninja_init]. *)
|
|
|
|
let ninja_building_context_init (ninja_init : Nj.ninja) : ninja_building_context
|
|
|
|
=
|
2022-02-25 20:00:10 +03:00
|
|
|
{
|
|
|
|
last_valid_ninja = ninja_init;
|
|
|
|
curr_ninja = Some ninja_init;
|
|
|
|
all_file_names = [];
|
|
|
|
all_test_builds = "";
|
|
|
|
all_failed_names = [];
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [collect_in_directory ctx file_or_folder ninja_start reset_test_outputs]
|
|
|
|
updates the building context [ctx] by adding new ninja build statements
|
|
|
|
needed to test files in [folder].*)
|
|
|
|
let collect_in_folder
|
|
|
|
(ctx : ninja_building_context)
|
|
|
|
(folder : string)
|
|
|
|
(ninja_start : Nj.ninja)
|
2022-02-25 20:00:10 +03:00
|
|
|
(reset_test_outputs : bool) : ninja_building_context =
|
|
|
|
let ninja, test_file_names =
|
|
|
|
List.fold_left
|
|
|
|
(fun (ninja, test_file_names) file ->
|
|
|
|
match collect_all_ninja_build ninja file reset_test_outputs with
|
|
|
|
| None ->
|
|
|
|
(* Skips none Catala file. *)
|
|
|
|
(ninja, test_file_names)
|
2022-03-08 17:03:14 +03:00
|
|
|
| Some (test_file_name, ninja) ->
|
|
|
|
(ninja, test_file_names ^ " $\n " ^ test_file_name))
|
2022-02-25 20:00:10 +03:00
|
|
|
(ninja_start, "")
|
|
|
|
(get_catala_files_in_folder folder)
|
|
|
|
in
|
2022-03-08 17:03:14 +03:00
|
|
|
let test_dir_name =
|
|
|
|
Printf.sprintf "test_dir_%s" (folder |> Nj.Build.unpath)
|
|
|
|
in
|
2022-02-25 20:00:10 +03:00
|
|
|
let curr_ninja =
|
|
|
|
if 0 = String.length test_file_names then None
|
|
|
|
else
|
|
|
|
Some
|
|
|
|
{
|
|
|
|
ninja with
|
|
|
|
builds =
|
|
|
|
Nj.BuildMap.add test_dir_name
|
2022-03-08 17:03:14 +03:00
|
|
|
(Nj.Build.make_with_vars_and_inputs
|
|
|
|
~outputs:[ Nj.Expr.Lit test_dir_name ]
|
|
|
|
~rule:"run_and_display_final_message"
|
|
|
|
~inputs:[ Nj.Expr.Lit test_file_names ]
|
|
|
|
~vars:
|
|
|
|
[
|
|
|
|
( "test_file_or_folder",
|
|
|
|
Nj.Expr.Lit ("in folder '" ^ folder ^ "'") );
|
|
|
|
])
|
2022-02-25 20:00:10 +03:00
|
|
|
ninja.builds;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
if Option.is_some curr_ninja then
|
|
|
|
{
|
|
|
|
ctx with
|
|
|
|
last_valid_ninja = ninja_start;
|
|
|
|
curr_ninja;
|
|
|
|
all_file_names = folder :: ctx.all_file_names;
|
|
|
|
all_test_builds = ctx.all_test_builds ^ " $\n " ^ test_dir_name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctx with
|
|
|
|
last_valid_ninja = ninja_start;
|
|
|
|
curr_ninja;
|
|
|
|
all_failed_names = folder :: ctx.all_failed_names;
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [collect_in_file ctx file_or_folder ninja_start reset_test_outputs] updates
|
|
|
|
the building context [ctx] by adding new ninja build statements needed to
|
|
|
|
test the [tested_file].*)
|
|
|
|
let collect_in_file
|
|
|
|
(ctx : ninja_building_context)
|
|
|
|
(tested_file : string)
|
|
|
|
(ninja_start : Nj.ninja)
|
2022-02-25 20:00:10 +03:00
|
|
|
(reset_test_outputs : bool) : ninja_building_context =
|
|
|
|
match collect_all_ninja_build ninja_start tested_file reset_test_outputs with
|
|
|
|
| Some (test_file_name, ninja) ->
|
|
|
|
{
|
|
|
|
ctx with
|
|
|
|
last_valid_ninja = ninja;
|
|
|
|
curr_ninja = Some ninja;
|
|
|
|
all_file_names = tested_file :: ctx.all_file_names;
|
|
|
|
all_test_builds = ctx.all_test_builds ^ " $\n " ^ test_file_name;
|
|
|
|
}
|
|
|
|
| None ->
|
|
|
|
{
|
|
|
|
ctx with
|
|
|
|
last_valid_ninja = ninja_start;
|
|
|
|
curr_ninja = None;
|
|
|
|
all_failed_names = tested_file :: ctx.all_failed_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
(** {1 Return code values} *)
|
|
|
|
|
|
|
|
let return_ok = 0
|
|
|
|
let return_err = 1
|
|
|
|
|
2022-02-26 21:49:13 +03:00
|
|
|
(** {1 Driver} *)
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** [add_root_test_build ctx files_or_folders reset_test_outputs] updates the
|
|
|
|
[ctx] by adding ninja build statements needed to test or
|
|
|
|
[reset_test_outputs] [files_or_folders]. *)
|
|
|
|
let add_test_builds
|
|
|
|
(ctx : ninja_building_context)
|
|
|
|
(files_or_folders : string list)
|
2022-03-07 13:09:47 +03:00
|
|
|
(reset_test_outputs : bool) : ninja_building_context =
|
2022-02-26 21:49:13 +03:00
|
|
|
files_or_folders
|
|
|
|
|> List.fold_left
|
|
|
|
(fun ctx file_or_folder ->
|
|
|
|
let curr_ninja =
|
2022-03-08 17:03:14 +03:00
|
|
|
match ctx.curr_ninja with
|
|
|
|
| Some ninja -> ninja
|
|
|
|
| None -> ctx.last_valid_ninja
|
2022-02-26 21:49:13 +03:00
|
|
|
in
|
|
|
|
if Sys.is_directory file_or_folder then
|
|
|
|
collect_in_folder ctx file_or_folder curr_ninja reset_test_outputs
|
|
|
|
else collect_in_file ctx file_or_folder curr_ninja reset_test_outputs)
|
|
|
|
ctx
|
|
|
|
|
2022-04-14 12:47:18 +03:00
|
|
|
let makeflags_to_ninja_flags (makeflags : string option) =
|
|
|
|
match makeflags with
|
|
|
|
| None -> ""
|
|
|
|
| Some makeflags ->
|
2022-05-05 15:14:44 +03:00
|
|
|
let ignore_rex = Re.(compile @@ word (char 'i')) in
|
|
|
|
let has_ignore = Re.execp ignore_rex makeflags in
|
2022-05-05 15:17:27 +03:00
|
|
|
let jobs_rex = Re.(compile @@ seq [ str "-j"; group (rep digit) ]) in
|
2022-04-14 12:47:18 +03:00
|
|
|
let number_of_jobs =
|
2022-05-05 15:17:27 +03:00
|
|
|
try int_of_string (Re.Group.get (Re.exec jobs_rex makeflags) 1)
|
2022-04-14 12:47:18 +03:00
|
|
|
with _ -> 0
|
|
|
|
in
|
|
|
|
String.concat " "
|
|
|
|
[
|
|
|
|
(if has_ignore then "-k0" else "");
|
|
|
|
"-j" ^ string_of_int number_of_jobs;
|
|
|
|
]
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let driver
|
|
|
|
(files_or_folders : string list)
|
|
|
|
(command : string)
|
|
|
|
(catala_exe : string option)
|
|
|
|
(catala_opts : string option)
|
2022-04-14 12:47:18 +03:00
|
|
|
(makeflags : string option)
|
2022-03-08 17:03:14 +03:00
|
|
|
(debug : bool)
|
|
|
|
(scope : string option)
|
|
|
|
(reset_test_outputs : bool)
|
2022-02-25 20:00:10 +03:00
|
|
|
(ninja_output : string option) : int =
|
|
|
|
if debug then Cli.debug_flag := true;
|
2022-04-14 12:47:18 +03:00
|
|
|
let ninja_flags = makeflags_to_ninja_flags makeflags in
|
2022-02-26 21:49:13 +03:00
|
|
|
let files_or_folders = List.sort_uniq String.compare files_or_folders
|
|
|
|
and catala_exe = Option.fold ~none:"catala" ~some:Fun.id catala_exe
|
|
|
|
and catala_opts = Option.fold ~none:"" ~some:Fun.id catala_opts
|
|
|
|
and ninja_output =
|
2022-03-08 17:03:14 +03:00
|
|
|
Option.fold
|
|
|
|
~none:(Filename.temp_file "clerk_build" ".ninja")
|
|
|
|
~some:Fun.id ninja_output
|
2022-02-25 20:00:10 +03:00
|
|
|
in
|
|
|
|
match String.lowercase_ascii command with
|
2022-02-25 22:03:58 +03:00
|
|
|
| "test" -> (
|
2022-02-25 20:00:10 +03:00
|
|
|
Cli.debug_print "building ninja rules...";
|
|
|
|
let ctx =
|
2022-02-26 21:49:13 +03:00
|
|
|
add_test_builds
|
2022-02-25 20:00:10 +03:00
|
|
|
(ninja_building_context_init (ninja_start catala_exe catala_opts))
|
2022-02-26 21:49:13 +03:00
|
|
|
files_or_folders reset_test_outputs
|
2022-02-25 20:00:10 +03:00
|
|
|
in
|
|
|
|
let there_is_some_fails = 0 <> List.length ctx.all_failed_names in
|
2022-03-08 17:03:14 +03:00
|
|
|
let ninja =
|
|
|
|
match ctx.curr_ninja with
|
|
|
|
| Some ninja -> ninja
|
|
|
|
| None -> ctx.last_valid_ninja
|
|
|
|
in
|
2022-02-25 20:00:10 +03:00
|
|
|
if there_is_some_fails then
|
|
|
|
List.iter
|
|
|
|
(fun f ->
|
|
|
|
f
|
2022-03-08 15:04:27 +03:00
|
|
|
|> Cli.with_style [ ANSITerminal.magenta ] "%s"
|
|
|
|
|> Cli.warning_print "No test case found for %s")
|
2022-02-25 20:00:10 +03:00
|
|
|
ctx.all_failed_names;
|
2022-03-08 17:03:14 +03:00
|
|
|
if 0 = List.compare_lengths ctx.all_failed_names files_or_folders then
|
|
|
|
return_ok
|
2022-02-25 20:00:10 +03:00
|
|
|
else
|
2022-02-25 22:03:58 +03:00
|
|
|
try
|
|
|
|
let out = open_out ninja_output in
|
2022-03-08 15:04:27 +03:00
|
|
|
Cli.debug_print "writing %s..." ninja_output;
|
2022-02-25 22:03:58 +03:00
|
|
|
Nj.format
|
|
|
|
(Format.formatter_of_out_channel out)
|
|
|
|
(add_root_test_build ninja ctx.all_file_names ctx.all_test_builds);
|
|
|
|
close_out out;
|
2022-04-14 12:47:18 +03:00
|
|
|
let ninja_cmd = "ninja " ^ ninja_flags ^ " test -f " ^ ninja_output in
|
2022-03-08 15:04:27 +03:00
|
|
|
Cli.debug_print "executing '%s'..." ninja_cmd;
|
2022-02-25 22:03:58 +03:00
|
|
|
Sys.command ninja_cmd
|
|
|
|
with Sys_error e ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Cli.error_print "can not write in %s" e;
|
2022-02-25 22:03:58 +03:00
|
|
|
return_err)
|
2022-02-25 20:00:10 +03:00
|
|
|
| "run" -> (
|
|
|
|
match scope with
|
|
|
|
| Some scope ->
|
|
|
|
let res =
|
|
|
|
List.fold_left
|
|
|
|
(fun ret f -> ret + run_file f catala_exe catala_opts scope)
|
|
|
|
0 files_or_folders
|
|
|
|
in
|
|
|
|
if 0 <> res then return_err else return_ok
|
|
|
|
| None ->
|
|
|
|
Cli.error_print "Please provide a scope to run with the -s option";
|
|
|
|
1)
|
|
|
|
| _ ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Cli.error_print "The command \"%s\" is unknown to clerk." command;
|
2022-02-25 20:00:10 +03:00
|
|
|
1
|
|
|
|
|
|
|
|
let main () =
|
2022-02-26 21:49:13 +03:00
|
|
|
match Cmdliner.Term.eval (clerk_t driver, info) with
|
2022-02-25 20:00:10 +03:00
|
|
|
| `Ok 0 -> Cmdliner.Term.exit (`Ok 0)
|
|
|
|
| _ -> Cmdliner.Term.exit (`Error `Term)
|