From 49a0d1dca182a4f555fd96ea76f70578f3d47b95 Mon Sep 17 00:00:00 2001 From: Andrey Rakhmatullin Date: Mon, 13 Jul 2026 23:04:51 +0500 Subject: [PATCH] Extract write_recording_editor(). --- tests/test_command_genspider.py | 9 +-------- tests/test_commands.py | 6 ++---- tests/utils/cmdline.py | 12 +++++++++++- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/test_command_genspider.py b/tests/test_command_genspider.py index 94db14c30..2872a0e19 100644 --- a/tests/test_command_genspider.py +++ b/tests/test_command_genspider.py @@ -7,14 +7,7 @@ from pathlib import Path import pytest from tests.test_commands import TestProjectBase -from tests.utils.cmdline import call, proc - - -def write_recording_editor(editor: Path) -> None: - """Create an executable editor script that writes the path it is asked to - open (its last argument) into the file given as its first argument.""" - editor.write_text('#!/bin/sh\nprintf "%s" "$2" > "$1"\n', encoding="utf-8") - editor.chmod(0o755) +from tests.utils.cmdline import call, proc, write_recording_editor def find_in_file(filename: Path, regex: str) -> re.Match[str] | None: diff --git a/tests/test_commands.py b/tests/test_commands.py index bb4d96f00..4b65062a0 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -16,7 +16,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.cmdline import call, proc +from tests.utils.cmdline import call, proc, write_recording_editor if TYPE_CHECKING: from pathlib import Path @@ -430,9 +430,7 @@ class TestEditCommand(TestProjectBase): spider = proj_path / self.project_name / "spiders" / "example.py" edited = proj_path / "edited.txt" editor = proj_path / "fake-editor.sh" - # Records the file it is asked to open ($2) into the file given as $1. - editor.write_text('#!/bin/sh\nprintf "%s" "$2" > "$1"\n', encoding="utf-8") - editor.chmod(0o755) + write_recording_editor(editor) monkeypatch.setenv("EDITOR", f"{editor} {edited}") assert call("genspider", "example", "example.com", cwd=proj_path) == 0 diff --git a/tests/utils/cmdline.py b/tests/utils/cmdline.py index 122e0236d..62dff3d4c 100644 --- a/tests/utils/cmdline.py +++ b/tests/utils/cmdline.py @@ -2,12 +2,15 @@ from __future__ import annotations import subprocess import sys -from typing import Any +from typing import TYPE_CHECKING, Any import pytest from scrapy.utils.test import get_testenv +if TYPE_CHECKING: + from pathlib import Path + def call(*args: str, **popen_kwargs: Any) -> int: args = (sys.executable, "-m", "scrapy.cmdline", *args) @@ -36,3 +39,10 @@ def proc(*args: str, **popen_kwargs: Any) -> tuple[int, str, str]: pytest.fail("Command took too much time to complete") return p.returncode, p.stdout, p.stderr + + +def write_recording_editor(editor: Path) -> None: + """Create an executable editor script that writes the path it is asked to + open (its last argument) into the file given as its first argument.""" + editor.write_text('#!/bin/sh\nprintf "%s" "$2" > "$1"\n', encoding="utf-8") + editor.chmod(0o755)