Skip to content

Commit

Permalink
fix backtrace filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Oct 5, 2017
1 parent 92fa0f3 commit 74936a6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
15 changes: 4 additions & 11 deletions stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 16 additions & 10 deletions stdlib/Test/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 74936a6

Please sign in to comment.