|
| 1 | +# https://github.com/elixir-gettext/gettext/issues/330 |
1 | 2 | defmodule Gettext.NewBackendSetupTest do |
2 | | - # https://github.com/elixir-gettext/gettext/issues/330 |
3 | | - use ExUnit.Case, async: true |
4 | | - |
5 | | - import ExUnit.CaptureIO |
6 | | - import GettextTest.MixProjectHelpers |
| 3 | + # Has to be async: false since it changes Elixir compiler options. |
| 4 | + use ExUnit.Case, async: false |
7 | 5 |
|
8 | 6 | @moduletag :tmp_dir |
9 | 7 |
|
@@ -34,29 +32,37 @@ defmodule Gettext.NewBackendSetupTest do |
34 | 32 | end |
35 | 33 |
|
36 | 34 | describe "compile-time dependencies" do |
37 | | - @tag :skip |
38 | | - test "are not created for modules that use the backend", |
39 | | - %{test: test, tmp_dir: tmp_dir} = context do |
40 | | - create_test_mix_file(context) |
41 | | - |
42 | | - write_file(context, "lib/my_app.ex", """ |
43 | | - defmodule MyApp.Gettext do |
44 | | - use Gettext.Backend, otp_app: #{inspect(test)} |
45 | | - end |
46 | | -
|
47 | | - defmodule MyApp do |
48 | | - use Gettext, backend: MyApp.Gettext |
49 | | - end |
50 | | - """) |
51 | | - |
52 | | - output = |
53 | | - in_project(test, tmp_dir, fn _module -> |
54 | | - capture_io(fn -> Mix.Task.run("compile") end) |
55 | | - capture_io(fn -> Mix.Task.run("xref", ["trace", "lib/my_app.ex"]) end) |
56 | | - end) |
57 | | - |
58 | | - assert output =~ ~r"lib/my_app\.ex:\d+: alias MyApp\.Gettext \(runtime\)\n" |
59 | | - refute output =~ ~r"lib/my_app\.ex:\d+: alias MyApp\.Gettext \(compile\)\n" |
| 35 | + test "are not created for modules that use the backend", %{test: test} do |
| 36 | + top_level_module = :"Elixir.Gettext_#{test}" |
| 37 | + backend_module = Module.concat(top_level_module, Gettext) |
| 38 | + |
| 39 | + Code.eval_quoted( |
| 40 | + quote do |
| 41 | + defmodule unquote(backend_module) do |
| 42 | + use Gettext.Backend, otp_app: unquote(test) |
| 43 | + end |
| 44 | + end |
| 45 | + ) |
| 46 | + |
| 47 | + old_compiler_opts = Code.compiler_options(tracers: [__MODULE__]) |
| 48 | + on_exit(fn -> Code.compiler_options(old_compiler_opts) end) |
| 49 | + |
| 50 | + Code.compile_quoted( |
| 51 | + quote do |
| 52 | + defmodule unquote(top_level_module) do |
| 53 | + use Gettext, backend: unquote(backend_module) |
| 54 | + end |
| 55 | + end |
| 56 | + ) |
| 57 | + |
| 58 | + assert_received {:trace, {:require, _meta, Gettext.Macros, _opts}} |
| 59 | + refute_received {:trace, {:require, _meta, ^backend_module, _opts}} |
| 60 | + refute_received {:trace, {:import, _meta, ^backend_module, _opts}} |
60 | 61 | end |
61 | 62 | end |
| 63 | + |
| 64 | + def trace(event, _env) do |
| 65 | + send(self(), {:trace, event}) |
| 66 | + :ok |
| 67 | + end |
62 | 68 | end |
0 commit comments