mirror of https://github.com/scrapy/scrapy.git
Extract write_recording_editor().
This commit is contained in:
parent
b3670369b8
commit
49a0d1dca1
|
|
@ -7,14 +7,7 @@ from pathlib import Path
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from tests.test_commands import TestProjectBase
|
from tests.test_commands import TestProjectBase
|
||||||
from tests.utils.cmdline import call, proc
|
from tests.utils.cmdline import call, proc, write_recording_editor
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def find_in_file(filename: Path, regex: str) -> re.Match[str] | None:
|
def find_in_file(filename: Path, regex: str) -> re.Match[str] | None:
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ from scrapy.commands import ScrapyCommand, ScrapyHelpFormatter, view
|
||||||
from scrapy.exceptions import ScrapyDeprecationWarning
|
from scrapy.exceptions import ScrapyDeprecationWarning
|
||||||
from scrapy.settings import Settings
|
from scrapy.settings import Settings
|
||||||
from scrapy.utils.reactor import _asyncio_reactor_path
|
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:
|
if TYPE_CHECKING:
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
@ -430,9 +430,7 @@ class TestEditCommand(TestProjectBase):
|
||||||
spider = proj_path / self.project_name / "spiders" / "example.py"
|
spider = proj_path / self.project_name / "spiders" / "example.py"
|
||||||
edited = proj_path / "edited.txt"
|
edited = proj_path / "edited.txt"
|
||||||
editor = proj_path / "fake-editor.sh"
|
editor = proj_path / "fake-editor.sh"
|
||||||
# Records the file it is asked to open ($2) into the file given as $1.
|
write_recording_editor(editor)
|
||||||
editor.write_text('#!/bin/sh\nprintf "%s" "$2" > "$1"\n', encoding="utf-8")
|
|
||||||
editor.chmod(0o755)
|
|
||||||
monkeypatch.setenv("EDITOR", f"{editor} {edited}")
|
monkeypatch.setenv("EDITOR", f"{editor} {edited}")
|
||||||
|
|
||||||
assert call("genspider", "example", "example.com", cwd=proj_path) == 0
|
assert call("genspider", "example", "example.com", cwd=proj_path) == 0
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,15 @@ from __future__ import annotations
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from typing import Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from scrapy.utils.test import get_testenv
|
from scrapy.utils.test import get_testenv
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def call(*args: str, **popen_kwargs: Any) -> int:
|
def call(*args: str, **popen_kwargs: Any) -> int:
|
||||||
args = (sys.executable, "-m", "scrapy.cmdline", *args)
|
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")
|
pytest.fail("Command took too much time to complete")
|
||||||
|
|
||||||
return p.returncode, p.stdout, p.stderr
|
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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue