Skip to content

Preserve argument quoting in mix cmd #14181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/mix/lib/mix/tasks/cmd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ defmodule Mix.Tasks.Cmd do
def run(args) do
{opts, args} = OptionParser.parse_head!(args, strict: @switches)

if args == [] do
Mix.raise("Expected at least one argument in mix cmd")
end

apps =
opts
|> Keyword.get_values(:app)
Expand All @@ -69,7 +73,7 @@ defmodule Mix.Tasks.Cmd do
if apps == [] or Mix.Project.config()[:app] in apps do
cmd_opts = Keyword.take(opts, [:cd])

case Mix.shell().cmd(Enum.join(args, " "), cmd_opts) do
case Mix.shell().cmd({hd(args), tl(args)}, cmd_opts) do
0 -> :ok
status -> exit(status)
end
Expand Down
22 changes: 9 additions & 13 deletions lib/mix/test/mix/tasks/cmd_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ defmodule Mix.Tasks.CmdTest do
use MixTest.Case

test "can be called multiple times" do
nl = os_newline()
Mix.Task.run("cmd", ["echo", "hello"])
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
Mix.Task.run("cmd", ["echo", "hello"])
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
assert_received {:mix_shell, :run, ["hello\n"]}
Mix.Task.run("cmd", ["echo", "hello world"])
assert_received {:mix_shell, :run, ["hello world\n"]}
end

test "runs the command for each app" do
in_fixture("umbrella_dep/deps/umbrella", fn ->
Mix.Project.in_project(:umbrella, ".", fn _ ->
Mix.Task.run("cmd", ["echo", "hello"])
nl = os_newline()
assert_received {:mix_shell, :info, ["==> bar"]}
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
assert_received {:mix_shell, :run, ["hello\n"]}
assert_received {:mix_shell, :info, ["==> foo"]}
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
assert_received {:mix_shell, :run, ["hello\n"]}
end)
end)
end
Expand All @@ -29,11 +27,10 @@ defmodule Mix.Tasks.CmdTest do
in_fixture("umbrella_dep/deps/umbrella", fn ->
Mix.Project.in_project(:umbrella, ".", fn _ ->
Mix.Task.run("cmd", ["--app", "bar", "echo", "hello"])
nl = os_newline()
assert_received {:mix_shell, :info, ["==> bar"]}
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
assert_received {:mix_shell, :run, ["hello\n"]}
refute_received {:mix_shell, :info, ["==> foo"]}
refute_received {:mix_shell, :run, ["hello" <> ^nl]}
refute_received {:mix_shell, :run, ["hello\n"]}
end)
end)
end)
Expand All @@ -44,11 +41,10 @@ defmodule Mix.Tasks.CmdTest do
in_fixture("umbrella_dep/deps/umbrella", fn ->
Mix.Project.in_project(:umbrella, ".", fn _ ->
Mix.Task.run("cmd", ["--app", "bar", "--app", "foo", "echo", "hello"])
nl = os_newline()
assert_received {:mix_shell, :info, ["==> bar"]}
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
assert_received {:mix_shell, :run, ["hello\n"]}
assert_received {:mix_shell, :info, ["==> foo"]}
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
assert_received {:mix_shell, :run, ["hello\n"]}
end)
end)
end)
Expand Down
15 changes: 6 additions & 9 deletions lib/mix/test/mix/tasks/do_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ defmodule Mix.Tasks.DoTest do
Mix.Project.in_project(:umbrella, ".", fn _ ->
Mix.Tasks.Do.run(~w(--app bar compile --list + cmd echo hello))

nl = os_newline()
assert_received {:mix_shell, :info, ["==> bar"]}
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
assert_received {:mix_shell, :run, ["hello\n"]}
refute_received {:mix_shell, :info, ["==> foo"]}
refute_received {:mix_shell, :run, ["hello" <> ^nl]}
refute_received {:mix_shell, :run, ["hello\n"]}
end)
end)
end
Expand All @@ -66,11 +65,10 @@ defmodule Mix.Tasks.DoTest do
Mix.Project.in_project(:umbrella, ".", fn _ ->
Mix.Tasks.Do.run(~w(--app bar --app foo compile --list + cmd echo hello))

nl = os_newline()
assert_received {:mix_shell, :info, ["==> bar"]}
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
assert_received {:mix_shell, :run, ["hello\n"]}
assert_received {:mix_shell, :info, ["==> foo"]}
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
assert_received {:mix_shell, :run, ["hello\n"]}
end)
end)
end
Expand Down Expand Up @@ -105,13 +103,12 @@ defmodule Mix.Tasks.DoTest do
Mix.Tasks.Do.run(["compile"])
Mix.Tasks.Do.run(["--app", "bar", "--app", "foo", "e", "+", "p", "Foo"])

nl = os_newline()
assert_received {:mix_shell, :info, ["==> bar"]}
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
assert_received {:mix_shell, :run, ["hello\n"]}
assert_received {:mix_shell, :info, ["[\"Foo\"]"]}

assert_received {:mix_shell, :info, ["==> foo"]}
assert_received {:mix_shell, :run, ["hello" <> ^nl]}
assert_received {:mix_shell, :run, ["hello\n"]}
assert_received {:mix_shell, :info, ["[\"Foo\"]"]}
end)
end)
Expand Down
Loading