Skip to content

Commit f0e42f0

Browse files
author
José Valim
committed
Merge pull request #2017 from jeregrine/improve_error_on_bad_return
Improve error on bad return
2 parents bf1da40 + 070f6a0 commit f0e42f0

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

lib/mix/lib/mix/tasks/app.start.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ defmodule Mix.Tasks.App.Start do
2727
end
2828

2929
unless opts[:no_start] do
30-
start_app(project)
30+
start(project)
3131
end
3232
end
3333

34-
defp start_app(project) do
34+
def start(project) do
3535
if app = project[:app] do
3636
case Application.Behaviour.start(app) do
3737
:ok -> :ok
38+
{ :error, {:bad_return, err}} ->
39+
raise Exception.normalize(err)
3840
{ :error, reason } ->
3941
raise Mix.Error, message: "Could not start application #{app}: #{inspect reason}"
4042
end

lib/mix/test/mix/tasks/app.start_test.exs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ defmodule Mix.Tasks.App.StartTest do
99
end
1010
end
1111

12+
defmodule BadReturnSample do
13+
def project do
14+
[app: :bad_return_app, version: "0.1.0"]
15+
end
16+
# Configuration for the OTP application
17+
def application do
18+
[mod: { Mix.Tasks.App.StartTest.BadReturnApp, [] }]
19+
end
20+
end
21+
22+
defmodule BadReturnApp do
23+
use Application.Behaviour
24+
25+
def start(_type, _args) do
26+
:bar # Bad return
27+
end
28+
end
29+
1230
defmodule WrongElixirProject do
1331
def project do
1432
[app: :error, version: "0.1.0", elixir: "~> 0.8.1"]
@@ -70,4 +88,41 @@ defmodule Mix.Tasks.App.StartTest do
7088
Mix.Tasks.App.Start.run ["--no-start", "--no-elixir-version-check"]
7189
end
7290
end
91+
92+
test "start does nothing if project[:app] is nil" do
93+
project = Mix.project
94+
assert Mix.Tasks.App.Start.start(project) == nil
95+
end
96+
97+
test "start runs successfully in okay case" do
98+
Mix.Project.push MixTest.Case.Sample
99+
in_fixture "no_mixfile", fn ->
100+
Mix.Tasks.Compile.run []
101+
project = Mix.project
102+
assert Mix.Tasks.App.Start.start(project) == :ok
103+
end
104+
end
105+
106+
test "start raises an exception on :error" do
107+
Mix.Project.push AppStartSample
108+
109+
in_fixture "no_mixfile", fn ->
110+
project = Mix.project
111+
assert_raise Mix.Error, fn ->
112+
Mix.Tasks.App.Start.start(project)
113+
end
114+
end
115+
end
116+
117+
test "start raises a stacktrace on bad_return" do
118+
Mix.Project.push BadReturnSample
119+
120+
in_fixture "no_mixfile", fn ->
121+
Mix.Tasks.Compile.run []
122+
project = Mix.project
123+
assert_raise ErlangError, fn ->
124+
Mix.Tasks.App.Start.start(project)
125+
end
126+
end
127+
end
73128
end

0 commit comments

Comments
 (0)