Skip to content

Commit 3a51d1f

Browse files
Fix toolchain tests tmp_dir tag; propagate .tool-versions read errors
ExUnit only injects context.tmp_dir when :tmp_dir is tagged; add @moduletag so Desktop.ToolchainTest setup runs. Stop mapping all File.read failures to :enoent; surface real I/O errors via Desktop.Toolchain.verify with a clear message for the Mix task. Co-authored-by: Dominic Letz <dominicletz@users.noreply.github.com>
1 parent 52bda17 commit 3a51d1f

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

lib/desktop/tool_versions.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ defmodule Desktop.ToolVersions do
3434

3535
@doc """
3636
Reads `.tool-versions` from `directory` and parses it. Returns `{:ok, map}` or
37-
`{:error, :enoent}` when the file is missing.
37+
`{:error, reason}` from `File.read/1` (typically `:enoent` when the file is missing).
3838
"""
39-
@spec read_from_dir(String.t()) :: {:ok, map()} | {:error, :enoent}
39+
@spec read_from_dir(String.t()) :: {:ok, map()} | {:error, :enoent | File.posix() | :badarg}
4040
def read_from_dir(directory) do
4141
path = Path.join(directory, ".tool-versions")
4242

4343
case File.read(path) do
4444
{:ok, body} -> {:ok, parse(body)}
45-
{:error, :enoent} -> {:error, :enoent}
46-
{:error, _} -> {:error, :enoent}
45+
{:error, reason} -> {:error, reason}
4746
end
4847
end
4948
end

lib/desktop/toolchain.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ defmodule Desktop.Toolchain do
2222
{:error, :enoent} ->
2323
{:ok, :no_tool_versions}
2424

25+
{:error, reason} ->
26+
{:error, [read_tool_versions_failed_message(reason)]}
27+
2528
{:ok, tools} ->
2629
errors =
2730
[]
@@ -75,6 +78,14 @@ defmodule Desktop.Toolchain do
7578
end
7679
end
7780

81+
defp read_tool_versions_failed_message(reason) when is_atom(reason) do
82+
"Could not read .tool-versions: #{:file.format_error(reason)}"
83+
end
84+
85+
defp read_tool_versions_failed_message(reason) do
86+
"Could not read .tool-versions: #{inspect(reason)}"
87+
end
88+
7889
defp otp_major_from_declared(declared) do
7990
declared
8091
|> String.split(".", parts: 2)

test/desktop/toolchain_test.exs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule Desktop.ToolchainTest do
22
use ExUnit.Case, async: true
33

4+
@moduletag :tmp_dir
5+
46
setup context do
57
root = Path.join(context.tmp_dir, "proj")
68
File.mkdir_p!(root)
@@ -23,14 +25,18 @@ defmodule Desktop.ToolchainTest do
2325
test "error when OTP major mismatches", %{root: root} do
2426
File.write!(Path.join(root, ".tool-versions"), "erlang 26.0.1\n")
2527

26-
assert {:error, [msg]} = Desktop.Toolchain.verify(root, otp_release: "25", elixir_version: "1.19.1")
28+
assert {:error, [msg]} =
29+
Desktop.Toolchain.verify(root, otp_release: "25", elixir_version: "1.19.1")
30+
2731
assert msg =~ "OTP"
2832
end
2933

3034
test "error when Elixir semver mismatches", %{root: root} do
3135
File.write!(Path.join(root, ".tool-versions"), "elixir 1.19.1-otp-26\n")
3236

33-
assert {:error, [msg]} = Desktop.Toolchain.verify(root, otp_release: "26", elixir_version: "1.18.0")
37+
assert {:error, [msg]} =
38+
Desktop.Toolchain.verify(root, otp_release: "26", elixir_version: "1.18.0")
39+
3440
assert msg =~ "Elixir mismatch"
3541
end
3642
end

0 commit comments

Comments
 (0)