Skip to content

Commit 207fbaa

Browse files
committed
add tests for some common crashes
1 parent e014b9e commit 207fbaa

File tree

3 files changed

+186
-3
lines changed

3 files changed

+186
-3
lines changed

apps/elixir_ls_debugger/lib/debugger/server.ex

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,19 @@ defmodule ElixirLS.Debugger.Server do
401401
0
402402

403403
_ ->
404-
Output.debugger_important("Task failed: " <> Exception.format_exit(reason))
404+
Output.debugger_important("Mix task failed")
405405

406406
1
407407
end
408408

409+
IO.puts(
410+
"Mix task exited with reason\n#{Exception.format_exit(reason)}\nreturning code #{exit_code}"
411+
)
412+
413+
Output.debugger_console(
414+
"Mix task exited with reason\n#{Exception.format_exit(reason)}\nreturning code #{exit_code}"
415+
)
416+
409417
Output.send_event("exited", %{"exitCode" => exit_code})
410418
Output.send_event("terminated", %{"restart" => false})
411419

@@ -577,9 +585,14 @@ defmodule ElixirLS.Debugger.Server do
577585

578586
{:DOWN, ^ref, :process, _pid, reason} ->
579587
if reason != :normal do
580-
Output.debugger_important("Initialization failed: " <> Exception.format_exit(reason))
588+
Output.debugger_important("Launch request failed")
589+
590+
Output.debugger_console(
591+
"Launch request failed with reason\n" <> Exception.format_exit(reason)
592+
)
581593

582-
Output.send_event("exited", %{"exitCode" => 1})
594+
exit_code = 1
595+
Output.send_event("exited", %{"exitCode" => exit_code})
583596
Output.send_event("terminated", %{"restart" => false})
584597
config
585598
else

apps/elixir_ls_debugger/test/debugger_test.exs

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ defmodule ElixirLS.Debugger.ServerTest do
7272
end)
7373
end
7474

75+
test "fails when not supported arguments passed", %{server: server} do
76+
in_fixture(__DIR__, "mix_project", fn ->
77+
Server.receive_packet(
78+
server,
79+
initialize_req(1, %{"clientID" => "some_id", "linesStartAt1" => false})
80+
)
81+
82+
assert_receive(
83+
error_response(
84+
_,
85+
1,
86+
"initialize",
87+
"invalidRequest",
88+
"0-based lines are not supported",
89+
%{}
90+
)
91+
)
92+
end)
93+
end
94+
7595
test "rejects requests when not initialized", %{server: server} do
7696
in_fixture(__DIR__, "mix_project", fn ->
7797
Server.receive_packet(
@@ -372,6 +392,151 @@ defmodule ElixirLS.Debugger.ServerTest do
372392
assert_receive(event(_, "terminated", %{"restart" => false}))
373393
end)
374394
end
395+
396+
@tag :fixture
397+
test "launch mix task that raises", %{server: server} do
398+
in_fixture(__DIR__, "mix_project", fn ->
399+
Server.receive_packet(
400+
server,
401+
initialize_req(1, %{
402+
"supportsVariablePaging" => true,
403+
"supportsVariableType" => true
404+
})
405+
)
406+
407+
assert_receive(response(_, 1, "initialize", %{"supportsConfigurationDoneRequest" => true}))
408+
409+
Server.receive_packet(
410+
server,
411+
launch_req(2, %{
412+
"request" => "launch",
413+
"type" => "mix_task",
414+
"noDebug" => true,
415+
"task" => "run",
416+
"taskArgs" => ["-e", "MixProject.Crash.fun_that_raises()"],
417+
"projectDir" => File.cwd!()
418+
})
419+
)
420+
421+
assert_receive(response(_, 2, "launch", %{}), 5000)
422+
assert_receive(event(_, "initialized", %{}))
423+
424+
Server.receive_packet(server, request(5, "configurationDone", %{}))
425+
assert_receive(response(_, 5, "configurationDone", %{}))
426+
427+
assert_receive event(_, "output", %{
428+
"category" => "console",
429+
"output" =>
430+
"Mix task exited with reason\nan exception was raised:\n ** (RuntimeError) foo" <>
431+
_
432+
}),
433+
3000
434+
435+
assert_receive(
436+
event(_, "exited", %{
437+
"exitCode" => 1
438+
}),
439+
3000
440+
)
441+
442+
assert_receive(event(_, "terminated", %{"restart" => false}))
443+
end)
444+
end
445+
446+
@tag :fixture
447+
test "launch mix task that fails to initialze", %{server: server} do
448+
in_fixture(__DIR__, "mix_project", fn ->
449+
Server.receive_packet(
450+
server,
451+
initialize_req(1, %{
452+
"supportsVariablePaging" => true,
453+
"supportsVariableType" => true
454+
})
455+
)
456+
457+
assert_receive(response(_, 1, "initialize", %{"supportsConfigurationDoneRequest" => true}))
458+
459+
Server.receive_packet(
460+
server,
461+
launch_req(2, %{
462+
"request" => "launch",
463+
"type" => "mix_task",
464+
"noDebug" => true,
465+
"task" => "ru/n",
466+
"taskArgs" => [],
467+
"projectDir" => File.cwd!()
468+
})
469+
)
470+
471+
assert_receive(response(_, 2, "launch", %{}), 5000)
472+
refute_receive(event(_, "initialized", %{}))
473+
474+
assert_receive event(_, "output", %{
475+
"category" => "console",
476+
"output" =>
477+
"Launch request failed with reason\nan exception was raised:\n ** (Mix.NoTaskError)" <>
478+
_
479+
}),
480+
3000
481+
482+
assert_receive(
483+
event(_, "exited", %{
484+
"exitCode" => 1
485+
}),
486+
3000
487+
)
488+
489+
assert_receive(event(_, "terminated", %{"restart" => false}))
490+
end)
491+
end
492+
493+
@tag :fixture
494+
test "launch invalid mix task", %{server: server} do
495+
in_fixture(__DIR__, "mix_project", fn ->
496+
Server.receive_packet(
497+
server,
498+
initialize_req(1, %{
499+
"supportsVariablePaging" => true,
500+
"supportsVariableType" => true
501+
})
502+
)
503+
504+
assert_receive(response(_, 1, "initialize", %{"supportsConfigurationDoneRequest" => true}))
505+
506+
Server.receive_packet(
507+
server,
508+
launch_req(2, %{
509+
"request" => "launch",
510+
"type" => "mix_task",
511+
"noDebug" => true,
512+
"task" => "nonexisting",
513+
"taskArgs" => [],
514+
"projectDir" => File.cwd!()
515+
})
516+
)
517+
518+
assert_receive(response(_, 2, "launch", %{}), 5000)
519+
assert_receive(event(_, "initialized", %{}))
520+
521+
Server.receive_packet(server, request(5, "configurationDone", %{}))
522+
assert_receive(response(_, 5, "configurationDone", %{}))
523+
524+
assert_receive event(_, "output", %{
525+
"category" => "console",
526+
"output" =>
527+
"Mix task exited with reason\nan exception was raised:\n ** (Mix.NoTaskError) The task \"nonexisting\" could not be found" <>
528+
_
529+
}),
530+
3000
531+
532+
assert_receive(
533+
event(_, "exited", %{
534+
"exitCode" => 1
535+
}),
536+
3000
537+
)
538+
539+
assert_receive(event(_, "terminated", %{"restart" => false}))
375540
end)
376541
end
377542

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule MixProject.Crash do
2+
def fun_that_raises() do
3+
raise "foo"
4+
end
5+
end

0 commit comments

Comments
 (0)