Skip to content

Commit 8f8bb86

Browse files
ericentinjosevalim
authored andcommitted
Skip/fix tests on OTP 19 (#4855)
* Callback-related tests use beam AST to find callbacks * Fix invalid function call warning on OTP 19 * Skip dialyzer-related tests on OTP 19 * Fix IEx h helper for OTP 19 * Improve IEx h helper test for multi-arity callbacks
1 parent 41272f0 commit 8f8bb86

File tree

8 files changed

+62
-42
lines changed

8 files changed

+62
-42
lines changed

lib/elixir/test/elixir/behaviour_test.exs

+14-11
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,33 @@ Code.require_file "test_helper.exs", __DIR__
33
defmodule BehaviourTest do
44
use ExUnit.Case, async: true
55

6-
defmodule Sample do
7-
use Behaviour
6+
{_, _, sample_binary, _} =
7+
defmodule Sample do
8+
use Behaviour
89

9-
defcallback first(integer) :: integer
10+
defcallback first(integer) :: integer
1011

11-
defcallback foo(atom(), binary) :: binary
12+
defcallback foo(atom(), binary) :: binary
1213

13-
defcallback bar(External.hello, my_var :: binary) :: binary
14+
defcallback bar(External.hello, my_var :: binary) :: binary
1415

15-
defcallback guarded(my_var) :: my_var when my_var: binary
16+
defcallback guarded(my_var) :: my_var when my_var: binary
1617

17-
defcallback orr(atom | integer) :: atom
18+
defcallback orr(atom | integer) :: atom
1819

19-
defcallback literal(123, {atom}, :atom, [integer], true) :: atom
20+
defcallback literal(123, {atom}, :atom, [integer], true) :: atom
2021

21-
defmacrocallback last(integer) :: Macro.t
22-
end
22+
defmacrocallback last(integer) :: Macro.t
23+
end
24+
25+
@sample_binary sample_binary
2326

2427
test "callbacks" do
2528
assert Sample.__behaviour__(:callbacks) == [first: 1, guarded: 1, "MACRO-last": 2, literal: 5, orr: 1, foo: 2, bar: 2]
2629
end
2730

2831
test "specs" do
29-
assert length(Keyword.get_values(Sample.module_info[:attributes], :callback)) == 7
32+
assert length(Kernel.Typespec.beam_callbacks(@sample_binary)) == 7
3033
end
3134

3235
test "default is not supported" do

lib/elixir/test/elixir/kernel/dialyzer_test.exs

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ defmodule Kernel.DialyzerTest do
5353
{:ok, [outdir: dir, dialyzer: dialyzer]}
5454
end
5555

56+
@tag otp19: false
5657
test "no warnings on valid remote calls", context do
5758
copy_beam! context, Dialyzer.RemoteCall
5859
assert_dialyze_no_warnings! context
@@ -63,6 +64,7 @@ defmodule Kernel.DialyzerTest do
6364
assert_dialyze_no_warnings! context
6465
end
6566

67+
@tag otp19: false
6668
test "no warnings on raise", context do
6769
copy_beam! context, Dialyzer.Raise
6870
assert_dialyze_no_warnings! context

lib/elixir/test/elixir/kernel/raise_test.exs

+3-2
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,10 @@ defmodule Kernel.RaiseTest do
283283
end
284284

285285
test "badfun error" do
286-
x = :example
286+
# Avoid "invalid function call" warning in >= OTP 19
287+
x = fn -> :example end
287288
result = try do
288-
x.(2)
289+
x.().(2)
289290
rescue
290291
x in [BadFunctionError] -> Exception.message(x)
291292
end

lib/elixir/test/elixir/protocol_test.exs

+26-18
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@ defmodule ProtocolTest do
55

66
doctest Protocol
77

8-
defprotocol Sample do
9-
@type t :: any
10-
@doc "Ok"
11-
@spec ok(t) :: boolean
12-
def ok(term)
13-
end
8+
{_, _, sample_binary, _} =
9+
defprotocol Sample do
10+
@type t :: any
11+
@doc "Ok"
12+
@spec ok(t) :: boolean
13+
def ok(term)
14+
end
1415

15-
defprotocol WithAny do
16-
@fallback_to_any true
17-
@doc "Ok"
18-
def ok(term)
19-
end
16+
@sample_binary sample_binary
17+
18+
{_, _, with_any_binary, _} =
19+
defprotocol WithAny do
20+
@fallback_to_any true
21+
@doc "Ok"
22+
def ok(term)
23+
end
24+
25+
@with_any_binary with_any_binary
2026

2127
defprotocol Derivable do
2228
def ok(a)
@@ -123,11 +129,11 @@ defmodule ProtocolTest do
123129
end
124130

125131
test "protocol defines callbacks" do
126-
assert get_callbacks(Sample, :ok, 1) ==
127-
[{:type, [11], :fun, [{:type, [11], :product, [{:user_type, [11], :t, []}]}, {:type, [11], :boolean, []}]}]
132+
assert get_callbacks(@sample_binary, :ok, 1) ==
133+
[{:type, 12, :fun, [{:type, 12, :product, [{:user_type, 12, :t, []}]}, {:type, 12, :boolean, []}]}]
128134

129-
assert get_callbacks(WithAny, :ok, 1) ==
130-
[{:type, [18], :fun, [{:type, [18], :product, [{:user_type, [18], :t, []}]}, {:type, [18], :term, []}]}]
135+
assert get_callbacks(@with_any_binary, :ok, 1) ==
136+
[{:type, 22, :fun, [{:type, 22, :product, [{:user_type, 22, :t, []}]}, {:type, 22, :term, []}]}]
131137
end
132138

133139
test "protocol defines functions and attributes" do
@@ -182,8 +188,8 @@ defmodule ProtocolTest do
182188
assert Multi.test(:a) == :a
183189
end
184190

185-
defp get_callbacks(module, name, arity) do
186-
callbacks = for {:callback, info} <- module.__info__(:attributes), do: hd(info)
191+
defp get_callbacks(beam, name, arity) do
192+
callbacks = Kernel.Typespec.beam_callbacks(beam)
187193
List.keyfind(callbacks, {name, arity}, 0) |> elem(1)
188194
end
189195

@@ -308,6 +314,8 @@ defmodule Protocol.ConsolidationTest do
308314
{:ok, binary} = Protocol.consolidate(Sample, [Any, ImplStruct])
309315
:code.load_binary(Sample, 'protocol_test.exs', binary)
310316

317+
@sample_binary binary
318+
311319
# Any should be moved to the end
312320
:code.purge(WithAny)
313321
:code.delete(WithAny)
@@ -367,7 +375,7 @@ defmodule Protocol.ConsolidationTest do
367375
end
368376

369377
test "consolidated keeps callbacks" do
370-
callbacks = for {:callback, info} <- Sample.__info__(:attributes), do: hd(info)
378+
callbacks = Kernel.Typespec.beam_callbacks(@sample_binary)
371379
assert callbacks != []
372380
end
373381

lib/elixir/test/elixir/test_helper.exs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
ExUnit.start [trace: "--trace" in System.argv]
1+
exclude =
2+
case :erlang.system_info(:otp_release) do
3+
'19' -> [otp19: false]
4+
_ -> []
5+
end
6+
7+
ExUnit.start [exclude: exclude, trace: "--trace" in System.argv]
28

39
# Beam files compiled on demand
410
path = Path.expand("../../tmp/beams", __DIR__)

lib/iex/lib/iex/introspection.ex

+3-6
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ defmodule IEx.Introspection do
108108
if docs = Code.get_docs(mod, :docs) do
109109
if doc = find_doc(docs, fun, arity) do
110110
if callback_module = is_nil(elem(doc, 4)) and callback_module(mod, fun, arity) do
111-
filter = &match?({^fun, _}, elem(&1, 0))
111+
filter = &match?({^fun, ^arity}, elem(&1, 0))
112112
print_callback_docs(callback_module, filter, &print_doc/2)
113113
else
114114
print_doc(doc)
@@ -149,14 +149,11 @@ defmodule IEx.Introspection do
149149
do: true
150150

151151
defp callback_module(mod, fun, arity) do
152+
filter = &match?({{^fun, ^arity}, _}, &1)
152153
mod.module_info(:attributes)
153154
|> Keyword.get_values(:behaviour)
154155
|> Stream.concat()
155-
|> Enum.find(fn module ->
156-
module.module_info(:attributes)
157-
|> Enum.filter(&match?({:callback, _}, &1))
158-
|> Enum.any?(&match?({_, [{{^fun, ^arity}, _} | _]}, &1))
159-
end)
156+
|> Enum.find(&Enum.any?(Typespec.beam_callbacks(&1), filter))
160157
end
161158

162159
defp print_doc({{fun, _}, _line, kind, args, doc}) do

lib/iex/test/iex/helpers_test.exs

+7-3
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,29 @@ defmodule IEx.HelpersTest do
7979
@doc "Docs for MyBehaviour.first"
8080
@callback first(integer) :: integer
8181
@callback second(integer) :: integer
82+
@callback second(integer, integer) :: integer
8283
end
8384
"""
8485
impl = """
8586
defmodule Impl do
8687
@behaviour MyBehaviour
8788
def first(0), do: 0
88-
@doc "Docs for Impl.second"
89+
@doc "Docs for Impl.second/1"
8990
def second(0), do: 0
91+
@doc "Docs for Impl.second/2"
92+
def second(0, 0), do: 0
9093
end
9194
"""
9295
files = ["my_behaviour.ex", "impl.ex"]
9396
with_file files, [behaviour, impl], fn ->
9497
assert c(files, ".") |> Enum.sort == [Impl, MyBehaviour]
9598

9699
assert capture_io(fn -> h Impl.first/1 end) == "* @callback first(integer()) :: integer()\n\nDocs for MyBehaviour.first\n"
97-
assert capture_io(fn -> h Impl.second/1 end) == "* def second(int)\n\nDocs for Impl.second\n"
100+
assert capture_io(fn -> h Impl.second/1 end) == "* def second(int)\n\nDocs for Impl.second/1\n"
101+
assert capture_io(fn -> h Impl.second/2 end) == "* def second(int1, int2)\n\nDocs for Impl.second/2\n"
98102

99103
assert capture_io(fn -> h Impl.first end) == "* @callback first(integer()) :: integer()\n\nDocs for MyBehaviour.first\n"
100-
assert capture_io(fn -> h Impl.second end) == "* def second(int)\n\nDocs for Impl.second\n"
104+
assert capture_io(fn -> h Impl.second end) == "* def second(int)\n\nDocs for Impl.second/1\n* def second(int1, int2)\n\nDocs for Impl.second/2\n"
101105
end
102106
after
103107
cleanup_modules([Impl, MyBehaviour])

lib/iex/test/test_helper.exs

-1
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,3 @@ defmodule IEx.Case do
7070
|> String.trim
7171
end
7272
end
73-

0 commit comments

Comments
 (0)