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:
Jay Hayes
2016-06-11 08:43:37 -05:00
parent 3c217073f5
commit c6f2738fc7

View File

@@ -32,22 +32,21 @@ defmodule Execute do
defp exec(module, name, args, parent) do
result = apply(module, name, args)
send parent, expand(result)
send parent, expand(result, module)
Process.exit(self(), :kill)
end
defp expand(:ok), do: :ok
defp expand(error) do
defp expand(:ok, _), do: :ok
defp expand(error, module) do
{file, line} = System.stacktrace
|> Enum.drop_while(&in_ex_unit?/1)
|> Enum.drop_while(&!in_koan?(&1, module))
|> List.first
|> extract_file_and_line
%{error: error, file: file, line: line}
end
defp in_ex_unit?({ExUnit.Assertions, _, _, _}), do: true
defp in_ex_unit?(_), do: false
defp in_koan?({module, _, _, _}, koan), do: module == koan
defp extract_file_and_line({_, _, _, [file: file, line: line]}) do
{file, line}