Skip to content

Commit

Permalink
Improve warning testing for bad patterns (#3)
Browse files Browse the repository at this point in the history
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()).
  • Loading branch information
c42f authored Nov 12, 2020
1 parent 738dc1e commit bbb05b2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
31 changes: 12 additions & 19 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,26 @@ 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)
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

Expand Down
8 changes: 0 additions & 8 deletions test/setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bbb05b2

Please sign in to comment.