Skip to content

Commit bbb05b2

Browse files
authored
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()).
1 parent 738dc1e commit bbb05b2

2 files changed

Lines changed: 12 additions & 27 deletions

File tree

test/runtests.jl

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,26 @@ end
4545
"~", "* *", "*~*", "***", "", "~, ***",
4646
".com", "*com", ".*com", ".example.com", "*example.com",
4747
]
48-
for pattern in patterns
49-
ENV["JULIA_NO_VERIFY_HOSTS"] = pattern
50-
for url in TEST_URLS, transport in TRANSPORTS
51-
verify = without_warnings() do
52-
verify_host(url, transport)
53-
end
54-
@test verify
48+
for url in TEST_URLS, transport in TRANSPORTS
49+
for pattern in patterns
50+
# NB: Setting ENV here in the inner loop so that we defeat
51+
# the ENV_HOST_PATTERN_CACHE and get a warning every time.
52+
ENV["JULIA_NO_VERIFY_HOSTS"] = pattern
53+
@test @test_logs (:warn, r"bad host pattern in ENV") match_mode=:any verify_host(url, transport)
5554
end
5655
end
5756
clear_vars!(ENV)
5857
end
5958

6059
@testset "only ignore bad patterns in list" begin
6160
patterns = ["ok.com,~", "^, ok.com ,, !"]
62-
for pattern in patterns
63-
ENV["JULIA_NO_VERIFY_HOSTS"] = pattern
64-
for url in TEST_URLS
65-
verify = without_warnings() do
66-
verify_host(url)
67-
end
68-
@test verify
69-
end
70-
verify = without_warnings() do
71-
verify_host("ok.com")
61+
for url in TEST_URLS
62+
for pattern in patterns
63+
ENV["JULIA_NO_VERIFY_HOSTS"] = pattern
64+
@test @test_logs (:warn, r"bad host pattern in ENV") match_mode=:any verify_host(url)
7265
end
73-
@test !verify
74-
end
66+
@test @test_logs min_level=Logging.Error !verify_host("ok.com")
67+
end
7568
clear_vars!(ENV)
7669
end
7770

test/setup.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,3 @@ function clear_vars!(ENV)
5252
delete!(ENV, "JULIA_SSH_NO_VERIFY_HOSTS")
5353
end
5454

55-
function without_warnings(body::Function)
56-
log_level = Logging.min_enabled_level(current_logger())
57-
disable_logging(Logging.Warn)
58-
try body()
59-
finally
60-
disable_logging(log_level-1)
61-
end
62-
end

0 commit comments

Comments
 (0)