Skip to content

Commit 14df038

Browse files
authored
Test: Don't change scope kind in test_{warn,nowarn} (#56524)
This was part of #56509, but is an independent bugfix. The basic issue is that these macro were using `do` block internally. This is undesirable for test macros, because we would like them not to affect the behavior of what they're testing. E.g. right now: ``` julia> using Test julia> const x = 1 1 julia> @test_nowarn const x = 1 ERROR: syntax: `global const` declaration not allowed inside function around /home/keno/julia/usr/share/julia/stdlib/v1.12/Test/src/Test.jl:927 Stacktrace: [1] top-level scope @ REPL[3]:1 ``` This PR just writes out the try/finally manually, so the above works fine after this PR.
1 parent 3318941 commit 14df038

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

stdlib/Test/src/Test.jl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -890,10 +890,17 @@ macro test_warn(msg, expr)
890890
quote
891891
let fname = tempname()
892892
try
893-
ret = open(fname, "w") do f
894-
redirect_stderr(f) do
895-
$(esc(expr))
896-
end
893+
f = open(fname, "w")
894+
stdold = stderr
895+
redirect_stderr(f)
896+
ret = try
897+
# We deliberately don't use the thunk versions of open/redirect
898+
# to ensure that adding the macro does not change the toplevel-ness
899+
# of the resulting expression.
900+
$(esc(expr))
901+
finally
902+
redirect_stderr(stdold)
903+
close(f)
897904
end
898905
@test contains_warn(read(fname, String), $(esc(msg)))
899906
ret
@@ -922,10 +929,14 @@ macro test_nowarn(expr)
922929
# here.
923930
let fname = tempname()
924931
try
925-
ret = open(fname, "w") do f
926-
redirect_stderr(f) do
927-
$(esc(expr))
928-
end
932+
f = open(fname, "w")
933+
stdold = stderr
934+
redirect_stderr(f)
935+
ret = try
936+
$(esc(expr))
937+
finally
938+
redirect_stderr(stdold)
939+
close(f)
929940
end
930941
stderr_content = read(fname, String)
931942
print(stderr, stderr_content) # this is helpful for debugging

0 commit comments

Comments
 (0)