Skip to content
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

Remove stacktrace warning #242

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions lib/display/failure.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ defmodule Display.Failure do
end

defp format_error(error) do
trace = System.stacktrace() |> Enum.take(2)
Paint.red(Exception.format(:error, error, trace))
Paint.red(Exception.format(:error, error, []))
end

def show_compile_error(error) do
Expand Down
26 changes: 10 additions & 16 deletions lib/execute.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,20 @@ defmodule Execute do
end

defp exec(module, name, args, parent) do
result = apply(module, name, args)
send(parent, expand(result, module))
case apply(module, name, args) do
:ok -> send(parent, :ok)
err -> send(parent, expand(err))
end
Process.exit(self(), :kill)
end

defp expand(:ok, _), do: :ok

defp expand({:error, stacktrace, exception}, module) do
{file, line} =
stacktrace
|> Enum.drop_while(&(!in_koan?(&1, module)))
|> List.first()
|> extract_file_and_line

%{error: exception, file: file, line: line}
defp expand({:error, error, {:location, file_path, line}}) do
%{error: error, file: make_relative(file_path), line: line}
end

defp in_koan?({module, _, _, _}, koan), do: module == koan

defp extract_file_and_line({_, _, _, [file: file, line: line]}) do
{file, line}
defp make_relative(path) do
path
|> Path.relative_to_cwd()
|> to_string()
end
end
10 changes: 5 additions & 5 deletions lib/koans.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Koans do
unquote(compiled_body)
:ok
rescue
e -> {:error, __STACKTRACE__, e}
e -> {:error, e, {:location, __ENV__.file, __ENV__.line}}
end
end
end
Expand All @@ -39,7 +39,7 @@ defmodule Koans do
unquote(single_var)
:ok
rescue
e -> {:error, __STACKTRACE__, e}
e -> {:error, e, {:location, __ENV__.file, __ENV__.line}}
end
end
end
Expand All @@ -57,7 +57,7 @@ defmodule Koans do
unquote(multi_var)
:ok
rescue
e -> {:error, __STACKTRACE__, e}
e -> {:error, e, {:location, __ENV__.file, __ENV__.line}}
end
end
end
Expand Down Expand Up @@ -98,8 +98,8 @@ defmodule Koans do
end
end

defp koans(env) do
env.module
defp koans(%{module: module}) do
module
|> Module.get_attribute(:koans)
|> Enum.reverse()
end
Expand Down
22 changes: 20 additions & 2 deletions test/display/failure_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,31 @@ defmodule FailureTests do

test "only offending lines are displayed for errors" do
[koan] = SingleArity.all_koans()
error = apply(SingleArity, koan, []) |> Tuple.to_list |> List.last |> error
error = apply(SingleArity, koan, []) |> error()

assert Failure.format_failure(error) == """
Assertion failed in some_file.ex:42\nmatch?(:foo, ___)
Assertion failed in some_file.ex:42
match?(:foo, ___)
"""
end

test "formats errors such as arity mismatches as such" do
e = error({:error, %FunctionClauseError{args: nil, arity: 1, clauses: nil, function: :chardata_to_string, kind: nil, module: IO}, nil})

assert Failure.format_failure(e) == """
Error in some_file.ex:42
** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1
"""
end

defp error({:error, error, _}) do
%{
error: error,
file: "some_file.ex",
line: 42
}
end

defp error(error) do
%{
error: error,
Expand Down
2 changes: 1 addition & 1 deletion test/executor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule ExecuteTest do

test "stops at the first failing koan" do
{:failed, %{file: file, line: line}, SampleKoan, _name} = Execute.run_module(SampleKoan)
assert file == 'test/support/sample_koan.ex'
assert file == "test/support/sample_koan.ex"
assert line == 8
end

Expand Down