From bbb05b2b27e5696c176e897afbddd005c6544dca Mon Sep 17 00:00:00 2001 From: Chris Foster Date: Fri, 13 Nov 2020 00:08:34 +1000 Subject: [PATCH] Improve warning testing for bad patterns (#3) disable_logging() manipulates a global setting so it may interfere with any other concurrent tests. Instead, use the @test_logs macro to capture logs. I've also chosen to specifically test that a warning is produced here (rather than just use @test_logs min_level=Error which would be more like without_warnings()). --- test/runtests.jl | 31 ++++++++++++------------------- test/setup.jl | 8 -------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 455ecd3..d6a8041 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -45,13 +45,12 @@ end "~", "* *", "*~*", "***", "∀", "~, ***", ".com", "*com", ".*com", ".example.com", "*example.com", ] - for pattern in patterns - ENV["JULIA_NO_VERIFY_HOSTS"] = pattern - for url in TEST_URLS, transport in TRANSPORTS - verify = without_warnings() do - verify_host(url, transport) - end - @test verify + for url in TEST_URLS, transport in TRANSPORTS + for pattern in patterns + # NB: Setting ENV here in the inner loop so that we defeat + # the ENV_HOST_PATTERN_CACHE and get a warning every time. + ENV["JULIA_NO_VERIFY_HOSTS"] = pattern + @test @test_logs (:warn, r"bad host pattern in ENV") match_mode=:any verify_host(url, transport) end end clear_vars!(ENV) @@ -59,19 +58,13 @@ end @testset "only ignore bad patterns in list" begin patterns = ["ok.com,~", "^, ok.com ,, !"] - for pattern in patterns - ENV["JULIA_NO_VERIFY_HOSTS"] = pattern - for url in TEST_URLS - verify = without_warnings() do - verify_host(url) - end - @test verify - end - verify = without_warnings() do - verify_host("ok.com") + for url in TEST_URLS + for pattern in patterns + ENV["JULIA_NO_VERIFY_HOSTS"] = pattern + @test @test_logs (:warn, r"bad host pattern in ENV") match_mode=:any verify_host(url) end - @test !verify - end + @test @test_logs min_level=Logging.Error !verify_host("ok.com") + end clear_vars!(ENV) end diff --git a/test/setup.jl b/test/setup.jl index 49fca78..2e82efb 100644 --- a/test/setup.jl +++ b/test/setup.jl @@ -52,11 +52,3 @@ function clear_vars!(ENV) delete!(ENV, "JULIA_SSH_NO_VERIFY_HOSTS") end -function without_warnings(body::Function) - log_level = Logging.min_enabled_level(current_logger()) - disable_logging(Logging.Warn) - try body() - finally - disable_logging(log_level-1) - end -end