Skip to content

Commit e25f2b1

Browse files
committed
feat: return compiler diagnostics from reload
1 parent 27612fd commit e25f2b1

File tree

7 files changed

+133
-14
lines changed

7 files changed

+133
-14
lines changed

.github/workflows/ci.yaml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: CI
2+
on:
3+
pull_request:
4+
push:
5+
branches: main
6+
7+
jobs:
8+
tests:
9+
runs-on: ubuntu-latest
10+
name: Test (${{matrix.elixir}}/${{matrix.otp}})
11+
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
elixir: [1.17.x, 1.18.x]
16+
otp: [26.x, 27.x]
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
- uses: erlef/setup-beam@v1
21+
id: install
22+
with:
23+
otp-version: ${{matrix.otp}}
24+
elixir-version: ${{matrix.elixir}}
25+
- uses: actions/cache@v4
26+
id: cache
27+
with:
28+
path: |
29+
deps
30+
key: ${{ runner.os }}-mix-${{steps.install.outputs.otp-version}}-${{steps.install.outputs.elixir-version}}-${{ hashFiles('**/mix.lock') }}
31+
restore-keys: |
32+
${{ runner.os }}-mix-${{steps.install.outputs.otp-version}}-${{steps.install.outputs.elixir-version}}-
33+
34+
- name: Install Dependencies
35+
if: steps.cache.outputs.cache-hit != 'true'
36+
run: mix deps.get
37+
38+
- name: Run Tests
39+
run: mix test
40+
41+
formatter:
42+
runs-on: ubuntu-latest
43+
name: Formatter (${{matrix.elixir}}/${{matrix.otp}})
44+
45+
strategy:
46+
matrix:
47+
otp: [27.x]
48+
elixir: [1.17.x]
49+
50+
steps:
51+
- uses: actions/checkout@v4
52+
- uses: erlef/setup-beam@v1
53+
id: install
54+
with:
55+
otp-version: ${{matrix.otp}}
56+
elixir-version: ${{matrix.elixir}}
57+
- uses: actions/cache@v4
58+
id: cache
59+
with:
60+
path: |
61+
deps
62+
_build
63+
key: ${{ runner.os }}-mix-${{steps.install.outputs.otp-version}}-${{steps.install.outputs.elixir-version}}-${{ hashFiles('**/mix.lock') }}
64+
restore-keys: |
65+
${{ runner.os }}-mix-${{steps.install.outputs.otp-version}}-${{steps.install.outputs.elixir-version}}-
66+
67+
- name: Install Dependencies
68+
if: steps.cache.outputs.cache-hit != 'true'
69+
run: mix deps.get
70+
71+
- name: Run Formatter
72+
run: mix format --check-formatted

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ web_dev_utils-*.tar
2424

2525
# Temporary files, for example, from tests.
2626
/tmp/
27+
28+
/test/support/editable.ex

lib/web_dev_utils/code_reloader.ex

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ defmodule WebDevUtils.CodeReloader do
2525
{:ok, :nostate}
2626
end
2727

28-
def handle_call(:reload, from, state) do
29-
froms = all_waiting([from])
30-
mix_compile(Code.ensure_loaded(Mix.Task))
31-
Enum.each(froms, &GenServer.reply(&1, :ok))
28+
def handle_call(:reload, _from, state) do
29+
froms = all_waiting([])
30+
result = mix_compile(Code.ensure_loaded(Mix.Task))
31+
Enum.each(froms, &GenServer.reply(&1, result))
3232

33-
{:noreply, state}
33+
{:reply, result, state}
3434
end
3535

3636
defp all_waiting(acc) do
@@ -43,10 +43,11 @@ defmodule WebDevUtils.CodeReloader do
4343

4444
defp mix_compile({:error, _reason}) do
4545
Logger.error("Could not find Mix")
46+
:error
4647
end
4748

4849
defp mix_compile({:module, Mix.Task}) do
4950
Mix.Task.reenable("compile.elixir")
50-
Mix.Task.run("compile.elixir")
51+
Mix.Task.run("compile.elixir", ["--return-errors"])
5152
end
5253
end

mix.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule WebDevUtils.MixProject do
1010
"Library to enable awesome local development for websites and web applications",
1111
version: "0.2.0",
1212
elixir: "~> 1.14",
13+
elixirc_paths: elixirc_paths(Mix.env()),
1314
start_permanent: Mix.env() == :prod,
1415
package: package(),
1516
deps: deps()
@@ -23,6 +24,10 @@ defmodule WebDevUtils.MixProject do
2324
]
2425
end
2526

27+
# Specifies which paths to compile per environment.
28+
defp elixirc_paths(:test), do: ["lib", "test/support"]
29+
defp elixirc_paths(_), do: ["lib"]
30+
2631
# Run "mix help deps" to learn about dependencies.
2732
defp deps do
2833
[

test/support/.keep

Whitespace-only changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
defmodule WebDevUtils.CodeReloaderTest do
2+
use ExUnit.Case, async: true
3+
import ExUnit.CaptureIO
4+
5+
alias WebDevUtils.CodeReloader
6+
7+
setup do
8+
on_exit(fn ->
9+
File.rm("test/support/editable.ex")
10+
end)
11+
12+
:ok
13+
end
14+
15+
test "noops when nothing has changed" do
16+
start_supervised!(CodeReloader)
17+
18+
assert {_, []} = CodeReloader.reload()
19+
assert {:noop, []} == CodeReloader.reload()
20+
end
21+
22+
test "recompiles code" do
23+
File.write!("test/support/editable.ex", """
24+
defmodule Editable do
25+
end
26+
""")
27+
28+
start_supervised!(CodeReloader)
29+
30+
capture_io(:stderr, fn ->
31+
assert {:ok, []} == CodeReloader.reload()
32+
end)
33+
end
34+
35+
test "recompiles code and something failes" do
36+
File.write!("test/support/editable.ex", """
37+
defmodule Editable d
38+
end
39+
""")
40+
41+
start_supervised!(CodeReloader)
42+
43+
capture_io(:stderr, fn ->
44+
assert {:error, [%Mix.Task.Compiler.Diagnostic{}]} = CodeReloader.reload()
45+
end)
46+
end
47+
end

test/web_dev_utils_test.exs

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)