Skip to content

Commit 93f8996

Browse files
author
Étienne Lévesque
authored
Fix some test lenses bugs (#443)
* fix: Module test lenses are always placed on module declaration line * Sort environment info from the start
1 parent a696d94 commit 93f8996

File tree

2 files changed

+160
-4
lines changed
  • apps/language_server

2 files changed

+160
-4
lines changed

apps/language_server/lib/language_server/providers/code_lens/test.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ defmodule ElixirLS.LanguageServer.Providers.CodeLens.Test do
1313
alias ElixirLS.LanguageServer.Providers.CodeLens.Test.TestBlock
1414
alias ElixirLS.LanguageServer.SourceFile
1515
alias ElixirSense.Core.Parser
16-
alias ElixirSense.Core.Metadata
1716

1817
@run_test_command "elixir.lens.test.run"
1918

@@ -28,7 +27,9 @@ defmodule ElixirLS.LanguageServer.Providers.CodeLens.Test do
2827
|> Enum.map(fn {_k, v} -> v end)
2928
|> List.flatten()
3029

31-
lines_to_env_list = Map.to_list(buffer_file_metadata.lines_to_env)
30+
lines_to_env_list =
31+
buffer_file_metadata.lines_to_env
32+
|> Enum.sort_by(&elem(&1, 0))
3233

3334
describe_blocks = find_describe_blocks(lines_to_env_list, calls_list, source_lines)
3435
describe_lenses = get_describe_lenses(describe_blocks, file_path)
@@ -39,7 +40,7 @@ defmodule ElixirLS.LanguageServer.Providers.CodeLens.Test do
3940
|> get_test_lenses(file_path)
4041

4142
module_lenses =
42-
buffer_file_metadata
43+
lines_to_env_list
4344
|> get_test_modules()
4445
|> get_module_lenses(file_path)
4546

@@ -119,7 +120,7 @@ defmodule ElixirLS.LanguageServer.Providers.CodeLens.Test do
119120
end)
120121
end
121122

122-
defp get_test_modules(%Metadata{lines_to_env: lines_to_env}) do
123+
defp get_test_modules(lines_to_env) do
123124
lines_to_env
124125
|> Enum.group_by(fn {_line, env} -> env.module end)
125126
|> Enum.filter(fn {_module, module_lines_to_env} -> is_test_module?(module_lines_to_env) end)

apps/language_server/test/providers/code_lens/test_test.exs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,161 @@ defmodule ElixirLS.LanguageServer.Providers.CodeLens.TestTest do
153153
)
154154
end
155155

156+
describe "in large files" do
157+
setup do
158+
text = """
159+
defmodule ElixirLS.LanguageServer.DiagnosticsTest do
160+
alias ElixirLS.LanguageServer.Diagnostics
161+
use ExUnit.Case
162+
163+
describe "normalize/2" do
164+
test "extract the stacktrace from the message and format it" do
165+
root_path = Path.join(__DIR__, "fixtures/build_errors")
166+
file = Path.join(root_path, "lib/has_error.ex")
167+
position = 2
168+
169+
message = \"""
170+
** (CompileError) some message
171+
172+
Hint: Some hint
173+
(elixir 1.10.1) lib/macro.ex:304: Macro.pipe/3
174+
(stdlib 3.7.1) lists.erl:1263: :lists.foldl/3
175+
(elixir 1.10.1) expanding macro: Kernel.|>/2
176+
expanding macro: SomeModule.sigil_L/2
177+
lib/my_app/my_module.ex:10: MyApp.MyModule.render/1
178+
\"""
179+
180+
[diagnostic | _] =
181+
[build_diagnostic(message, file, position)]
182+
|> Diagnostics.normalize(root_path)
183+
184+
assert diagnostic.message == \"""
185+
(CompileError) some message
186+
187+
Hint: Some hint
188+
189+
Stacktrace:
190+
│ (elixir 1.10.1) lib/macro.ex:304: Macro.pipe/3
191+
│ (stdlib 3.7.1) lists.erl:1263: :lists.foldl/3
192+
│ (elixir 1.10.1) expanding macro: Kernel.|>/2
193+
│ expanding macro: SomeModule.sigil_L/2
194+
│ lib/my_app/my_module.ex:10: MyApp.MyModule.render/1
195+
\"""
196+
end
197+
198+
test "update file and position if file is present in the message" do
199+
root_path = Path.join(__DIR__, "fixtures/build_errors")
200+
file = Path.join(root_path, "lib/has_error.ex")
201+
position = 2
202+
203+
message = \"""
204+
** (CompileError) lib/has_error.ex:3: some message
205+
lib/my_app/my_module.ex:10: MyApp.MyModule.render/1
206+
\"""
207+
208+
[diagnostic | _] =
209+
[build_diagnostic(message, file, position)]
210+
|> Diagnostics.normalize(root_path)
211+
212+
assert diagnostic.message == \"""
213+
(CompileError) some message
214+
215+
Stacktrace:
216+
│ lib/my_app/my_module.ex:10: MyApp.MyModule.render/1
217+
\"""
218+
219+
assert diagnostic.position == 3
220+
end
221+
222+
test "update file and position if file is present in the message (umbrella)" do
223+
root_path = Path.join(__DIR__, "fixtures/umbrella")
224+
file = Path.join(root_path, "lib/file_to_be_replaced.ex")
225+
position = 3
226+
227+
message = \"""
228+
** (CompileError) lib/app2.ex:5: some message
229+
(elixir 1.10.1) lib/macro.ex:304: Macro.pipe/3
230+
lib/my_app/my_module.ex:10: MyApp.MyModule.render/1
231+
\"""
232+
233+
[diagnostic | _] =
234+
[build_diagnostic(message, file, position)]
235+
|> Diagnostics.normalize(root_path)
236+
237+
assert diagnostic.message =~ "(CompileError) some message"
238+
assert diagnostic.file =~ "umbrella/apps/app2/lib/app2.ex"
239+
assert diagnostic.position == 5
240+
end
241+
242+
test "don't update file nor position if file in message does not exist" do
243+
root_path = Path.join(__DIR__, "fixtures/build_errors_on_external_resource")
244+
file = Path.join(root_path, "lib/has_error.ex")
245+
position = 2
246+
247+
message = \"""
248+
** (CompileError) lib/non_existing.ex:3: some message
249+
lib/my_app/my_module.ex:10: MyApp.MyModule.render/1
250+
\"""
251+
252+
[diagnostic | _] =
253+
[build_diagnostic(message, file, position)]
254+
|> Diagnostics.normalize(root_path)
255+
256+
assert diagnostic.message == \"""
257+
(CompileError) lib/non_existing.ex:3: some message
258+
259+
Stacktrace:
260+
│ lib/my_app/my_module.ex:10: MyApp.MyModule.render/1
261+
\"""
262+
263+
assert diagnostic.position == 2
264+
end
265+
266+
defp build_diagnostic(message, file, position) do
267+
%Mix.Task.Compiler.Diagnostic{
268+
compiler_name: "Elixir",
269+
details: nil,
270+
file: file,
271+
message: message,
272+
position: position,
273+
severity: :error
274+
}
275+
end
276+
end
277+
end
278+
"""
279+
280+
%{text: text}
281+
end
282+
283+
test "returns module lens on the module declaration line", %{text: text} do
284+
uri = "file://project/file.ex"
285+
286+
{:ok, lenses} = CodeLens.Test.code_lens(uri, text)
287+
288+
assert Enum.member?(
289+
lenses,
290+
build_code_lens(0, :module, "/file.ex", %{
291+
"module" => ElixirLS.LanguageServer.DiagnosticsTest
292+
})
293+
)
294+
end
295+
296+
test "returns test lenses with describe info", %{text: text} do
297+
uri = "file://project/file.ex"
298+
299+
{:ok, lenses} = CodeLens.Test.code_lens(uri, text)
300+
301+
assert Enum.member?(
302+
lenses,
303+
build_code_lens(5, :test, "/file.ex", %{
304+
"testName" => "extract the stacktrace from the message and format it",
305+
"describe" => "normalize/2"
306+
})
307+
)
308+
end
309+
end
310+
156311
defp build_code_lens(line, target, file_path, args) do
157312
arguments =
158313
%{

0 commit comments

Comments
 (0)