diff --git a/core/Run.ml b/core/Run.ml index c29a83c..a997c0a 100644 --- a/core/Run.ml +++ b/core/Run.ml @@ -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 = diff --git a/core/Run.mli b/core/Run.mli index b102bbb..92bc136 100644 --- a/core/Run.mli +++ b/core/Run.mli @@ -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 diff --git a/core/Testo.ml b/core/Testo.ml index 607cdb1..6742a1d 100644 --- a/core/Testo.ml +++ b/core/Testo.ml @@ -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 *) (**************************************************************************) diff --git a/core/Testo.mli b/core/Testo.mli index bdbf66e..79394c2 100644 --- a/core/Testo.mli +++ b/core/Testo.mli @@ -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 diff --git a/tests/Test.ml b/tests/Test.ml index 48534c0..17dedbe 100644 --- a/tests/Test.ml +++ b/tests/Test.ml @@ -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" diff --git a/tests/snapshots/testo_meta_tests/fccf02a5c37e/stdxxx b/tests/snapshots/testo_meta_tests/fccf02a5c37e/stdxxx index 03e4f87..bd699a0 100644 --- a/tests/snapshots/testo_meta_tests/fccf02a5c37e/stdxxx +++ b/tests/snapshots/testo_meta_tests/fccf02a5c37e/stdxxx @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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. @@ -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 @@ -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. @@ -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 @@ -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 @@ -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. @@ -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. @@ -2699,9 +2724,9 @@ junk printed on stdout... Called from ──────────────────────────────────────────────────────────────────────────────── -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.