Skip to content

Commit d450189

Browse files
author
José Valim
committed
Add import_file_if_available, closes #4878
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
1 parent 744859e commit d450189

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/iex/lib/iex/helpers.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,15 @@ defmodule IEx.Helpers do
533533
end
534534
end
535535

536+
@doc """
537+
Similar to `import_file` but only imports it if it is available.
538+
"""
539+
defmacro import_file_if_available(path) do
540+
quote do
541+
import_file(unquote(path), optional: true)
542+
end
543+
end
544+
536545
@doc """
537546
Evaluates the contents of the file at `path` as if it were directly typed into
538547
the shell.

lib/iex/test/iex/helpers_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ defmodule IEx.HelpersTest do
257257
end
258258

259259
test "import_file when the file is missing" do
260-
assert "nil" == capture_iex("import_file \"nonexistent\", optional: true")
261-
262260
failing = capture_iex("import_file \"nonexistent\"")
263261
assert "** (File.Error) could not read file" <> _ = failing
264262
assert failing =~ "no such file or directory"
263+
264+
assert "nil" == capture_iex("import_file_if_available \"nonexistent\"")
265265
end
266266

267267
test "import_if_available helper" do

0 commit comments

Comments
 (0)