Skip to content

Commit 99c7d84

Browse files
committed
Update the r helper
1 parent 893a52a commit 99c7d84

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

lib/iex/lib/iex/helpers.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ defmodule IEx.Helpers do
233233
in the current IEx session.
234234
"""
235235
def r do
236-
Enum.map iex_reloaded, r(&1)
236+
List.flatten(Enum.map(iex_reloaded, do_r(&1)))
237237
end
238238

239239
@doc """
@@ -243,9 +243,16 @@ defmodule IEx.Helpers do
243243
are recompiled and reloaded.
244244
"""
245245
def r(module) do
246+
case do_r(module) do
247+
mods when is_list(mods) -> { module, mods }
248+
other -> other
249+
end
250+
end
251+
252+
defp do_r(module) do
246253
if source = source(module) do
247254
Process.put(:iex_reloaded, :ordsets.add_element(module, iex_reloaded))
248-
{ module, Code.load_file source }
255+
Enum.map(Code.load_file(source), fn {name, _} -> name end)
249256
else
250257
:nosource
251258
end
@@ -257,6 +264,7 @@ defmodule IEx.Helpers do
257264
"""
258265
def l(module) do
259266
:code.purge(module)
267+
# FIXME: shouldn't we use Code.load_file here?
260268
:code.load_file(module)
261269
end
262270

lib/iex/test/iex/helpers_test.exs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,15 @@ defmodule IEx.HelpersTest do
155155
end) >= 2
156156
end
157157

158+
defp purge(mod) do
159+
true = :code.delete mod
160+
:code.purge mod
161+
end
162+
158163
defp cleanup_modules(mods) do
159164
Enum.each mods, fn mod ->
160165
File.rm! "#{mod}.beam"
161-
true = :code.delete mod
162-
:code.purge mod
166+
purge mod
163167
end
164168
end
165169

@@ -183,7 +187,7 @@ defmodule IEx.HelpersTest do
183187
Sample.run
184188
end
185189

186-
filename = "test-module-code.ex"
190+
filename = "sample.ex"
187191
with_file filename, test_module_code, fn ->
188192
assert c(filename) == [Sample]
189193
assert Sample.run == :run
@@ -197,7 +201,7 @@ defmodule IEx.HelpersTest do
197201
Sample.run
198202
end
199203

200-
filename = "test-module-code.ex"
204+
filename = "sample.ex"
201205
with_file filename, test_module_code <> "\n" <> another_test_module, fn ->
202206
assert c(filename) |> Enum.sort == [Sample,Sample2]
203207
assert Sample.run == :run
@@ -212,7 +216,7 @@ defmodule IEx.HelpersTest do
212216
Sample.run
213217
end
214218

215-
filenames = ["test-module-code-1.ex", "test-module-code-2.ex"]
219+
filenames = ["sample1.ex", "sample2.ex"]
216220
with_file filenames, [test_module_code, another_test_module], fn ->
217221
assert c(filenames) |> Enum.sort == [Sample,Sample2]
218222
assert Sample.run == :run
@@ -229,14 +233,14 @@ defmodule IEx.HelpersTest do
229233

230234
assert l(:non_existent_module) == {:error,:nofile}
231235

232-
filename = "test-module-code.ex"
236+
filename = "sample.ex"
233237
with_file filename, test_module_code, fn ->
234238
assert c(filename) == [Sample]
235239
assert Sample.run == :run
236240

237241
File.write! filename, "defmodule Sample do end"
238242
# FIXME: is there another way to compile a file without loading its module?
239-
System.cmd "elixirc test-module-code.ex"
243+
System.cmd "../../bin/elixirc sample.ex"
240244

241245
assert l(Sample) == {:module, Sample}
242246
assert_raise UndefinedFunctionError, "undefined function: Sample.run/0", fn ->
@@ -249,25 +253,38 @@ defmodule IEx.HelpersTest do
249253
cleanup_modules([Sample])
250254
end
251255

252-
test "r helper" do
253-
assert_raise UndefinedFunctionError, "undefined function: Sample.run/0", fn ->
254-
Sample.run
255-
end
256-
256+
test "r helper basic" do
257257
assert r == []
258-
assert r(Kernel) == :nosource
259258
assert_raise UndefinedFunctionError, "undefined function: :non_existent_module.module_info/1", fn ->
260259
r :non_existent_module
261260
end
262261

263-
filename = "test-module-code.ex"
264-
with_file filename, test_module_code, fn ->
265-
assert c(filename) == [Sample]
266-
assert Sample.run == :run
267-
# FIXME: `r Sample` returns :nosource
268-
assert r(Sample) == [Sample]
262+
# There is no source file for the module defined in IEx
263+
assert ":ok\n** (Code.LoadError) could not load" <> _
264+
= capture_iex("{:module, Sample, _, {:run,0}} = #{String.strip test_module_code}; :ok\nr Sample")
265+
after
266+
purge Sample
267+
end
269268

270-
assert r == [Sample]
269+
test "r helper" do
270+
assert_raise UndefinedFunctionError, "undefined function: Sample.run/0", fn ->
271+
Sample.run
272+
end
273+
274+
filename = "sample.ex"
275+
with_file filename, test_module_code, fn ->
276+
assert capture_io(:stdio, fn ->
277+
assert c(filename) == [Sample]
278+
assert Sample.run == :run
279+
280+
File.write! filename, "defmodule Sample do end"
281+
assert {Sample, [Sample]} = r(Sample)
282+
assert_raise UndefinedFunctionError, "undefined function: Sample.run/0", fn ->
283+
Sample.run
284+
end
285+
286+
assert [Sample] = r()
287+
end) =~ %r"^.+?sample\.ex:1: redefining module Sample\n.+?sample\.ex:1: redefining module Sample\n$"
271288
end
272289
after
273290
# Clean up old version produced by the r helper

lib/iex/test/test_helper.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ defmodule IEx.Case do
5858
IEx.Options.set(opt, value)
5959
end
6060

61-
ExUnit.CaptureIO.capture_io(input, fn ->
61+
ExUnit.CaptureIO.capture_io([input: input, capture_prompt: false], fn ->
6262
IEx.Server.start(IEx.boot_config(dot_iex_path: dot_iex_path))
6363
end) |> strip_iex
6464
end

0 commit comments

Comments
 (0)