Skip to content

Commit d997cd7

Browse files
Test: move space in result prints to help grouping
1 parent db75908 commit d997cd7

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

base/errorshow.jl

+20-13
Original file line numberDiff line numberDiff line change
@@ -638,14 +638,16 @@ const update_stackframes_callback = Ref{Function}(identity)
638638
const STACKTRACE_MODULECOLORS = Iterators.Stateful(Iterators.cycle([:magenta, :cyan, :green, :yellow]))
639639
const STACKTRACE_FIXEDCOLORS = IdDict(Base => :light_black, Core => :light_black)
640640

641-
function show_full_backtrace(io::IO, trace::Vector; print_linebreaks::Bool)
641+
function show_full_backtrace(io::IO, trace::Vector; print_linebreaks::Bool, prefix=nothing)
642642
num_frames = length(trace)
643643
ndigits_max = ndigits(num_frames)
644644

645-
println(io, "\nStacktrace:")
645+
println(io)
646+
prefix === nothing || print(io, prefix)
647+
println(io, "Stacktrace:")
646648

647649
for (i, (frame, n)) in enumerate(trace)
648-
print_stackframe(io, i, frame, n, ndigits_max, STACKTRACE_FIXEDCOLORS, STACKTRACE_MODULECOLORS)
650+
print_stackframe(io, i, frame, n, ndigits_max, STACKTRACE_FIXEDCOLORS, STACKTRACE_MODULECOLORS; prefix)
649651
if i < num_frames
650652
println(io)
651653
print_linebreaks && println(io)
@@ -655,7 +657,7 @@ end
655657

656658
const BIG_STACKTRACE_SIZE = 50 # Arbitrary constant chosen here
657659

658-
function show_reduced_backtrace(io::IO, t::Vector)
660+
function show_reduced_backtrace(io::IO, t::Vector; prefix=nothing)
659661
recorded_positions = IdDict{UInt, Vector{Int}}()
660662
#= For each frame of hash h, recorded_positions[h] is the list of indices i
661663
such that hash(t[i-1]) == h, ie the list of positions in which the
@@ -701,16 +703,18 @@ function show_reduced_backtrace(io::IO, t::Vector)
701703

702704
try invokelatest(update_stackframes_callback[], displayed_stackframes) catch end
703705

704-
println(io, "\nStacktrace:")
706+
println(io)
707+
prefix === nothing || print(io, prefix)
708+
println(io, "Stacktrace:")
705709

706710
ndigits_max = ndigits(length(t))
707711

708712
push!(repeated_cycle, (0,0,0)) # repeated_cycle is never empty
709713
frame_counter = 1
710714
for i in eachindex(displayed_stackframes)
711715
(frame, n) = displayed_stackframes[i]
712-
713-
print_stackframe(io, frame_counter, frame, n, ndigits_max, STACKTRACE_FIXEDCOLORS, STACKTRACE_MODULECOLORS)
716+
prefix === nothing || print(io, prefix)
717+
print_stackframe(io, frame_counter, frame, n, ndigits_max, STACKTRACE_FIXEDCOLORS, STACKTRACE_MODULECOLORS; prefix)
714718

715719
if i < length(displayed_stackframes)
716720
println(io)
@@ -721,6 +725,7 @@ function show_reduced_backtrace(io::IO, t::Vector)
721725
cycle_length = repeated_cycle[1][2]
722726
repetitions = repeated_cycle[1][3]
723727
popfirst!(repeated_cycle)
728+
prefix === nothing || print(io, prefix)
724729
printstyled(io,
725730
"--- the above ", cycle_length, " lines are repeated ",
726731
repetitions, " more time", repetitions>1 ? "s" : "", " ---", color = :light_black)
@@ -738,15 +743,15 @@ end
738743
# Print a stack frame where the module color is determined by looking up the parent module in
739744
# `modulecolordict`. If the module does not have a color, yet, a new one can be drawn
740745
# from `modulecolorcycler`.
741-
function print_stackframe(io, i, frame::StackFrame, n::Int, ndigits_max, modulecolordict, modulecolorcycler)
746+
function print_stackframe(io, i, frame::StackFrame, n::Int, ndigits_max, modulecolordict, modulecolorcycler; prefix=nothing)
742747
m = Base.parentmodule(frame)
743748
modulecolor = if m !== nothing
744749
m = parentmodule_before_main(m)
745750
get!(() -> popfirst!(modulecolorcycler), modulecolordict, m)
746751
else
747752
:default
748753
end
749-
print_stackframe(io, i, frame, n, ndigits_max, modulecolor)
754+
print_stackframe(io, i, frame, n, ndigits_max, modulecolor; prefix)
750755
end
751756

752757
# Gets the topmost parent module that isn't Main
@@ -761,7 +766,7 @@ end
761766
parentmodule_before_main(x) = parentmodule_before_main(parentmodule(x))
762767

763768
# Print a stack frame where the module color is set manually with `modulecolor`.
764-
function print_stackframe(io, i, frame::StackFrame, n::Int, ndigits_max, modulecolor)
769+
function print_stackframe(io, i, frame::StackFrame, n::Int, ndigits_max, modulecolor; prefix=nothing)
765770
file, line = string(frame.file), frame.line
766771

767772
# Used by the REPL to make it possible to open
@@ -776,6 +781,7 @@ function print_stackframe(io, i, frame::StackFrame, n::Int, ndigits_max, modulec
776781
digit_align_width = ndigits_max + 2
777782

778783
# frame number
784+
prefix === nothing || print(io, prefix)
779785
print(io, " ", lpad("[" * string(i) * "]", digit_align_width))
780786
print(io, " ")
781787

@@ -785,6 +791,7 @@ function print_stackframe(io, i, frame::StackFrame, n::Int, ndigits_max, modulec
785791
end
786792
println(io)
787793

794+
prefix === nothing || print(io, prefix)
788795
# @ Module path / file : line
789796
print_module_path_file(io, modul, file, line; modulecolor, digit_align_width)
790797

@@ -813,7 +820,7 @@ function print_module_path_file(io, modul, file, line; modulecolor = :light_blac
813820
printstyled(io, basename(file), ":", line; color = :light_black, underline = true)
814821
end
815822

816-
function show_backtrace(io::IO, t::Vector)
823+
function show_backtrace(io::IO, t::Vector; prefix=nothing)
817824
if haskey(io, :last_shown_line_infos)
818825
empty!(io[:last_shown_line_infos])
819826
end
@@ -835,12 +842,12 @@ function show_backtrace(io::IO, t::Vector)
835842
end
836843

837844
if length(filtered) > BIG_STACKTRACE_SIZE
838-
show_reduced_backtrace(IOContext(io, :backtrace => true), filtered)
845+
show_reduced_backtrace(IOContext(io, :backtrace => true), filtered; prefix)
839846
return
840847
else
841848
try invokelatest(update_stackframes_callback[], filtered) catch end
842849
# process_backtrace returns a Vector{Tuple{Frame, Int}}
843-
show_full_backtrace(io, filtered; print_linebreaks = stacktrace_linebreaks())
850+
show_full_backtrace(io, filtered; print_linebreaks = stacktrace_linebreaks(), prefix)
844851
end
845852
nothing
846853
end

stdlib/Test/docs/src/index.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ julia> @test foo("f") == 20
5959
Test Failed at none:1
6060
Expression: foo("f") == 20
6161
Evaluated: 1 == 20
62-
63-
ERROR: There was an error during testing
62+
ERROR: There was an error during testing
6463
```
6564

6665
If the condition could not be evaluated because an exception was thrown, which occurs in this
@@ -230,8 +229,7 @@ Test Passed
230229
julia> @test 1 ≈ 0.999999
231230
Test Failed at none:1
232231
Expression: 1 ≈ 0.999999
233-
234-
ERROR: There was an error during testing
232+
ERROR: There was an error during testing
235233
```
236234
You can specify relative and absolute tolerances by setting the `rtol` and `atol` keyword arguments of `isapprox`, respectively,
237235
after the `` comparison:

stdlib/Test/src/Test.jl

+7-9
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function Base.show(io::IO, t::Fail)
182182
print(io, "\n")
183183
if t.backtrace !== nothing
184184
# Capture error message and indent to match
185-
join(io, (" " * line for line in split(t.backtrace, "\n")), "\n")
185+
join(io, (" " * line for line in filter!(!isempty, split(t.backtrace, "\n"))), "\n")
186186
end
187187
end
188188
elseif t.test_type === :test_throws_nothing
@@ -199,7 +199,6 @@ function Base.show(io::IO, t::Fail)
199199
print(io, "\n Context: ", t.context)
200200
end
201201
end
202-
println(io) # add some visual space to separate sequential failures
203202
end
204203

205204
"""
@@ -269,7 +268,7 @@ function Base.show(io::IO, t::Error)
269268
println(io, " Test threw exception")
270269
println(io, " Expression: ", t.orig_expr)
271270
# Capture error message and indent to match
272-
join(io, (" " * line for line in split(t.backtrace, "\n")), "\n")
271+
join(io, (" " * line for line in filter!(!isempty, split(t.backtrace, "\n"))), "\n")
273272
elseif t.test_type === :test_unbroken
274273
# A test that was expected to fail did not
275274
println(io, " Unexpected Pass")
@@ -279,7 +278,7 @@ function Base.show(io::IO, t::Error)
279278
# we had an error outside of a @test
280279
println(io, " Got exception outside of a @test")
281280
# Capture error message and indent to match
282-
join(io, (" " * line for line in split(t.backtrace, "\n")), "\n")
281+
join(io, (" " * line for line in filter!(!isempty, split(t.backtrace, "\n"))), "\n")
283282
end
284283
end
285284

@@ -1169,12 +1168,13 @@ end
11691168
# but do not terminate. Print a backtrace.
11701169
function record(ts::DefaultTestSet, t::Union{Fail, Error}; print_result::Bool=TESTSET_PRINT_ENABLE[])
11711170
if print_result
1171+
println() # add some visual space to separate sequential failures
11721172
print(ts.description, ": ")
11731173
# don't print for interrupted tests
11741174
if !(t isa Error) || t.test_type !== :test_interrupted
11751175
print(t)
11761176
if !isa(t, Error) # if not gets printed in the show method
1177-
Base.show_backtrace(stdout, scrub_backtrace(backtrace(), ts.file, extract_file(t.source)))
1177+
Base.show_backtrace(stdout, scrub_backtrace(backtrace(), ts.file, extract_file(t.source)); prefix=" ")
11781178
end
11791179
println()
11801180
end
@@ -1687,8 +1687,7 @@ Test Failed at none:3
16871687
Expression: !(iszero(real(logi)))
16881688
Evaluated: !(iszero(0.0))
16891689
Context: logi = 0.0 + 1.5707963267948966im
1690-
1691-
ERROR: There was an error during testing
1690+
ERROR: There was an error during testing
16921691
16931692
julia> @testset let logi = log(im), op = !iszero
16941693
@test imag(logi) == π/2
@@ -1699,8 +1698,7 @@ Test Failed at none:3
16991698
Evaluated: op(0.0)
17001699
Context: logi = 0.0 + 1.5707963267948966im
17011700
op = !iszero
1702-
1703-
ERROR: There was an error during testing
1701+
ERROR: There was an error during testing
17041702
```
17051703
"""
17061704
macro testset(args...)

stdlib/Test/test/runtests.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,9 @@ end
816816
""")
817817
msg = read(pipeline(ignorestatus(`$(Base.julia_cmd()) --startup-file=no --color=no $runtests`), stderr=devnull), String)
818818
msg = win2unix(msg)
819-
regex = r"((?:Tests|Other tests|Testset without source): Test Failed (?:.|\n)*?)\n\nStacktrace:(?:.|\n)*?(?=\n(?:Tests|Other tests))"
819+
regex = r"((?:Tests|Other tests|Testset without source): Test Failed (?:.|\n)*?)\n Stacktrace:(?:.|\n)*?(?=\n(?:Tests|Other tests))"
820820
failures = map(eachmatch(regex, msg)) do m
821-
m = match(r"(Tests|Other tests|Testset without source): .*? at (.*?)\n Expression: (.*)(?:.|\n)*\n+Stacktrace:\n((?:.|\n)*)", m.match)
821+
m = match(r"(Tests|Other tests|Testset without source): .*? at (.*?)\n Expression: (.*)(?:.|\n)*\n Stacktrace:\n((?:.|\n)*)", m.match)
822822
(; testset = m[1], source = m[2], ex = m[3], stacktrace = m[4])
823823
end
824824
@test length(failures) == 8 # 8 failed tests

test/buildkitetestjson.jl

+3
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ const TEST_TYPE_MAP = Dict(
145145
:test_throws_nothing => "@test_throws"
146146
)
147147
function get_test_call_str(result)
148+
if result.test_type === :nontest_error
149+
return "Got exception outside of a @test"
150+
end
148151
prefix = get(TEST_TYPE_MAP, result.test_type, nothing)
149152
prefix === nothing && return error("Unknown test type $(repr(result.test_type))")
150153
return prefix == "@test_throws" ? "@test_throws $(result.data) $(result.orig_expr)" : "$prefix $(result.orig_expr)"

0 commit comments

Comments
 (0)