Assuming, of course I wrote this proposal without the typo in the final 
sentence and properly used an anonymous function for *on_exit(fn state -> 
#... end)* in my examples.

On Monday, June 15, 2026 at 6:27:48 PM UTC-5 Christopher Keele wrote:

> I am creating a resource during test setup based on a test's context 
> (namely, its module and name):
>
>
>
> *setup context do  prepare_resource_for_test_context(context)end*
>
> I would like to be able to teardown this resource upon test conclusion. 
> This is possible today:
>
>
> *setup context do  prepare_resource_for_test_context(context)*
> *  on_exit do*
> *    cleanup_resource_for_test_context(context)*
> *  end*
> *end*
>
> *Proposal*
>
> However, I would also like to leave the resource in-place on test failure 
> for inspection. (My *prepare_resource* function can handle the situation 
> where a resource already exists on setup. This is opposite to the common 
> tmpdir pattern where the resource cleans itself up automatically upon test 
> conclusion but would have unexpected side effects if already setup.)
>
> As far as I know, this is not possible today. I would like to do something 
> like receiving the *ExUnit.state()* 
> <https://ex-unit.hexdocs.pm/ExUnit.html#t:state/0> in the *on_exit* 
> callback:
>
>
> *setup context do  prepare_resource_for_test_context(context)*
> *  on_exit state do*
>     *case state do*
> *      {:failed, _} -> :ok*
> *      _ -> cleanup_resource_for_test_context(context)*
> *    end*
> *  end*
> *end*
>
> Are there reasons to not entertain this functionality? Is there another 
> way to accomplish it that doesn't rely on a test formatter to notice 
> *{:test_finished, 
> test}* and do the cleanup at a global level?
>
> *Implementation*
>
> AFAICT we could enable this usecase trivially by threading 
> *test_or_case.state* into  
> <https://github.com/elixir-lang/elixir/blob/1d599978f7d36ea6896b294b3d26d110212be7ab/lib/ex_unit/lib/ex_unit/runner.ex#L541>*ExUnit.OnExitHandler.run
>  
> <https://github.com/elixir-lang/elixir/blob/1d599978f7d36ea6896b294b3d26d110212be7ab/lib/ex_unit/lib/ex_unit/runner.ex#L541>
>  *and 
> on to its helper functions.
>
> We could retain backwards-compatibility by having *exec_callback(callback, 
> state)* 
> <https://github.com/elixir-lang/elixir/blob/1d599978f7d36ea6896b294b3d26d110212be7ab/lib/ex_unit/lib/ex_unit/on_exit_handler.ex#L139>
>  
> check the arity of the callback before invoking it.
>
> Documentation could describe the optional callback parameter and elaborate 
> that an *on_exit* callback defined in a *setup_all* would have some other 
> behaviour (raise an error, receive *nil* or the case name instead of a 
> state, etc—open to ideas).
>
> Are would such an implementation be welcome?
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/elixir-lang-core/71b223e9-3815-4b8d-9ac0-cb32b98b05e8n%40googlegroups.com.

Reply via email to