Skip to content

Commit 9192907

Browse files
author
José Valim
committed
Do not leak external variables used in guards in assert_receive
1 parent 5533cc4 commit 9192907

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,12 @@ defmodule ExUnit.Assertions do
427427
Enum.uniq_by(pins, &elem(&1, 0))
428428
end
429429

430+
defp collect_vars_from_pattern({:when, _, [left, right]}) do
431+
pattern = collect_vars_from_pattern(left)
432+
guards = for var <- collect_vars_from_pattern(right), var in pattern, do: var
433+
pattern ++ guards
434+
end
435+
430436
defp collect_vars_from_pattern(expr) do
431437
Macro.prewalk(expr, [], fn
432438
{:::, _, [left, _]}, acc ->

lib/ex_unit/test/ex_unit/assertions_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ defmodule ExUnit.AssertionsTest do
266266
:world = world
267267
end
268268

269+
test "assert received does not leak external variables used in guards" do
270+
send self, {:hello, :world}
271+
guard_world = :world
272+
assert_received {:hello, world} when world == guard_world
273+
:world = world
274+
end
275+
269276
test "refute received does not wait" do
270277
false = refute_received :hello
271278
end

0 commit comments

Comments
 (0)