summaryrefslogtreecommitdiff
path: root/test/elixir/lib/setup.ex
blob: 037988521533d00d157efe495b504027e6cd8813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
defmodule Couch.Test.Setup do
  @moduledoc """
  Allows to chain setup functions.
  Example of using:

  ```
    alias Couch,Test.Utils
    def with_db_name(context, setup) do
      setup =
        setup
          |> Step.Start.new(:start, extra_apps: [:chttpd])
          |> Step.User.new(:admin, roles: [:server_admin])
          |> Setup.run()

      context =
        Map.merge(context, %{
          db_name: Utils.random_name("db")
          base_url: setup |> Setup.get(:start) |> Step.Start.clustered_url(),
          user: setup |> Setup.get(:admin) |> Step.User.name()
        })
      {context, setup}
    end

    @tag setup: &__MODULE__.with_db_name/2
      test "Create", %{db_name: db_name, user: user} do
        ...
      end
  ```
  """
  import ExUnit.Callbacks, only: [on_exit: 1]
  import ExUnit.Assertions, only: [assert: 2]
  require Logger

  alias Couch.Test.Setup
  alias Couch.Test.Setup.Step
  defstruct stages: [], by_type: %{}, state: %{}

  def step(%Setup{stages: stages} = setup, id, step) do
    %{setup | stages: [{id, step} | stages]}
  end

  defp setup_step({id, step}, %Setup{state: state, by_type: by_type} = setup) do
    %module{} = step
    # credo:disable-for-next-line Credo.Check.Warning.LazyLogging
    Logger.debug("Calling 'setup/2' for '#{module}'")
    step = module.setup(setup, step)
    state = Map.put(state, id, step)
    by_type = Map.update(by_type, module, [id], fn ids -> [id | ids] end)
    on_exit(fn ->
      # credo:disable-for-next-line Credo.Check.Warning.LazyLogging
      Logger.debug("Calling 'teardown/3' for '#{module}'")
      try do
        module.teardown(setup, step)
        :ok
      catch
        _ -> :ok
        _, _ -> :ok
      end
    end)
    {{id, step}, %{setup | state: state, by_type: by_type}}
  end

  def run(%Setup{stages: stages} = setup) do
    {stages, setup} = stages
      |> Enum.reverse
      |> Enum.map_reduce(setup, &setup_step/2)
    %{setup | stages: stages}
  end

  def setup(ctx) do
    Map.get(ctx, :__setup)
  end

  def setup(ctx, setup_fun) do
    setup = %Setup{} |> Step.Config.new(:test_config, config_file: nil)
    {ctx, setup} = setup_fun.(ctx, setup)
    assert not Map.has_key?(ctx, :__setup), "Key `__setup` is reserved for internal purposes"
    Map.put(ctx, :__setup, setup)
  end

  def completed?(%Setup{by_type: by_type}, step) do
    Map.has_key?(by_type, step)
  end

  def all_for(%Setup{by_type: by_type, state: state}, step_module) do
    Map.take(state, by_type[step_module] || [])
  end

  def reduce_for(setup, step_module, acc, fun) do
    Enum.reduce(all_for(setup, step_module), acc, fun)
  end

  def get(%Setup{state: state}, id) do
    state[id]
  end

end