Skip to content

Commit

Permalink
update dune lang to 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Khady committed Dec 23, 2023
1 parent ec0a6a7 commit 9510a6d
Show file tree
Hide file tree
Showing 20 changed files with 480 additions and 619 deletions.
2 changes: 2 additions & 0 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
profile=janestreet
version=0.26.1
3 changes: 1 addition & 2 deletions alcotest/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
(public_name junit_alcotest)
(wrapped false)
(synopsis "JUnit XML reports generation for alcotest tests")
(libraries junit alcotest)
(flags :standard -safe-string -short-paths))
(libraries junit alcotest))
28 changes: 13 additions & 15 deletions alcotest/junit_alcotest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ let wrap_test ?classname handle_result (name, s, test) =
let test () =
try
test ();
Junit.Testcase.pass
~name
~classname
~time:0.
|> handle_result
Junit.Testcase.pass ~name ~classname ~time:0. |> handle_result
with
| Failure exn_msg as exn ->
Junit.Testcase.failure
Expand All @@ -46,25 +42,27 @@ let wrap_test ?classname handle_result (name, s, test) =
|> handle_result;
raise exn
in
(name, s, test)
name, s, test
;;

let run ?argv name tl =
A.run ~and_exit:false ?argv name tl
let run ?argv name tl = A.run ~and_exit:false ?argv name tl

let run_and_report ?(and_exit=true) ?package ?timestamp ?argv name tests =
let run_and_report ?(and_exit = true) ?package ?timestamp ?argv name tests =
let testcases = ref [] in
let testsuite = Junit.Testsuite.make ?package ?timestamp ~name () in
let tests =
List.map (fun (title, test_set) ->
let classname = Printf.sprintf "%s.%s" name title in
(title, List.map (wrap_test ~classname (push testcases)) test_set)
) tests
List.map
(fun (title, test_set) ->
let classname = Printf.sprintf "%s.%s" name title in
title, List.map (wrap_test ~classname (push testcases)) test_set)
tests
in
let exit =
try
run ?argv name tests;
fun () -> if and_exit then exit 0 else ()
with A.Test_error ->
fun () -> if and_exit then exit 1 else raise A.Test_error
with
| A.Test_error -> fun () -> if and_exit then exit 1 else raise A.Test_error
in
Junit.Testsuite.add_testcases !testcases testsuite, exit
;;
58 changes: 27 additions & 31 deletions alcotest/junit_alcotest.mli
Original file line number Diff line number Diff line change
@@ -1,49 +1,38 @@
(** Interface to product JUnit reports for Alcotest
It tries to provide a layer as thin as possible on top of Alcotest
to allow to port existing test without writing a lot a boilerplate.
*)

val wrap_test :
?classname:string ->
(Junit.Testcase.t -> unit) ->
unit Alcotest.test_case ->
unit Alcotest.test_case
to allow to port existing test without writing a lot a boilerplate. *)

(** [wrap_test handle_result test_cases] wraps test cases to create
Junit testcases and pass them to [handle_result].
Can be used with {!run} to create customized Junit testsuites if
the output of {!run_and_report} is not as expected.
@param classname will populate the 'classname' attribute
for the test case. For best hierarchic rendering in Jenkins, it
should contain a period. For example, "foo.bar.baz" will be rendered
a package "foo.bar" that contains a class "baz", which contains the
current test case and others. Defaults to the name of the test case.
*)
@param classname
will populate the 'classname' attribute
for the test case. For best hierarchic rendering in Jenkins, it
should contain a period. For example, "foo.bar.baz" will be rendered
a package "foo.bar" that contains a class "baz", which contains the
current test case and others. Defaults to the name of the test case. *)
val wrap_test
: ?classname:string
-> (Junit.Testcase.t -> unit)
-> unit Alcotest.test_case
-> unit Alcotest.test_case

val run: ?argv:string array -> string -> unit Alcotest.test list -> unit
(** [run ?argv n t] is a wrapper around {!Alcotest.run}, only setting
[and_exit] to false. It is mandatory to be able to process results
after the end of the run.
Low level function. It is easier to use {!run_and_report}.
*)
Low level function. It is easier to use {!run_and_report}. *)
val run : ?argv:string array -> string -> unit Alcotest.test list -> unit

type exit = unit -> unit
(** [exit ()] exists with appropriate code if {!run_and_report}'s
[and_exit] was [true] or raise {!Alcotest.Test_error} in case of
error.
*)

val run_and_report:
?and_exit:bool ->
?package:string ->
?timestamp:Ptime.t ->
?argv:string array ->
string ->
(string * unit Alcotest.test_case list) list ->
(Junit.Testsuite.t * exit)
error. *)
type exit = unit -> unit

(** [run name tests] is a wrapper around {!run} and {!wrap_test}. It
runs the tests and creates a Junit testsuite from the results.
Expand All @@ -58,5 +47,12 @@ val run_and_report:
raises [Test_error] on error.
[?argv] is forwarded to {!run}. [?package] and [?timestamp] are
forwarded to {!Junit.Testsuite.make}.
*)
forwarded to {!Junit.Testsuite.make}. *)
val run_and_report
: ?and_exit:bool
-> ?package:string
-> ?timestamp:Ptime.t
-> ?argv:string array
-> string
-> (string * unit Alcotest.test_case list) list
-> Junit.Testsuite.t * exit
74 changes: 38 additions & 36 deletions alcotest/test/alcotest_report.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,61 @@ module To_test = struct
let plus int_list = List.fold_left (fun a b -> a + b) 0 int_list
end

let capit () =
A.(check char) "Check A" 'A' (To_test.capit 'a')

let plus () =
A.(check int)"Sum equals to 7" 7 (To_test.plus [1;1;2;3])

let wrong_result () =
A.(check string) "string_of_int equals to '7'" "7" (string_of_int 8)
let capit () = A.(check char) "Check A" 'A' (To_test.capit 'a')
let plus () = A.(check int) "Sum equals to 7" 7 (To_test.plus [ 1; 1; 2; 3 ])
let wrong_result () = A.(check string) "string_of_int equals to '7'" "7" (string_of_int 8)

let raise_unexpected_exn () =
A.(check int) "int_of_string equals to 7" 7 (invalid_arg "7")
;;

let test_set = [
A.test_case "Test with unexpected exception" `Quick raise_unexpected_exn;
A.test_case "Capitalize" `Quick capit;
A.test_case "Add entries" `Slow plus;
A.test_case "Test with wrong result" `Quick wrong_result;
]
let test_set =
[ A.test_case "Test with unexpected exception" `Quick raise_unexpected_exn
; A.test_case "Capitalize" `Quick capit
; A.test_case "Add entries" `Slow plus
; A.test_case "Test with wrong result" `Quick wrong_result
]
;;

let success_test_set = [
A.test_case "Capitalize" `Quick capit;
A.test_case "Add entries" `Slow plus;
]
let success_test_set =
[ A.test_case "Capitalize" `Quick capit; A.test_case "Add entries" `Slow plus ]
;;

let timestamp =
match Ptime.of_date_time ((2013, 5, 24), ((10, 23, 58), 0)) with
| Some t -> t
| None -> assert false
;;

let alcotest path =
let package = "junit_alcotest" in
let (testsuite1, _) = JA.run_and_report ~package ~timestamp "My first test" [
"Basic tests", test_set;
]
let testsuite1, _ =
JA.run_and_report ~package ~timestamp "My first test" [ "Basic tests", test_set ]
in
let (testsuite2, _) = JA.run_and_report ~package ~timestamp "My second test" [
"Basic tests", test_set;
]
let testsuite2, _ =
JA.run_and_report ~package ~timestamp "My second test" [ "Basic tests", test_set ]
in
let (testsuite3, exit) = JA.run_and_report ~and_exit:false ~package ~timestamp "Success test suite" [
"Good tests", success_test_set;
]
let testsuite3, exit =
JA.run_and_report
~and_exit:false
~package
~timestamp
"Success test suite"
[ "Good tests", success_test_set ]
in
let report = Junit.make [testsuite1; testsuite2; testsuite3] in
begin match path with
| None ->
let xml_report = Junit.to_xml report in
Format.printf "%a\n" (Tyxml.Xml.pp ()) xml_report
| Some path ->
Junit.to_file report path
end;
let report = Junit.make [ testsuite1; testsuite2; testsuite3 ] in
(match path with
| None ->
let xml_report = Junit.to_xml report in
Format.printf "%a\n" (Tyxml.Xml.pp ()) xml_report
| Some path -> Junit.to_file report path);
exit ()
;;

let () =
let path = try Some (Sys.getenv "REPORT_PATH") with _ -> None in
let path =
try Some (Sys.getenv "REPORT_PATH") with
| _ -> None
in
alcotest path
;;
11 changes: 6 additions & 5 deletions alcotest/test/dune
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
(executable
(name alcotest_report)
(modules alcotest_report)
(libraries junit junit_alcotest)
(flags :standard -safe-string -short-paths))
(libraries junit junit_alcotest))

(rule
(targets alcotest_report.xml)
(action
(setenv REPORT_PATH %{targets}
(setenv
REPORT_PATH
%{targets}
(run %{dep:alcotest_report.exe}))))

(alias
(name runtest)
(rule
(alias runtest)
(package junit_alcotest)
(action
(diff %{dep:alcotest_report.expected} %{dep:alcotest_report.xml}))
Expand Down
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(lang dune 1.0)
(lang dune 3.0)
(name junit)
2 changes: 1 addition & 1 deletion junit.opam
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dev-repo: "git+https://github.com/Khady/ocaml-junit.git"
doc: "https://khady.github.io/ocaml-junit/"
tags: ["junit" "jenkins"]
depends: [
"dune" {>= "1.0"}
"dune" {>= "3.0"}
"ptime"
"tyxml" {>= "4.0.0"}
"odoc" {with-doc & >= "1.1.1"}
Expand Down
3 changes: 1 addition & 2 deletions junit/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
(public_name junit)
(wrapped false)
(synopsis "JUnit XML reports generation library")
(libraries tyxml ptime ptime.clock.os)
(flags :standard -safe-string -short-paths))
(libraries tyxml ptime ptime.clock.os))
Loading

0 comments on commit 9510a6d

Please sign in to comment.