Skip to content

Commit

Permalink
Test: Don't change scope kind in test_{warn,nowarn}
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 committed Nov 11, 2024
1 parent afdba95 commit 0bc071a
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 0bc071a

Please sign in to comment.