diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index 3fb64bdff5bd3..2081425563780 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -1100,6 +1100,7 @@ function testset_beginend(args, tests, source) # action (such as reporting the results) ex = quote _check_testset($testsettype, $(QuoteNode(testsettype.args[1]))) + local ret local ts = $(testsettype)($desc; $options...) push_testset(ts) # we reproduce the logic of guardseed, but this function @@ -1120,9 +1121,10 @@ function testset_beginend(args, tests, source) record(ts, Error(:nontest_error, Expr(:tuple), err, Base.catch_stack(), $(QuoteNode(source)))) finally copy!(RNG, oldrng) + pop_testset() + ret = finish(ts) end - pop_testset() - finish(ts) + ret end # preserve outer location if possible if tests isa Expr && tests.head === :block && !isempty(tests.args) && tests.args[1] isa LineNumberNode diff --git a/stdlib/Test/test/runtests.jl b/stdlib/Test/test/runtests.jl index f3c0d4969ddfe..3307fd48e0d3c 100644 --- a/stdlib/Test/test/runtests.jl +++ b/stdlib/Test/test/runtests.jl @@ -919,3 +919,14 @@ end # Issue 20620 @test @inferred(.![true, false]) == [false, true] @test @inferred([3, 4] .- [1, 2] .+ [-2, -2]) == [0, 0] + +@testset "push/pop_testset invariance (Issue 32937)" begin + io = IOBuffer() + path = joinpath(@__DIR__(), "test_pop_testset_exec.jl") + cmd = `$(Base.julia_cmd()) $path` + ok = !success(pipeline(cmd; stdout = io, stderr = io)) + if !ok + @error "push/pop_testset invariance test failed" cmd Text(String(take!(io))) + end + @test ok +end diff --git a/stdlib/Test/test/test_pop_testset_exec.jl b/stdlib/Test/test/test_pop_testset_exec.jl new file mode 100644 index 0000000000000..3c5fde63f4ad4 --- /dev/null +++ b/stdlib/Test/test/test_pop_testset_exec.jl @@ -0,0 +1,6 @@ +using Test + +@testset begin + @test false + return +end