Skip to content

Commit 056584b

Browse files
Merge pull request #935 from RonnyPfannschmidt/fix-925-turn-write-to-absolute-to-warning
fix #925: allow absolute write_to when its below root
2 parents ef3ee88 + 3277b7b commit 056584b

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
### Changed
3+
4+
- fix #925: allow write_to to be a absolute path when it's a subdirectory of the root

src/setuptools_scm/_integration/dump_version.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@ def dump_version(
4242
scm_version: ScmVersion | None = None,
4343
) -> None:
4444
assert isinstance(version, str)
45-
# todo: assert write_to doesnt escape
45+
root = Path(root)
4646
write_to = Path(write_to)
47-
48-
assert not write_to.is_absolute(), f"{write_to=}"
49-
target = Path(root).joinpath(write_to)
47+
if write_to.is_absolute():
48+
# trigger warning on escape
49+
write_to.relative_to(root)
50+
warnings.warn(
51+
f"{write_to=!s} is a absolute path,"
52+
" please switch to using a relative version file",
53+
DeprecationWarning,
54+
)
55+
target = write_to
56+
else:
57+
target = Path(root).joinpath(write_to)
5058
write_version_to_path(
5159
target, template=template, version=version, scm_version=scm_version
5260
)

testing/test_regressions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pprint
44
import subprocess
55
import sys
6+
from dataclasses import replace
67
from importlib.metadata import distribution
78
from importlib.metadata import EntryPoint
89
from pathlib import Path
@@ -13,6 +14,7 @@
1314
from setuptools_scm._run_cmd import run
1415
from setuptools_scm.git import parse
1516
from setuptools_scm.integration import data_from_mime
17+
from setuptools_scm.version import meta
1618

1719

1820
def test_data_from_mime_ignores_body() -> None:
@@ -104,3 +106,21 @@ def test_entrypoints_load() -> None:
104106
failed.append((ep, e))
105107
if failed:
106108
pytest.fail(pprint.pformat(failed))
109+
110+
111+
def test_write_to_absolute_path_passes_when_subdir_of_root(tmp_path: Path) -> None:
112+
c = Configuration(root=tmp_path, write_to=tmp_path / "VERSION.py")
113+
v = meta("1.0", config=c)
114+
from setuptools_scm._get_version_impl import write_version_files
115+
116+
with pytest.warns(DeprecationWarning, match=".*write_to=.* is a absolute.*"):
117+
write_version_files(c, "1.0", v)
118+
write_version_files(replace(c, write_to="VERSION.py"), "1.0", v)
119+
subdir = tmp_path / "subdir"
120+
subdir.mkdir()
121+
with pytest.raises(
122+
# todo: python version specific error list
123+
ValueError,
124+
match=".*VERSION.py' .* .*subdir.*",
125+
):
126+
write_version_files(replace(c, root=subdir), "1.0", v)

0 commit comments

Comments
 (0)