Skip to content

Commit 93a5f69

Browse files
authored
Test.TestLogger: add respect_maxlog option (#44085)
1 parent f090992 commit 93a5f69

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

stdlib/Test/src/logging.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ mutable struct TestLogger <: AbstractLogger
2727
catch_exceptions::Bool
2828
shouldlog_args
2929
message_limits::Dict{Any,Int}
30+
respect_maxlog::Bool
3031
end
3132

32-
TestLogger(; min_level=Info, catch_exceptions=false) =
33-
TestLogger(LogRecord[], min_level, catch_exceptions, nothing, Dict{Any, Int}())
33+
TestLogger(; min_level=Info, catch_exceptions=false, respect_maxlog=true) =
34+
TestLogger(LogRecord[], min_level, catch_exceptions, nothing, Dict{Any, Int}(), respect_maxlog)
3435
Logging.min_enabled_level(logger::TestLogger) = logger.min_level
3536

3637
function Logging.shouldlog(logger::TestLogger, level, _module, group, id)
@@ -45,11 +46,13 @@ end
4546
function Logging.handle_message(logger::TestLogger, level, msg, _module,
4647
group, id, file, line; kwargs...)
4748
@nospecialize
48-
maxlog = get(kwargs, :maxlog, nothing)
49-
if maxlog isa Core.BuiltinInts
50-
remaining = get!(logger.message_limits, id, Int(maxlog)::Int)
51-
logger.message_limits[id] = remaining - 1
52-
remaining > 0 || return
49+
if logger.respect_maxlog
50+
maxlog = get(kwargs, :maxlog, nothing)
51+
if maxlog isa Core.BuiltinInts
52+
remaining = get!(logger.message_limits, id, Int(maxlog)::Int)
53+
logger.message_limits[id] = remaining - 1
54+
remaining > 0 || return
55+
end
5356
end
5457
push!(logger.logs, LogRecord(level, msg, _module, group, id, file, line, kwargs))
5558
end

stdlib/Test/test/runtests.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using Test: guardseed
55
using Serialization
66
using Distributed: RemoteException
77

8-
import Logging: Debug, Info, Warn
8+
import Logging: Debug, Info, Warn, with_logger
99

1010
@testset "@test" begin
1111
atol = 1
@@ -870,6 +870,14 @@ erronce() = @error "an error" maxlog=1
870870
# Respect `maxlog` (#41625). We check we only find one logging message.
871871
@test_logs (:error, "an error") (erronce(); erronce())
872872

873+
# Test `respect_maxlog=false`:
874+
test_logger = Test.TestLogger(; respect_maxlog=false)
875+
with_logger(test_logger) do
876+
erronce()
877+
erronce()
878+
end
879+
@test length(test_logger.logs) == 2
880+
873881
# Test failures
874882
fails = @testset NoThrowTestSet "check that @test_logs detects bad input" begin
875883
@test_logs (Warn,) foo(1)

0 commit comments

Comments
 (0)