View Source Repatch.Application (Repatch v1.6.0)

Helper module for patching application env.

Usage

  1. In your test_helper.exs add this line

    Repatch.Application.setup()
  2. Add this to your test file

    use Repatch.ExUnit, isolate_env: :local # or :shared or :global

    Or if just call Repatch.Application.patch_application_env/1 and Repatch.Application.cleanup/1 manually in setup or tests.

  3. Done! Now you can just call regular Application functions and they will affect only local (or shared) application env, so changes are not affecting other processes

How it works

It just patches :application module functions which work with application env and replaces their implementation with a thing which calls repatch-driven application env.

Drawbacks

It may not work for code which calls to internal and unspecified Erlang application env implementation like ac_tab ets table or application_controller directly. It also ignores persistent and timeout options.

Summary

Functions

Cleans up a temporary env set up by the process. Use it after the test ends or if you want to reset application env back to what it used to be before the patch_application_env call.

Patches functions related to application env so that env is isolated. Accepts a list of options like the regular process.

Sets up the state for patching application env

Types

Link to this type

patch_application_env_option()

View Source
@type patch_application_env_option() :: Repatch.patch_option()

See Repatch.patch_option/0 for more information.

Functions

@spec cleanup(pid()) :: :ok

Cleans up a temporary env set up by the process. Use it after the test ends or if you want to reset application env back to what it used to be before the patch_application_env call.

Example

iex> Application.get_env(:ex_unit, :any)
nil
iex> Repatch.Application.patch_application_env(force: true)
iex> Application.put_env(:ex_unit, :any, :thing)
iex> Application.get_env(:ex_unit, :any)
:thing
iex> Repatch.Application.cleanup()
iex> Application.get_env(:ex_unit, :any)
nil
Link to this function

patch_application_env(opts \\ [])

View Source
@spec patch_application_env([patch_application_env_option()]) :: :ok

Patches functions related to application env so that env is isolated. Accepts a list of options like the regular process.

Example

iex> Application.get_env(:ex_unit, :any)
nil
iex> Repatch.Application.patch_application_env(force: true)
iex> Application.put_env(:ex_unit, :any, :thing)
iex> Application.get_env(:ex_unit, :any)
:thing
iex> Task.await(Task.async(fn -> Application.get_env(:ex_unit, :any) end))
nil
@spec setup() :: :ok

Sets up the state for patching application env

Example

in your test/test_helper.exs file:

ExUnit.start()
Repatch.setup()
Repatch.Application.setup()