Skip to content

Commit 9fa72cf

Browse files
authored
Update mix case (#754)
* do not start logger in debugger * Update mix case basing on elixir improvements this file was based on 5 years old implementation mix project stack etc clean has been changed since 1.6 elixir no longer sets :in_memory (elixir-lang/elixir@73ab6b8) restoring mix stack turned out to be unnecessary plugged apps leakage (in debugger tests)
1 parent 6a52eb9 commit 9fa72cf

File tree

2 files changed

+27
-71
lines changed

2 files changed

+27
-71
lines changed

apps/elixir_ls_debugger/mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule ElixirLS.Debugger.Mixfile do
2020
end
2121

2222
def application do
23-
[mod: {ElixirLS.Debugger, []}, extra_applications: [:mix, :logger]]
23+
[mod: {ElixirLS.Debugger, []}, extra_applications: [:mix]]
2424
end
2525

2626
defp deps do

apps/elixir_ls_utils/test/support/mix_test.case.ex

Lines changed: 26 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,59 @@
11
defmodule ElixirLS.Utils.MixTest.Case do
22
# This module is based heavily on MixTest.Case in Elixir's tests
3+
# https://github.com/elixir-lang/elixir/blob/db64b413a036c01c8e1cac8dd5e1c65107d90176/lib/mix/test/test_helper.exs#L29
34
use ExUnit.CaseTemplate
4-
alias ElixirLS.Utils.MixfileHelpers
55

66
using do
77
quote do
88
import ElixirLS.Utils.MixTest.Case
99
end
1010
end
1111

12-
setup config do
13-
if apps = config[:apps] do
14-
Logger.remove_backend(:console)
15-
end
12+
@apps Enum.map(Application.loaded_applications(), &elem(&1, 0))
13+
@allowed_apps ~w(docsh xmerl syntax_tools edoc elixir_sense elixir_ls_debugger elixir_ls_utils language_server stream_data)a
1614

15+
setup do
1716
on_exit(fn ->
1817
Application.start(:logger)
18+
Mix.env(:dev)
19+
Mix.target(:host)
1920
Mix.Task.clear()
2021
Mix.Shell.Process.flush()
22+
Mix.State.clear_cache()
23+
Mix.ProjectStack.clear_stack()
2124
delete_tmp_paths()
2225

23-
if apps do
24-
for app <- apps do
25-
Application.stop(app)
26-
Application.unload(app)
27-
end
28-
29-
Logger.add_backend(:console, flush: true)
26+
for {app, _, _} <- Application.loaded_applications(),
27+
app not in @apps,
28+
app not in @allowed_apps do
29+
Application.stop(app)
30+
Application.unload(app)
3031
end
3132
end)
3233

3334
:ok
3435
end
3536

37+
def fixture_path(dir) do
38+
Path.expand("fixtures", dir)
39+
end
40+
3641
def fixture_path(dir, extension) do
37-
Path.join(Path.expand("fixtures", dir), extension)
42+
Path.join(fixture_path(dir), remove_colons(extension))
3843
end
3944

4045
def tmp_path do
4146
Path.expand("../../.tmp", __DIR__)
4247
end
4348

4449
def tmp_path(extension) do
45-
Path.join(tmp_path(), to_string(extension))
50+
Path.join(tmp_path(), remove_colons(extension))
51+
end
52+
53+
defp remove_colons(term) do
54+
term
55+
|> to_string()
56+
|> String.replace(":", "")
4657
end
4758

4859
def purge(modules) do
@@ -73,27 +84,17 @@ defmodule ElixirLS.Utils.MixTest.Case do
7384

7485
get_path = :code.get_path()
7586
previous = :code.all_loaded()
76-
project_stack = clear_project_stack!()
77-
78-
ExUnit.CaptureLog.capture_log(fn ->
79-
Application.stop(:mix)
80-
Application.stop(:hex)
81-
end)
82-
83-
Application.start(:mix)
8487

8588
try do
8689
File.cd!(dest, function)
8790
after
8891
:code.set_path(get_path)
8992

9093
for {mod, file} <- :code.all_loaded() -- previous,
91-
file == :in_memory or file == [] or (is_list(file) and :lists.prefix(flag, file)) do
94+
file == [] or (is_list(file) and List.starts_with?(file, flag)) do
9295
mod
9396
end
9497
|> purge
95-
96-
restore_project_stack!(project_stack)
9798
end
9899
end
99100

@@ -102,51 +103,6 @@ defmodule ElixirLS.Utils.MixTest.Case do
102103
for path <- :code.get_path(), :string.str(path, tmp) != 0, do: :code.del_path(path)
103104
end
104105

105-
defp clear_project_stack! do
106-
stack = clear_project_stack!([])
107-
108-
# FIXME: Private API
109-
Mix.State.clear_cache()
110-
111-
# Attempt to purge mixfiles for dependencies to avoid module redefinition warnings
112-
mix_exs = MixfileHelpers.mix_exs()
113-
114-
for {mod, :in_memory} <- :code.all_loaded(),
115-
source = mod.module_info[:compile][:source],
116-
is_list(source),
117-
String.ends_with?(to_string(source), mix_exs),
118-
do: purge([mod])
119-
120-
stack
121-
end
122-
123-
defp clear_project_stack!(stack) do
124-
# FIXME: Private API
125-
case Mix.Project.pop() do
126-
nil ->
127-
stack
128-
129-
project ->
130-
clear_project_stack!([project | stack])
131-
end
132-
end
133-
134-
defp restore_project_stack!(stack) do
135-
# FIXME: Private API
136-
Mix.ProjectStack.clear_stack()
137-
# FIXME: Private API
138-
Mix.State.clear_cache()
139-
140-
for %{name: module, file: file} <- stack do
141-
:code.purge(module)
142-
:code.delete(module)
143-
# It's important to use `compile_file` here instead of `require_file`
144-
# because we are recompiling this file to reload the mix project back onto
145-
# the project stack.
146-
Code.compile_file(file)
147-
end
148-
end
149-
150106
def capture_log_and_io(device, fun) when is_function(fun, 0) do
151107
# Logger gets stopped during some tests so restart it to be able to capture logs (and kept the
152108
# test output clean)

0 commit comments

Comments
 (0)