Skip to content

Commit

Permalink
Also scrub inputs from the configuration before formatters
Browse files Browse the repository at this point in the history
This includes a slight workaround to preserve the names of inputs
as people may be relying on those. Doesn't feel great but...
somehow reasonable?
  • Loading branch information
PragTob committed Dec 14, 2023
1 parent c43880d commit be1f13a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
20 changes: 17 additions & 3 deletions lib/benchee/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,26 @@ defmodule Benchee.Formatter do
# Hence, we scrub them away here. If you maintain a formatter plugin and rely on these please
# get in touch so we can work on a solution.
defp scrub_suite(suite) do
update_in(suite.scenarios, fn scenarios ->
Enum.map(scenarios, &scrub_scenario/1)
end)
suite =
update_in(suite.scenarios, fn scenarios ->
Enum.map(scenarios, &scrub_scenario/1)
end)

update_in(suite.configuration, &scrub_configuration/1)
end

defp scrub_scenario(scenario) do
%Scenario{scenario | function: nil, input: nil}
end

defp scrub_configuration(configuration) do
update_in(configuration.inputs, &scrub_inputs/1)
end

defp scrub_inputs(nil), do: nil
# Feels somewhat hacky, but while people should not be relying on the input itself, they
# may be relying on the input names/order
defp scrub_inputs(inputs) do
Enum.map(inputs, fn {name, _value} -> {name, :scrubbed_see_1_3_0_changelog} end)
end
end
28 changes: 20 additions & 8 deletions test/benchee/formatter_test.exs
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
defmodule Benchee.FormatterTest do
use ExUnit.Case, async: true
alias Benchee.{Formatter, Suite, Test.FakeFormatter}
alias Benchee.{Configuration, Formatter, Suite, Test.FakeFormatter}

@no_print_assigns %{test: %{progress_printer: Benchee.Test.FakeProgressPrinter}}

describe "output/1" do
test "calls `write/1` with the output of `format/1` on each module" do
Formatter.output(%Suite{
configuration: %{formatters: [{FakeFormatter, %{}}], assigns: @no_print_assigns}
configuration: %Configuration{
formatters: [{FakeFormatter, %{}}],
assigns: @no_print_assigns
}
})

assert_receive {:write, "output of `format/1` with %{}", %{}}
end

test "works with just modules without option tuple, defaults to empty map" do
Formatter.output(%Suite{
configuration: %{formatters: [FakeFormatter], assigns: @no_print_assigns}
configuration: %Configuration{formatters: [FakeFormatter], assigns: @no_print_assigns}
})

assert_receive {:write, "output of `format/1` with %{}", %{}}
end

test "options are passed on correctly" do
Formatter.output(%Suite{
configuration: %{formatters: [{FakeFormatter, %{a: :b}}], assigns: @no_print_assigns}
configuration: %Configuration{
formatters: [{FakeFormatter, %{a: :b}}],
assigns: @no_print_assigns
}
})

assert_receive {:write, "output of `format/1` with %{a: :b}", %{a: :b}}
end

test "keyword list options are deep converted to maps" do
Formatter.output(%Suite{
configuration: %{formatters: [{FakeFormatter, [a: [b: :c]]}], assigns: @no_print_assigns}
configuration: %Configuration{
formatters: [{FakeFormatter, [a: [b: :c]]}],
assigns: @no_print_assigns
}
})

assert_receive {:write, "output of `format/1` with %{a: %{b: :c}}", %{a: %{b: :c}}}
end

test "mixing functions and formatters works" do
suite = %Suite{
configuration: %{
configuration: %Configuration{
formatters: [
{FakeFormatter, %{}},
fn suite -> send(self(), {:fun, suite, "me"}) end
Expand All @@ -56,15 +65,18 @@ defmodule Benchee.FormatterTest do

test "returns the suite passed in as the first argument unchanged" do
suite = %Suite{
configuration: %{formatters: [{FakeFormatter, %{}}], assigns: @no_print_assigns}
configuration: %Configuration{
formatters: [{FakeFormatter, %{}}],
assigns: @no_print_assigns
}
}

assert Formatter.output(suite) == suite
end

test "lets you know it starts formatting now" do
Formatter.output(%Suite{
configuration: %{formatters: [], assigns: @no_print_assigns}
configuration: %Configuration{formatters: [], assigns: @no_print_assigns}
})

assert_received :formatting
Expand Down

0 comments on commit be1f13a

Please sign in to comment.