Fix codecov: remove dead code, add test for engine close branch

This commit is contained in:
Diogo Castro 2026-06-19 00:32:29 -03:00
parent ddf90685b9
commit b3a9a6de86
2 changed files with 22 additions and 6 deletions

View File

@ -131,9 +131,7 @@ class OffsiteMiddleware:
return re.compile("") # allow all by default
domains = self._process_domains(allowed_domains, "allowed_domains")
if domains:
return re.compile(rf"^(.*\.)?({'|'.join(domains)})$")
return re.compile("") # allow all if no valid domains remain
return re.compile(rf"^(.*\.)?({'|'.join(domains)})$")
def _get_disallowed_host_regex(self, spider: Spider) -> re.Pattern[str] | None:
"""Build a regex that positively matches disallowed hosts.
@ -146,6 +144,4 @@ class OffsiteMiddleware:
return None
domains = self._process_domains(disallowed_domains, "disallowed_domains")
if domains:
return re.compile(rf"^(.*\.)?({'|'.join(domains)})$")
return None
return re.compile(rf"^(.*\.)?({'|'.join(domains)})$")

View File

@ -1,4 +1,5 @@
import logging
from unittest.mock import AsyncMock, patch
import pytest
@ -125,6 +126,25 @@ def test_process_request_invalid_domains(caplog):
assert "Invalid domain configuration" in caplog.text
def test_invalid_domains_closes_spider(caplog):
crawler = get_crawler(Spider)
crawler.spider = crawler._create_spider(
name="a", allowed_domains=["a.example", None]
)
mw = OffsiteMiddleware.from_crawler(crawler)
mock_engine = AsyncMock()
crawler.engine = mock_engine
with (
patch(
"scrapy.downloadermiddlewares.offsite._schedule_coro"
) as mock_schedule,
caplog.at_level(logging.ERROR),
):
mw.spider_opened(crawler.spider)
assert "Invalid domain configuration" in caplog.text
mock_schedule.assert_called_once()
@pytest.mark.parametrize(
("allowed_domain", "url", "allowed"),
[