Skip to content

Commit

Permalink
Basic fix for #384
Browse files Browse the repository at this point in the history
In escript we can not find our elixir executable, so warn
and default to true (most things should have protocol consolidation
turned on)
  • Loading branch information
PragTob committed Nov 8, 2023
1 parent 6adcf8e commit 7cb1b5d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/benchee/system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,18 @@ defmodule Benchee.System do
end
end

defp all_protocols_consolidated? do
path = :code.lib_dir(:elixir, :ebin)

[path]
|> Protocol.extract_protocols()
|> Enum.all?(&Protocol.consolidated?/1)
# just made public for easy testing purposes
@doc false
def all_protocols_consolidated?(lib_dir_fun \\ &:code.lib_dir/2) do
case lib_dir_fun.(:elixir, :ebin) do
# do we get a good old erlang charlist?
path when is_list(path) ->
[path]
|> Protocol.extract_protocols()
|> Enum.all?(&Protocol.consolidated?/1)
_error ->
IO.puts("Could not check if protocols are consolidated. Running as escript? Defaulting to they are consolidated.")
true
end
end
end
20 changes: 20 additions & 0 deletions test/benchee/system_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,24 @@ defmodule Benchee.SystemTest do
assert system_cmd("cat", "dev/null", system_func) == "N/A"
end)
end

describe "all_protocols_consolidated?/1" do
test "normally it just works and is true for Bechee and does not log a warning" do
warning = capture_io(fn ->
assert true == all_protocols_consolidated?()
end)

assert warning == ""
end

test "when it borks out it warns and defaults to true, see #384" do
fake_lib_dir = fn _, _ -> {:error, :bad_name} end

warning = capture_io(fn ->
assert true == all_protocols_consolidated?(fake_lib_dir)
end)

assert warning =~ ~r/not.*check.*protocol.*consolidat/i
end
end
end

0 comments on commit 7cb1b5d

Please sign in to comment.