Skip to content

Commit e381804

Browse files
committed
Reintroduce full server testing with different approach
Now we start manually all application children instead of ensuring the application has started
1 parent fe14474 commit e381804

File tree

2 files changed

+82
-19
lines changed

2 files changed

+82
-19
lines changed

apps/expert/lib/expert.ex

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ defmodule Expert do
5959
Task.Supervisor.start_child(:expert_task_queue, fn ->
6060
config = state.configuration
6161

62-
start_message = "Starting project at uri #{config.project.root_uri}"
63-
GenLSP.info(lsp, start_message)
64-
Logger.info(start_message)
62+
log_info("Starting project at uri #{config.project.root_uri}")
6563

6664
start_result = Project.Supervisor.start(config.project)
6765

@@ -205,39 +203,32 @@ defmodule Expert do
205203

206204
def handle_info({:engine_initialized, {:error, reason}}, lsp) do
207205
error_message = initialization_error_message(reason)
208-
error(error_message)
206+
log_error(error_message)
209207

210208
{:noreply, lsp}
211209
end
212210

213-
def info(message) do
211+
def log_info(message) do
214212
Logger.info(message)
215213

216-
log_show(message, Enumerations.MessageType.info())
214+
GenLSP.log(get_lsp(), message)
217215
end
218216

219-
def error(message) do
217+
# When logging errors we also notify the client to display the message
218+
def log_error(message) do
220219
Logger.error(message)
221220

222-
log_show(message, Enumerations.MessageType.error())
223-
end
224-
225-
defp log_show(message, log_level) do
221+
log_level = Enumerations.MessageType.error()
226222
lsp = get_lsp()
227223

224+
GenLSP.error(lsp, message)
225+
228226
GenLSP.notify(lsp, %GenLSP.Notifications.WindowShowMessage{
229227
params: %GenLSP.Structures.ShowMessageParams{
230228
type: log_level,
231229
message: message
232230
}
233231
})
234-
235-
GenLSP.notify(lsp, %GenLSP.Notifications.WindowLogMessage{
236-
params: %GenLSP.Structures.LogMessageParams{
237-
type: log_level,
238-
message: message
239-
}
240-
})
241232
end
242233

243234
defp apply_to_state(%State{} = state, %{} = request_or_notification) do

apps/expert/test/expert_test.exs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,83 @@
11
defmodule Expert.ExpertTest do
2-
use ExUnit.Case, async: true
2+
alias Forge.Test.Fixtures
3+
4+
use ExUnit.Case, async: false
5+
use Forge.Test.EventualAssertions
36
use Patch
47

58
require GenLSP.Test
69

710
import Expert.Test.Protocol.TransportSupport
811

12+
describe "server testing" do
13+
defp buffer_opts do
14+
[communication: {GenLSP.Communication.TCP, [port: 0]}]
15+
end
16+
17+
defp start_application_children do
18+
pids =
19+
for child_spec <- Expert.Application.children(buffer: buffer_opts()) do
20+
start_supervised!(child_spec)
21+
end
22+
23+
on_exit(fn ->
24+
# NOTE: The test hangs for some reason without manually exiting
25+
for pid <- pids do
26+
Process.exit(pid, :normal)
27+
end
28+
end)
29+
end
30+
31+
setup do
32+
start_application_children()
33+
34+
comm_state = GenLSP.Buffer.comm_state(Expert.Buffer)
35+
36+
{:ok, port} = :inet.port(comm_state.lsocket)
37+
%{lsp: lsp} = :sys.get_state(Expert.Buffer)
38+
39+
expert = %{lsp: lsp, port: port}
40+
client = GenLSP.Test.client(expert)
41+
42+
[expert: expert, client: client]
43+
end
44+
45+
test "replies to initialize with expert info and capabilities", %{client: client} do
46+
id = System.unique_integer([:positive])
47+
48+
project = Fixtures.project()
49+
50+
root_uri = project.root_uri
51+
root_path = Forge.Project.root_path(project)
52+
53+
assert :ok ==
54+
GenLSP.Test.request(client, %{
55+
"id" => id,
56+
"jsonrpc" => "2.0",
57+
"method" => "initialize",
58+
"params" => %{
59+
"rootPath" => root_path,
60+
"rootUri" => root_uri,
61+
"capabilities" => %{},
62+
"workspaceFolders" => [
63+
%{
64+
uri: root_uri,
65+
name: root_path
66+
}
67+
]
68+
}
69+
})
70+
71+
GenLSP.Test.assert_result(^id, result, 500)
72+
73+
{:ok, initialize_result} =
74+
GenLSP.Requests.Initialize.result()
75+
|> Schematic.dump(Expert.State.initialize_result())
76+
77+
assert result == initialize_result
78+
end
79+
end
80+
981
test "sends an error message on engine initialization error" do
1082
with_patched_transport()
1183

0 commit comments

Comments
 (0)