From 01b403ee7408bac6563534ec2bbc61686229e610 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Wed, 2 Mar 2022 15:26:08 +0100 Subject: [PATCH] Make alcotest-lwt compatible with jsoo --- src/alcotest-lwt/dune | 1 - .../failing/async_failure-js.expected | 16 +++++++ .../e2e/alcotest-lwt/failing/async_failure.ml | 4 +- test/e2e/alcotest-lwt/failing/dune | 3 +- test/e2e/alcotest-lwt/failing/dune.inc | 42 ++++++++++++++++++- .../failing/fail_with-js.expected | 15 +++++++ test/e2e/alcotest-lwt/failing/fail_with.ml | 2 +- .../passing/cli_options-js.expected | 8 ++++ test/e2e/alcotest-lwt/passing/cli_options.ml | 2 +- test/e2e/alcotest-lwt/passing/dune | 3 +- test/e2e/alcotest-lwt/passing/dune.inc | 23 +++++++++- test/e2e/helper/alcotest_test_helper.ml | 7 ++++ test/e2e/helper/dune | 3 ++ 13 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 test/e2e/alcotest-lwt/failing/async_failure-js.expected create mode 100644 test/e2e/alcotest-lwt/failing/fail_with-js.expected create mode 100644 test/e2e/alcotest-lwt/passing/cli_options-js.expected create mode 100644 test/e2e/helper/alcotest_test_helper.ml create mode 100644 test/e2e/helper/dune diff --git a/src/alcotest-lwt/dune b/src/alcotest-lwt/dune index a917a1e3..a39faa68 100644 --- a/src/alcotest-lwt/dune +++ b/src/alcotest-lwt/dune @@ -7,5 +7,4 @@ fmt alcotest.engine alcotest - (re_export lwt.unix) ;; Unused dependency added for convenience to consumers )) diff --git a/test/e2e/alcotest-lwt/failing/async_failure-js.expected b/test/e2e/alcotest-lwt/failing/async_failure-js.expected new file mode 100644 index 00000000..27527193 --- /dev/null +++ b/test/e2e/alcotest-lwt/failing/async_failure-js.expected @@ -0,0 +1,16 @@ +Testing `test/e2e/alcotest-lwt/failing/async_failure.ml'. +This run has ID `'. + +> [FAIL] all 0 one. + [OK] all 1 two. + +┌──────────────────────────────────────────────────────────────────────────────┐ +│ [FAIL] all 0 one. │ +└──────────────────────────────────────────────────────────────────────────────┘ +freeing all resources +[failure] All is broken +Logs saved to `/_build/_tests//all.000.output'. + ────────────────────────────────────────────────────────────────────────────── + +Full test results in `/_build/_tests/'. +1 failure! in s. 2 tests run. diff --git a/test/e2e/alcotest-lwt/failing/async_failure.ml b/test/e2e/alcotest-lwt/failing/async_failure.ml index c09391ae..5f1e25e5 100644 --- a/test/e2e/alcotest-lwt/failing/async_failure.ml +++ b/test/e2e/alcotest-lwt/failing/async_failure.ml @@ -5,11 +5,11 @@ let free () = let test_lwt switch () = Lwt_switch.add_hook (Some switch) free; Lwt.async (fun () -> failwith "All is broken"); - Lwt_unix.sleep 10. + Lwt.pause () let () = let open Alcotest_lwt in - Lwt_main.run + Alcotest_test_helper.wakeup_until_resolved @@ run __FILE__ [ ( "all", diff --git a/test/e2e/alcotest-lwt/failing/dune b/test/e2e/alcotest-lwt/failing/dune index 15e4b6a2..d32bde84 100644 --- a/test/e2e/alcotest-lwt/failing/dune +++ b/test/e2e/alcotest-lwt/failing/dune @@ -18,7 +18,8 @@ --package "alcotest-lwt" --libraries - "alcotest-lwt lwt lwt.unix")))) + "alcotest-lwt lwt alcotest_test_helper" + --js)))) (rule (alias runtest) diff --git a/test/e2e/alcotest-lwt/failing/dune.inc b/test/e2e/alcotest-lwt/failing/dune.inc index b606e08e..a435c08c 100644 --- a/test/e2e/alcotest-lwt/failing/dune.inc +++ b/test/e2e/alcotest-lwt/failing/dune.inc @@ -3,8 +3,8 @@ async_failure fail_with ) - (libraries alcotest alcotest.stdlib_ext alcotest-lwt lwt lwt.unix) - (modes native) + (libraries alcotest alcotest.stdlib_ext alcotest-lwt lwt alcotest_test_helper) + (modes native js) (modules async_failure fail_with @@ -30,6 +30,25 @@ (action (diff async_failure.expected async_failure.processed))) +(rule + (target async_failure-js.actual) + (action + (with-outputs-to %{target} + (with-accepted-exit-codes (or 1 2 125) + (run node %{dep:async_failure.bc.js}))))) + +(rule + (target async_failure-js.processed) + (action + (with-outputs-to %{target} + (run ../../strip_randomness.exe %{dep:async_failure-js.actual})))) + +(rule + (alias runtest-js) + (package alcotest-lwt) + (action + (diff async_failure-js.expected async_failure-js.processed))) + (rule (target fail_with.actual) (action @@ -48,3 +67,22 @@ (package alcotest-lwt) (action (diff fail_with.expected fail_with.processed))) + +(rule + (target fail_with-js.actual) + (action + (with-outputs-to %{target} + (with-accepted-exit-codes (or 1 2 125) + (run node %{dep:fail_with.bc.js}))))) + +(rule + (target fail_with-js.processed) + (action + (with-outputs-to %{target} + (run ../../strip_randomness.exe %{dep:fail_with-js.actual})))) + +(rule + (alias runtest-js) + (package alcotest-lwt) + (action + (diff fail_with-js.expected fail_with-js.processed))) diff --git a/test/e2e/alcotest-lwt/failing/fail_with-js.expected b/test/e2e/alcotest-lwt/failing/fail_with-js.expected new file mode 100644 index 00000000..eb83569d --- /dev/null +++ b/test/e2e/alcotest-lwt/failing/fail_with-js.expected @@ -0,0 +1,15 @@ +Testing `test/e2e/alcotest-lwt/failing/fail_with.ml'. +This run has ID `'. + +> [FAIL] all 0 one. + [OK] all 1 two. + +┌──────────────────────────────────────────────────────────────────────────────┐ +│ [FAIL] all 0 one. │ +└──────────────────────────────────────────────────────────────────────────────┘ +[failure] should fail +Logs saved to `/_build/_tests//all.000.output'. + ────────────────────────────────────────────────────────────────────────────── + +Full test results in `/_build/_tests/'. +1 failure! in s. 2 tests run. diff --git a/test/e2e/alcotest-lwt/failing/fail_with.ml b/test/e2e/alcotest-lwt/failing/fail_with.ml index 8dc52ca0..ca1cd0b4 100644 --- a/test/e2e/alcotest-lwt/failing/fail_with.ml +++ b/test/e2e/alcotest-lwt/failing/fail_with.ml @@ -2,7 +2,7 @@ let test_lwt _switch () = Lwt.fail_with "should fail" let () = let open Alcotest_lwt in - Lwt_main.run + Alcotest_test_helper.wakeup_until_resolved @@ run ~record_backtrace:false __FILE__ [ ( "all", diff --git a/test/e2e/alcotest-lwt/passing/cli_options-js.expected b/test/e2e/alcotest-lwt/passing/cli_options-js.expected new file mode 100644 index 00000000..7d5f31c5 --- /dev/null +++ b/test/e2e/alcotest-lwt/passing/cli_options-js.expected @@ -0,0 +1,8 @@ +Testing `Results should be in JSON, since --json is passed'. +This run has ID `'. + +{ + "success": 1, + "failures": 0, + "time": +} diff --git a/test/e2e/alcotest-lwt/passing/cli_options.ml b/test/e2e/alcotest-lwt/passing/cli_options.ml index 19d8a24e..d67ac49c 100644 --- a/test/e2e/alcotest-lwt/passing/cli_options.ml +++ b/test/e2e/alcotest-lwt/passing/cli_options.ml @@ -1,5 +1,5 @@ let () = let open Alcotest_lwt in - Lwt_main.run + Alcotest_test_helper.wakeup_until_resolved @@ run "Results should be in JSON, since --json is passed" [ ("test", [ test_case "alpha" `Quick (fun _ () -> Lwt.return_unit) ]) ] diff --git a/test/e2e/alcotest-lwt/passing/dune b/test/e2e/alcotest-lwt/passing/dune index c054cf83..97235e97 100644 --- a/test/e2e/alcotest-lwt/passing/dune +++ b/test/e2e/alcotest-lwt/passing/dune @@ -17,7 +17,8 @@ --package "alcotest-lwt" --libraries - "alcotest-lwt lwt lwt.unix")))) + "alcotest-lwt lwt alcotest_test_helper" + --js)))) (rule (alias runtest) diff --git a/test/e2e/alcotest-lwt/passing/dune.inc b/test/e2e/alcotest-lwt/passing/dune.inc index 8e743c69..000f8253 100644 --- a/test/e2e/alcotest-lwt/passing/dune.inc +++ b/test/e2e/alcotest-lwt/passing/dune.inc @@ -2,8 +2,8 @@ (names cli_options ) - (libraries alcotest alcotest.stdlib_ext alcotest-lwt lwt lwt.unix) - (modes native) + (libraries alcotest alcotest.stdlib_ext alcotest-lwt lwt alcotest_test_helper) + (modes native js) (modules cli_options ) @@ -27,3 +27,22 @@ (package alcotest-lwt) (action (diff cli_options.expected cli_options.processed))) + +(rule + (target cli_options-js.actual) + (action + (with-outputs-to %{target} + (with-accepted-exit-codes (or 0 125) + (run node %{dep:cli_options.bc.js} --json))))) + +(rule + (target cli_options-js.processed) + (action + (with-outputs-to %{target} + (run ../../strip_randomness.exe %{dep:cli_options-js.actual})))) + +(rule + (alias runtest-js) + (package alcotest-lwt) + (action + (diff cli_options-js.expected cli_options-js.processed))) diff --git a/test/e2e/helper/alcotest_test_helper.ml b/test/e2e/helper/alcotest_test_helper.ml new file mode 100644 index 00000000..70b8febc --- /dev/null +++ b/test/e2e/helper/alcotest_test_helper.ml @@ -0,0 +1,7 @@ +let rec wakeup_until_resolved p = + Lwt.wakeup_paused (); + match Lwt.poll p with + | Some x -> x + | None -> + if Lwt.paused_count () > 0 then wakeup_until_resolved p + else failwith "unresolved promise" diff --git a/test/e2e/helper/dune b/test/e2e/helper/dune new file mode 100644 index 00000000..2c8c7cea --- /dev/null +++ b/test/e2e/helper/dune @@ -0,0 +1,3 @@ +(library + (name alcotest_test_helper) + (libraries lwt)) \ No newline at end of file