Skip to content

Commit

Permalink
Fix test code lens when umbrella app names have the same prefix. (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlindingDark authored Nov 6, 2022
1 parent badf5dc commit 3c8a6e0
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 1 deletion.
9 changes: 8 additions & 1 deletion apps/language_server/lib/language_server/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ defmodule ElixirLS.LanguageServer.Server do
file_path = SourceFile.Path.from_uri(uri)

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

defp under_app?(file_path, project_dir, app_path) do
file_path_list = file_path |> Path.relative_to(project_dir) |> Path.split()
app_path_list = app_path |> Path.split()

List.starts_with?(file_path_list, app_path_list)
end

# Build

defp trigger_build(state = %__MODULE__{project_dir: project_dir}) do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule App.Mixfile do
use Mix.Project

def project do
[
app: :app,
version: "0.1.0"
]
end

def application do
[]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule App.UmbrellaTestCodeLensTest do
use ExUnit.Case

test "fixture test" do
assert true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule App1.Mixfile do
use Mix.Project

def project do
[
app: :app1,
version: "0.1.0"
]
end

def application do
[]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule App1.UmbrellaTestCodeLensTest do
use ExUnit.Case

test "fixture test" do
assert true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule UmbrellaTestCodeLens.Mixfile do
use Mix.Project

def project do
[apps_path: "apps"]
end
end
69 changes: 69 additions & 0 deletions apps/language_server/test/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,75 @@ defmodule ElixirLS.LanguageServer.ServerTest do
end)
end

@tag :fixture
test "returns code lenses for runnable tests in umbrella apps",
%{
server: server
} do
in_fixture(__DIR__, "umbrella_test_code_lens", fn ->
file_path = "apps/app1/test/fixture_custom_test.exs"
file_uri = SourceFile.Path.to_uri(file_path)
file_absolute_path = SourceFile.Path.from_uri(file_uri)
text = File.read!(file_path)
project_dir = SourceFile.Path.from_uri("#{root_uri()}/apps/app1")

initialize(server)

Server.receive_packet(
server,
did_change_configuration(%{"elixirLS" => %{"enableTestLenses" => true}})
)

Server.receive_packet(server, did_open(file_uri, "elixir", 1, text))

wait_until_compiled(server)

Server.receive_packet(
server,
code_lens_req(4, file_uri)
)

resp = assert_receive(%{"id" => 4}, 5000)

assert response(4, [
%{
"command" => %{
"arguments" => [
%{
"filePath" => ^file_absolute_path,
"testName" => "fixture test",
"projectDir" => ^project_dir
}
],
"command" => "elixir.lens.test.run",
"title" => "Run test"
},
"range" => %{
"end" => %{"character" => 0, "line" => 3},
"start" => %{"character" => 0, "line" => 3}
}
},
%{
"command" => %{
"arguments" => [
%{
"filePath" => ^file_absolute_path,
"module" => "Elixir.App1.UmbrellaTestCodeLensTest",
"projectDir" => ^project_dir
}
],
"command" => "elixir.lens.test.run",
"title" => "Run tests in module"
},
"range" => %{
"end" => %{"character" => 0, "line" => 0},
"start" => %{"character" => 0, "line" => 0}
}
}
]) = resp
end)
end

@tag :fixture
test "does not return code lenses for runnable tests when test lenses settings is not set", %{
server: server
Expand Down

0 comments on commit 3c8a6e0

Please sign in to comment.