Skip to content

Commit

Permalink
Test: make Error constructor more robust to broken objects (JuliaLang…
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Oct 30, 2021
1 parent 8bee11d commit ee1926f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
23 changes: 21 additions & 2 deletions stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,32 @@ struct Error <: Result
bt = scrub_exc_stack(bt)
end
if test_type === :test_error || test_type === :nontest_error
bt_str = sprint(Base.show_exception_stack, bt; context=stdout)
bt_str = try # try the latest world for this, since we might have eval'd new code for show
Base.invokelatest(sprint, Base.show_exception_stack, bt; context=stdout)
catch ex
"#=ERROR showing exception stack=# " *
try
sprint(Base.showerror, ex, catch_backtrace(); context=stdout)
catch
"of type " * string(typeof(ex))
end
end
else
bt_str = ""
end
value = try # try the latest world for this, since we might have eval'd new code for show
Base.invokelatest(sprint, show, value, context = :limit => true)
catch ex
"#=ERROR showing error of type " * string(typeof(value)) * "=# " *
try
sprint(Base.showerror, ex, catch_backtrace(); context=stdout)
catch
"of type " * string(typeof(ex))
end
end
return new(test_type,
string(orig_expr),
sprint(show, value, context = :limit => true),
value,
bt_str,
source)
end
Expand Down
18 changes: 18 additions & 0 deletions stdlib/Test/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,17 @@ let fails = @testset NoThrowTestSet begin

end

struct BadError <: Exception end
Base.show(io::IO, ::BadError) = throw("I am a bad error")
let errors = @testset NoThrowTestSet begin
# 1 - Error - unexpected pass
@test_broken true
# 2 - Error - converting a call into a comparison
@test ==(1, 1:2...)
# 3 - Error - objects with broken show
@test throw(BadError())
@test BadError()
throw(BadError())
end

for err in errors
Expand All @@ -320,6 +326,18 @@ let errors = @testset NoThrowTestSet begin
@test occursin("Expression: ==(1, 1:2...)", str)
@test occursin("MethodError: no method matching ==(::$Int, ::$Int, ::$Int)", str)
end

let str = sprint(show, errors[3])
@test occursin("Expression: throw(BadError())\n #=ERROR showing exception stack=# \"I am a bad error\"\n Stacktrace:\n", str)
end

let str = sprint(show, errors[4])
@test occursin("Expression: BadError()\n Value: #=ERROR showing error of type $BadError=# \"I am a bad error\"\nStacktrace:\n", str)
end

let str = sprint(show, errors[5])
@test occursin("Got exception outside of a @test\n #=ERROR showing exception stack=# \"I am a bad error\"\n Stacktrace:\n", str)
end
end

let retval_tests = @testset NoThrowTestSet begin
Expand Down

0 comments on commit ee1926f

Please sign in to comment.