Skip to content

Commit 82729eb

Browse files
Allow installing relative paths that go outside tox root folder (#2554)
Co-authored-by: Bernát Gábor <[email protected]> Fixes #2366
1 parent 0b5f95a commit 82729eb

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

docs/changelog/2366.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow installing relative paths that go outside tox root folder. - by :user:`ssbarnea`.

src/tox/tox_env/python/pip/req/file.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@ def __init__(self, req: str, options: dict[str, Any], from_file: str, lineno: in
5757
else:
5858
path = root / req
5959
extra_part = f"[{','.join(sorted(extras))}]" if extras else ""
60-
rel_path = str(path.resolve().relative_to(root))
61-
if rel_path != "." and os.sep not in rel_path: # prefix paths in cwd to not convert them to requirement
62-
rel_path = f".{os.sep}{rel_path}"
60+
try:
61+
rel_path = str(path.resolve().relative_to(root))
62+
# prefix paths in cwd to not convert them to requirement
63+
if rel_path != "." and os.sep not in rel_path:
64+
rel_path = f".{os.sep}{rel_path}"
65+
except ValueError:
66+
rel_path = str(path.resolve())
67+
6368
self._requirement = f"{rel_path}{extra_part}"
6469
self._options = options
6570
self._from_file = from_file

tests/tox_env/python/pip/req/test_file.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,11 @@ def test_requirement_via_file_protocol_na(tmp_path: Path) -> None:
484484
pattern = r"non-local file URIs are not supported on this platform: 'file://magic\.com\.*"
485485
with pytest.raises(ValueError, match=pattern):
486486
assert req_file.options
487+
488+
489+
def test_requirement_to_path_one_level_up(tmp_path: Path) -> None:
490+
other_req = tmp_path / "other.txt"
491+
other_req.write_text("-e ..")
492+
req_file = RequirementsFile(other_req, constraint=False)
493+
result = req_file.requirements
494+
assert result[0].requirement == str(tmp_path.parent.resolve())

tests/tox_env/python/pip/test_req_file.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,8 @@ def test_deps_with_requirements_with_hash(tmp_path: Path) -> None:
3030
"""deps can point to a requirements file that has --hash."""
3131
exp_hash = "sha256:97a702083b0d906517b79672d8501eee470d60ae55df0fa9d4cfba56c7f65a82"
3232
requirements = tmp_path / "requirements.txt"
33-
requirements.write_text(
34-
f"foo==1 --hash {exp_hash}",
35-
)
36-
python_deps = PythonDeps(
37-
raw="-r requirements.txt",
38-
root=tmp_path,
39-
)
33+
requirements.write_text(f"foo==1 --hash {exp_hash}")
34+
python_deps = PythonDeps(raw="-r requirements.txt", root=tmp_path)
4035
assert len(python_deps.requirements) == 1
4136
parsed_req = python_deps.requirements[0]
4237
assert str(parsed_req.requirement) == "foo==1"

0 commit comments

Comments
 (0)