Skip to content

Commit 6fadf5a

Browse files
nickrobinson251giordano
authored andcommitted
Restore support for checking for UndefVarError variable name in at-test_throws (#56231)
Fix #54082 Arguably this was a breaking change (as a consequence of #51979). But regardless, it seems like useful functionality to have a public API for testing that an `UndefVarError` was thrown for the expected variable name (regardless of scope). This is particularly useful if you don't know what the scope is (for example, in my use-case i want to test that a specific `UndefVarError` is thrown from a module with a `gensym`'d name). Pre-v1.11 the syntax for this was ```julia @test_throws UndefVarError(:x) foo() ``` but that stopped working in v1.11 when `UndefVarError` got a second field (in fact in v1.11.1 this is an error when before it would pass) This PR restores that functionality. We might want to backport it to v1.11.x so that v1.11 isn't the only version that doesn't support this. (cherry picked from commit b0c1525)
1 parent 7d3cbd8 commit 6fadf5a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

stdlib/Test/src/Test.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,11 @@ function do_test_throws(result::ExecutionResult, orig_expr, extype)
809809
if extype isa LoadError && !(exc isa LoadError) && typeof(extype.error) == typeof(exc)
810810
extype = extype.error # deprecated
811811
end
812-
if isa(exc, typeof(extype))
812+
# Support `UndefVarError(:x)` meaning `UndefVarError(:x, scope)` for any `scope`.
813+
# Retains the behaviour from pre-v1.11 when `UndefVarError` didn't have `scope`.
814+
if isa(extype, UndefVarError) && !isdefined(extype, :scope)
815+
success = exc isa UndefVarError && exc.var == extype.var
816+
else isa(exc, typeof(extype))
813817
success = true
814818
for fld in 1:nfields(extype)
815819
if !isequal(getfield(extype, fld), getfield(exc, fld))

stdlib/Test/test/runtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,3 +1710,20 @@ end
17101710
@test occursin(expected, result)
17111711
end
17121712
end
1713+
1714+
# Issue #54082
1715+
module M54082 end
1716+
@testset "@test_throws UndefVarError(:var)" begin
1717+
# Single-arg `UndefVarError` should match all `UndefVarError` for the
1718+
# same variable name, regardless of scope, to keep pre-v1.11 behaviour.
1719+
f54082() = var
1720+
@test_throws UndefVarError(:var) f54082()
1721+
# But if scope is set, then it has to match.
1722+
@test_throws UndefVarError(:var, M54082) M54082.var
1723+
let result = @testset NoThrowTestSet begin
1724+
# Wrong module scope
1725+
@test_throws UndefVarError(:var, Main) M54082.var
1726+
end
1727+
@test only(result) isa Test.Fail
1728+
end
1729+
end

0 commit comments

Comments
 (0)