Refactor error expansion to locate errors in koan module
The previous strategy wasn't robust enough to detect typos in function calls. For example, if you misremembered String.duplicate/2 as String.repeat/2 the koan runner would hang and have to be restarted. This is because the stacktrace for an error like that doesn't have line numbers. To avoid the problem, we detect the first pieces of the stracktrace that is in the koan module. I can't decide if that's a perfect solution, but it seemed to work in my testing. Hoping to get other's feedback.
This commit is contained in:
@@ -32,22 +32,21 @@ defmodule Execute do
|
|||||||
|
|
||||||
defp exec(module, name, args, parent) do
|
defp exec(module, name, args, parent) do
|
||||||
result = apply(module, name, args)
|
result = apply(module, name, args)
|
||||||
send parent, expand(result)
|
send parent, expand(result, module)
|
||||||
Process.exit(self(), :kill)
|
Process.exit(self(), :kill)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp expand(:ok), do: :ok
|
defp expand(:ok, _), do: :ok
|
||||||
defp expand(error) do
|
defp expand(error, module) do
|
||||||
{file, line} = System.stacktrace
|
{file, line} = System.stacktrace
|
||||||
|> Enum.drop_while(&in_ex_unit?/1)
|
|> Enum.drop_while(&!in_koan?(&1, module))
|
||||||
|> List.first
|
|> List.first
|
||||||
|> extract_file_and_line
|
|> extract_file_and_line
|
||||||
|
|
||||||
%{error: error, file: file, line: line}
|
%{error: error, file: file, line: line}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp in_ex_unit?({ExUnit.Assertions, _, _, _}), do: true
|
defp in_koan?({module, _, _, _}, koan), do: module == koan
|
||||||
defp in_ex_unit?(_), do: false
|
|
||||||
|
|
||||||
defp extract_file_and_line({_, _, _, [file: file, line: line]}) do
|
defp extract_file_and_line({_, _, _, [file: file, line: line]}) do
|
||||||
{file, line}
|
{file, line}
|
||||||
|
|||||||
Reference in New Issue
Block a user