Skip to content

Commit 3c8a6e0

Browse files
authored
Fix test code lens when umbrella app names have the same prefix. (#759)
1 parent badf5dc commit 3c8a6e0

File tree

7 files changed

+126
-1
lines changed

7 files changed

+126
-1
lines changed

apps/language_server/lib/language_server/server.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ defmodule ElixirLS.LanguageServer.Server do
869869
file_path = SourceFile.Path.from_uri(uri)
870870

871871
Mix.Project.apps_paths()
872-
|> Enum.find(fn {_app, app_path} -> String.contains?(file_path, app_path) end)
872+
|> Enum.find(fn {_app, app_path} -> under_app?(file_path, project_dir, app_path) end)
873873
|> case do
874874
nil ->
875875
{:ok, []}
@@ -929,6 +929,13 @@ defmodule ElixirLS.LanguageServer.Server do
929929
|> Enum.any?(&(&1 == file_path))
930930
end
931931

932+
defp under_app?(file_path, project_dir, app_path) do
933+
file_path_list = file_path |> Path.relative_to(project_dir) |> Path.split()
934+
app_path_list = app_path |> Path.split()
935+
936+
List.starts_with?(file_path_list, app_path_list)
937+
end
938+
932939
# Build
933940

934941
defp trigger_build(state = %__MODULE__{project_dir: project_dir}) do
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule App.Mixfile do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :app,
7+
version: "0.1.0"
8+
]
9+
end
10+
11+
def application do
12+
[]
13+
end
14+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule App.UmbrellaTestCodeLensTest do
2+
use ExUnit.Case
3+
4+
test "fixture test" do
5+
assert true
6+
end
7+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule App1.Mixfile do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :app1,
7+
version: "0.1.0"
8+
]
9+
end
10+
11+
def application do
12+
[]
13+
end
14+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule App1.UmbrellaTestCodeLensTest do
2+
use ExUnit.Case
3+
4+
test "fixture test" do
5+
assert true
6+
end
7+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule UmbrellaTestCodeLens.Mixfile do
2+
use Mix.Project
3+
4+
def project do
5+
[apps_path: "apps"]
6+
end
7+
end

apps/language_server/test/server_test.exs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,75 @@ defmodule ElixirLS.LanguageServer.ServerTest do
12871287
end)
12881288
end
12891289

1290+
@tag :fixture
1291+
test "returns code lenses for runnable tests in umbrella apps",
1292+
%{
1293+
server: server
1294+
} do
1295+
in_fixture(__DIR__, "umbrella_test_code_lens", fn ->
1296+
file_path = "apps/app1/test/fixture_custom_test.exs"
1297+
file_uri = SourceFile.Path.to_uri(file_path)
1298+
file_absolute_path = SourceFile.Path.from_uri(file_uri)
1299+
text = File.read!(file_path)
1300+
project_dir = SourceFile.Path.from_uri("#{root_uri()}/apps/app1")
1301+
1302+
initialize(server)
1303+
1304+
Server.receive_packet(
1305+
server,
1306+
did_change_configuration(%{"elixirLS" => %{"enableTestLenses" => true}})
1307+
)
1308+
1309+
Server.receive_packet(server, did_open(file_uri, "elixir", 1, text))
1310+
1311+
wait_until_compiled(server)
1312+
1313+
Server.receive_packet(
1314+
server,
1315+
code_lens_req(4, file_uri)
1316+
)
1317+
1318+
resp = assert_receive(%{"id" => 4}, 5000)
1319+
1320+
assert response(4, [
1321+
%{
1322+
"command" => %{
1323+
"arguments" => [
1324+
%{
1325+
"filePath" => ^file_absolute_path,
1326+
"testName" => "fixture test",
1327+
"projectDir" => ^project_dir
1328+
}
1329+
],
1330+
"command" => "elixir.lens.test.run",
1331+
"title" => "Run test"
1332+
},
1333+
"range" => %{
1334+
"end" => %{"character" => 0, "line" => 3},
1335+
"start" => %{"character" => 0, "line" => 3}
1336+
}
1337+
},
1338+
%{
1339+
"command" => %{
1340+
"arguments" => [
1341+
%{
1342+
"filePath" => ^file_absolute_path,
1343+
"module" => "Elixir.App1.UmbrellaTestCodeLensTest",
1344+
"projectDir" => ^project_dir
1345+
}
1346+
],
1347+
"command" => "elixir.lens.test.run",
1348+
"title" => "Run tests in module"
1349+
},
1350+
"range" => %{
1351+
"end" => %{"character" => 0, "line" => 0},
1352+
"start" => %{"character" => 0, "line" => 0}
1353+
}
1354+
}
1355+
]) = resp
1356+
end)
1357+
end
1358+
12901359
@tag :fixture
12911360
test "does not return code lenses for runnable tests when test lenses settings is not set", %{
12921361
server: server

0 commit comments

Comments
 (0)