Skip to content

Commit

Permalink
Merge pull request #935 from RonnyPfannschmidt/fix-925-turn-write-to-…
Browse files Browse the repository at this point in the history
…absolute-to-warning

fix #925: allow absolute write_to when its below root
  • Loading branch information
RonnyPfannschmidt authored Sep 29, 2023
2 parents ef3ee88 + 3277b7b commit 056584b
Show file tree
Hide file tree
Showing 3 changed files with 36 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
20 changes: 20 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,21 @@ 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(
# todo: python version specific error list
ValueError,
match=".*VERSION.py' .* .*subdir.*",
):
write_version_files(replace(c, root=subdir), "1.0", v)

0 comments on commit 056584b

Please sign in to comment.