Skip to content

Best-effort automatic reporting of the location of Alcotest.check - #366

Merged
samoht merged 3 commits into
mirage:mainfrom
MisterDA:location-report
Nov 30, 2022
Merged

samoht merged 3 commits into
mirage:mainfrom
MisterDA:location-report

Conversation

@MisterDA

@MisterDA MisterDA commented Nov 21, 2022 •

Copy link
Copy Markdown
Collaborator

This is an attempt of automatically discover the location of calls to Alcotest.check, in order to not force the user to pass ~here or ~pos arguments. It uses Printexc.get_callstack. It's an early attempt that is fragile and requires OCaml 4.11.
The test need to end with the unit value in order to prevent the compiler from optimizing the last call to a tail-call, erasing the location in the process. Is there a way to change Alcotest' code so that this optimization doesn't happen?
A more advanced attempt should probably walk the call stack to find recognizable enclosing functions calls, add regression testing, and make it work (or default to no-op) on pre-4.11. I don't know whether the code is efficient enough.

Try it out with

dune exec --display=quiet -- examples/bad/bad.exe -e

cc @TheLortex

Comment thread src/alcotest-engine/test.ml Resolved
@MisterDA

Copy link
Copy Markdown
Collaborator Author

Reworked the code a bit thanks to @TheLortex review.
It now supports pre-4.11, is a bit more robust, and includes tests.
This only works with Alcotest.check and Alcotest.check', not with Alcotest.fail and related functions, because they're always in tail-call position since they raise, and adding a statement after these is unsound…
There's no way to force the compiler not to optimize the tail-call of Alcotest.check, so I've added a note explaining how Alcotest may "guess" the location.

@MisterDA
MisterDA marked this pull request as ready for review November 22, 2022 13:50
@MisterDA
MisterDA force-pushed the location-report branch 7 times, most recently from e5134fc to 31b74ce Compare November 22, 2022 15:54
@TheLortex

Copy link
Copy Markdown
Member

Thanks for this PR. It's a free improvement of the location reporting feature of alcotest.

The drawback is that it's not simple to predict when it works (backtrace enabled and call to Alcotest.check is not a tailcall).
The addition of a return unit instruction after the check to eliminate the tailcall might be somewhat brittle (maybe it will be optimized away at some point) and it's hard to discover its reason without having the context (meaning the unsuspecting user will want to remove it)

- Alcotest.check ... 
+ Alcotest.check ... ;
+ ()

I wonder what other alcotest users think about it.

@MisterDA

Copy link
Copy Markdown
Collaborator Author

I've changed the code to use the latest Printexc API, introduced in 4.12. Alcotest will fallback to a no-op before that.
I've also added an environment variable to disable position guessing: for instance Alcotest uses expect tests on its output, and the tests would fail on earlier OCaml versions. I don't expect they'll be many users of it apart from Alcotest itself.
I've added regression tests. Maybe they won't really work and strip_randomness should be modified to remove that extra information instead?

@samoht

samoht commented Nov 29, 2022

Copy link
Copy Markdown
Member

Thanks!

Could you show an example of what this changes in the output?

Also, for the new files, could you keep the same license as the rest of the project?

@MisterDA

MisterDA commented Nov 29, 2022 •

Copy link
Copy Markdown
Collaborator Author

This just shows the position of the call to Alcotest.check in the test source file, if available. The position is almost identical to using Alcotest.check ~here:[%here] from ppx here or Alcotest.check ~pos:Stdlib.__POSITION__ but the user doesn't have to specify the location themselves.
It only works if the call to Alcotest.check is a tailcall (usually the last one is not), and backtraces have been enabled (usually the case); and with OCaml >= 4.12.

An output with the location reported:

ASSERT strings
File "test/e2e/alcotest/source_code_position/test_source_code_position.ml", line 37, character 2:
FAIL strings

   Expected: `"A"'
   Received: `"B"'

one with disabled:

ASSERT strings
FAIL strings

   Expected: `"A"'
   Received: `"B"'

Also, for the new files, could you keep the same license as the rest of the project?

Done.

@samoht

samoht commented Nov 29, 2022 •

Copy link
Copy Markdown
Member

That looks like a great feature!

ASSERT strings
--
507 | -File "test/e2e/alcotest/source_code_position/test_source_code_position.ml", line 37, character 2:
508 | +File "test/e2e/alcotest/source_code_position/test_source_code_position.ml", line 24, character 2:
509 | FAIL strings

However, it doesn't seem very stable across OCaml versions 😁 https://ci.ocamllabs.io/github/mirage/alcotest/commit/7fad5b2e3cb68e8c3d2f29128850f16b166bbd4b/variant/alpine-3.15-4.14_opam-2.1#L507-507

Do we care? Should we just disable it for older OCaml versions?

@MisterDA

Copy link
Copy Markdown
Collaborator Author

However, it doesn't seem very stable across OCaml versions grin https://ci.ocamllabs.io/github/mirage/alcotest/commit/7fad5b2e3cb68e8c3d2f29128850f16b166bbd4b/variant/alpine-3.15-4.14_opam-2.1#L507-507

Do we care? Should we just disable it for older OCaml versions?

It's already the case, I just forgot to promote the tests when I added the copyright notice…

@samoht

samoht commented Nov 30, 2022

Copy link
Copy Markdown
Member

Looks great, thanks for the patch!

@samoht
samoht merged commit 838987d into mirage:main Nov 30, 2022
@MisterDA
MisterDA deleted the location-report branch December 1, 2022 00:04
samoht added a commit to samoht/opam-repository that referenced this pull request Feb 24, 2023
…d alcotest-async (1.7.0)

CHANGES:

- compile with MSVC (mirage/alcotest#369, @jonahbeckfordm review by @TheLortex
  and @MisterDA)

- Allow skipping a test case from inside the test case (mirage/alcotest#368, @apeschar)

- Fix compilation on bytecode architectures (mirage/alcotest#335, @glondu)

- Get `alcotest_stubs.c` to compile with MSVC (mirage/alcotest#369, @jonahbeckford)

- Try automatically reporting the location of calls to Alcotest.check.
  (mirage/alcotest#366, @MisterDA, review by @TheLortex)

- Detect that Alcotest is running in CI and change output accordingly.
  (mirage/alcotest#364, @MisterDA)

- Upgrade to `dune >= 3.0`. (mirage/alcotest#360, @MisterDA)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants