Skip to content

Commit

Permalink
Add context to errorshow except_str tests (#56199)
Browse files Browse the repository at this point in the history
With this, the error message in
https://buildkite.com/julialang/julia-master/builds/41054#019294ca-e2c5-41f2-a897-e2959715f154
would become
```julia
Error in testset errorshow:
Test Failed at /home/jishnu/juliaPR/usr/share/julia/test/errorshow.jl:226
  Expression: typeof(err) === $(Expr(:escape, :MethodError))
   Evaluated: StackOverflowError === MethodError
     Context: expr = :(+())
```
Having the failing expression displayed makes it easier to locate the
source of the error.
  • Loading branch information
jishnub authored Oct 17, 2024
1 parent 727a57e commit c4effc3
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,17 @@ Base.show_method_candidates(buf, try bad_vararg_decl("hello", 3) catch e e end)
@test occursin("bad_vararg_decl(!Matched::$Int, ::Any...)", String(take!(buf)))

macro except_str(expr, err_type)
source_info = __source__
return quote
let err = nothing
try
$(esc(expr))
catch err
end
err === nothing && error("expected failure, but no exception thrown")
@test typeof(err) === $(esc(err_type))
@testset let expr=$(repr(expr))
$(Expr(:macrocall, Symbol("@test"), source_info, :(typeof(err) === $(esc(err_type)))))
end
buf = IOBuffer()
showerror(buf, err)
String(take!(buf))
Expand All @@ -231,6 +234,7 @@ macro except_str(expr, err_type)
end

macro except_strbt(expr, err_type)
source_info = __source__
errmsg = "expected failure, but no exception thrown for $expr"
return quote
let err = nothing
Expand All @@ -239,7 +243,9 @@ macro except_strbt(expr, err_type)
catch err
end
err === nothing && error($errmsg)
@test typeof(err) === $(esc(err_type))
@testset let expr=$(repr(expr))
$(Expr(:macrocall, Symbol("@test"), source_info, :(typeof(err) === $(esc(err_type)))))
end
buf = IOBuffer()
showerror(buf, err, catch_backtrace())
String(take!(buf))
Expand All @@ -248,6 +254,7 @@ macro except_strbt(expr, err_type)
end

macro except_stackframe(expr, err_type)
source_info = __source__
return quote
let err = nothing
local st
Expand All @@ -257,7 +264,9 @@ macro except_stackframe(expr, err_type)
st = stacktrace(catch_backtrace())
end
err === nothing && error("expected failure, but no exception thrown")
@test typeof(err) === $(esc(err_type))
@testset let expr=$(repr(expr))
$(Expr(:macrocall, Symbol("@test"), source_info, :(typeof(err) === $(esc(err_type)))))
end
sprint(show, st[1])
end
end
Expand Down

0 comments on commit c4effc3

Please sign in to comment.