Skip to content
Draft
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
24 changes: 23 additions & 1 deletion core/Run.ml
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,37 @@ let with_flip_xfail_outcome (test : T.test) func =
(Printexc.raw_backtrace_to_string trace);
P.return ())

let current_test : T.test option ref = ref None

let get_current_test () = !current_test

(* Set the current_test ref for the duration of the test.

The user-specified test function must have access to the current test.
Some of the other wrappers such as the calling the 'normalize' function
on captured output might want to access the current test, so we
make this the outmost wrapper in 'wrap_test_function'.
*)
let with_current_test_ref test func =
fun () ->
P.protect
(fun () ->
current_test := Some test;
func ()
)
~finally:(fun () -> current_test := None; P.return ())

let conditional_wrap condition wrapper func =
if condition then wrapper func else func

let wrap_test_function ~with_storage ~flip_xfail_outcome test func =
let wrap_test_function ~with_storage ~flip_xfail_outcome test
(func : unit -> unit Promise.t) : unit -> unit Promise.t =
func
|> conditional_wrap with_storage (with_store_exception test)
|> conditional_wrap flip_xfail_outcome (with_flip_xfail_outcome test)
|> protect_globals test
|> conditional_wrap with_storage (Store.with_result_capture test)
|> with_current_test_ref test

let to_alcotest_internal ~alcotest_skip ~with_storage ~flip_xfail_outcome tests
=
Expand Down
7 changes: 7 additions & 0 deletions core/Run.mli
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ val cmd_approve :
int

val introduction_text : string

(* This returns the test being executed, if any.
It allows a test function to obtain the test object or to call functions
that depend on the test object while keeping the type of the test
function simple.
*)
val get_current_test : unit -> Types.test option
7 changes: 7 additions & 0 deletions core/Testo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ let with_chdir path func =
~finally:(fun () -> Unix.chdir orig_cwd)
func

(* We need this to allow the user's test function to call
'stash_output_files' *)
let get_current_test () = Run.get_current_test ()

(* TODO: stash_output_files: copy the output files to compare against
snapshots before cleaning up the workspace *)

(**************************************************************************)
(* Hygiene *)
(**************************************************************************)
Expand Down
3 changes: 3 additions & 0 deletions core/Testo.mli
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ val with_chdir : Fpath.t -> (unit -> 'a) -> 'a
The original value of the current directory is restored when the function
terminates. *)

val get_current_test : unit -> Types.test option
(** Return the test currently running. *)

(** {2 Output masking functions}

Functions with the [mask_] prefix are string replacement
Expand Down
5 changes: 5 additions & 0 deletions tests/Test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,11 @@ let tests env =
test_diff "trailing-context";
test_diff "joined-context";
test_diff "gap-in-context";
t "current test" (fun () ->
match Testo.get_current_test () with
| None -> Alcotest.fail "current test is unset"
| Some test -> Alcotest.(check string) "test name" "current test" test.name
)
]
@ categorized @ test_internal_files
@ Testo.categorize "Slice"
Expand Down
57 changes: 41 additions & 16 deletions tests/snapshots/testo_meta_tests/fccf02a5c37e/stdxxx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ junk printed on stdout...
[MISS] 1cf5a6371f59 diff > trailing-context
[MISS] 8ae0ad03ce59 diff > joined-context
[MISS] 360c4b690be4 diff > gap-in-context
[MISS] 08e4221951ee current test
[MISS] 33a93d234d01 biohazard > fruit > apple
[MISS] ec7514c8554d biohazard > fruit > kiwi
[MISS] b8cd199f2e62 biohazard > animal > banana slug
Expand Down Expand Up @@ -323,6 +324,9 @@ Try '--help' for options.
• Path to expected gap-in-context.diff: tests/diff-data/gap-in-context.diff
• Path to captured gap-in-context.diff: _build/testo/status/testo_tests/360c4b690be4/stdout
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
[RUN] 08e4221951ee current test
[PASS] 08e4221951ee current test
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
[RUN] 33a93d234d01 biohazard > fruit > apple
[PASS] 33a93d234d01 biohazard > fruit > apple
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
Expand Down Expand Up @@ -494,9 +498,9 @@ Try '--help' for options.
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
tests/snapshots/testo_tests/unnamed-junk ??
69/69 selected tests:
70/70 selected tests:
1 skipped
67 successful (64 pass, 3 xfail)
68 successful (65 pass, 3 xfail)
1 unsuccessful (1 fail, 0 xpass)
9 tests whose output needs first-time approval
overall status: failure
Expand Down Expand Up @@ -696,6 +700,8 @@ Try '--help' for options.
• Path to expected gap-in-context.diff: tests/diff-data/gap-in-context.diff
• Path to captured gap-in-context.diff: _build/testo/status/testo_tests/360c4b690be4/stdout
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
[PASS] 08e4221951ee current test
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
[PASS] 33a93d234d01 biohazard > fruit > apple
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
[PASS] ec7514c8554d biohazard > fruit > kiwi
Expand Down Expand Up @@ -836,9 +842,9 @@ Try '--help' for options.
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
tests/snapshots/testo_tests/unnamed-junk ??
69/69 selected tests:
70/70 selected tests:
1 skipped
67 successful (64 pass, 3 xfail)
68 successful (65 pass, 3 xfail)
1 unsuccessful (1 fail, 0 xpass)
9 tests whose output needs first-time approval
overall status: failure
Expand Down Expand Up @@ -955,7 +961,7 @@ Try '--help' for options.
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
tests/snapshots/testo_tests/unnamed-junk ??
1/69 selected tests:
1/70 selected tests:
0 successful (0 pass, 0 xfail)
1 unsuccessful (1 fail, 0 xpass)
overall status: failure
Expand Down Expand Up @@ -985,7 +991,7 @@ Try '--help' for options.
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
tests/snapshots/testo_tests/unnamed-junk ??
1/69 selected tests:
1/70 selected tests:
1 successful (1 pass, 0 xfail)
0 unsuccessful (0 fail, 0 xpass)
overall status: success
Expand Down Expand Up @@ -1200,6 +1206,9 @@ Try '--help' for options.
• Missing file containing the test output: _build/testo/status/testo_tests/360c4b690be4/completion_status
• Path to captured gap-in-context.diff: _build/testo/status/testo_tests/360c4b690be4/stdout
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
[MISS] 08e4221951ee current test
• Missing file containing the test output: _build/testo/status/testo_tests/08e4221951ee/completion_status
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
[MISS] 33a93d234d01 biohazard > fruit > apple
• Missing file containing the test output: _build/testo/status/testo_tests/33a93d234d01/completion_status
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
Expand Down Expand Up @@ -1597,6 +1606,12 @@ Try '--help' for options.
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
────────────────────────────────────────────────────────────────────────────────
┌──────────────────────────────────────────────────────────────────────────────┐
│ [MISS] 08e4221951ee current test │
└──────────────────────────────────────────────────────────────────────────────┘
• Missing file containing the test output: _build/testo/status/testo_tests/08e4221951ee/completion_status
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
────────────────────────────────────────────────────────────────────────────────
┌──────────────────────────────────────────────────────────────────────────────┐
│ [MISS] 33a93d234d01 biohazard > fruit > apple │
└──────────────────────────────────────────────────────────────────────────────┘
• Missing file containing the test output: _build/testo/status/testo_tests/33a93d234d01/completion_status
Expand Down Expand Up @@ -1733,11 +1748,11 @@ Try '--help' for options.
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
tests/snapshots/testo_tests/unnamed-junk ??
69/69 selected tests:
70/70 selected tests:
1 skipped
0 successful (0 pass, 0 xfail)
0 unsuccessful (0 fail, 0 xpass)
68 new tests
69 new tests
overall status: failure
The status of 1 "broken" test was ignored! Use '--strict' to override.
<handling result before exiting>
Expand Down Expand Up @@ -2072,6 +2087,12 @@ junk printed on stdout...
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
────────────────────────────────────────────────────────────────────────────────
┌──────────────────────────────────────────────────────────────────────────────┐
│ [MISS] 08e4221951ee current test │
└──────────────────────────────────────────────────────────────────────────────┘
• Missing file containing the test output: _build/testo/status/testo_tests/08e4221951ee/completion_status
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
────────────────────────────────────────────────────────────────────────────────
┌──────────────────────────────────────────────────────────────────────────────┐
│ [MISS] 33a93d234d01 biohazard > fruit > apple │
└──────────────────────────────────────────────────────────────────────────────┘
• Missing file containing the test output: _build/testo/status/testo_tests/33a93d234d01/completion_status
Expand Down Expand Up @@ -2208,11 +2229,11 @@ junk printed on stdout...
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
tests/snapshots/testo_tests/unnamed-junk ??
69/69 selected tests:
70/70 selected tests:
1 skipped
0 successful (0 pass, 0 xfail)
0 unsuccessful (0 fail, 0 xpass)
68 new tests
69 new tests
overall status: failure
The status of 1 "broken" test was ignored! Use '--strict' to override.
<handling result before exiting>
Expand Down Expand Up @@ -2266,6 +2287,7 @@ junk printed on stdout...
[MISS] 1cf5a6371f59 diff > trailing-context
[MISS] 8ae0ad03ce59 diff > joined-context
[MISS] 360c4b690be4 diff > gap-in-context
[MISS] 08e4221951ee current test
[MISS] 33a93d234d01 biohazard > fruit > apple
[MISS] ec7514c8554d biohazard > fruit > kiwi
[MISS] b8cd199f2e62 biohazard > animal > banana slug
Expand Down Expand Up @@ -2528,6 +2550,9 @@ Try '--help' for options.
• Path to expected gap-in-context.diff: tests/diff-data/gap-in-context.diff
• Path to captured gap-in-context.diff: _build/testo/status/testo_tests/360c4b690be4/stdout
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
[RUN] 08e4221951ee current test
[PASS] 08e4221951ee current test
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
[RUN] 33a93d234d01 biohazard > fruit > apple
[PASS] 33a93d234d01 biohazard > fruit > apple
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
Expand Down Expand Up @@ -2614,9 +2639,9 @@ Try '--help' for options.
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
tests/snapshots/testo_tests/unnamed-junk ??
69/69 selected tests:
70/70 selected tests:
1 skipped
67 successful (64 pass, 3 xfail)
68 successful (65 pass, 3 xfail)
1 unsuccessful (1 fail, 0 xpass)
overall status: success
The status of 1 "broken" test was ignored! Use '--strict' to override.
Expand Down Expand Up @@ -2673,9 +2698,9 @@ junk printed on stdout...
2 folders no longer belong to the test suite and are being removed:
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
tests/snapshots/testo_tests/unnamed-junk ??
69/69 selected tests:
70/70 selected tests:
1 skipped
67 successful (64 pass, 3 xfail)
68 successful (65 pass, 3 xfail)
1 unsuccessful (1 fail, 0 xpass)
overall status: success
The status of 1 "broken" test was ignored! Use '--strict' to override.
Expand All @@ -2699,9 +2724,9 @@ junk printed on stdout...
Called from <MASKED>

────────────────────────────────────────────────────────────────────────────────
69/69 selected tests:
70/70 selected tests:
1 skipped
67 successful (64 pass, 3 xfail)
68 successful (65 pass, 3 xfail)
1 unsuccessful (1 fail, 0 xpass)
overall status: success
The status of 1 "broken" test was ignored! Use '--strict' to override.
Expand Down