Re: [elixir-core:7804] Proposal: [ExUnit] with_application_env(key, value, update_fun, fun)

2018-01-26 Thread Louis Pilfold
Hi Michael

I've written a tiny lib for this -> https://github.com/lpil/temporary-env

Perhaps you might find this useful :)

Cheers,
Louis

On Fri, 26 Jan 2018, 14:24 Michael Schaefermeyer, <
michael.schaeferme...@gmail.com> wrote:

> Hey all,
>
> I regularly find myself changing the Application configuration in tests to
> temporarily adjust a setting (i.e. when using Bypass and changing the
> configured endpoint for HTTP requests).
>
> My current workflow looks as follows:
>
> test "something" do
>   old_env = Application.get_env(:app, Module)
>   new_env = Keyword.put(old_env, :key, value)
>
>   Application.put_env(:app, Module, new_env)
>
>   # Do something that reads the config
>   Application.put_env(:app, Module, old_env)
> end
>
> Besides being quite repetitive it disallows async tests, as another test
> might be reading from the Application environment while this test is
> running.
>
> Ideally, ExUnit would allow me to temporarily change the Application
> environment in a async-safe way:
>
> test "something" do
>   with_application_env :app, Module, fn(config) -> Keyword.put(config, :
> key, value), fn ->
> # Do something that reads the config
>   end
> end
>
> Of course I could make the configuration module configurable and use
> something other than Application. The reason I am still suggesting this is
> that I feel like configuring your app via Application is still the way to
> go and I wouldn't want to diverge from that standard just for tests.
>
> Hope that makes sense. I would love to hear your thoughts.
>
> Thanks,
> Michael
>
> --
> 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/4b863c84-2062-42f3-b0a0-92b1d122f40c%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CABu8xFDWPftNWFJGeRNXRsXRNXXL7NjKxEKkLn%3DtcnrFqchjRw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[elixir-core:7804] Proposal: [ExUnit] with_application_env(key, value, update_fun, fun)

2018-01-26 Thread Michael Schaefermeyer
Hey all,

I regularly find myself changing the Application configuration in tests to 
temporarily adjust a setting (i.e. when using Bypass and changing the 
configured endpoint for HTTP requests).

My current workflow looks as follows:

test "something" do
  old_env = Application.get_env(:app, Module)
  new_env = Keyword.put(old_env, :key, value)
  
  Application.put_env(:app, Module, new_env)
 
  # Do something that reads the config
  Application.put_env(:app, Module, old_env)
end

Besides being quite repetitive it disallows async tests, as another test 
might be reading from the Application environment while this test is 
running.

Ideally, ExUnit would allow me to temporarily change the Application 
environment in a async-safe way:

test "something" do
  with_application_env :app, Module, fn(config) -> Keyword.put(config, :key, 
value), fn ->
# Do something that reads the config
  end
end

Of course I could make the configuration module configurable and use 
something other than Application. The reason I am still suggesting this is 
that I feel like configuring your app via Application is still the way to 
go and I wouldn't want to diverge from that standard just for tests.

Hope that makes sense. I would love to hear your thoughts.

Thanks,
Michael

-- 
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/4b863c84-2062-42f3-b0a0-92b1d122f40c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.