Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make alcotest-lwt compatible with jsoo #348

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/alcotest-lwt/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
fmt
alcotest.engine
alcotest
(re_export lwt.unix) ;; Unused dependency added for convenience to consumers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a breaking change.. I don't know how many packages depend on lwt.unix being re-exported though.

))
16 changes: 16 additions & 0 deletions test/e2e/alcotest-lwt/failing/async_failure-js.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Testing `test/e2e/alcotest-lwt/failing/async_failure.ml'.
This run has ID `<uuid>'.

> [FAIL] all 0 one.
[OK] all 1 two.

┌──────────────────────────────────────────────────────────────────────────────┐
│ [FAIL] all 0 one. │
└──────────────────────────────────────────────────────────────────────────────┘
freeing all resources
[failure] All is broken
Logs saved to `<build-context>/_build/_tests/<test-dir>/all.000.output'.
──────────────────────────────────────────────────────────────────────────────

Full test results in `<build-context>/_build/_tests/<test-dir>'.
1 failure! in <test-duration>s. 2 tests run.
4 changes: 2 additions & 2 deletions test/e2e/alcotest-lwt/failing/async_failure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the reason for this 10 seconds sleep ?

Lwt.pause ()

let () =
let open Alcotest_lwt in
Lwt_main.run
Alcotest_test_helper.wakeup_until_resolved
@@ run __FILE__
[
( "all",
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/alcotest-lwt/failing/dune
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
--package
"alcotest-lwt"
--libraries
"alcotest-lwt lwt lwt.unix"))))
"alcotest-lwt lwt alcotest_test_helper"
--js))))

(rule
(alias runtest)
Expand Down
42 changes: 40 additions & 2 deletions test/e2e/alcotest-lwt/failing/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)))
15 changes: 15 additions & 0 deletions test/e2e/alcotest-lwt/failing/fail_with-js.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Testing `test/e2e/alcotest-lwt/failing/fail_with.ml'.
This run has ID `<uuid>'.

> [FAIL] all 0 one.
[OK] all 1 two.

┌──────────────────────────────────────────────────────────────────────────────┐
│ [FAIL] all 0 one. │
└──────────────────────────────────────────────────────────────────────────────┘
[failure] should fail
Logs saved to `<build-context>/_build/_tests/<test-dir>/all.000.output'.
──────────────────────────────────────────────────────────────────────────────

Full test results in `<build-context>/_build/_tests/<test-dir>'.
1 failure! in <test-duration>s. 2 tests run.
2 changes: 1 addition & 1 deletion test/e2e/alcotest-lwt/failing/fail_with.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/alcotest-lwt/passing/cli_options-js.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Testing `Results should be in JSON, since --json is passed'.
This run has ID `<uuid>'.

{
"success": 1,
"failures": 0,
"time": <test-duration>
}
2 changes: 1 addition & 1 deletion test/e2e/alcotest-lwt/passing/cli_options.ml
Original file line number Diff line number Diff line change
@@ -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) ]) ]
3 changes: 2 additions & 1 deletion test/e2e/alcotest-lwt/passing/dune
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
--package
"alcotest-lwt"
--libraries
"alcotest-lwt lwt lwt.unix"))))
"alcotest-lwt lwt alcotest_test_helper"
--js))))

(rule
(alias runtest)
Expand Down
23 changes: 21 additions & 2 deletions test/e2e/alcotest-lwt/passing/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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)))
7 changes: 7 additions & 0 deletions test/e2e/helper/alcotest_test_helper.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let rec wakeup_until_resolved p =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be explained somewhere that to make the tests work with js we use a custom lwt event loop.
Either in CONTRIBUTING.md or in alcotest_test_helper.ml.

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"
3 changes: 3 additions & 0 deletions test/e2e/helper/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
(name alcotest_test_helper)
(libraries lwt))