diff --git a/changelog.d/20230922_200957_ronny_fix_925_turn_write_to_absolute_to_warning.md b/changelog.d/20230922_200957_ronny_fix_925_turn_write_to_absolute_to_warning.md new file mode 100644 index 00000000..e7ecbfe0 --- /dev/null +++ b/changelog.d/20230922_200957_ronny_fix_925_turn_write_to_absolute_to_warning.md @@ -0,0 +1,4 @@ + +### Changed + +- fix #925: allow write_to to be a absolute path when it's a subdirectory of the root diff --git a/src/setuptools_scm/_integration/dump_version.py b/src/setuptools_scm/_integration/dump_version.py index 27c17b81..d8902432 100644 --- a/src/setuptools_scm/_integration/dump_version.py +++ b/src/setuptools_scm/_integration/dump_version.py @@ -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 ) diff --git a/testing/test_regressions.py b/testing/test_regressions.py index 6bd52463..e2594e89 100644 --- a/testing/test_regressions.py +++ b/testing/test_regressions.py @@ -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 @@ -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: @@ -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)