1
1
defmodule ElixirLS.Utils.MixTest.Case do
2
2
# 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
3
4
use ExUnit.CaseTemplate
4
- alias ElixirLS.Utils.MixfileHelpers
5
5
6
6
using do
7
7
quote do
8
8
import ElixirLS.Utils.MixTest.Case
9
9
end
10
10
end
11
11
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
16
14
15
+ setup do
17
16
on_exit ( fn ->
18
17
Application . start ( :logger )
18
+ Mix . env ( :dev )
19
+ Mix . target ( :host )
19
20
Mix.Task . clear ( )
20
21
Mix.Shell.Process . flush ( )
22
+ Mix.State . clear_cache ( )
23
+ Mix.ProjectStack . clear_stack ( )
21
24
delete_tmp_paths ( )
22
25
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 )
30
31
end
31
32
end )
32
33
33
34
:ok
34
35
end
35
36
37
+ def fixture_path ( dir ) do
38
+ Path . expand ( "fixtures" , dir )
39
+ end
40
+
36
41
def fixture_path ( dir , extension ) do
37
- Path . join ( Path . expand ( "fixtures" , dir ) , extension )
42
+ Path . join ( fixture_path ( dir ) , remove_colons ( extension ) )
38
43
end
39
44
40
45
def tmp_path do
41
46
Path . expand ( "../../.tmp" , __DIR__ )
42
47
end
43
48
44
49
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 ( ":" , "" )
46
57
end
47
58
48
59
def purge ( modules ) do
@@ -73,27 +84,17 @@ defmodule ElixirLS.Utils.MixTest.Case do
73
84
74
85
get_path = :code . get_path ( )
75
86
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 )
84
87
85
88
try do
86
89
File . cd! ( dest , function )
87
90
after
88
91
:code . set_path ( get_path )
89
92
90
93
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
92
95
mod
93
96
end
94
97
|> purge
95
-
96
- restore_project_stack! ( project_stack )
97
98
end
98
99
end
99
100
@@ -102,51 +103,6 @@ defmodule ElixirLS.Utils.MixTest.Case do
102
103
for path <- :code . get_path ( ) , :string . str ( path , tmp ) != 0 , do: :code . del_path ( path )
103
104
end
104
105
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
-
150
106
def capture_log_and_io ( device , fun ) when is_function ( fun , 0 ) do
151
107
# Logger gets stopped during some tests so restart it to be able to capture logs (and kept the
152
108
# test output clean)
0 commit comments