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 invalid_alias: [:not_a_string_or_function] 17 ] 18 ] 19 end 20 21 defp call_cmd(args), do: Mix.Task.run("cmd", args) 22 end 23 24 setup do 25 Mix.Project.push(Aliases) 26 :ok 27 end 28 29 test "runs string aliases" do 30 assert Mix.Task.run("h", []) == "Hello, World!" 31 assert Mix.Task.run("h", []) == :noop 32 assert Mix.Task.run("hello", []) == :noop 33 34 Mix.Task.reenable("h") 35 Mix.Task.reenable("hello") 36 assert Mix.Task.run("h", ["foo", "bar"]) == "Hello, foo bar!" 37 end 38 39 test "runs function aliases" do 40 assert Mix.Task.run("p", []) == "[]" 41 assert Mix.Task.run("p", []) == :noop 42 43 Mix.Task.reenable("p") 44 assert Mix.Task.run("p", ["foo", "bar"]) == "[\"foo\", \"bar\"]" 45 end 46 47 test "runs list aliases" do 48 assert Mix.Task.run("nested.h", ["baz"]) == "Hello, foo bar baz!" 49 assert_received {:mix_shell, :info, ["[]"]} 50 end 51 52 test "fails for invalid aliases" do 53 assert_raise Mix.Error, ~r/Invalid Mix alias format/, fn -> 54 Mix.Task.run("invalid_alias", []) 55 end 56 end 57 58 test "run alias override" do 59 assert Mix.Task.run("compile", []) == "Hello, World!" 60 assert Mix.Task.run("compile", []) == :noop 61 end 62 63 test "run alias override with name-recursion" do 64 assert Mix.Task.rerun("help", []) == "Hello, World!" 65 assert_received {:mix_shell, :info, ["mix test" <> _]} 66 67 # Arguments are passed to the recursive task and not the last one. 68 assert ExUnit.CaptureIO.capture_io(fn -> 69 Mix.Task.rerun("help", ["test"]) 70 end) =~ "mix test" 71 end 72 73 test "run alias override with code-recursion" do 74 assert Mix.Task.rerun("cmd", ["echo", "hello"]) == :ok 75 assert_received {:mix_shell, :run, ["hello" <> _]} 76 end 77end 78