Skip to content

Commit f405adf

Browse files
committed
Use Test Modules in tests instead of Elixir built-in modules
Prep for Coverage Reporting in tests
1 parent 624ae0c commit f405adf

9 files changed

Lines changed: 304 additions & 185 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# SPDX-FileCopyrightText: 2021 The Elixir Team
3+
# SPDX-FileCopyrightText: 2012 Plataformatec
4+
5+
# Beam files compiled on demand
6+
path = Path.expand("../tmp/beams", __DIR__)
7+
File.rm_rf!(path)
8+
File.mkdir_p!(path)
9+
Code.prepend_path(path)
10+
11+
defmodule PathHelpers do
12+
def fixture_path() do
13+
Path.expand("../test/elixir/fixtures", __DIR__)
14+
end
15+
16+
def tmp_path() do
17+
Path.expand("../tmp", __DIR__)
18+
end
19+
20+
def fixture_path(extra) do
21+
Path.join(fixture_path(), extra)
22+
end
23+
24+
def tmp_path(extra) do
25+
Path.join(tmp_path(), extra)
26+
end
27+
28+
def elixir(args, executable_extension \\ "") do
29+
run_cmd(elixir_executable(executable_extension), args)
30+
end
31+
32+
def elixir_executable(extension \\ "") do
33+
executable_path("elixir", extension)
34+
end
35+
36+
def elixirc(args, executable_extension \\ "") do
37+
run_cmd(elixirc_executable(executable_extension), args)
38+
end
39+
40+
def elixirc_executable(extension \\ "") do
41+
executable_path("elixirc", extension)
42+
end
43+
44+
def iex(args, executable_extension \\ "") do
45+
run_cmd(iex_executable(executable_extension), args)
46+
end
47+
48+
def iex_executable(extension \\ "") do
49+
executable_path("iex", extension)
50+
end
51+
52+
def write_beam({:module, name, bin, _} = res) do
53+
File.mkdir_p!(unquote(path))
54+
beam_path = Path.join(unquote(path), Atom.to_string(name) <> ".beam")
55+
File.write!(beam_path, bin)
56+
res
57+
end
58+
59+
defp run_cmd(executable, args) do
60+
~c"#{executable} #{IO.chardata_to_string(args)}#{redirect_std_err_on_win()}"
61+
|> :os.cmd()
62+
|> :unicode.characters_to_binary()
63+
end
64+
65+
defp executable_path(name, extension) do
66+
Path.expand("../../../bin/#{name}#{extension}", __DIR__)
67+
end
68+
69+
if match?({:win32, _}, :os.type()) do
70+
def windows?, do: true
71+
def executable_extension, do: ".bat"
72+
def redirect_std_err_on_win, do: " 2>&1"
73+
else
74+
def windows?, do: false
75+
def executable_extension, do: ""
76+
def redirect_std_err_on_win, do: ""
77+
end
78+
end

lib/elixir/test/elixir/exception_test.exs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -556,20 +556,36 @@ defmodule ExceptionTest do
556556
end
557557

558558
test "annotates function clause errors" do
559-
assert blame_message(Access, & &1.fetch(:foo, :bar)) =~ """
560-
no function clause matching in Access.fetch/2
559+
import PathHelpers
560+
561+
write_beam(
562+
defmodule ExampleModule do
563+
def fun(arg1, arg2)
564+
def fun(:one, :one), do: :ok
565+
def fun(:two, :two), do: :ok
566+
end
567+
)
568+
569+
:code.purge(ExampleModule)
570+
:code.delete(ExampleModule)
571+
572+
message = blame_message(ExceptionTest.ExampleModule, & &1.fun(:three, :four))
573+
574+
assert message =~ """
575+
no function clause matching in ExceptionTest.ExampleModule.fun/2
561576
562-
The following arguments were given to Access.fetch/2:
577+
The following arguments were given to ExceptionTest.ExampleModule.fun/2:
563578
564579
# 1
565-
:foo
580+
:three
566581
567582
# 2
568-
:bar
583+
:four
569584
570-
Attempted function clauses (showing 5 out of 5):
585+
Attempted function clauses (showing 2 out of 2):
571586
572-
def fetch(-%module{} = container-, key)
587+
def fun(-:one-, -:one-)
588+
def fun(-:two-, -:two-)
573589
"""
574590
end
575591

@@ -858,13 +874,25 @@ defmodule ExceptionTest do
858874

859875
describe "blaming unit tests" do
860876
test "annotates clauses errors" do
861-
args = [%{}, :key, nil]
877+
import PathHelpers
878+
879+
write_beam(
880+
defmodule ExampleModule do
881+
def fun(arg), do: arg
882+
end
883+
)
884+
885+
:code.purge(ExampleModule)
886+
:code.delete(ExampleModule)
887+
888+
args = [nil]
862889

863890
{exception, stack} =
864-
Exception.blame(:error, :function_clause, [{Keyword, :pop, args, [line: 13]}])
891+
Exception.blame(:error, :function_clause, [{ExampleModule, :fun, args, [line: 13]}])
865892

866893
assert %FunctionClauseError{kind: :def, args: ^args, clauses: [_]} = exception
867-
assert stack == [{Keyword, :pop, 3, [line: 13]}]
894+
895+
assert stack == [{ExampleModule, :fun, 1, [line: 13]}]
868896
end
869897

870898
test "annotates args and clauses from mfa" do

lib/elixir/test/elixir/module_test.exs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,21 @@ defmodule ModuleTest do
432432
end
433433

434434
test "compiles to core" do
435-
{:ok, {Atom, [{~c"Dbgi", dbgi}]}} = Atom |> :code.which() |> :beam_lib.chunks([~c"Dbgi"])
435+
import PathHelpers
436+
437+
write_beam(
438+
defmodule ExampleModule do
439+
end
440+
)
441+
442+
:code.purge(ExampleModule)
443+
:code.delete(ExampleModule)
444+
445+
{:ok, {ExampleModule, [{~c"Dbgi", dbgi}]}} =
446+
ExampleModule |> :code.which() |> :beam_lib.chunks([~c"Dbgi"])
447+
436448
{:debug_info_v1, backend, data} = :erlang.binary_to_term(dbgi)
437-
{:ok, core} = backend.debug_info(:core_v1, Atom, data, [])
449+
{:ok, core} = backend.debug_info(:core_v1, ExampleModule, data, [])
438450
assert is_tuple(core)
439451
end
440452

lib/elixir/test/elixir/protocol/consolidation_test.exs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,21 @@ defmodule Protocol.ConsolidationTest do
247247
end
248248

249249
test "consolidation errors on missing BEAM files" do
250+
import PathHelpers
251+
252+
write_beam(
253+
defmodule ExampleModule do
254+
end
255+
)
256+
257+
:code.purge(ExampleModule)
258+
:code.delete(ExampleModule)
259+
250260
defprotocol NoBeam do
251261
def example(arg)
252262
end
253263

254-
assert Protocol.consolidate(String, []) == {:error, :not_a_protocol}
264+
assert Protocol.consolidate(ExampleModule, []) == {:error, :not_a_protocol}
255265
assert Protocol.consolidate(NoBeam, []) == {:error, :no_beam_info}
256266
end
257267

lib/elixir/test/elixir/test_helper.exs

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,10 @@
22
# SPDX-FileCopyrightText: 2021 The Elixir Team
33
# SPDX-FileCopyrightText: 2012 Plataformatec
44

5-
# Beam files compiled on demand
6-
path = Path.expand("../../tmp/beams", __DIR__)
7-
File.rm_rf!(path)
8-
File.mkdir_p!(path)
9-
Code.prepend_path(path)
10-
115
Application.put_env(:elixir, :ansi_enabled, true)
126
Code.compiler_options(debug_info: true, infer_signatures: [:elixir])
137

14-
defmodule PathHelpers do
15-
def fixture_path() do
16-
Path.expand("fixtures", __DIR__)
17-
end
18-
19-
def tmp_path() do
20-
Path.expand("../../tmp", __DIR__)
21-
end
22-
23-
def fixture_path(extra) do
24-
Path.join(fixture_path(), extra)
25-
end
26-
27-
def tmp_path(extra) do
28-
Path.join(tmp_path(), extra)
29-
end
30-
31-
def elixir(args, executable_extension \\ "") do
32-
run_cmd(elixir_executable(executable_extension), args)
33-
end
34-
35-
def elixir_executable(extension \\ "") do
36-
executable_path("elixir", extension)
37-
end
38-
39-
def elixirc(args, executable_extension \\ "") do
40-
run_cmd(elixirc_executable(executable_extension), args)
41-
end
42-
43-
def elixirc_executable(extension \\ "") do
44-
executable_path("elixirc", extension)
45-
end
46-
47-
def iex(args, executable_extension \\ "") do
48-
run_cmd(iex_executable(executable_extension), args)
49-
end
50-
51-
def iex_executable(extension \\ "") do
52-
executable_path("iex", extension)
53-
end
54-
55-
def write_beam({:module, name, bin, _} = res) do
56-
File.mkdir_p!(unquote(path))
57-
beam_path = Path.join(unquote(path), Atom.to_string(name) <> ".beam")
58-
File.write!(beam_path, bin)
59-
res
60-
end
61-
62-
defp run_cmd(executable, args) do
63-
~c"#{executable} #{IO.chardata_to_string(args)}#{redirect_std_err_on_win()}"
64-
|> :os.cmd()
65-
|> :unicode.characters_to_binary()
66-
end
67-
68-
defp executable_path(name, extension) do
69-
Path.expand("../../../../bin/#{name}#{extension}", __DIR__)
70-
end
71-
72-
if match?({:win32, _}, :os.type()) do
73-
def windows?, do: true
74-
def executable_extension, do: ".bat"
75-
def redirect_std_err_on_win, do: " 2>&1"
76-
else
77-
def windows?, do: false
78-
def executable_extension, do: ""
79-
def redirect_std_err_on_win, do: ""
80-
end
81-
end
8+
Code.eval_file("../../scripts/path_helpers.exs", __DIR__)
829

8310
defmodule CodeFormatterHelpers do
8411
defmacro assert_same(good, opts \\ []) do

0 commit comments

Comments
 (0)