Skip to content

Commit 893a52a

Browse files
committed
Clean up test code for module helpers
1 parent a8d2e45 commit 893a52a

File tree

1 file changed

+80
-76
lines changed

1 file changed

+80
-76
lines changed

lib/iex/test/iex/helpers_test.exs

Lines changed: 80 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -155,97 +155,103 @@ defmodule IEx.HelpersTest do
155155
end) >= 2
156156
end
157157

158+
defp cleanup_modules(mods) do
159+
Enum.each mods, fn mod ->
160+
File.rm! "#{mod}.beam"
161+
true = :code.delete mod
162+
:code.purge mod
163+
end
164+
end
165+
166+
defp with_file(names, codes, fun) when is_list(names) and is_list(codes) do
167+
Enum.each Enum.zip(names, codes), fn { name, code } ->
168+
File.write! name, code
169+
end
170+
try do
171+
fun.()
172+
after
173+
Enum.each names, File.rm(&1)
174+
end
175+
end
176+
177+
defp with_file(name, code, fun) do
178+
with_file(List.wrap(name), List.wrap(code), fun)
179+
end
180+
158181
test "c helper" do
159-
assert_raise UndefinedFunctionError, "undefined function: Helpers_test_module.run/0", fn ->
160-
Helpers_test_module.run
182+
assert_raise UndefinedFunctionError, "undefined function: Sample.run/0", fn ->
183+
Sample.run
161184
end
162185

163-
File.write! "test-module-code.ex", test_module_code
164-
assert c("test-module-code.ex") == [Helpers_test_module]
165-
assert Helpers_test_module.run == :run
186+
filename = "test-module-code.ex"
187+
with_file filename, test_module_code, fn ->
188+
assert c(filename) == [Sample]
189+
assert Sample.run == :run
190+
end
166191
after
167-
File.rm "test-module-code.ex"
168-
File.rm! "Elixir.Helpers_test_module.beam"
169-
true = :code.delete Helpers_test_module
170-
:code.purge Helpers_test_module
192+
cleanup_modules([Sample])
171193
end
172194

173195
test "c helper multiple modules" do
174-
assert_raise UndefinedFunctionError, "undefined function: Helpers_test_module.run/0", fn ->
175-
Helpers_test_module.run
196+
assert_raise UndefinedFunctionError, "undefined function: Sample.run/0", fn ->
197+
Sample.run
176198
end
177199

178-
File.write! "test-module-code.ex", test_module_code <> "\n" <> another_test_module
179-
assert c("test-module-code.ex") |> Enum.sort == [Another_test_module,Helpers_test_module]
180-
assert Helpers_test_module.run == :run
181-
assert Another_test_module.hello == :world
200+
filename = "test-module-code.ex"
201+
with_file filename, test_module_code <> "\n" <> another_test_module, fn ->
202+
assert c(filename) |> Enum.sort == [Sample,Sample2]
203+
assert Sample.run == :run
204+
assert Sample2.hello == :world
205+
end
182206
after
183-
File.rm "test-module-code.ex"
184-
File.rm "Elixir.Helpers_test_module.beam"
185-
true = :code.delete Helpers_test_module
186-
:code.purge Helpers_test_module
187-
188-
File.rm! "Elixir.Another_test_module.beam"
189-
true = :code.delete Another_test_module
190-
:code.purge Another_test_module
207+
cleanup_modules([Sample, Sample2])
191208
end
192209

193210
test "c helper list" do
194-
assert_raise UndefinedFunctionError, "undefined function: Helpers_test_module.run/0", fn ->
195-
Helpers_test_module.run
211+
assert_raise UndefinedFunctionError, "undefined function: Sample.run/0", fn ->
212+
Sample.run
196213
end
197214

198-
File.write! "test-module-code-1.ex", test_module_code
199-
File.write! "test-module-code-2.ex", another_test_module
200-
assert c(["test-module-code-1.ex", "test-module-code-2.ex"]) |> Enum.sort
201-
== [Another_test_module,Helpers_test_module]
202-
assert Helpers_test_module.run == :run
203-
assert Another_test_module.hello == :world
215+
filenames = ["test-module-code-1.ex", "test-module-code-2.ex"]
216+
with_file filenames, [test_module_code, another_test_module], fn ->
217+
assert c(filenames) |> Enum.sort == [Sample,Sample2]
218+
assert Sample.run == :run
219+
assert Sample2.hello == :world
220+
end
204221
after
205-
File.rm "test-module-code-1.ex"
206-
File.rm "test-module-code-2.ex"
207-
208-
File.rm "Elixir.Helpers_test_module.beam"
209-
true = :code.delete Helpers_test_module
210-
:code.purge Helpers_test_module
211-
212-
File.rm! "Elixir.Another_test_module.beam"
213-
true = :code.delete Another_test_module
214-
:code.purge Another_test_module
222+
cleanup_modules([Sample, Sample2])
215223
end
216224

217225
test "l helper" do
218-
assert_raise UndefinedFunctionError, "undefined function: Helpers_test_module.run/0", fn ->
219-
Helpers_test_module.run
226+
assert_raise UndefinedFunctionError, "undefined function: Sample.run/0", fn ->
227+
Sample.run
220228
end
221229

222230
assert l(:non_existent_module) == {:error,:nofile}
223231

224-
File.write! "test-module-code.ex", test_module_code
225-
assert c("test-module-code.ex") == [Helpers_test_module]
226-
assert Helpers_test_module.run == :run
232+
filename = "test-module-code.ex"
233+
with_file filename, test_module_code, fn ->
234+
assert c(filename) == [Sample]
235+
assert Sample.run == :run
227236

228-
File.write! "test-module-code.ex", "defmodule Helpers_test_module do end"
229-
# FIXME: is there another way to compile a file without loading its module?
230-
System.cmd "elixirc test-module-code.ex"
237+
File.write! filename, "defmodule Sample do end"
238+
# FIXME: is there another way to compile a file without loading its module?
239+
System.cmd "elixirc test-module-code.ex"
231240

232-
assert l(Helpers_test_module) == {:module, Helpers_test_module}
233-
assert_raise UndefinedFunctionError, fn ->
234-
Helpers_test_module.run
241+
assert l(Sample) == {:module, Sample}
242+
assert_raise UndefinedFunctionError, "undefined function: Sample.run/0", fn ->
243+
Sample.run
244+
end
235245
end
236246
after
237-
File.rm "test-module-code.ex"
238-
File.rm! "Elixir.Helpers_test_module.beam"
239-
240-
# FIXME: This errors out with "Module 'Elixir.Helpers_test_module' must be purged before loading"
241-
#true = :code.delete Helpers_test_module
242-
243-
:code.purge Helpers_test_module
247+
# Clean up the old version left over after l()
248+
:code.purge(Sample)
249+
cleanup_modules([Sample])
244250
end
245251

246252
test "r helper" do
247-
assert_raise UndefinedFunctionError, "undefined function: Helpers_test_module.run/0", fn ->
248-
Helpers_test_module.run
253+
assert_raise UndefinedFunctionError, "undefined function: Sample.run/0", fn ->
254+
Sample.run
249255
end
250256

251257
assert r == []
@@ -254,26 +260,24 @@ defmodule IEx.HelpersTest do
254260
r :non_existent_module
255261
end
256262

257-
File.write! "test-module-code.ex", test_module_code
258-
assert c("test-module-code.ex") == [Helpers_test_module]
259-
assert Helpers_test_module.run == :run
260-
# FIXME: `r Helpers_test_module` returns :nosource
261-
assert r(Helpers_test_module) == [Helpers_test_module]
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]
262269

263-
assert r == [Helpers_test_module]
270+
assert r == [Sample]
271+
end
264272
after
265-
File.rm "test-module-code.ex"
266-
File.rm! "Elixir.Helpers_test_module.beam"
267-
268-
# FIXME: This errors out with "Module 'Elixir.Helpers_test_module' must be purged before loading"
269-
#true = :code.delete Helpers_test_module
270-
271-
:code.purge Helpers_test_module
273+
# Clean up old version produced by the r helper
274+
:code.purge(Sample)
275+
cleanup_modules([Sample])
272276
end
273277

274278
defp test_module_code do
275279
"""
276-
defmodule Helpers_test_module do
280+
defmodule Sample do
277281
def run do
278282
:run
279283
end
@@ -283,7 +287,7 @@ defmodule IEx.HelpersTest do
283287

284288
defp another_test_module do
285289
"""
286-
defmodule Another_test_module do
290+
defmodule Sample2 do
287291
def hello do
288292
:world
289293
end

0 commit comments

Comments
 (0)