diff --git a/src/pip/_internal/req/req_file.py b/src/pip/_internal/req/req_file.py index b15a6ea9a3e..195bde8d12e 100644 --- a/src/pip/_internal/req/req_file.py +++ b/src/pip/_internal/req/req_file.py @@ -367,12 +367,12 @@ def _parse_and_recurse( if req_path in self._parsed_files.keys(): initial_file = self._parsed_files[req_path] tail = ( - f"and again in {initial_file}" + f" and again in {initial_file}" if initial_file is not None else "" ) raise RequirementsFileParseError( - f"{req_path} recursively references itself in {filename} {tail}" + f"{req_path} recursively references itself in {filename}{tail}" ) # Keeping a track where was each file first included in self._parsed_files[req_path] = filename diff --git a/tests/unit/test_req_file.py b/tests/unit/test_req_file.py index d0501e2c254..1ddccdee58e 100644 --- a/tests/unit/test_req_file.py +++ b/tests/unit/test_req_file.py @@ -1,6 +1,7 @@ import collections import logging import os +import re import textwrap from optparse import Values from pathlib import Path @@ -78,12 +79,6 @@ def parse_reqfile( ) -def path_to_string(p: Path) -> str: - # Escape the back slashes in windows path before using it - # in a regex - return str(p).replace("\\", "\\\\") - - def test_read_file_url(tmp_path: Path, session: PipSession) -> None: reqs = tmp_path.joinpath("requirements.txt") reqs.write_text("foo") @@ -365,8 +360,8 @@ def test_recursive_requirements_file( with pytest.raises( RequirementsFileParseError, match=( - f"{path_to_string(req_files[0])} recursively references itself" - f" in {path_to_string(req_files[req_file_count - 1])}" + f"{re.escape(str(req_files[0]))} recursively references itself" + f" in {re.escape(str(req_files[req_file_count - 1]))}" ), ): list(parse_requirements(filename=str(req_files[0]), session=session)) @@ -379,10 +374,10 @@ def test_recursive_requirements_file( with pytest.raises( RequirementsFileParseError, match=( - f"{path_to_string(req_files[req_file_count - 2])} recursively" + f"{re.escape(str(req_files[req_file_count - 2]))} recursively" " references itself in" - f" {path_to_string(req_files[req_file_count - 1])} and again in" - f" {path_to_string(req_files[req_file_count - 3])}" + f" {re.escape(str(req_files[req_file_count - 1]))} and again in" + f" {re.escape(str(req_files[req_file_count - 3]))}" ), ): list(parse_requirements(filename=str(req_files[0]), session=session)) @@ -402,8 +397,8 @@ def test_recursive_relative_requirements_file( with pytest.raises( RequirementsFileParseError, match=( - f"{path_to_string(root_req_file)} recursively references itself in" - f" {path_to_string(level_2_req_file)}" + f"{re.escape(str(root_req_file))} recursively references itself in" + f" {re.escape(str(level_2_req_file))}" ), ): list(parse_requirements(filename=str(root_req_file), session=session))