Skip to content
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
20 changes: 15 additions & 5 deletions apps/expert_credo/lib/expert_credo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,25 @@ defmodule ExpertCredo do
{:ok, stdio} = StringIO.open(stdin_contents)
caller = self()

spawn(fn ->
Process.group_leader(self(), stdio)
result = function.()
send(caller, {:result, result})
end)
{pid, ref} =
spawn_monitor(fn ->
Process.group_leader(self(), stdio)
result = function.()
send(caller, {:result, result})
end)

receive do
{:result, result} ->
Process.demonitor(ref, [:flush])
{:ok, result}

{:DOWN, ^ref, :process, ^pid, reason} ->
{:error, reason}
after
30_000 ->
Process.demonitor(ref, [:flush])
Process.exit(pid, :kill)
{:error, :timeout}
end
end

Expand Down
9 changes: 9 additions & 0 deletions apps/expert_credo/test/expert_credo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ defmodule ExpertCredoTest do
Document.new("file:///file.ex", contents, 1)
end

test "with_stdin returns result on success" do
assert {:ok, :hello} = ExpertCredo.with_stdin("input", fn -> :hello end)
end

test "with_stdin returns error when function raises" do
assert {:error, {%RuntimeError{message: "boom"}, _stacktrace}} =
ExpertCredo.with_stdin("input", fn -> raise "boom" end)
end

test "reports errors on documents" do
has_inspect =
"""
Expand Down