diff --git a/lib/elixir/lib/kernel/cli.ex b/lib/elixir/lib/kernel/cli.ex index 4bcb9d94b8..0978d504de 100644 --- a/lib/elixir/lib/kernel/cli.ex +++ b/lib/elixir/lib/kernel/cli.ex @@ -56,7 +56,8 @@ defmodule Kernel.CLI do ## Private defp at_exit(status) do - hooks = :gen_server.call(:elixir_code_server, :at_exit) + hooks = :gen_server.call(:elixir_code_server, :flush_at_exit) + lc hook inlist hooks do try do hook.(status) @@ -72,6 +73,10 @@ defmodule Kernel.CLI do IO.puts Exception.formatted_stacktrace(trace) end end + + # If an at_exit callback adds a + # new hook we need to invoke it. + unless hooks == [], do: at_exit(status) end defp invalid_option(option) do diff --git a/lib/elixir/src/elixir_code_server.erl b/lib/elixir/src/elixir_code_server.erl index ff7f626d12..10b90233d8 100644 --- a/lib/elixir/src/elixir_code_server.erl +++ b/lib/elixir/src/elixir_code_server.erl @@ -47,6 +47,9 @@ handle_call(loaded, _From, Config) -> handle_call(at_exit, _From, Config) -> { reply, Config#elixir_code_server.at_exit, Config }; +handle_call(flush_at_exit, _From, Config) -> + { reply, Config#elixir_code_server.at_exit, Config#elixir_code_server{at_exit=[]} }; + handle_call(argv, _From, Config) -> { reply, Config#elixir_code_server.argv, Config }; diff --git a/lib/ex_unit/lib/ex_unit.ex b/lib/ex_unit/lib/ex_unit.ex index 6654afb2f9..27fb7b876d 100644 --- a/lib/ex_unit/lib/ex_unit.ex +++ b/lib/ex_unit/lib/ex_unit.ex @@ -65,11 +65,14 @@ defmodule ExUnit do options = Keyword.merge(user_options, options) ExUnit.Server.start_link configure(options) - System.at_exit fn status -> - if status == 0 do + System.at_exit fn + 0 -> failures = ExUnit.run - if failures > 0, do: System.halt(1), else: System.halt(0) - end + System.at_exit fn _ -> + if failures > 0, do: System.halt(1), else: System.halt(0) + end + _ -> + :ok end end