diff --git a/lib/iex/lib/iex/helpers.ex b/lib/iex/lib/iex/helpers.ex index cf470754feb..493f046195b 100644 --- a/lib/iex/lib/iex/helpers.ex +++ b/lib/iex/lib/iex/helpers.ex @@ -1080,11 +1080,17 @@ defmodule IEx.Helpers do {:error, :enoent} -> IO.puts(IEx.color(:eval_error, "No such file or directory #{path}")) - {:error, :enotdir} -> - IO.puts(IEx.color(:eval_info, Path.absname(path))) - {:error, reason} -> - IO.puts(IEx.color(:eval_error, :file.format_error(reason))) + cond do + File.exists?(path) and not File.dir?(path) -> + IO.puts(IEx.color(:eval_info, Path.absname(path))) + + reason == :enotdir -> + IO.puts(IEx.color(:eval_error, "No such file or directory #{path}")) + + true -> + IO.puts(IEx.color(:eval_error, :file.format_error(reason))) + end end dont_display_result() diff --git a/lib/iex/test/iex/helpers_test.exs b/lib/iex/test/iex/helpers_test.exs index 77f35f5529e..170073edaeb 100644 --- a/lib/iex/test/iex/helpers_test.exs +++ b/lib/iex/test/iex/helpers_test.exs @@ -1200,6 +1200,28 @@ defmodule IEx.HelpersTest do test "lists the given directory" do assert capture_io(fn -> ls("~") end) == capture_io(fn -> ls(System.user_home()) end) end + + test "returns an existing file" do + File.cd!(iex_path(), fn -> + assert capture_io(fn -> ls("mix.exs") end) == Path.join(iex_path(), "mix.exs") <> "\n" + end) + end + + test "prints an error if directory doesn't exist" do + File.cd!(iex_path(), fn -> + assert capture_io(fn -> ls("unknown_dir") end) == + "No such file or directory unknown_dir\n" + end) + end + + test "prints an error if part of the path is not a dir (enotdir)" do + File.cd!(iex_path(), fn -> + path = Path.join("mix.exs", "foo") + + assert capture_io(fn -> ls(path) end) == + "No such file or directory #{path}\n" + end) + end end describe "exports" do