From c6f2738fc759747e0e5dee48870cd9abc107a131 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Sat, 11 Jun 2016 08:43:37 -0500 Subject: [PATCH] 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. --- lib/execute.ex | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/execute.ex b/lib/execute.ex index be98273..ee59247 100644 --- a/lib/execute.ex +++ b/lib/execute.ex @@ -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}