Personally, given the gotchas you mentioned, I feel like it's easy enough
to do this manually in a setup block that it doesn't warrant more stuff
added to stdlib.

Andrea

On Sat, 27 Jun 2020 at 13:19, Wojtek Mach <woj...@wojtekmach.pl> wrote:

> It’s common to create temporary directories for tests, ideally a unique
> directory per test to run them concurrently.
>
> I’d like to propose adding `ExUnit.Callbacks.tmp_dir!/0` and this is how
> we could use it:
>
>     defmodule MyTest do
>       use ExUnit.Case, async: true
>
>       test "my test" do
>         assert tmp_dir!() =~ "my test"
>         assert File.dir?(tmp_dir!())
>       end
>     end
>
> The directory is lazily created on the first call, the second call to that
> function within the same test simply returns the same path.
>
> While the path must be unique per test, I believe it should also be
> predictable to ease debugging, I picked: tmp/<module>/<test>.
>
> To extract the module & test name, we have two options:
>
> 1. Define tmp_dir!/0 as a regular function and use stack trace
> 2. Define tmp_dir!/0 as a macro
>
> Here’s a proof of concept for option 1:
>
> - the code:
> https://github.com/wojtekmach/elixir/commit/4c399540802a3cae583c086d865dc90d865df6c8
> - example of usage in Elixir test suite:
> https://github.com/wojtekmach/elixir/commit/01df2551582dd9acdaa2f6ff982d6767763070f1
>
> The downside of using stack trace is it can easily get mangled, people
> shouldn’t do this:
>
>     test "my test" do
>       tmp_dir!()
>
>       Task.async(fn ->
>         tmp_dir!()
>       end)
>       |> Task.await()
>     end
>
> And instead do that:
>
>     test "my test" do
>       tmp_dir = tmp_dir!()
>
>       Task.async(fn ->
>         tmp_dir
>       end)
>       |> Task.await()
>     end
>
> I believe documenting this might be enough.
>
> This proposal is inspired by https://github.com/golang/go/issues/35998.
>
> Any feedback appreciated!
>
> --
> 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 elixir-lang-core+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/066A99E3-B470-44C3-9632-F52E97AB8791%40wojtekmach.pl
> .
>

-- 
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 elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/CAM9Rf%2BKYBZ8SFAGONa6BxrrcnRbZdxUyVHjNaFrLoL4Y6FewWA%40mail.gmail.com.

Reply via email to