From fbb4aedf4e51763b5a2a416144a5e8e6b0cc096f Mon Sep 17 00:00:00 2001 From: Lia Launtz Date: Thu, 26 Feb 2026 13:58:33 -0500 Subject: [PATCH] removed edit help syntax test --- tests/test_command_edit.py | 93 ++++++++++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 8 deletions(-) diff --git a/tests/test_command_edit.py b/tests/test_command_edit.py index 1a040912e..c4160c201 100644 --- a/tests/test_command_edit.py +++ b/tests/test_command_edit.py @@ -1,10 +1,15 @@ from __future__ import annotations +import os +from contextlib import suppress from pathlib import Path +from unittest import mock import pytest +from scrapy.cmdline import ScrapyArgumentParser from scrapy.commands.edit import Command +from scrapy.utils.project import get_project_settings from tests.test_commands import TestProjectBase from tests.utils.cmdline import call, proc @@ -15,14 +20,45 @@ class TestEditCommand(TestProjectBase): @pytest.fixture def create_spider(self, proj_path: Path): """creates spider needed for tests""" + # setup: add "cat" as test environment editor + try: + editor_to_restore = os.environ["EDITOR"] + except KeyError: + editor_to_restore = None + finally: + os.environ["EDITOR"] = "cat" + + # setup: preserve scrapy settings in local environment + try: + scrapy_settings_to_restore = os.environ["SCRAPY_SETTINGS_MODULE"] + except KeyError: + scrapy_settings_to_restore = None + # setup: create spider to edit test_name = "test_name" call("genspider", test_name, "test.com", cwd=proj_path) spider = proj_path / self.project_name / "spiders" / "test_name.py" yield proj_path, spider, test_name + # teardown: remove spider from project Path.unlink(spider) + # teardown: restore previous editor + if editor_to_restore is not None: + os.environ["EDITOR"] = editor_to_restore + else: + # remove editor from os.environ if it exists + with suppress(KeyError): + os.environ.pop("EDITOR") + + # teardown: restore project settings in local environment + if scrapy_settings_to_restore is not None: + os.environ["SCRAPY_SETTINGS_MODULE"] = scrapy_settings_to_restore + else: + # remove "SCRAPY_SETTINGS_MODULE" from os.environ if it exists + with suppress(KeyError): + os.environ.pop("SCRAPY_SETTINGS_MODULE") + def test_edit_valid_spider(self, create_spider) -> None: """test call to edit command with correct spider name""" proj_path, spider, test_name = create_spider @@ -33,17 +69,58 @@ class TestEditCommand(TestProjectBase): """test call to edit if no spider has been specified""" assert call("edit", "not_a_valid_spider", cwd=proj_path) == 1 - def test_edit_help_syntax(self, proj_path: Path) -> None: - """Check that long description and syntax are included in edit -h""" - rtn_code, out, _ = proc("edit", "-h", cwd=proj_path) - assert rtn_code == 0 - cmd = Command() - assert cmd.long_desc() in out - assert cmd.syntax() in out - def test_edit_short_desc(self, proj_path: Path) -> None: """Check that short description included in scrapy -h""" rtn_code, out, _ = proc("-h", cwd=proj_path) assert rtn_code == 0 cmd = Command() assert cmd.short_desc() in out + + def test_edit_command_valid_directory(self, create_spider): + """calls editor command directly from project directory""" + proj_path, spider, test_name = create_spider + + # change into cwd + current = Path.cwd() + os.chdir(proj_path) + + # create edit command object + # teardown required as get_project_settings() mutates os.environ + cmd = Command() + cmd.settings = get_project_settings() + # grabs system editor to mock + editor = cmd.settings.get("EDITOR") + + # parse commandline arguments + parser = ScrapyArgumentParser() + opts, _ = parser.parse_known_args(["edit", test_name]) + + with mock.patch("scrapy.commands.edit.os.system", return_value=0) as mock_sys: + cmd.run([test_name], opts) + mock_sys.assert_called_once_with(f'{editor} "{spider}"') + + # move back into previous cwd + os.chdir(current) + + def test_edit_as_subprocess(self, create_spider): + """check that subprocess calls editor""" + proj_path, _, test_name = create_spider + + spider_text = """ +class TestNameSpider(scrapy.Spider): + name = "test_name" +""" + + _, out, _ = proc("edit", test_name, cwd=proj_path) + assert spider_text in out + + def test_edit_subprocess_no_project(self, create_spider): + """check that subprocess does not call editor outside project""" + proj_path, _, test_name = create_spider + + no_proj = """ +The edit command is not available from this location. +These commands are only available from within a project: check, crawl, edit, list, parse. +""" + _, out, _ = proc("edit", test_name, cwd=proj_path.parent) + assert no_proj in out