Skip to content

Commit 804406c

Browse files
committed
Extract file/line from koan and make stacktrace obsolete
1 parent 2c4e1f5 commit 804406c

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

lib/display/failure.ex

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ defmodule Display.Failure do
4949
end
5050

5151
defp format_error(error) do
52-
trace = :erlang.get_stacktrace() |> Enum.take(2)
53-
Paint.red(Exception.format(:error, error, trace))
52+
Paint.red(Exception.format(:error, error, []))
5453
end
5554

5655
def show_compile_error(error) do

lib/execute.ex

+10-16
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,20 @@ defmodule Execute do
3131
end
3232

3333
defp exec(module, name, args, parent) do
34-
result = apply(module, name, args)
35-
send(parent, expand(result, module))
34+
case apply(module, name, args) do
35+
:ok -> send(parent, :ok)
36+
err -> send(parent, expand(err))
37+
end
3638
Process.exit(self(), :kill)
3739
end
3840

39-
defp expand(:ok, _), do: :ok
40-
41-
defp expand({:error, stacktrace, exception}, module) do
42-
{file, line} =
43-
:erlang.get_stacktrace()
44-
|> Enum.drop_while(&(!in_koan?(&1, module)))
45-
|> List.first()
46-
|> extract_file_and_line
47-
48-
%{error: exception, file: file, line: line}
41+
defp expand({:error, error, {:location, file_path, line}}) do
42+
%{error: error, file: make_relative(file_path), line: line}
4943
end
5044

51-
defp in_koan?({module, _, _, _}, koan), do: module == koan
52-
53-
defp extract_file_and_line({_, _, _, [file: file, line: line]}) do
54-
{file, line}
45+
defp make_relative(path) do
46+
path
47+
|> Path.relative_to_cwd()
48+
|> to_string()
5549
end
5650
end

lib/koans.ex

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule Koans do
2222
unquote(compiled_body)
2323
:ok
2424
rescue
25-
e -> {:error, __STACKTRACE__, e}
25+
e -> {:error, e, {:location, __ENV__.file, __ENV__.line}}
2626
end
2727
end
2828
end
@@ -39,7 +39,7 @@ defmodule Koans do
3939
unquote(single_var)
4040
:ok
4141
rescue
42-
e -> {:error, __STACKTRACE__, e}
42+
e -> {:error, e, {:location, __ENV__.file, __ENV__.line}}
4343
end
4444
end
4545
end
@@ -57,7 +57,7 @@ defmodule Koans do
5757
unquote(multi_var)
5858
:ok
5959
rescue
60-
e -> {:error, __STACKTRACE__, e}
60+
e -> {:error, e, {:location, __ENV__.file, __ENV__.line}}
6161
end
6262
end
6363
end
@@ -98,8 +98,8 @@ defmodule Koans do
9898
end
9999
end
100100

101-
defp koans(env) do
102-
env.module
101+
defp koans(%{module: module}) do
102+
module
103103
|> Module.get_attribute(:koans)
104104
|> Enum.reverse()
105105
end

test/display/failure_test.exs

+20-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,31 @@ defmodule FailureTests do
3939

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

4444
assert Failure.format_failure(error) == """
45-
Assertion failed in some_file.ex:42\nmatch?(:foo, ___)
45+
Assertion failed in some_file.ex:42
46+
match?(:foo, ___)
47+
"""
48+
end
49+
50+
test "formats errors such as arity mismatches as such" do
51+
e = error({:error, %FunctionClauseError{args: nil, arity: 1, clauses: nil, function: :chardata_to_string, kind: nil, module: IO}, nil})
52+
53+
assert Failure.format_failure(e) == """
54+
Error in some_file.ex:42
55+
** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1
4656
"""
4757
end
4858

59+
defp error({:error, error, _}) do
60+
%{
61+
error: error,
62+
file: "some_file.ex",
63+
line: 42
64+
}
65+
end
66+
4967
defp error(error) do
5068
%{
5169
error: error,

test/executor_test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule ExecuteTest do
77

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

0 commit comments

Comments
 (0)