Skip to content

Commit

Permalink
Test: Don't change scope kind in test_{warn,nowarn} (#56524)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Keno authored Nov 11, 2024
1 parent 3318941 commit 14df038
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -890,10 +890,17 @@ macro test_warn(msg, expr)
quote
let fname = tempname()
try
ret = open(fname, "w") do f
redirect_stderr(f) do
$(esc(expr))
end
f = open(fname, "w")
stdold = stderr
redirect_stderr(f)
ret = try
# We deliberately don't use the thunk versions of open/redirect
# to ensure that adding the macro does not change the toplevel-ness
# of the resulting expression.
$(esc(expr))
finally
redirect_stderr(stdold)
close(f)
end
@test contains_warn(read(fname, String), $(esc(msg)))
ret
Expand Down Expand Up @@ -922,10 +929,14 @@ macro test_nowarn(expr)
# here.
let fname = tempname()
try
ret = open(fname, "w") do f
redirect_stderr(f) do
$(esc(expr))
end
f = open(fname, "w")
stdold = stderr
redirect_stderr(f)
ret = try
$(esc(expr))
finally
redirect_stderr(stdold)
close(f)
end
stderr_content = read(fname, String)
print(stderr, stderr_content) # this is helpful for debugging
Expand Down

0 comments on commit 14df038

Please sign in to comment.