Skip to content

Commit

Permalink
Test.TestLogger: add respect_maxlog option (JuliaLang#44085)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericphanson authored and LilithHafner committed Mar 8, 2022
1 parent f2506f5 commit fb9aa5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
17 changes: 10 additions & 7 deletions stdlib/Test/src/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion stdlib/Test/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fb9aa5d

Please sign in to comment.