diff --git a/tests/test_command_check.py b/tests/test_command_check.py index 815f87026..794c6d400 100644 --- a/tests/test_command_check.py +++ b/tests/test_command_check.py @@ -7,7 +7,7 @@ from unittest import TestCase from unittest.mock import MagicMock, Mock, PropertyMock, call, patch from scrapy.commands.check import Command, TextTestResult -from tests.test_commands import TestProjectBase +from tests.utils.base_commands import TestProjectBase from tests.utils.cmdline import proc if TYPE_CHECKING: diff --git a/tests/test_command_crawl.py b/tests/test_command_crawl.py index dd69d36ba..6293e973e 100644 --- a/tests/test_command_crawl.py +++ b/tests/test_command_crawl.py @@ -2,7 +2,7 @@ from __future__ import annotations from typing import TYPE_CHECKING -from tests.test_commands import TestProjectBase +from tests.utils.base_commands import TestProjectBase from tests.utils.cmdline import proc if TYPE_CHECKING: diff --git a/tests/test_command_genspider.py b/tests/test_command_genspider.py index 2872a0e19..f465d0b30 100644 --- a/tests/test_command_genspider.py +++ b/tests/test_command_genspider.py @@ -6,7 +6,7 @@ from pathlib import Path import pytest -from tests.test_commands import TestProjectBase +from tests.utils.base_commands import TestProjectBase from tests.utils.cmdline import call, proc, write_recording_editor diff --git a/tests/test_command_parse.py b/tests/test_command_parse.py index c210a06e8..a3581d764 100644 --- a/tests/test_command_parse.py +++ b/tests/test_command_parse.py @@ -8,7 +8,7 @@ import pytest from scrapy.commands import parse from scrapy.settings import Settings -from tests.test_commands import TestProjectBase +from tests.utils.base_commands import TestProjectBase from tests.utils.cmdline import call, proc if TYPE_CHECKING: diff --git a/tests/test_commands.py b/tests/test_commands.py index 4b65062a0..9e7d4d5a1 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -4,7 +4,6 @@ import argparse import json import sys from io import StringIO -from shutil import copytree from typing import TYPE_CHECKING from unittest import mock @@ -16,6 +15,7 @@ from scrapy.commands import ScrapyCommand, ScrapyHelpFormatter, view from scrapy.exceptions import ScrapyDeprecationWarning from scrapy.settings import Settings from scrapy.utils.reactor import _asyncio_reactor_path +from tests.utils.base_commands import TestProjectBase from tests.utils.cmdline import call, proc, write_recording_editor if TYPE_CHECKING: @@ -109,29 +109,6 @@ class TestCommandSettings: ) -class TestProjectBase: - """A base class for tests that may need a Scrapy project.""" - - project_name = "testproject" - - @pytest.fixture(scope="session") - def _proj_path_cached(self, tmp_path_factory: pytest.TempPathFactory) -> Path: - """Create a Scrapy project in a temporary directory and return its path. - - Used as a cache for ``proj_path``. - """ - tmp_path = tmp_path_factory.mktemp("proj") - call("startproject", self.project_name, cwd=tmp_path) - return tmp_path / self.project_name - - @pytest.fixture - def proj_path(self, tmp_path: Path, _proj_path_cached: Path) -> Path: - """Copy a pre-generated Scrapy project into a temporary directory and return its path.""" - proj_path = tmp_path / self.project_name - copytree(_proj_path_cached, proj_path) - return proj_path - - class TestCommandCrawlerProcess(TestProjectBase): """Test that the command uses the expected kind of *CrawlerProcess and produces expected errors when needed.""" diff --git a/tests/utils/base_commands.py b/tests/utils/base_commands.py new file mode 100644 index 000000000..594544c83 --- /dev/null +++ b/tests/utils/base_commands.py @@ -0,0 +1,34 @@ +from __future__ import annotations + +from shutil import copytree +from typing import TYPE_CHECKING + +import pytest + +from tests.utils.cmdline import call + +if TYPE_CHECKING: + from pathlib import Path + + +class TestProjectBase: + """A base class for tests that may need a Scrapy project.""" + + project_name = "testproject" + + @pytest.fixture(scope="session") + def _proj_path_cached(self, tmp_path_factory: pytest.TempPathFactory) -> Path: + """Create a Scrapy project in a temporary directory and return its path. + + Used as a cache for ``proj_path``. + """ + tmp_path = tmp_path_factory.mktemp("proj") + call("startproject", self.project_name, cwd=tmp_path) + return tmp_path / self.project_name + + @pytest.fixture + def proj_path(self, tmp_path: Path, _proj_path_cached: Path) -> Path: + """Copy a pre-generated Scrapy project into a temporary directory and return its path.""" + proj_path = tmp_path / self.project_name + copytree(_proj_path_cached, proj_path) + return proj_path