Skip to content

Commit

Permalink
fix pypa#925: allow absolute write_to when its below root
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Sep 22, 2023
1 parent 0b2eee6 commit edb7575
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

### Changed

- fix #925: allow write_to to be a absolute path when it's a subdirectory of the root
16 changes: 12 additions & 4 deletions src/setuptools_scm/_integration/dump_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ def dump_version(
scm_version: ScmVersion | None = None,
) -> None:
assert isinstance(version, str)
# todo: assert write_to doesnt escape
root = Path(root)
write_to = Path(write_to)

assert not write_to.is_absolute(), f"{write_to=}"
target = Path(root).joinpath(write_to)
if write_to.is_absolute():
# trigger warning on escape
write_to.relative_to(root)
warnings.warn(
f"{write_to=!s} is a absolute path,"
" please switch to using a relative version file",
DeprecationWarning,
)
target = write_to
else:
target = Path(root).joinpath(write_to)
write_version_to_path(
target, template=template, version=version, scm_version=scm_version
)
Expand Down
18 changes: 18 additions & 0 deletions testing/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pprint
import subprocess
import sys
from dataclasses import replace
from importlib.metadata import distribution
from importlib.metadata import EntryPoint
from pathlib import Path
Expand All @@ -13,6 +14,7 @@
from setuptools_scm._run_cmd import run
from setuptools_scm.git import parse
from setuptools_scm.integration import data_from_mime
from setuptools_scm.version import meta


def test_data_from_mime_ignores_body() -> None:
Expand Down Expand Up @@ -104,3 +106,19 @@ def test_entrypoints_load() -> None:
failed.append((ep, e))
if failed:
pytest.fail(pprint.pformat(failed))


def test_write_to_absolute_path_passes_when_subdir_of_root(tmp_path: Path) -> None:
c = Configuration(root=tmp_path, write_to=tmp_path / "VERSION.py")
v = meta("1.0", config=c)
from setuptools_scm._get_version_impl import write_version_files

with pytest.warns(DeprecationWarning, match=".*write_to=.* is a absolute.*"):
write_version_files(c, "1.0", v)
write_version_files(replace(c, write_to="VERSION.py"), "1.0", v)
subdir = tmp_path / "subdir"
subdir.mkdir()
with pytest.raises(
ValueError, match=".*VERSION.py' does not start with .*subdir.*"
):
write_version_files(replace(c, root=subdir), "1.0", v)

0 comments on commit edb7575

Please sign in to comment.