Skip to content

Commit

Permalink
Raise a helpful error if mneme was not started
Browse files Browse the repository at this point in the history
  • Loading branch information
axelson authored and zachallaun committed Sep 9, 2023
1 parent 8f26e11 commit 3fe146b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,5 @@ jobs:
- run: mix deps.compile

- run: mix test

- run: mix test.mneme_not_started
9 changes: 8 additions & 1 deletion lib/mneme/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ defmodule Mneme.Options do
|> collect_attributes(Map.get(attrs, @test_attr, []))
|> collect_attributes(Map.get(attrs, @describe_attr, []))
|> collect_attributes(Map.get(attrs, @module_attr, []))
|> collect_attributes([:persistent_term.get(@config_cache)])
|> collect_attributes([persistent_term_get!(@config_cache)])
end

defp collect_attributes(_), do: %{}
Expand All @@ -240,4 +240,11 @@ defmodule Mneme.Options do

Map.merge(acc, new, fn _, vs1, vs2 -> vs1 ++ vs2 end)
end

defp persistent_term_get!(key) do
:persistent_term.get(key)
rescue
ArgumentError ->
raise "Config key not found (#{inspect(@config_cache)}). Did you start Mneme in test_helper.exs?"
end
end
7 changes: 6 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@ defmodule Mneme.MixProject do
"coveralls.html": [
&export_integration_coverage/1,
"coveralls.html --import-cover cover"
],
"test.mneme_not_started": [
fn _ -> System.put_env("START_MNEME", "false") end,
"test --only mneme_not_started test/mneme_not_started_test.exs"
]
]
end

defp preferred_cli_env do
[
coveralls: :test,
"coveralls.html": :test
"coveralls.html": :test,
"test.mneme_not_started": :test
]
end

Expand Down
22 changes: 22 additions & 0 deletions test/mneme_not_started_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Mneme.MnemeNotStartedTest do
use ExUnit.Case
use Mneme

@tag :mneme_not_started
test "error" do
assert_raise RuntimeError, ~r/Did you start Mneme/, fn ->
code = """
defmodule Mneme.StartedTest.ExampleTest do
use ExUnit.Case
use Mneme
test "a test" do
auto_assert 2 + 2
end
end
"""

assert [{_, _}] = Code.compile_string(code)
end
end
end
7 changes: 5 additions & 2 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
ExUnit.start()
Mneme.start()
ExUnit.start(exclude: [mneme_not_started: true])

if System.get_env("START_MNEME") != "false" do
Mneme.start()
end

0 comments on commit 3fe146b

Please sign in to comment.