From fb9aa5d0bef10f2135fcf13febde7e7b9171a313 Mon Sep 17 00:00:00 2001 From: Eric Hanson <5846501+ericphanson@users.noreply.github.com> Date: Wed, 9 Feb 2022 17:59:56 -0500 Subject: [PATCH] Test.TestLogger: add `respect_maxlog` option (#44085) --- stdlib/Test/src/logging.jl | 17 ++++++++++------- stdlib/Test/test/runtests.jl | 10 +++++++++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/stdlib/Test/src/logging.jl b/stdlib/Test/src/logging.jl index 1fb21e6c93434e..b8e4d00e33b5be 100644 --- a/stdlib/Test/src/logging.jl +++ b/stdlib/Test/src/logging.jl @@ -27,10 +27,11 @@ mutable struct TestLogger <: AbstractLogger catch_exceptions::Bool shouldlog_args message_limits::Dict{Any,Int} + respect_maxlog::Bool end -TestLogger(; min_level=Info, catch_exceptions=false) = - TestLogger(LogRecord[], min_level, catch_exceptions, nothing, Dict{Any, Int}()) +TestLogger(; min_level=Info, catch_exceptions=false, respect_maxlog=true) = + TestLogger(LogRecord[], min_level, catch_exceptions, nothing, Dict{Any, Int}(), respect_maxlog) Logging.min_enabled_level(logger::TestLogger) = logger.min_level function Logging.shouldlog(logger::TestLogger, level, _module, group, id) @@ -45,11 +46,13 @@ end function Logging.handle_message(logger::TestLogger, level, msg, _module, group, id, file, line; kwargs...) @nospecialize - maxlog = get(kwargs, :maxlog, nothing) - if maxlog isa Core.BuiltinInts - remaining = get!(logger.message_limits, id, Int(maxlog)::Int) - logger.message_limits[id] = remaining - 1 - remaining > 0 || return + if logger.respect_maxlog + maxlog = get(kwargs, :maxlog, nothing) + if maxlog isa Core.BuiltinInts + remaining = get!(logger.message_limits, id, Int(maxlog)::Int) + logger.message_limits[id] = remaining - 1 + remaining > 0 || return + end end push!(logger.logs, LogRecord(level, msg, _module, group, id, file, line, kwargs)) end diff --git a/stdlib/Test/test/runtests.jl b/stdlib/Test/test/runtests.jl index 260202462d81a3..579b81cd5ace96 100644 --- a/stdlib/Test/test/runtests.jl +++ b/stdlib/Test/test/runtests.jl @@ -5,7 +5,7 @@ using Test: guardseed using Serialization using Distributed: RemoteException -import Logging: Debug, Info, Warn +import Logging: Debug, Info, Warn, with_logger @testset "@test" begin atol = 1 @@ -870,6 +870,14 @@ erronce() = @error "an error" maxlog=1 # Respect `maxlog` (#41625). We check we only find one logging message. @test_logs (:error, "an error") (erronce(); erronce()) + # Test `respect_maxlog=false`: + test_logger = Test.TestLogger(; respect_maxlog=false) + with_logger(test_logger) do + erronce() + erronce() + end + @test length(test_logger.logs) == 2 + # Test failures fails = @testset NoThrowTestSet "check that @test_logs detects bad input" begin @test_logs (Warn,) foo(1)