Implement the response to webhooks

Implement the response to webhooks

defmodule Aelita2.WebhookController do
  use Aelita2.Web, :controller

  alias Aelita2.Installation
  alias Aelita2.Project

  @doc """
  This action is reached via `/webhook/:provider`
  """
  def webhook(conn, %{"provider" => "github"}) do
    do_webhook conn, "github", conn.req_headers["X-GitHub-Event"]
    conn
    |> send_resp(200, "")
  end

  def do_webhook(conn, "github", "ping") do
    :ok
  end

  def do_webhook(conn, "github", "integration_installation") do
    payload = Poison.decode!(conn.body)
    installation_id = payload["installation"]["id"]
    case payload["action"] do
      "deleted" -> Repo.delete_all(from(
        i in Installation,
        where: i.installation_id == ^installation_id
      ))
      "created" -> Repo.insert!(%Installation{
        installation_id: installation_id
      })
    end
    :ok
  end

  def do_webhook(conn, "github", "integration_installation_repositories") do
    payload = Poison.decode!(conn.body)
    installation_id = payload["installation"]["id"]
    installation = Repo.get_by!(Installation, installation_id: installation_id)
    :ok = case payload["action"] do
      "removed" -> :ok
      "added" -> :ok
    end
    Enum.each(
      payload["repositories_removed"],
      fn(r) -> Repo.delete_all(from(
        p in Project,
        where: p.repo_id == ^r["id"])) end)
    Enum.each(
      payload["repositories_added"],
      fn(r) -> Repo.insert! %Project{
        repo_id: r["id"], name: r["full_name"], installation: installation} end)
    :ok
  end
end

defmodule Aelita2.WebhookController do
  use Aelita2.Web, :controller

  alias Aelita2.Installation
  alias Aelita2.Project

  @doc """
  This action is reached via `/webhook/:provider`
  """
  def webhook(conn, %{"provider" => "github"}) do
    do_webhook conn, "github", conn.req_headers["X-GitHub-Event"]
  end

  def do_webhook(conn, "github", "integration_installation") do
    payload = Poison.decode!(conn.body)
    installation_id = payload["installation"]["id"]
    case payload["action"] do
      "deleted" -> Repo.delete_all(from(
        i in Installation,
        where: i.installation_id == ^installation_id
      ))
      "created" -> Repo.insert!(%Installation{
        installation_id: installation_id
      })
    end
  end

  def do_webhook(conn, "github", "integration_installation_repositories") do
    payload = Poison.decode!(conn.body)
    installation_id = payload["installation"]["id"]
    installation = Repo.get_by!(Installation, installation_id: installation_id)
    :ok = case payload["action"] do
      "removed" -> :ok
      "added" -> :ok
    end
    Enum.each(
      payload["repositories_removed"],
      fn(r) -> Repo.delete_all(from(
        p in Project,
        where: p.repo_id == ^r["id"])) end)
    Enum.each(
      payload["repositories_added"],
      fn(r) -> Repo.insert! %Project{
        repo_id: r["id"], name: r["full_name"], installation: installation} end)
  end
end


Fix docs example in ExUnit.Case (#10754)

defmodule ExUnit.DuplicateTestError do
  defexception [:message]
end

defmodule ExUnit.DuplicateDescribeError do
  defexception [:message]
end

defmodule ExUnit.Case do
  @moduledoc """
  Helpers for defining test cases.

  This module must be used in other modules as a way to configure
  and prepare them for testing.

  When used, it accepts the following options:

    * `:async` - configures tests in this module to run concurrently with
      tests in other modules. Tests in the same module never run concurrently.
      It should be enabled only if tests do not change any global state.
      Defaults to `false`.

  This module automatically includes all callbacks defined in
  `ExUnit.Callbacks`. See that module for more information on `setup`,
  `start_supervised`, `on_exit` and the test process life cycle.

  For grouping tests together, see `describe/2` in this module.

  ## Examples

      defmodule AssertionTest do
        # Use the module
        use ExUnit.Case, async: true

        # The "test" macro is imported by ExUnit.Case
        test "always pass" do
          assert true
        end
      end

  ## Context

  All tests receive a context as an argument. The context is particularly
  useful for sharing information between callbacks and tests:

      defmodule KVTest do
        use ExUnit.Case

        setup do
          {:ok, pid} = KV.start_link()
          {:ok, pid: pid}
        end

        test "stores key-value pairs", context do
          assert KV.put(context[:pid], :hello, :world) == :ok
          assert KV.get(context[:pid], :hello) == :world
        end
      end

  As the context is a map, it can be pattern matched on to extract
  information:

      test "stores key-value pairs", %{pid: pid} = _context do
        assert KV.put(pid, :hello, :world) == :ok
        assert KV.get(pid, :hello) == :world
      end

  ## Tags

  The context is used to pass information from the callbacks to
  the test. In order to pass information from the test to the
  callback, ExUnit provides tags.

  By tagging a test, the tag value can be accessed in the context,
  allowing the developer to customize the test. Let's see an
  example:

      defmodule FileTest do
        # Changing directory cannot be async
        use ExUnit.Case, async: false

        setup context do
          # Read the :cd tag value
          if cd = context[:cd] do
            prev_cd = File.cwd!()
            File.cd!(cd)
            on_exit(fn -> File.cd!(prev_cd) end)
          end

          :ok
        end

        @tag cd: "fixtures"
        test "reads UTF-8 fixtures" do
          File.read("README.md")
        end
      end

  In the example above, we have defined a tag called `:cd` that is
  read in the setup callback to configure the working directory the
  test is going to run on.

  Tags are also very effective when used with case templates
  (`ExUnit.CaseTemplate`) allowing callbacks in the case template
  to customize the test behaviour.

  Note a tag can be set in two different ways:

      @tag key: value
      @tag :key       # equivalent to setting @tag key: true

  If a tag is given more than once, the last value wins.

  ### Module and describe tags

  A tag can be set for all tests in a module or describe block by
  setting `@moduletag` or `@describetag` inside each context
  respectively:

      defmodule ApiTest do
        use ExUnit.Case
        @moduletag :external

        describe "makes calls to the right endpoint" do
          @describetag :endpoint

          # ...
        end
      end

  If you are setting a `@moduletag` or `@describetag` attribute, you must
  set them after your call to `use ExUnit.Case` otherwise you will see
  compilation errors.

  If the same key is set via `@tag`, the `@tag` value has higher
  precedence.

  ### Known tags

  The following tags are set automatically by ExUnit and are
  therefore reserved:

    * `:module`     - the module on which the test was defined

    * `:file`       - the file on which the test was defined

    * `:line`       - the line on which the test was defined

    * `:test`       - the test name

    * `:async`      - if the test case is in async mode

    * `:registered` - used for `ExUnit.Case.register_attribute/3` values

    * `:describe`   - the describe block the test belongs to

  The following tags customize how tests behave:

    * `:capture_log` - see the "Log Capture" section below

    * `:skip` - skips the test with the given reason

    * `:timeout` - customizes the test timeout in milliseconds (defaults to 60000).
      Accepts `:infinity` as a timeout value.

    * `:tmp_dir` - (since v1.11.0) see the "Tmp Dir" section below

  The `:test_type` tag is automatically set by ExUnit, but is **not** reserved.
  This tag is available for users to customize if they desire.

  ## Filters

  Tags can also be used to identify specific tests, which can then
  be included or excluded using filters. The most common functionality
  is to exclude some particular tests from running, which can be done
  via `ExUnit.configure/1`:

      # Exclude all external tests from running
      ExUnit.configure(exclude: [external: true])

  From now on, ExUnit will not run any test that has the `:external` option
  set to `true`. This behaviour can be reversed with the `:include` option
  which is usually passed through the command line:

      mix test --include external:true

  Run `mix help test` for more information on how to run filters via Mix.

  Another use case for tags and filters is to exclude all tests that have
  a particular tag by default, regardless of its value, and include only
  a certain subset:

      ExUnit.configure(exclude: :os, include: [os: :unix])

  A given include/exclude filter can be given more than once:

      ExUnit.configure(exclude: [os: :unix, os: :windows])

  Keep in mind that all tests are included by default, so unless they are
  excluded first, the `include` option has no effect.

  ## Log Capture

  ExUnit can optionally suppress printing of log messages that are generated
  during a test. Log messages generated while running a test are captured and
  only if the test fails are they printed to aid with debugging.

  You can opt into this behaviour for individual tests by tagging them with
  `:capture_log` or enable log capture for all tests in the ExUnit configuration:

      ExUnit.start(capture_log: true)

  This default can be overridden by `@tag capture_log: false` or
  `@moduletag capture_log: false`.

  Since `setup_all` blocks don't belong to a specific test, log messages generated
  in them (or between tests) are never captured. If you want to suppress these
  messages as well, remove the console backend globally by setting:

      config :logger, backends: []

  ## Tmp Dir

  ExUnit automatically creates a temporary directory for tests tagged with
  `:tmp_dir` and puts the path to that directory into the test context.
  The directory is removed before being created to ensure we start with a blank
  slate.

  The temporary directory path is unique (includes the test module and test name)
  and thus appropriate for running tests concurrently. You can customize the path
  further by setting the tag to a string, e.g.: `tmp_dir: "my_path"`, which would
  make the final path to be: `tmp/<module>/<test>/my_path`.

  Example:

      defmodule MyTest do
        use ExUnit.Case, async: true

        @tag :tmp_dir
        test "with tmp_dir", %{tmp_dir: tmp_dir} do
          assert tmp_dir =~ "with tmp_dir"
          assert File.dir?(tmp_dir)
        end
      end

  As with other tags, `:tmp_dir` can also be set as `@moduletag` and
  `@describetag`.
  """

  @type env :: module() | Macro.Env.t()

  @reserved [:module, :file, :line, :test, :async, :registered, :describe]

  @doc false
  defmacro __using__(opts) do
    unless Process.whereis(ExUnit.Server) do
      raise "cannot use ExUnit.Case without starting the ExUnit application, " <>
              "please call ExUnit.start() or explicitly start the :ex_unit app"
    end

    quote do
      unless ExUnit.Case.__register__(__MODULE__, unquote(opts)) do
        use ExUnit.Callbacks
      end

      import ExUnit.Callbacks
      import ExUnit.Assertions
      import ExUnit.Case, only: [describe: 2, test: 1, test: 2, test: 3]
      import ExUnit.DocTest
    end
  end

  @doc false
  def __register__(module, opts) do
    registered? = Module.has_attribute?(module, :ex_unit_tests)

    unless registered? do
      tag_check = Enum.any?([:moduletag, :describetag, :tag], &Module.has_attribute?(module, &1))

      if tag_check do
        raise "you must set @tag, @describetag, and @moduletag after the call to \"use ExUnit.Case\""
      end

      attributes = [
        :ex_unit_tests,
        :tag,
        :describetag,
        :moduletag,
        :ex_unit_registered_test_attributes,
        :ex_unit_registered_describe_attributes,
        :ex_unit_registered_module_attributes,
        :ex_unit_used_describes
      ]

      Enum.each(attributes, &Module.register_attribute(module, &1, accumulate: true))

      attributes = [
        before_compile: ExUnit.Case,
        after_compile: ExUnit.Case,
        ex_unit_async: false,
        ex_unit_describe: nil
      ]

      Enum.each(attributes, fn {k, v} -> Module.put_attribute(module, k, v) end)
    end

    async? = opts[:async]

    if is_boolean(async?) do
      Module.put_attribute(module, :ex_unit_async, async?)
    end

    registered?
  end

  @doc """
  Defines a test with `message`.

  The test may also define a pattern, which will be matched
  against the test context. For more information on contexts, see
  `ExUnit.Callbacks`.

  ## Examples

      test "true is equal to true" do
        assert true == true
      end

  """
  defmacro test(message, var \\ quote(do: _), contents) do
    unless is_tuple(var) do
      IO.warn(
        "test context is always a map. The pattern " <>
          "#{inspect(Macro.to_string(var))} will never match",
        Macro.Env.stacktrace(__CALLER__)
      )
    end

    contents =
      case contents do
        [do: block] ->
          quote do
            unquote(block)
            :ok
          end

        _ ->
          quote do
            try(unquote(contents))
            :ok
          end
      end

    var = Macro.escape(var)
    contents = Macro.escape(contents, unquote: true)
    %{module: mod, file: file, line: line} = __CALLER__

    quote bind_quoted: [
            var: var,
            contents: contents,
            message: message,
            mod: mod,
            file: file,
            line: line
          ] do
      name = ExUnit.Case.register_test(mod, file, line, :test, message, [])
      def unquote(name)(unquote(var)), do: unquote(contents)
    end
  end

  @doc """
  Defines a not implemented test with a string.

  Provides a convenient macro that allows a test to be defined
  with a string, but not yet implemented. The resulting test will
  always fail and print a "Not implemented" error message. The
  resulting test case is also tagged with `:not_implemented`.

  ## Examples

      test "this will be a test in future"

  """
  defmacro test(message) do
    %{module: mod, file: file, line: line} = __CALLER__

    quote bind_quoted: binding() do
      name = ExUnit.Case.register_test(mod, file, line, :test, message, [:not_implemented])
      def unquote(name)(_), do: flunk("Not implemented")
    end
  end

  @doc """
  Describes tests together.

  Every describe block receives a name which is used as prefix for
  upcoming tests. Inside a block, `ExUnit.Callbacks.setup/1` may be
  invoked and it will define a setup callback to run only for the
  current block. The describe name is also added as a tag, allowing
  developers to run tests for specific blocks.

  ## Examples

      defmodule StringTest do
        use ExUnit.Case, async: true

        describe "String.capitalize/1" do
          test "first grapheme is in uppercase" do
            assert String.capitalize("hello") == "Hello"
          end

          test "converts remaining graphemes to lowercase" do
            assert String.capitalize("HELLO") == "Hello"
          end
        end
      end

  When using Mix, you can run all tests in a describe block by name:

      mix test --only describe:"String.capitalize/1"

  or by passing the exact line the describe block starts on:

      mix test path/to/file:123

  Note describe blocks cannot be nested. Instead of relying on hierarchy
  for composition, developers should build on top of named setups. For
  example:

      defmodule UserManagementTest do
        use ExUnit.Case, async: true

        describe "when user is logged in and is an admin" do
          setup [:log_user_in, :set_type_to_admin]

          test ...
        end

        describe "when user is logged in and is a manager" do
          setup [:log_user_in, :set_type_to_manager]

          test ...
        end

        defp log_user_in(context) do
          # ...
        end
      end

  By forbidding hierarchies in favor of named setups, it is straightforward
  for the developer to glance at each describe block and know exactly the
  setup steps involved.
  """
  defmacro describe(message, do: block) do
    quote do
      ExUnit.Case.__describe__(__MODULE__, __ENV__.line, unquote(message), fn ->
        unquote(block)
      end)
    end
  end

  @doc false
  def __describe__(module, line, message, fun) do
    if Module.get_attribute(module, :ex_unit_describe) do
      raise "cannot call \"describe\" inside another \"describe\". See the documentation " <>
              "for ExUnit.Case.describe/2 on named setups and how to handle hierarchies"
    end

    cond do
      not is_binary(message) ->
        raise ArgumentError, "describe name must be a string, got: #{inspect(message)}"

      message in Module.get_attribute(module, :ex_unit_used_describes) ->
        raise ExUnit.DuplicateDescribeError,
              "describe #{inspect(message)} is already defined in #{inspect(module)}"

      true ->
        :ok
    end

    if Module.get_attribute(module, :describetag) != [] do
      raise "@describetag must be set inside describe/2 blocks"
    end

    Module.put_attribute(module, :ex_unit_describe, {line, message})
    Module.put_attribute(module, :ex_unit_used_describes, message)

    try do
      fun.()
    after
      Module.put_attribute(module, :ex_unit_describe, nil)
      Module.delete_attribute(module, :describetag)

      for attribute <- Module.get_attribute(module, :ex_unit_registered_describe_attributes) do
        Module.delete_attribute(module, attribute)
      end
    end
  end

  @doc false
  defmacro __before_compile__(_) do
    quote do
      def __ex_unit__ do
        %ExUnit.TestModule{file: __ENV__.file, name: __MODULE__, tests: @ex_unit_tests}
      end
    end
  end

  @doc false
  def __after_compile__(%{module: module}, _) do
    if Module.get_attribute(module, :ex_unit_async) do
      ExUnit.Server.add_async_module(module)
    else
      ExUnit.Server.add_sync_module(module)
    end
  end

  @doc """
  Registers a function to run as part of this case.

  This is used by third-party projects, like QuickCheck, to
  implement macros like `property/3` that works like `test`
  but instead defines a property. See `test/3` implementation
  for an example of invoking this function.

  The test type will be converted to a string and pluralized for
  display. You can use `ExUnit.plural_rule/2` to set a custom
  pluralization.
  """
  def register_test(mod, file, line, test_type, name, tags) do
    unless Module.has_attribute?(mod, :ex_unit_tests) do
      raise "cannot define #{test_type}. Please make sure you have invoked " <>
              "\"use ExUnit.Case\" in the current module"
    end

    registered_attribute_keys = [
      :ex_unit_registered_module_attributes,
      :ex_unit_registered_describe_attributes,
      :ex_unit_registered_test_attributes
    ]

    registered =
      for key <- registered_attribute_keys,
          attribute <- Module.get_attribute(mod, key),
          into: %{} do
        {attribute, Module.get_attribute(mod, attribute)}
      end

    moduletag = Module.get_attribute(mod, :moduletag)
    tag = Module.delete_attribute(mod, :tag)
    async = Module.get_attribute(mod, :ex_unit_async)

    {name, describe, describe_line, describetag} =
      case Module.get_attribute(mod, :ex_unit_describe) do
        {line, describe} ->
          description = :"#{test_type} #{describe} #{name}"
          {description, describe, line, Module.get_attribute(mod, :describetag)}

        _ ->
          {:"#{test_type} #{name}", nil, nil, []}
      end

    if Module.defines?(mod, {name, 1}) do
      raise ExUnit.DuplicateTestError, ~s("#{name}" is already defined in #{inspect(mod)})
    end

    tags =
      (tags ++ tag ++ describetag ++ moduletag)
      |> normalize_tags
      |> validate_tags
      |> Map.merge(%{
        line: line,
        file: file,
        registered: registered,
        async: async,
        describe: describe,
        describe_line: describe_line,
        test_type: test_type
      })

    test = %ExUnit.Test{name: name, case: mod, tags: tags, module: mod}
    Module.put_attribute(mod, :ex_unit_tests, test)

    for attribute <- Module.get_attribute(mod, :ex_unit_registered_test_attributes) do
      Module.delete_attribute(mod, attribute)
    end

    name
  end

  @doc """
  Registers a test with the given environment.

  This function is deprecated in favor of `register_test/6` which performs
  better under tight loops by avoiding `__ENV__`.
  """
  @doc deprecated: "Use register_test/6 instead"
  def register_test(%{module: mod, file: file, line: line}, test_type, name, tags) do
    register_test(mod, file, line, test_type, name, tags)
  end

  @doc """
  Registers a new attribute to be used during `ExUnit.Case` tests.

  The attribute values will be available through `context.registered`.
  Registered values are cleared after each `test/3` similar
  to `@tag`.

  This function takes the same options as `Module.register_attribute/3`.

  ## Examples

      defmodule MyTest do
        use ExUnit.Case

        ExUnit.Case.register_attribute(__MODULE__, :fixtures, accumulate: true)

        @fixtures :user
        @fixtures {:post, insert: false}
        test "using custom attribute", context do
          assert context.registered.fixtures == [{:post, insert: false}, :user]
        end

        test "custom attributes are cleared per test", context do
          assert context.registered.fixtures == []
        end
      end

  """
  @spec register_attribute(env, atom, keyword) :: :ok
  def register_attribute(env, name, opts \\ [])
  def register_attribute(%{module: mod}, name, opts), do: register_attribute(mod, name, opts)

  def register_attribute(mod, name, opts) when is_atom(mod) and is_atom(name) and is_list(opts) do
    register_attribute(:ex_unit_registered_test_attributes, mod, name, opts)
  end

  @doc """
  Registers a new describe attribute to be used during `ExUnit.Case` tests.

  The attribute values will be available through `context.registered`.
  Registered values are cleared after each `describe/2` similar
  to `@describetag`.

  This function takes the same options as `Module.register_attribute/3`.

  ## Examples

      defmodule MyTest do
        use ExUnit.Case

        ExUnit.Case.register_describe_attribute(__MODULE__, :describe_fixtures, accumulate: true)

        describe "using custom attribute" do
          @describe_fixtures :user
          @describe_fixtures {:post, insert: false}

          test "has attribute", context do
            assert context.registered.describe_fixtures == [{:post, insert: false}, :user]
          end
        end

        describe "custom attributes are cleared per describe" do
          test "doesn't have attributes", context do
            assert context.registered.describe_fixtures == []
          end
        end
      end

  """
  @doc since: "1.10.0"
  @spec register_describe_attribute(env, atom, keyword) :: :ok
  def register_describe_attribute(env, name, opts \\ [])

  def register_describe_attribute(%{module: mod}, name, opts) do
    register_describe_attribute(mod, name, opts)
  end

  def register_describe_attribute(mod, name, opts)
      when is_atom(mod) and is_atom(name) and is_list(opts) do
    register_attribute(:ex_unit_registered_describe_attributes, mod, name, opts)
  end

  @doc """
  Registers a new module attribute to be used during `ExUnit.Case` tests.

  The attribute values will be available through `context.registered`.

  This function takes the same options as `Module.register_attribute/3`.

  ## Examples

      defmodule MyTest do
        use ExUnit.Case

        ExUnit.Case.register_module_attribute(__MODULE__, :module_fixtures, accumulate: true)

        @module_fixtures :user
        @module_fixtures {:post, insert: false}

        test "using custom attribute", context do
          assert context.registered.module_fixtures == [{:post, insert: false}, :user]
        end

        test "still using custom attribute", context do
          assert context.registered.module_fixtures == [{:post, insert: false}, :user]
        end
      end

  """
  @doc since: "1.10.0"
  @spec register_module_attribute(env, atom, keyword) :: :ok
  def register_module_attribute(env, name, opts \\ [])

  def register_module_attribute(%{module: mod}, name, opts) do
    register_module_attribute(mod, name, opts)
  end

  def register_module_attribute(mod, name, opts)
      when is_atom(mod) and is_atom(name) and is_list(opts) do
    register_attribute(:ex_unit_registered_module_attributes, mod, name, opts)
  end

  defp register_attribute(type, mod, name, opts) do
    validate_registered_attribute!(type, mod, name)
    Module.register_attribute(mod, name, opts)
    Module.put_attribute(mod, type, name)
  end

  defp validate_registered_attribute!(type, mod, name) do
    registered_attribute_keys = [
      :ex_unit_registered_module_attributes,
      :ex_unit_registered_describe_attributes,
      :ex_unit_registered_test_attributes
    ]

    for key <- registered_attribute_keys,
        type != key and name in Module.get_attribute(mod, key) do
      raise ArgumentError, "cannot register attribute #{inspect(name)} multiple times"
    end

    if Module.has_attribute?(mod, name) do
      raise "you must set @#{name} after it has been registered"
    end
  end

  defp validate_tags(tags) do
    for tag <- @reserved, Map.has_key?(tags, tag) do
      raise "cannot set tag #{inspect(tag)} because it is reserved by ExUnit"
    end

    unless is_atom(tags[:test_type]) do
      raise("value for tag \":test_type\" must be an atom")
    end

    tags
  end

  defp normalize_tags(tags) do
    Enum.reduce(Enum.reverse(tags), %{}, fn
      tag, acc when is_atom(tag) -> Map.put(acc, tag, true)
      tag, acc when is_list(tag) -> tag |> Enum.into(acc)
    end)
  end
end

defmodule ExUnit.DuplicateTestError do
  defexception [:message]
end

defmodule ExUnit.DuplicateDescribeError do
  defexception [:message]
end

defmodule ExUnit.Case do
  @moduledoc """
  Helpers for defining test cases.

  This module must be used in other modules as a way to configure
  and prepare them for testing.

  When used, it accepts the following options:

    * `:async` - configures tests in this module to run concurrently with
      tests in other modules. Tests in the same module never run concurrently.
      It should be enabled only if tests do not change any global state.
      Defaults to `false`.

  This module automatically includes all callbacks defined in
  `ExUnit.Callbacks`. See that module for more information on `setup`,
  `start_supervised`, `on_exit` and the test process life cycle.

  For grouping tests together, see `describe/2` in this module.

  ## Examples

      defmodule AssertionTest do
        # Use the module
        use ExUnit.Case, async: true

        # The "test" macro is imported by ExUnit.Case
        test "always pass" do
          assert true
        end
      end

  ## Context

  All tests receive a context as an argument. The context is particularly
  useful for sharing information between callbacks and tests:

      defmodule KVTest do
        use ExUnit.Case

        setup do
          {:ok, pid} = KV.start_link()
          {:ok, pid: pid}
        end

        test "stores key-value pairs", context do
          assert KV.put(context[:pid], :hello, :world) == :ok
          assert KV.get(context[:pid], :hello) == :world
        end
      end

  As the context is a map, it can be pattern matched on to extract
  information:

      test "stores key-value pairs", %{pid: pid} = _context do
        assert KV.put(pid, :hello, :world) == :ok
        assert KV.get(pid, :hello) == :world
      end

  ## Tags

  The context is used to pass information from the callbacks to
  the test. In order to pass information from the test to the
  callback, ExUnit provides tags.

  By tagging a test, the tag value can be accessed in the context,
  allowing the developer to customize the test. Let's see an
  example:

      defmodule FileTest do
        # Changing directory cannot be async
        use ExUnit.Case, async: false

        setup context do
          # Read the :cd tag value
          if cd = context[:cd] do
            prev_cd = File.cwd!()
            File.cd!(cd)
            on_exit(fn -> File.cd!(prev_cd) end)
          end

          :ok
        end

        @tag cd: "fixtures"
        test "reads UTF-8 fixtures" do
          File.read("README.md")
        end
      end

  In the example above, we have defined a tag called `:cd` that is
  read in the setup callback to configure the working directory the
  test is going to run on.

  Tags are also very effective when used with case templates
  (`ExUnit.CaseTemplate`) allowing callbacks in the case template
  to customize the test behaviour.

  Note a tag can be set in two different ways:

      @tag key: value
      @tag :key       # equivalent to setting @tag key: true

  If a tag is given more than once, the last value wins.

  ### Module and describe tags

  A tag can be set for all tests in a module or describe block by
  setting `@moduletag` or `@describetag` inside each context
  respectively:

      defmodule ApiTest do
        use ExUnit.Case
        @moduletag :external

        describe "makes calls to the right endpoint" do
          @describetag :endpoint

          # ...
        end
      end

  If you are setting a `@moduletag` or `@describetag` attribute, you must
  set them after your call to `use ExUnit.Case` otherwise you will see
  compilation errors.

  If the same key is set via `@tag`, the `@tag` value has higher
  precedence.

  ### Known tags

  The following tags are set automatically by ExUnit and are
  therefore reserved:

    * `:module`     - the module on which the test was defined

    * `:file`       - the file on which the test was defined

    * `:line`       - the line on which the test was defined

    * `:test`       - the test name

    * `:async`      - if the test case is in async mode

    * `:registered` - used for `ExUnit.Case.register_attribute/3` values

    * `:describe`   - the describe block the test belongs to

  The following tags customize how tests behave:

    * `:capture_log` - see the "Log Capture" section below

    * `:skip` - skips the test with the given reason

    * `:timeout` - customizes the test timeout in milliseconds (defaults to 60000).
      Accepts `:infinity` as a timeout value.

    * `:tmp_dir` - (since v1.11.0) see the "Tmp Dir" section below

  The `:test_type` tag is automatically set by ExUnit, but is **not** reserved.
  This tag is available for users to customize if they desire.

  ## Filters

  Tags can also be used to identify specific tests, which can then
  be included or excluded using filters. The most common functionality
  is to exclude some particular tests from running, which can be done
  via `ExUnit.configure/1`:

      # Exclude all external tests from running
      ExUnit.configure(exclude: [external: true])

  From now on, ExUnit will not run any test that has the `:external` option
  set to `true`. This behaviour can be reversed with the `:include` option
  which is usually passed through the command line:

      mix test --include external:true

  Run `mix help test` for more information on how to run filters via Mix.

  Another use case for tags and filters is to exclude all tests that have
  a particular tag by default, regardless of its value, and include only
  a certain subset:

      ExUnit.configure(exclude: :os, include: [os: :unix])

  A given include/exclude filter can be given more than once:

      ExUnit.configure(exclude: [os: :unix, os: :windows])

  Keep in mind that all tests are included by default, so unless they are
  excluded first, the `include` option has no effect.

  ## Log Capture

  ExUnit can optionally suppress printing of log messages that are generated
  during a test. Log messages generated while running a test are captured and
  only if the test fails are they printed to aid with debugging.

  You can opt into this behaviour for individual tests by tagging them with
  `:capture_log` or enable log capture for all tests in the ExUnit configuration:

      ExUnit.start(capture_log: true)

  This default can be overridden by `@tag capture_log: false` or
  `@moduletag capture_log: false`.

  Since `setup_all` blocks don't belong to a specific test, log messages generated
  in them (or between tests) are never captured. If you want to suppress these
  messages as well, remove the console backend globally by setting:

      config :logger, backends: []

  ## Tmp Dir

  ExUnit automatically creates a temporary directory for tests tagged with
  `:tmp_dir` and puts the path to that directory into the test context.
  The directory is removed before being created to ensure we start with a blank
  slate.

  The temporary directory path is unique (includes the test module and test name)
  and thus appropriate for running tests concurrently. You can customize the path
  further by setting the tag to a string, e.g.: `tmp_dir: "my_path"`, which would
  make the final path to be: `tmp/<module>/<test>/my_path`.

  Example:

      defmodule MyTest do
        use ExUnit.Case, async: true

        @tag :tmp_dir
        test "with tmp_dir", %{tmp_dir: tmp_dir} do
          assert tmp_dir =~ "with tmp_dir"
          assert File.dir?(tmp_dir)
        end
      end

  As with other tags, `:tmp_dir` can also be set as `@moduletag` and
  `@describetag`.
  """

  @type env :: module() | Macro.Env.t()

  @reserved [:module, :file, :line, :test, :async, :registered, :describe]

  @doc false
  defmacro __using__(opts) do
    unless Process.whereis(ExUnit.Server) do
      raise "cannot use ExUnit.Case without starting the ExUnit application, " <>
              "please call ExUnit.start() or explicitly start the :ex_unit app"
    end

    quote do
      unless ExUnit.Case.__register__(__MODULE__, unquote(opts)) do
        use ExUnit.Callbacks
      end

      import ExUnit.Callbacks
      import ExUnit.Assertions
      import ExUnit.Case, only: [describe: 2, test: 1, test: 2, test: 3]
      import ExUnit.DocTest
    end
  end

  @doc false
  def __register__(module, opts) do
    registered? = Module.has_attribute?(module, :ex_unit_tests)

    unless registered? do
      tag_check = Enum.any?([:moduletag, :describetag, :tag], &Module.has_attribute?(module, &1))

      if tag_check do
        raise "you must set @tag, @describetag, and @moduletag after the call to \"use ExUnit.Case\""
      end

      attributes = [
        :ex_unit_tests,
        :tag,
        :describetag,
        :moduletag,
        :ex_unit_registered_test_attributes,
        :ex_unit_registered_describe_attributes,
        :ex_unit_registered_module_attributes,
        :ex_unit_used_describes
      ]

      Enum.each(attributes, &Module.register_attribute(module, &1, accumulate: true))

      attributes = [
        before_compile: ExUnit.Case,
        after_compile: ExUnit.Case,
        ex_unit_async: false,
        ex_unit_describe: nil
      ]

      Enum.each(attributes, fn {k, v} -> Module.put_attribute(module, k, v) end)
    end

    async? = opts[:async]

    if is_boolean(async?) do
      Module.put_attribute(module, :ex_unit_async, async?)
    end

    registered?
  end

  @doc """
  Defines a test with `message`.

  The test may also define a pattern, which will be matched
  against the test context. For more information on contexts, see
  `ExUnit.Callbacks`.

  ## Examples

      test "true is equal to true" do
        assert true == true
      end

  """
  defmacro test(message, var \\ quote(do: _), contents) do
    unless is_tuple(var) do
      IO.warn(
        "test context is always a map. The pattern " <>
          "#{inspect(Macro.to_string(var))} will never match",
        Macro.Env.stacktrace(__CALLER__)
      )
    end

    contents =
      case contents do
        [do: block] ->
          quote do
            unquote(block)
            :ok
          end

        _ ->
          quote do
            try(unquote(contents))
            :ok
          end
      end

    var = Macro.escape(var)
    contents = Macro.escape(contents, unquote: true)
    %{module: mod, file: file, line: line} = __CALLER__

    quote bind_quoted: [
            var: var,
            contents: contents,
            message: message,
            mod: mod,
            file: file,
            line: line
          ] do
      name = ExUnit.Case.register_test(mod, file, line, :test, message, [])
      def unquote(name)(unquote(var)), do: unquote(contents)
    end
  end

  @doc """
  Defines a not implemented test with a string.

  Provides a convenient macro that allows a test to be defined
  with a string, but not yet implemented. The resulting test will
  always fail and print a "Not implemented" error message. The
  resulting test case is also tagged with `:not_implemented`.

  ## Examples

      test "this will be a test in future"

  """
  defmacro test(message) do
    %{module: mod, file: file, line: line} = __CALLER__

    quote bind_quoted: binding() do
      name = ExUnit.Case.register_test(mod, file, line, :test, message, [:not_implemented])
      def unquote(name)(_), do: flunk("Not implemented")
    end
  end

  @doc """
  Describes tests together.

  Every describe block receives a name which is used as prefix for
  upcoming tests. Inside a block, `ExUnit.Callbacks.setup/1` may be
  invoked and it will define a setup callback to run only for the
  current block. The describe name is also added as a tag, allowing
  developers to run tests for specific blocks.

  ## Examples

      defmodule StringTest do
        use ExUnit.Case, async: true

        describe "String.capitalize/1" do
          test "first grapheme is in uppercase" do
            assert String.capitalize("hello") == "Hello"
          end

          test "converts remaining graphemes to lowercase" do
            assert String.capitalize("HELLO") == "Hello"
          end
        end
      end

  When using Mix, you can run all tests in a describe block by name:

      mix test --only describe:"String.capitalize/1"

  or by passing the exact line the describe block starts on:

      mix test path/to/file:123

  Note describe blocks cannot be nested. Instead of relying on hierarchy
  for composition, developers should build on top of named setups. For
  example:

      defmodule UserManagementTest do
        use ExUnit.Case, async: true

        describe "when user is logged in and is an admin" do
          setup [:log_user_in, :set_type_to_admin]

          test ...
        end

        describe "when user is logged in and is a manager" do
          setup [:log_user_in, :set_type_to_manager]

          test ...
        end

        defp log_user_in(context) do
          # ...
        end
      end

  By forbidding hierarchies in favor of named setups, it is straightforward
  for the developer to glance at each describe block and know exactly the
  setup steps involved.
  """
  defmacro describe(message, do: block) do
    quote do
      ExUnit.Case.__describe__(__MODULE__, __ENV__.line, unquote(message), fn ->
        unquote(block)
      end)
    end
  end

  @doc false
  def __describe__(module, line, message, fun) do
    if Module.get_attribute(module, :ex_unit_describe) do
      raise "cannot call \"describe\" inside another \"describe\". See the documentation " <>
              "for ExUnit.Case.describe/2 on named setups and how to handle hierarchies"
    end

    cond do
      not is_binary(message) ->
        raise ArgumentError, "describe name must be a string, got: #{inspect(message)}"

      message in Module.get_attribute(module, :ex_unit_used_describes) ->
        raise ExUnit.DuplicateDescribeError,
              "describe #{inspect(message)} is already defined in #{inspect(module)}"

      true ->
        :ok
    end

    if Module.get_attribute(module, :describetag) != [] do
      raise "@describetag must be set inside describe/2 blocks"
    end

    Module.put_attribute(module, :ex_unit_describe, {line, message})
    Module.put_attribute(module, :ex_unit_used_describes, message)

    try do
      fun.()
    after
      Module.put_attribute(module, :ex_unit_describe, nil)
      Module.delete_attribute(module, :describetag)

      for attribute <- Module.get_attribute(module, :ex_unit_registered_describe_attributes) do
        Module.delete_attribute(module, attribute)
      end
    end
  end

  @doc false
  defmacro __before_compile__(_) do
    quote do
      def __ex_unit__ do
        %ExUnit.TestModule{file: __ENV__.file, name: __MODULE__, tests: @ex_unit_tests}
      end
    end
  end

  @doc false
  def __after_compile__(%{module: module}, _) do
    if Module.get_attribute(module, :ex_unit_async) do
      ExUnit.Server.add_async_module(module)
    else
      ExUnit.Server.add_sync_module(module)
    end
  end

  @doc """
  Registers a function to run as part of this case.

  This is used by third-party projects, like QuickCheck, to
  implement macros like `property/3` that works like `test`
  but instead defines a property. See `test/3` implementation
  for an example of invoking this function.

  The test type will be converted to a string and pluralized for
  display. You can use `ExUnit.plural_rule/2` to set a custom
  pluralization.
  """
  def register_test(mod, file, line, test_type, name, tags) do
    unless Module.has_attribute?(mod, :ex_unit_tests) do
      raise "cannot define #{test_type}. Please make sure you have invoked " <>
              "\"use ExUnit.Case\" in the current module"
    end

    registered_attribute_keys = [
      :ex_unit_registered_module_attributes,
      :ex_unit_registered_describe_attributes,
      :ex_unit_registered_test_attributes
    ]

    registered =
      for key <- registered_attribute_keys,
          attribute <- Module.get_attribute(mod, key),
          into: %{} do
        {attribute, Module.get_attribute(mod, attribute)}
      end

    moduletag = Module.get_attribute(mod, :moduletag)
    tag = Module.delete_attribute(mod, :tag)
    async = Module.get_attribute(mod, :ex_unit_async)

    {name, describe, describe_line, describetag} =
      case Module.get_attribute(mod, :ex_unit_describe) do
        {line, describe} ->
          description = :"#{test_type} #{describe} #{name}"
          {description, describe, line, Module.get_attribute(mod, :describetag)}

        _ ->
          {:"#{test_type} #{name}", nil, nil, []}
      end

    if Module.defines?(mod, {name, 1}) do
      raise ExUnit.DuplicateTestError, ~s("#{name}" is already defined in #{inspect(mod)})
    end

    tags =
      (tags ++ tag ++ describetag ++ moduletag)
      |> normalize_tags
      |> validate_tags
      |> Map.merge(%{
        line: line,
        file: file,
        registered: registered,
        async: async,
        describe: describe,
        describe_line: describe_line,
        test_type: test_type
      })

    test = %ExUnit.Test{name: name, case: mod, tags: tags, module: mod}
    Module.put_attribute(mod, :ex_unit_tests, test)

    for attribute <- Module.get_attribute(mod, :ex_unit_registered_test_attributes) do
      Module.delete_attribute(mod, attribute)
    end

    name
  end

  @doc """
  Registers a test with the given environment.

  This function is deprecated in favor of `register_test/6` which performs
  better under tight loops by avoiding `__ENV__`.
  """
  @doc deprecated: "Use register_test/6 instead"
  def register_test(%{module: mod, file: file, line: line}, test_type, name, tags) do
    register_test(mod, file, line, test_type, name, tags)
  end

  @doc """
  Registers a new attribute to be used during `ExUnit.Case` tests.

  The attribute values will be available through `context.registered`.
  Registered values are cleared after each `test/3` similar
  to `@tag`.

  This function takes the same options as `Module.register_attribute/3`.

  ## Examples

      defmodule MyTest do
        use ExUnit.Case

        ExUnit.Case.register_attribute(__MODULE__, :fixtures, accumulate: true)

        @fixtures :user
        @fixtures {:post, insert: false}
        test "using custom attribute", context do
          assert context.registered.fixtures == [{:post, insert: false}, :user]
        end

        test "custom attributes are cleared per test", context do
          assert context.registered.fixtures == []
        end
      end

  """
  @spec register_attribute(env, atom, keyword) :: :ok
  def register_attribute(env, name, opts \\ [])
  def register_attribute(%{module: mod}, name, opts), do: register_attribute(mod, name, opts)

  def register_attribute(mod, name, opts) when is_atom(mod) and is_atom(name) and is_list(opts) do
    register_attribute(:ex_unit_registered_test_attributes, mod, name, opts)
  end

  @doc """
  Registers a new describe attribute to be used during `ExUnit.Case` tests.

  The attribute values will be available through `context.registered`.
  Registered values are cleared after each `describe/2` similar
  to `@describetag`.

  This function takes the same options as `Module.register_attribute/3`.

  ## Examples

      defmodule MyTest do
        use ExUnit.Case

        ExUnit.Case.register_describe_attribute(__MODULE__, :describe_fixtures, accumulate: true)

        describe "using custom attribute" do
          @describe_fixtures :user
          @describe_fixtures {:post, insert: false}

          test "has attribute", context do
            assert context.registered.describe_fixtures == [{:post, insert: false}, :user]
          end
        end

        describe "custom attributes are cleared per describe" do
          test "doesn't have attributes", context do
            assert context.registered.describe_fixtures == []
          end
        end
      end

  """
  @doc since: "1.10.0"
  @spec register_describe_attribute(env, atom, keyword) :: :ok
  def register_describe_attribute(env, name, opts \\ [])

  def register_describe_attribute(%{module: mod}, name, opts) do
    register_describe_attribute(mod, name, opts)
  end

  def register_describe_attribute(mod, name, opts)
      when is_atom(mod) and is_atom(name) and is_list(opts) do
    register_attribute(:ex_unit_registered_describe_attributes, mod, name, opts)
  end

  @doc """
  Registers a new module attribute to be used during `ExUnit.Case` tests.

  The attribute values will be available through `context.registered`.

  This function takes the same options as `Module.register_attribute/3`.

  ## Examples

      defmodule MyTest do
        use ExUnit.Case

        ExUnit.Case.register_module_attribute(__MODULE__, :module_fixtures, accumulate: true)

        @module_fixtures :user
        @module_fixtures {:post, insert: false}

        test "using custom attribute", context do
          assert context.registered.fixtures == [{:post, insert: false}, :user]
        end

        test "still using custom attribute", context do
          assert context.registered.fixtures == [{:post, insert: false}, :user]
        end
      end

  """
  @doc since: "1.10.0"
  @spec register_module_attribute(env, atom, keyword) :: :ok
  def register_module_attribute(env, name, opts \\ [])

  def register_module_attribute(%{module: mod}, name, opts) do
    register_module_attribute(mod, name, opts)
  end

  def register_module_attribute(mod, name, opts)
      when is_atom(mod) and is_atom(name) and is_list(opts) do
    register_attribute(:ex_unit_registered_module_attributes, mod, name, opts)
  end

  defp register_attribute(type, mod, name, opts) do
    validate_registered_attribute!(type, mod, name)
    Module.register_attribute(mod, name, opts)
    Module.put_attribute(mod, type, name)
  end

  defp validate_registered_attribute!(type, mod, name) do
    registered_attribute_keys = [
      :ex_unit_registered_module_attributes,
      :ex_unit_registered_describe_attributes,
      :ex_unit_registered_test_attributes
    ]

    for key <- registered_attribute_keys,
        type != key and name in Module.get_attribute(mod, key) do
      raise ArgumentError, "cannot register attribute #{inspect(name)} multiple times"
    end

    if Module.has_attribute?(mod, name) do
      raise "you must set @#{name} after it has been registered"
    end
  end

  defp validate_tags(tags) do
    for tag <- @reserved, Map.has_key?(tags, tag) do
      raise "cannot set tag #{inspect(tag)} because it is reserved by ExUnit"
    end

    unless is_atom(tags[:test_type]) do
      raise("value for tag \":test_type\" must be an atom")
    end

    tags
  end

  defp normalize_tags(tags) do
    Enum.reduce(Enum.reverse(tags), %{}, fn
      tag, acc when is_atom(tag) -> Map.put(acc, tag, true)
      tag, acc when is_list(tag) -> tag |> Enum.into(acc)
    end)
  end
end


Run the code formatter on Mix.DepTest (#6731)

Code.require_file("../test_helper.exs", __DIR__)

defmodule Mix.DepTest do
  use MixTest.Case

  defmodule DepsApp do
    def project do
      [
        deps: [
          {:ok, "0.1.0", path: "deps/ok"},
          {:invalidvsn, "0.2.0", path: "deps/invalidvsn"},
          {:invalidapp, "0.1.0", path: "deps/invalidapp"},
          {:noappfile, "0.1.0", path: "deps/noappfile"},
          {:uncloned, git: "https://github.com/elixir-lang/uncloned.git"},
          {:optional, git: "https://github.com/elixir-lang/optional.git", optional: true}
        ]
      ]
    end
  end

  defmodule ProcessDepsApp do
    def project do
      [app: :process_deps_app, deps: Process.get(:mix_deps)]
    end
  end

  defp with_deps(deps, fun) do
    Process.put(:mix_deps, deps)
    Mix.Project.push(ProcessDepsApp)
    fun.()
  after
    Mix.Project.pop()
  end

  defp assert_wrong_dependency(deps) do
    with_deps(deps, fn ->
      assert_raise Mix.Error, ~r"Dependency specified in the wrong format", fn ->
        Mix.Dep.loaded([])
      end
    end)
  end

  test "respects the MIX_NO_DEPS flag" do
    Mix.Project.push(DepsApp)

    in_fixture "deps_status", fn ->
      deps = Mix.Dep.cached()
      assert length(deps) == 6

      System.put_env("MIX_NO_DEPS", "1")
      deps = Mix.Dep.cached()
      assert length(deps) == 0
    end
  after
    System.delete_env("MIX_NO_DEPS")
  end

  test "extracts all dependencies from the given project" do
    Mix.Project.push(DepsApp)

    in_fixture "deps_status", fn ->
      deps = Mix.Dep.loaded([])
      assert length(deps) == 6
      assert Enum.find(deps, &match?(%Mix.Dep{app: :ok, status: {:ok, _}}, &1))
      assert Enum.find(deps, &match?(%Mix.Dep{app: :invalidvsn, status: {:invalidvsn, :ok}}, &1))
      assert Enum.find(deps, &match?(%Mix.Dep{app: :invalidapp, status: {:invalidapp, _}}, &1))
      assert Enum.find(deps, &match?(%Mix.Dep{app: :noappfile, status: {:noappfile, _}}, &1))
      assert Enum.find(deps, &match?(%Mix.Dep{app: :uncloned, status: {:unavailable, _}}, &1))
      assert Enum.find(deps, &match?(%Mix.Dep{app: :optional, status: {:unavailable, _}}, &1))
    end
  end

  test "extracts all dependencies paths from the given project" do
    Mix.Project.push(DepsApp)

    in_fixture "deps_status", fn ->
      paths = Mix.Project.deps_paths()
      assert map_size(paths) == 6
      assert paths[:ok] =~ "deps/ok"
      assert paths[:uncloned] =~ "deps/uncloned"
    end
  end

  test "fails on invalid dependencies" do
    assert_wrong_dependency([{:ok}])
    assert_wrong_dependency([{:ok, nil}])
    assert_wrong_dependency([{:ok, nil, []}])
  end

  test "use requirements for dependencies" do
    with_deps([{:ok, "~> 0.1", path: "deps/ok"}], fn ->
      in_fixture "deps_status", fn ->
        deps = Mix.Dep.loaded([])
        assert Enum.find(deps, &match?(%Mix.Dep{app: :ok, status: {:ok, _}}, &1))
      end
    end)
  end

  test "raises when no SCM is specified" do
    with_deps([{:ok, "~> 0.1", not_really: :ok}], fn ->
      in_fixture "deps_status", fn ->
        send(self(), {:mix_shell_input, :yes?, false})
        msg = "Could not find an SCM for dependency :ok from Mix.DepTest.ProcessDepsApp"
        assert_raise Mix.Error, msg, fn -> Mix.Dep.loaded([]) end
      end
    end)
  end

  test "does not set the manager before the dependency was loaded" do
    # It is important to not eagerly set the manager because the dependency
    # needs to be loaded (i.e. available in the filesystem) in order to get
    # the proper manager.
    Mix.Project.push(DepsApp)

    {_, true, _} =
      Mix.Dep.Converger.converge(false, [], nil, fn dep, acc, lock ->
        assert is_nil(dep.manager)
        {dep, acc or true, lock}
      end)
  end

  test "raises on invalid deps req" do
    with_deps([{:ok, "+- 0.1.0", path: "deps/ok"}], fn ->
      in_fixture "deps_status", fn ->
        assert_raise Mix.Error, ~r"Invalid requirement", fn ->
          Mix.Dep.loaded([])
        end
      end
    end)
  end

  test "nested deps come first" do
    with_deps([{:deps_repo, "0.1.0", path: "custom/deps_repo"}], fn ->
      in_fixture "deps_status", fn ->
        assert Enum.map(Mix.Dep.loaded([]), & &1.app) == [:git_repo, :deps_repo]
      end
    end)
  end

  test "nested optional deps are never added" do
    with_deps([{:deps_repo, "0.1.0", path: "custom/deps_repo"}], fn ->
      in_fixture "deps_status", fn ->
        File.write!("custom/deps_repo/mix.exs", """
        defmodule DepsRepo do
          use Mix.Project

          def project do
            [app: :deps_repo,
             version: "0.1.0",
             deps: [{:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), optional: true}]]
          end
        end
        """)

        assert Enum.map(Mix.Dep.loaded([]), & &1.app) == [:deps_repo]
      end
    end)
  end

  test "nested deps with convergence" do
    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo"},
      {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo")}
    ]

    with_deps(deps, fn ->
      in_fixture "deps_status", fn ->
        assert Enum.map(Mix.Dep.loaded([]), & &1.app) == [:git_repo, :deps_repo]
      end
    end)
  end

  test "nested deps with convergence and managers" do
    Process.put(:custom_deps_git_repo_opts, manager: :make)

    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo", manager: :rebar},
      {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo")}
    ]

    with_deps(deps, fn ->
      in_fixture "deps_status", fn ->
        [dep1, dep2] = Mix.Dep.loaded([])
        assert dep1.manager == nil
        assert dep2.manager == :rebar
      end
    end)
  end

  test "nested deps with convergence and optional dependencies" do
    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo"},
      {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo")}
    ]

    with_deps(deps, fn ->
      in_fixture "deps_status", fn ->
        File.write!("custom/deps_repo/mix.exs", """
        defmodule DepsRepo do
          use Mix.Project

          def project do
            [app: :deps_repo,
             version: "0.1.0",
             deps: [{:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), optional: true}]]
          end
        end
        """)

        assert Enum.map(Mix.Dep.loaded([]), & &1.app) == [:git_repo, :deps_repo]
      end
    end)
  end

  test "nested deps with optional dependencies and cousin conflict" do
    with_deps(
      [
        {:deps_repo1, "0.1.0", path: "custom/deps_repo1"},
        {:deps_repo2, "0.1.0", path: "custom/deps_repo2"}
      ],
      fn ->
        in_fixture "deps_status", fn ->
          File.mkdir_p!("custom/deps_repo1")

          File.write!("custom/deps_repo1/mix.exs", """
          defmodule DepsRepo1 do
            use Mix.Project

            def project do
              [app: :deps_repo1,
               version: "0.1.0",
               deps: [{:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), optional: true}]]
            end
          end
          """)

          File.mkdir_p!("custom/deps_repo2")

          File.write!("custom/deps_repo2/mix.exs", """
          defmodule DepsRepo2 do
            use Mix.Project

            def project do
              [app: :deps_repo2,
               version: "0.1.0",
               deps: [{:git_repo, "0.2.0", path: "somewhere"}]]
            end
          end
          """)

          Mix.Tasks.Deps.run([])
          assert_received {:mix_shell, :info, ["* git_repo" <> _]}
          assert_received {:mix_shell, :info, [msg]}
          assert msg =~ "different specs were given for the git_repo"
        end
      end
    )
  end

  ## Remove converger

  defmodule IdentityRemoteConverger do
    @behaviour Mix.RemoteConverger

    def remote?(%Mix.Dep{app: :deps_repo}), do: false
    def remote?(%Mix.Dep{}), do: true
    def deps(_dep, _lock), do: []
    def post_converge, do: :ok

    def converge(deps, lock) do
      Process.put(:remote_converger, deps)
      lock
    end
  end

  test "remote converger" do
    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo"},
      {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo")}
    ]

    with_deps(deps, fn ->
      Mix.RemoteConverger.register(IdentityRemoteConverger)

      in_fixture "deps_status", fn ->
        Mix.Tasks.Deps.Get.run([])

        message = "* Getting git_repo (#{fixture_path("git_repo")})"
        assert_received {:mix_shell, :info, [^message]}

        assert Process.get(:remote_converger)
      end
    end)
  after
    Mix.RemoteConverger.register(nil)
  end

  test "pass dependencies to remote converger in defined order" do
    deps = [
      {:ok, "0.1.0", path: "deps/ok"},
      {:invalidvsn, "0.2.0", path: "deps/invalidvsn"},
      {:deps_repo, "0.1.0", path: "custom/deps_repo"},
      {:invalidapp, "0.1.0", path: "deps/invalidapp"},
      {:noappfile, "0.1.0", path: "deps/noappfile"}
    ]

    with_deps(deps, fn ->
      Mix.RemoteConverger.register(IdentityRemoteConverger)

      in_fixture "deps_status", fn ->
        Mix.Tasks.Deps.Get.run([])

        deps = Process.get(:remote_converger) |> Enum.map(& &1.app)
        assert deps == [:ok, :invalidvsn, :deps_repo, :invalidapp, :noappfile, :git_repo]
      end
    end)
  after
    Mix.RemoteConverger.register(nil)
  end

  defmodule RaiseRemoteConverger do
    @behaviour Mix.RemoteConverger

    def remote?(_app), do: false
    def deps(_dep, _lock), do: :ok
    def post_converge, do: :ok

    def converge(_deps, lock) do
      Process.put(:remote_converger, true)
      lock
    end
  end

  test "remote converger is not invoked if deps diverge" do
    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo"},
      {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: :test}
    ]

    with_deps(deps, fn ->
      Mix.RemoteConverger.register(RaiseRemoteConverger)

      in_fixture "deps_status", fn ->
        assert_raise Mix.Error, fn ->
          Mix.Tasks.Deps.Get.run([])
        end

        assert_received {:mix_shell, :error, ["Dependencies have diverged:"]}
        refute Process.get(:remote_converger)
      end
    end)
  after
    Mix.RemoteConverger.register(nil)
  end

  test "remote converger is not invoked if deps graph has cycles" do
    deps = [{:app1, "0.1.0", path: "app1"}, {:app2, "0.1.0", path: "app2"}]

    with_deps(deps, fn ->
      Mix.RemoteConverger.register(RaiseRemoteConverger)

      in_fixture "deps_cycle", fn ->
        assert_raise Mix.Error, ~r/cycles in the dependency graph/, fn ->
          Mix.Tasks.Deps.Get.run([])
        end

        refute Process.get(:remote_converger)
      end
    end)
  after
    Mix.RemoteConverger.register(nil)
  end

  ## Only handling

  test "only extracts deps matching environment" do
    with_deps(
      [{:foo, github: "elixir-lang/foo"}, {:bar, github: "elixir-lang/bar", only: :other_env}],
      fn ->
        in_fixture "deps_status", fn ->
          deps = Mix.Dep.loaded(env: :other_env)
          assert length(deps) == 2

          deps = Mix.Dep.loaded([])
          assert length(deps) == 2

          assert [dep] = Mix.Dep.loaded(env: :prod)
          assert dep.app == :foo
        end
      end
    )
  end

  test "only fetches parent deps matching specified env" do
    with_deps([{:only, github: "elixir-lang/only", only: [:dev]}], fn ->
      in_fixture "deps_status", fn ->
        Mix.Tasks.Deps.Get.run(["--only", "prod"])
        refute_received {:mix_shell, :info, ["* Getting" <> _]}

        assert_raise Mix.Error, "Can't continue due to errors on dependencies", fn ->
          Mix.Tasks.Deps.Loadpaths.run([])
        end

        Mix.ProjectStack.clear_cache()
        Mix.env(:prod)
        Mix.Tasks.Deps.Loadpaths.run([])
      end
    end)
  end

  test "nested deps selects only prod dependencies" do
    Process.put(:custom_deps_git_repo_opts, only: :test)
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo"}]

    with_deps(deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:deps_repo] = Enum.map(loaded, & &1.app)

        loaded = Mix.Dep.loaded(env: :test)
        assert [:deps_repo] = Enum.map(loaded, & &1.app)
      end
    end)
  end

  test "nested deps on only matching" do
    # deps_repo wants git_repo for test, git_repo is restricted to only test
    # We assert the dependencies match as expected, happens in umbrella apps
    Process.put(:custom_deps_git_repo_opts, only: :test)

    # We need to pass env: :test so the child dependency is loaded
    # in the first place (otherwise only :prod deps are loaded)
    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo", env: :test},
      {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: :test}
    ]

    with_deps(deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :dev)
        assert [:deps_repo] = Enum.map(loaded, & &1.app)
        assert [noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :test)
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, & &1.status)
      end
    end)
  end

  test "nested deps on only conflict" do
    # deps_repo wants all git_repo, git_repo is restricted to only test
    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo"},
      {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: :test}
    ]

    with_deps(deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [divergedonly: _, noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :dev)
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [divergedonly: _, noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :test)
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [divergedonly: _, noappfile: _] = Enum.map(loaded, & &1.status)

        Mix.Tasks.Deps.run([])
        assert_received {:mix_shell, :info, ["* git_repo" <> _]}
        assert_received {:mix_shell, :info, [msg]}
        assert msg =~ "Remove the :only restriction from your dep"
      end
    end)
  end

  test "nested deps on only conflict does not happen with optional deps" do
    Process.put(:custom_deps_git_repo_opts, optional: true)

    # deps_repo wants all git_repo, git_repo is restricted to only test
    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo"},
      {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: :test}
    ]

    with_deps(deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :dev)
        assert [:deps_repo] = Enum.map(loaded, & &1.app)
        assert [noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :test)
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, & &1.status)
      end
    end)
  end

  test "nested deps with valid only subset" do
    # deps_repo wants git_repo for prod, git_repo is restricted to only prod and test
    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo", only: :prod},
      {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: [:prod, :test]}
    ]

    with_deps(deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :dev)
        assert [] = Enum.map(loaded, & &1.app)

        loaded = Mix.Dep.loaded(env: :test)
        assert [:git_repo] = Enum.map(loaded, & &1.app)
        assert [unavailable: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :prod)
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, & &1.status)
      end
    end)
  end

  test "nested deps with invalid only subset" do
    # deps_repo wants git_repo for dev, git_repo is restricted to only test
    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo", only: :dev},
      {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: [:test]}
    ]

    with_deps(deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [divergedonly: _, noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :dev)
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [divergedonly: _, noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :test)
        assert [:git_repo] = Enum.map(loaded, & &1.app)
        assert [unavailable: _] = Enum.map(loaded, & &1.status)

        Mix.Tasks.Deps.run([])
        assert_received {:mix_shell, :info, ["* git_repo" <> _]}
        assert_received {:mix_shell, :info, [msg]}
        assert msg =~ "Ensure you specify at least the same environments in :only in your dep"
      end
    end)
  end

  test "nested deps with valid only in both parent and child" do
    Process.put(:custom_deps_git_repo_opts, only: :test)

    # deps_repo has environment set to test so it loads the deps_git_repo set to test too
    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo", env: :test, only: [:dev, :test]},
      {:git_repo, "0.1.0", git: MixTest.Case.fixture_path("git_repo"), only: :test}
    ]

    with_deps(deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :dev)
        assert [:deps_repo] = Enum.map(loaded, & &1.app)
        assert [noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :test)
        assert [:git_repo, :deps_repo] = Enum.map(loaded, & &1.app)
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, & &1.status)

        loaded = Mix.Dep.loaded(env: :prod)
        assert [] = Enum.map(loaded, & &1.app)
      end
    end)
  end

  test "nested deps converge and diverge when only is not in_upper" do
    loaded_only = fn deps ->
      with_deps(deps, fn ->
        in_fixture "deps_status", fn ->
          File.mkdir_p!("custom/other_repo")

          File.write!("custom/other_repo/mix.exs", """
          defmodule OtherRepo do
            use Mix.Project

            def project do
              [app: :deps_repo,
               version: "0.1.0",
               deps: [{:git_repo, "0.1.0", git: MixTest.Case.fixture_path("git_repo")}]]
            end
          end
          """)

          Mix.ProjectStack.clear_cache()
          loaded = Mix.Dep.loaded([])
          assert [:git_repo, _, _] = Enum.map(loaded, & &1.app)
          hd(loaded).opts[:only]
        end
      end)
    end

    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo", only: :prod},
      {:other_repo, "0.1.0", path: "custom/other_repo", only: :test}
    ]

    assert loaded_only.(deps) == [:test, :prod]

    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo"},
      {:other_repo, "0.1.0", path: "custom/other_repo", only: :test}
    ]

    refute loaded_only.(deps)

    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo", only: :prod},
      {:other_repo, "0.1.0", path: "custom/other_repo"}
    ]

    refute loaded_only.(deps)

    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo"},
      {:other_repo, "0.1.0", path: "custom/other_repo"}
    ]

    refute loaded_only.(deps)

    Process.put(:custom_deps_git_repo_opts, optional: true)

    deps = [
      {:deps_repo, "0.1.0", path: "custom/deps_repo", only: :prod},
      {:other_repo, "0.1.0", path: "custom/other_repo", only: :test}
    ]

    assert loaded_only.(deps) == :test
  end
end

Code.require_file "../test_helper.exs", __DIR__

defmodule Mix.DepTest do
  use MixTest.Case

  defmodule DepsApp do
    def project do
      [deps: [
         {:ok,         "0.1.0", path: "deps/ok"},
         {:invalidvsn, "0.2.0", path: "deps/invalidvsn"},
         {:invalidapp, "0.1.0", path: "deps/invalidapp"},
         {:noappfile,  "0.1.0", path: "deps/noappfile"},
         {:uncloned,            git: "https://github.com/elixir-lang/uncloned.git"},
         {:optional,            git: "https://github.com/elixir-lang/optional.git", optional: true}
       ]]
    end
  end

  defmodule ProcessDepsApp do
    def project do
      [app: :process_deps_app, deps: Process.get(:mix_deps)]
    end
  end

  defp with_deps(deps, fun) do
    Process.put(:mix_deps, deps)
    Mix.Project.push ProcessDepsApp
    fun.()
  after
    Mix.Project.pop
  end

  defp assert_wrong_dependency(deps) do
    with_deps deps, fn ->
      assert_raise Mix.Error, ~r"Dependency specified in the wrong format", fn ->
        Mix.Dep.loaded([])
      end
    end
  end

  test "respects the MIX_NO_DEPS flag" do
    Mix.Project.push DepsApp

    in_fixture "deps_status", fn ->
      deps = Mix.Dep.cached()
      assert length(deps) == 6

      System.put_env("MIX_NO_DEPS", "1")
      deps = Mix.Dep.cached()
      assert length(deps) == 0
    end
  after
    System.delete_env("MIX_NO_DEPS")
  end

  test "extracts all dependencies from the given project" do
    Mix.Project.push DepsApp

    in_fixture "deps_status", fn ->
      deps = Mix.Dep.loaded([])
      assert length(deps) == 6
      assert Enum.find deps, &match?(%Mix.Dep{app: :ok, status: {:ok, _}}, &1)
      assert Enum.find deps, &match?(%Mix.Dep{app: :invalidvsn, status: {:invalidvsn, :ok}}, &1)
      assert Enum.find deps, &match?(%Mix.Dep{app: :invalidapp, status: {:invalidapp, _}}, &1)
      assert Enum.find deps, &match?(%Mix.Dep{app: :noappfile, status: {:noappfile, _}}, &1)
      assert Enum.find deps, &match?(%Mix.Dep{app: :uncloned, status: {:unavailable, _}}, &1)
      assert Enum.find deps, &match?(%Mix.Dep{app: :optional, status: {:unavailable, _}}, &1)
    end
  end

  test "extracts all dependencies paths from the given project" do
    Mix.Project.push DepsApp

    in_fixture "deps_status", fn ->
      paths = Mix.Project.deps_paths
      assert map_size(paths) == 6
      assert paths[:ok] =~ "deps/ok"
      assert paths[:uncloned] =~ "deps/uncloned"
    end
  end

  test "fails on invalid dependencies" do
    assert_wrong_dependency [{:ok}]
    assert_wrong_dependency [{:ok, nil}]
    assert_wrong_dependency [{:ok, nil, []}]
  end

  test "use requirements for dependencies" do
    with_deps [{:ok, "~> 0.1", path: "deps/ok"}], fn ->
      in_fixture "deps_status", fn ->
        deps = Mix.Dep.loaded([])
        assert Enum.find deps, &match?(%Mix.Dep{app: :ok, status: {:ok, _}}, &1)
      end
    end
  end

  test "raises when no SCM is specified" do
    with_deps [{:ok, "~> 0.1", not_really: :ok}], fn ->
      in_fixture "deps_status", fn ->
        send self(), {:mix_shell_input, :yes?, false}
        msg = "Could not find an SCM for dependency :ok from Mix.DepTest.ProcessDepsApp"
        assert_raise Mix.Error, msg, fn -> Mix.Dep.loaded([]) end
      end
    end
  end

  test "does not set the manager before the dependency was loaded" do
    # It is important to not eagerly set the manager because the dependency
    # needs to be loaded (i.e. available in the filesystem) in order to get
    # the proper manager.
    Mix.Project.push DepsApp

    {_, true, _} =
      Mix.Dep.Converger.converge(false, [], nil, fn dep, acc, lock ->
        assert is_nil(dep.manager)
        {dep, acc or true, lock}
      end)
  end

  test "raises on invalid deps req" do
    with_deps [{:ok, "+- 0.1.0", path: "deps/ok"}], fn ->
      in_fixture "deps_status", fn ->
        assert_raise Mix.Error, ~r"Invalid requirement", fn ->
          Mix.Dep.loaded([])
        end
      end
    end
  end

  test "nested deps come first" do
    with_deps [{:deps_repo, "0.1.0", path: "custom/deps_repo"}], fn ->
      in_fixture "deps_status", fn ->
        assert Enum.map(Mix.Dep.loaded([]), &(&1.app)) == [:git_repo, :deps_repo]
      end
    end
  end

  test "nested optional deps are never added" do
    with_deps [{:deps_repo, "0.1.0", path: "custom/deps_repo"}], fn ->
      in_fixture "deps_status", fn ->
        File.write! "custom/deps_repo/mix.exs", """
        defmodule DepsRepo do
          use Mix.Project

          def project do
            [app: :deps_repo,
             version: "0.1.0",
             deps: [{:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), optional: true}]]
          end
        end
        """

        assert Enum.map(Mix.Dep.loaded([]), &(&1.app)) == [:deps_repo]
      end
    end
  end

  test "nested deps with convergence" do
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo"},
            {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo")}]

    with_deps deps, fn ->
      in_fixture "deps_status", fn ->
        assert Enum.map(Mix.Dep.loaded([]), &(&1.app)) == [:git_repo, :deps_repo]
      end
    end
  end

  test "nested deps with convergence and managers" do
    Process.put(:custom_deps_git_repo_opts, [manager: :make])

    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo", manager: :rebar},
            {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo")}]

    with_deps deps, fn ->
      in_fixture "deps_status", fn ->
        [dep1, dep2] = Mix.Dep.loaded([])
        assert dep1.manager == nil
        assert dep2.manager == :rebar
      end
    end
  end

  test "nested deps with convergence and optional dependencies" do
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo"},
            {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo")}]

    with_deps deps, fn ->
      in_fixture "deps_status", fn ->
        File.write! "custom/deps_repo/mix.exs", """
        defmodule DepsRepo do
          use Mix.Project

          def project do
            [app: :deps_repo,
             version: "0.1.0",
             deps: [{:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), optional: true}]]
          end
        end
        """

        assert Enum.map(Mix.Dep.loaded([]), &(&1.app)) == [:git_repo, :deps_repo]
      end
    end
  end

  test "nested deps with optional dependencies and cousin conflict" do
    with_deps [{:deps_repo1, "0.1.0", path: "custom/deps_repo1"},
               {:deps_repo2, "0.1.0", path: "custom/deps_repo2"}], fn ->
      in_fixture "deps_status", fn ->
        File.mkdir_p!("custom/deps_repo1")
        File.write! "custom/deps_repo1/mix.exs", """
        defmodule DepsRepo1 do
          use Mix.Project

          def project do
            [app: :deps_repo1,
             version: "0.1.0",
             deps: [{:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), optional: true}]]
          end
        end
        """

        File.mkdir_p!("custom/deps_repo2")
        File.write! "custom/deps_repo2/mix.exs", """
        defmodule DepsRepo2 do
          use Mix.Project

          def project do
            [app: :deps_repo2,
             version: "0.1.0",
             deps: [{:git_repo, "0.2.0", path: "somewhere"}]]
          end
        end
        """

        Mix.Tasks.Deps.run([])
        assert_received {:mix_shell, :info, ["* git_repo" <> _]}
        assert_received {:mix_shell, :info, [msg]}
        assert msg =~ "different specs were given for the git_repo"
      end
    end
  end

  ## Remove converger

  defmodule IdentityRemoteConverger do
    @behaviour Mix.RemoteConverger

    def remote?(%Mix.Dep{app: :deps_repo}), do: false
    def remote?(%Mix.Dep{}), do: true
    def deps(_dep, _lock), do: []
    def post_converge, do: :ok

    def converge(deps, lock) do
      Process.put(:remote_converger, deps)
      lock
    end

  end

  test "remote converger" do
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo"},
            {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo")}]

    with_deps deps, fn ->
      Mix.RemoteConverger.register(IdentityRemoteConverger)

      in_fixture "deps_status", fn ->
        Mix.Tasks.Deps.Get.run([])

        message = "* Getting git_repo (#{fixture_path("git_repo")})"
        assert_received {:mix_shell, :info, [^message]}

        assert Process.get(:remote_converger)
      end
    end
  after
    Mix.RemoteConverger.register(nil)
  end

  test "pass dependencies to remote converger in defined order" do
    deps = [
      {:ok, "0.1.0", path: "deps/ok"},
      {:invalidvsn, "0.2.0", path: "deps/invalidvsn"},
      {:deps_repo, "0.1.0", path: "custom/deps_repo"},
      {:invalidapp, "0.1.0", path: "deps/invalidapp"},
      {:noappfile, "0.1.0", path: "deps/noappfile"}
    ]

    with_deps deps, fn ->
      Mix.RemoteConverger.register(IdentityRemoteConverger)

      in_fixture "deps_status", fn ->
        Mix.Tasks.Deps.Get.run([])

        deps = Process.get(:remote_converger) |> Enum.map(& &1.app)
        assert deps == [:ok, :invalidvsn, :deps_repo, :invalidapp, :noappfile, :git_repo]
      end
    end
  after
    Mix.RemoteConverger.register(nil)
  end

  defmodule RaiseRemoteConverger do
    @behaviour Mix.RemoteConverger

    def remote?(_app), do: false
    def deps(_dep, _lock), do: :ok
    def post_converge, do: :ok

    def converge(_deps, lock) do
      Process.put(:remote_converger, true)
      lock
    end
  end

  test "remote converger is not invoked if deps diverge" do
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo"},
            {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: :test}]

    with_deps deps, fn ->
      Mix.RemoteConverger.register(RaiseRemoteConverger)

      in_fixture "deps_status", fn ->
        assert_raise Mix.Error, fn ->
          Mix.Tasks.Deps.Get.run([])
        end

        assert_received {:mix_shell, :error, ["Dependencies have diverged:"]}
        refute Process.get(:remote_converger)
      end
    end
  after
    Mix.RemoteConverger.register(nil)
  end

  test "remote converger is not invoked if deps graph has cycles" do
    deps = [{:app1, "0.1.0", path: "app1"},
            {:app2, "0.1.0", path: "app2"}]

    with_deps deps, fn ->
      Mix.RemoteConverger.register(RaiseRemoteConverger)

      in_fixture "deps_cycle", fn ->
        assert_raise Mix.Error, ~r/cycles in the dependency graph/, fn ->
          Mix.Tasks.Deps.Get.run([])
        end

        refute Process.get(:remote_converger)
      end
    end
  after
    Mix.RemoteConverger.register(nil)
  end

  ## Only handling

  test "only extracts deps matching environment" do
    with_deps [{:foo, github: "elixir-lang/foo"},
               {:bar, github: "elixir-lang/bar", only: :other_env}], fn ->
      in_fixture "deps_status", fn ->
        deps = Mix.Dep.loaded([env: :other_env])
        assert length(deps) == 2

        deps = Mix.Dep.loaded([])
        assert length(deps) == 2

        assert [dep] = Mix.Dep.loaded([env: :prod])
        assert dep.app == :foo
      end
    end
  end

  test "only fetches parent deps matching specified env" do
    with_deps [{:only, github: "elixir-lang/only", only: [:dev]}], fn ->
      in_fixture "deps_status", fn ->
        Mix.Tasks.Deps.Get.run(["--only", "prod"])
        refute_received {:mix_shell, :info, ["* Getting" <> _]}

        assert_raise Mix.Error, "Can't continue due to errors on dependencies", fn ->
          Mix.Tasks.Deps.Loadpaths.run([])
        end

        Mix.ProjectStack.clear_cache()
        Mix.env(:prod)
        Mix.Tasks.Deps.Loadpaths.run([])
      end
    end
  end

  test "nested deps selects only prod dependencies" do
    Process.put(:custom_deps_git_repo_opts, [only: :test])
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo"}]

    with_deps deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:deps_repo] = Enum.map(loaded, &(&1.app))

        loaded = Mix.Dep.loaded([env: :test])
        assert [:deps_repo] = Enum.map(loaded, &(&1.app))
      end
    end
  end

  test "nested deps on only matching" do
    # deps_repo wants git_repo for test, git_repo is restricted to only test
    # We assert the dependencies match as expected, happens in umbrella apps
    Process.put(:custom_deps_git_repo_opts, [only: :test])

    # We need to pass env: :test so the child dependency is loaded
    # in the first place (otherwise only :prod deps are loaded)
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo", env: :test},
            {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: :test}]

    with_deps deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :dev])
        assert [:deps_repo] = Enum.map(loaded, &(&1.app))
        assert [noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :test])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, &(&1.status))
      end
    end
  end

  test "nested deps on only conflict" do
    # deps_repo wants all git_repo, git_repo is restricted to only test
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo"},
            {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: :test}]

    with_deps deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [divergedonly: _, noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :dev])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [divergedonly: _, noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :test])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [divergedonly: _, noappfile: _] = Enum.map(loaded, &(&1.status))

        Mix.Tasks.Deps.run([])
        assert_received {:mix_shell, :info, ["* git_repo" <> _]}
        assert_received {:mix_shell, :info, [msg]}
        assert msg =~ "Remove the :only restriction from your dep"
      end
    end
  end

  test "nested deps on only conflict does not happen with optional deps" do
    Process.put(:custom_deps_git_repo_opts, [optional: true])

    # deps_repo wants all git_repo, git_repo is restricted to only test
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo"},
            {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: :test}]

    with_deps deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :dev])
        assert [:deps_repo] = Enum.map(loaded, &(&1.app))
        assert [noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :test])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, &(&1.status))
      end
    end
  end

  test "nested deps with valid only subset" do
    # deps_repo wants git_repo for prod, git_repo is restricted to only prod and test
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo", only: :prod},
            {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: [:prod, :test]}]

    with_deps deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :dev])
        assert [] = Enum.map(loaded, &(&1.app))

        loaded = Mix.Dep.loaded([env: :test])
        assert [:git_repo] = Enum.map(loaded, &(&1.app))
        assert [unavailable: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :prod])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, &(&1.status))
      end
    end
  end

  test "nested deps with invalid only subset" do
    # deps_repo wants git_repo for dev, git_repo is restricted to only test
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo", only: :dev},
            {:git_repo, "0.2.0", git: MixTest.Case.fixture_path("git_repo"), only: [:test]}]

    with_deps deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [divergedonly: _, noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :dev])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [divergedonly: _, noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :test])
        assert [:git_repo] = Enum.map(loaded, &(&1.app))
        assert [unavailable: _] = Enum.map(loaded, &(&1.status))

        Mix.Tasks.Deps.run([])
        assert_received {:mix_shell, :info, ["* git_repo" <> _]}
        assert_received {:mix_shell, :info, [msg]}
        assert msg =~ "Ensure you specify at least the same environments in :only in your dep"
      end
    end
  end

  test "nested deps with valid only in both parent and child" do
    Process.put(:custom_deps_git_repo_opts, [only: :test])

    # deps_repo has environment set to test so it loads the deps_git_repo set to test too
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo", env: :test, only: [:dev, :test]},
            {:git_repo, "0.1.0", git: MixTest.Case.fixture_path("git_repo"), only: :test}]

    with_deps deps, fn ->
      in_fixture "deps_status", fn ->
        loaded = Mix.Dep.loaded([])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :dev])
        assert [:deps_repo] = Enum.map(loaded, &(&1.app))
        assert [noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :test])
        assert [:git_repo, :deps_repo] = Enum.map(loaded, &(&1.app))
        assert [unavailable: _, noappfile: _] = Enum.map(loaded, &(&1.status))

        loaded = Mix.Dep.loaded([env: :prod])
        assert [] = Enum.map(loaded, &(&1.app))
      end
    end
  end

  test "nested deps converge and diverge when only is not in_upper" do
    loaded_only = fn deps ->
      with_deps deps, fn ->
        in_fixture "deps_status", fn ->
          File.mkdir_p! "custom/other_repo"
          File.write! "custom/other_repo/mix.exs", """
          defmodule OtherRepo do
            use Mix.Project

            def project do
              [app: :deps_repo,
               version: "0.1.0",
               deps: [{:git_repo, "0.1.0", git: MixTest.Case.fixture_path("git_repo")}]]
            end
          end
          """

          Mix.ProjectStack.clear_cache
          loaded = Mix.Dep.loaded([])
          assert [:git_repo, _, _] = Enum.map(loaded, &(&1.app))
          hd(loaded).opts[:only]
        end
      end
    end

    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo", only: :prod},
            {:other_repo, "0.1.0", path: "custom/other_repo", only: :test}]
    assert loaded_only.(deps) == [:test, :prod]

    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo"},
            {:other_repo, "0.1.0", path: "custom/other_repo", only: :test}]
    refute loaded_only.(deps)

    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo", only: :prod},
            {:other_repo, "0.1.0", path: "custom/other_repo"}]
    refute loaded_only.(deps)

    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo"},
            {:other_repo, "0.1.0", path: "custom/other_repo"}]
    refute loaded_only.(deps)

    Process.put(:custom_deps_git_repo_opts, [optional: true])
    deps = [{:deps_repo, "0.1.0", path: "custom/deps_repo", only: :prod},
            {:other_repo, "0.1.0", path: "custom/other_repo", only: :test}]
    assert loaded_only.(deps) == :test
  end
end


Fix #295 - Missing function clause when rejecting low priority checks

defmodule Credo.Check.Runner do
  alias Credo.CLI.Output.UI
  alias Credo.Config
  alias Credo.SourceFile
  alias Credo.Service.SourceFileIssues

  @doc false
  def run(source_files, config) when is_list(source_files) do
    {_time_run_on_all, source_files_after_run_on_all} =
      :timer.tc fn ->
        run_checks_that_run_on_all(source_files, config)
      end

    {_time_run, source_files} =
      :timer.tc fn ->
        source_files_after_run_on_all
        |> Enum.map(&Task.async(fn -> run(&1, config) end))
        |> Enum.map(&Task.await(&1, :infinity))
      end

    {source_files, config}
  end
  def run(%SourceFile{} = source_file, config) do
    checks =
      config
      |> Config.checks
      |> Enum.reject(&run_on_all_check?/1)

    issues = run_checks(source_file, checks, config)

    %SourceFile{source_file | issues: source_file.issues ++ issues}
  end

  @doc """
  Prepares the Config struct based on a given list of `source_files`.
  """
  def prepare_config(source_files, config) do
    config
    |> set_lint_attributes(source_files)
    |> exclude_low_priority_checks(config.min_priority - 9)
    |> exclude_checks_based_on_elixir_version
  end

  defp set_lint_attributes(config, source_files) do
    lint_attribute_map =
      source_files
      |> run_linter_attribute_reader(config)
      |> Enum.reduce(%{}, fn(source_file, memo) ->
          # TODO: we should modify the config "directly" instead of going
          # through the SourceFile
          Map.put(memo, source_file.filename, source_file.lint_attributes)
        end)

    %Config{config | lint_attribute_map: lint_attribute_map}
  end

  defp run_linter_attribute_reader(source_files, config) do
    checks = [{Credo.Check.FindLintAttributes}]

    Enum.reduce(checks, source_files, fn(check_tuple, source_files) ->
      run_check(check_tuple, source_files, config)
    end)
  end

  defp exclude_low_priority_checks(config, below_priority) do
    checks =
      Enum.reject(config.checks, fn
        ({check}) -> check.base_priority < below_priority
        ({check, false}) -> true
        ({check, opts}) ->
          (opts[:priority] || check.base_priority) < below_priority
      end)

    %Config{config | checks: checks}
  end

  defp exclude_checks_based_on_elixir_version(config) do
    version = System.version()
    skipped_checks = Enum.reject(config.checks, &matches_requirement?(&1, version))
    checks = Enum.filter(config.checks, &matches_requirement?(&1, version))

    %Config{config | checks: checks, skipped_checks: skipped_checks}
  end

  defp matches_requirement?({check, _}, version) do
    matches_requirement?({check}, version)
  end
  defp matches_requirement?({check}, version) do
    Version.match?(version, check.elixir_version)
  end

  defp run_checks_that_run_on_all(source_files, config) do
    checks =
      config
      |> Config.checks
      |> Enum.filter(&run_on_all_check?/1)

    checks
    |> Enum.map(&Task.async(fn ->
        run_check(&1, source_files, config)
      end))
    |> Enum.each(&Task.await(&1, :infinity))

    SourceFileIssues.update_in_source_files(source_files)
  end

  defp run_checks(%SourceFile{} = source_file, checks, config) when is_list(checks) do
    Enum.flat_map(checks, &run_check(&1, source_file, config))
  end

  defp run_check({_check, false}, source_files, _config) when is_list(source_files) do
    source_files
  end
  defp run_check({_check, false}, _source_file, _config) do
    []
  end
  defp run_check({check}, source_file, config) do
    run_check({check, []}, source_file, config)
  end
  defp run_check({check, params}, source_file, config) do
    try do
      check.run(source_file, params)
    rescue
      error ->
        warn_about_failed_run(check, source_file)
        if config.crash_on_error do
          reraise error, System.stacktrace()
        else
          []
        end
    end
  end

  defp warn_about_failed_run(check, %SourceFile{} = source_file) do
    UI.warn("Error while running #{check} on #{source_file.filename}")
  end
  defp warn_about_failed_run(check, _) do
    UI.warn("Error while running #{check}")
  end

  defp run_on_all_check?({check}), do: check.run_on_all?
  defp run_on_all_check?({check, _params}), do: check.run_on_all?
end

defmodule Credo.Check.Runner do
  alias Credo.CLI.Output.UI
  alias Credo.Config
  alias Credo.SourceFile
  alias Credo.Service.SourceFileIssues

  @doc false
  def run(source_files, config) when is_list(source_files) do
    {_time_run_on_all, source_files_after_run_on_all} =
      :timer.tc fn ->
        run_checks_that_run_on_all(source_files, config)
      end

    {_time_run, source_files} =
      :timer.tc fn ->
        source_files_after_run_on_all
        |> Enum.map(&Task.async(fn -> run(&1, config) end))
        |> Enum.map(&Task.await(&1, :infinity))
      end

    {source_files, config}
  end
  def run(%SourceFile{} = source_file, config) do
    checks =
      config
      |> Config.checks
      |> Enum.reject(&run_on_all_check?/1)

    issues = run_checks(source_file, checks, config)

    %SourceFile{source_file | issues: source_file.issues ++ issues}
  end

  @doc """
  Prepares the Config struct based on a given list of `source_files`.
  """
  def prepare_config(source_files, config) do
    config
    |> set_lint_attributes(source_files)
    |> exclude_low_priority_checks(config.min_priority - 9)
    |> exclude_checks_based_on_elixir_version
  end

  defp set_lint_attributes(config, source_files) do
    lint_attribute_map =
      source_files
      |> run_linter_attribute_reader(config)
      |> Enum.reduce(%{}, fn(source_file, memo) ->
          # TODO: we should modify the config "directly" instead of going
          # through the SourceFile
          Map.put(memo, source_file.filename, source_file.lint_attributes)
        end)

    %Config{config | lint_attribute_map: lint_attribute_map}
  end

  defp run_linter_attribute_reader(source_files, config) do
    checks = [{Credo.Check.FindLintAttributes}]

    Enum.reduce(checks, source_files, fn(check_tuple, source_files) ->
      run_check(check_tuple, source_files, config)
    end)
  end

  defp exclude_low_priority_checks(config, below_priority) do
    checks =
      Enum.reject(config.checks, fn
        ({check}) -> check.base_priority < below_priority
        ({check, opts}) ->
          (opts[:priority] || check.base_priority) < below_priority
      end)

    %Config{config | checks: checks}
  end

  defp exclude_checks_based_on_elixir_version(config) do
    version = System.version()
    skipped_checks = Enum.reject(config.checks, &matches_requirement?(&1, version))
    checks = Enum.filter(config.checks, &matches_requirement?(&1, version))

    %Config{config | checks: checks, skipped_checks: skipped_checks}
  end

  defp matches_requirement?({check, _}, version) do
    matches_requirement?({check}, version)
  end
  defp matches_requirement?({check}, version) do
    Version.match?(version, check.elixir_version)
  end

  defp run_checks_that_run_on_all(source_files, config) do
    checks =
      config
      |> Config.checks
      |> Enum.filter(&run_on_all_check?/1)

    checks
    |> Enum.map(&Task.async(fn ->
        run_check(&1, source_files, config)
      end))
    |> Enum.each(&Task.await(&1, :infinity))

    SourceFileIssues.update_in_source_files(source_files)
  end

  defp run_checks(%SourceFile{} = source_file, checks, config) when is_list(checks) do
    Enum.flat_map(checks, &run_check(&1, source_file, config))
  end

  defp run_check({_check, false}, source_files, _config) when is_list(source_files) do
    source_files
  end
  defp run_check({_check, false}, _source_file, _config) do
    []
  end
  defp run_check({check}, source_file, config) do
    run_check({check, []}, source_file, config)
  end
  defp run_check({check, params}, source_file, config) do
    try do
      check.run(source_file, params)
    rescue
      error ->
        warn_about_failed_run(check, source_file)
        if config.crash_on_error do
          reraise error, System.stacktrace()
        else
          []
        end
    end
  end

  defp warn_about_failed_run(check, %SourceFile{} = source_file) do
    UI.warn("Error while running #{check} on #{source_file.filename}")
  end
  defp warn_about_failed_run(check, _) do
    UI.warn("Error while running #{check}")
  end

  defp run_on_all_check?({check}), do: check.run_on_all?
  defp run_on_all_check?({check, _params}), do: check.run_on_all?
end