diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index cca73159264be..5133be77772f4 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -25,23 +25,16 @@ export GenericString, GenericSet, GenericDict, GenericArray #----------------------------------------------------------------------- # Backtrace utility functions -function ip_matches_func_and_name(ip, func::Symbol, dir::String, file::String) - for fr in StackTraces.lookup(ip) - if fr === StackTraces.UNKNOWN || fr.from_c - return false - end - path = string(fr.file) - fr.func == func && dirname(path) == dir && basename(path) == file && return true - end - return false +function ip_has_file_and_func(ip, file, funcs) + return any(fr -> (string(fr.file) == file && fr.func in funcs), StackTraces.lookup(ip)) end function scrub_backtrace(bt) - do_test_ind = findfirst(addr->ip_matches_func_and_name(addr, :do_test, ".", "test.jl"), bt) + do_test_ind = findfirst(ip -> ip_has_file_and_func(ip, @__FILE__, (:do_test, :do_test_throws)), bt) if do_test_ind != 0 && length(bt) > do_test_ind bt = bt[do_test_ind + 1:end] end - name_ind = findfirst(addr->ip_matches_func_and_name(addr, Symbol("macro expansion"), ".", "test.jl"), bt) + name_ind = findfirst(ip -> ip_has_file_and_func(ip, @__FILE__, (Symbol("macro expansion"),)), bt) if name_ind != 0 && length(bt) != 0 bt = bt[1:name_ind] end diff --git a/stdlib/Test/test/runtests.jl b/stdlib/Test/test/runtests.jl index a1d9d661110ce..646d5c0326fed 100644 --- a/stdlib/Test/test/runtests.jl +++ b/stdlib/Test/test/runtests.jl @@ -533,16 +533,22 @@ end end @testset "backtraces in test errors" begin - let io = IOBuffer() - # calls backtrace() from inside @test - @test (print(io, Test.Error(:test_error, "woot", 5, backtrace())); 1) == 1 - let str = String(take!(io)) - # NOTE: This test depends on the code generated by @testset getting compiled, - # to get good backtraces. If it fails, check the implementation of `testset_beginend`. - @test contains(str, "Test.jl") - @test_broken !contains(str, "client.jl") - end - end + f = tempname() + write(f, + """ + using Test + @testset begin + @test 1==2 + @test_throws MethodError 1 + end + """) + msg = read(pipeline(ignorestatus(`$(Base.julia_cmd()) --startup-file=no --color=no $f`), stderr=DevNull), String) + # NOTE: This test depends on the code generated by @testset getting compiled, + # to get good backtraces. If it fails, check the implementation of `testset_beginend`. + @test !contains(msg, "do_test(") + @test !contains(msg, "include(") + @test contains(msg, "at " * f * ":3") + @test contains(msg, "at " * f * ":4") end let io = IOBuffer()