1Code.require_file("../test_helper.exs", __DIR__)
2
3defmodule Mix.AliasesTest do
4  use MixTest.Case
5
6  defmodule Aliases do
7    def project do
8      [
9        aliases: [
10          h: "hello",
11          p: &inspect/1,
12          compile: "hello",
13          cmd: &call_cmd/1,
14          help: ["help", "hello"],
15          "nested.h": [&Mix.shell().info(inspect(&1)), "h foo bar"]
16        ]
17      ]
18    end
19
20    defp call_cmd(args), do: Mix.Task.run("cmd", args)
21  end
22
23  setup do
24    Mix.Project.push(Aliases)
25    :ok
26  end
27
28  test "runs string aliases" do
29    assert Mix.Task.run("h", []) == "Hello, World!"
30    assert Mix.Task.run("h", []) == :noop
31    assert Mix.Task.run("hello", []) == :noop
32
33    Mix.Task.reenable("h")
34    Mix.Task.reenable("hello")
35    assert Mix.Task.run("h", ["foo", "bar"]) == "Hello, foo bar!"
36  end
37
38  test "runs function aliases" do
39    assert Mix.Task.run("p", []) == "[]"
40    assert Mix.Task.run("p", []) == :noop
41
42    Mix.Task.reenable("p")
43    assert Mix.Task.run("p", ["foo", "bar"]) == "[\"foo\", \"bar\"]"
44  end
45
46  test "runs list aliases" do
47    assert Mix.Task.run("nested.h", ["baz"]) == "Hello, foo bar baz!"
48    assert_received {:mix_shell, :info, ["[]"]}
49  end
50
51  test "run alias override" do
52    assert Mix.Task.run("compile", []) == "Hello, World!"
53    assert Mix.Task.run("compile", []) == :noop
54  end
55
56  test "run alias override with name-recursion" do
57    assert Mix.Task.rerun("help", []) == "Hello, World!"
58    assert_received {:mix_shell, :info, ["mix test" <> _]}
59
60    # Arguments are passed to the recursive task and not the last one.
61    assert ExUnit.CaptureIO.capture_io(fn ->
62             Mix.Task.rerun("help", ["test"])
63           end) =~ "mix test"
64  end
65
66  test "run alias override with code-recursion" do
67    assert Mix.Task.rerun("cmd", ["echo", "hello"]) == :ok
68    assert_received {:mix_shell, :run, ["hello" <> _]}
69  end
70end
71