From edb7575a45c670cfbb11ce7f27a9e07ee296c227 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 22 Sep 2023 20:18:52 +0200 Subject: [PATCH 1/2] fix #925: allow absolute write_to when its below root --- ...ix_925_turn_write_to_absolute_to_warning.md | 4 ++++ .../_integration/dump_version.py | 16 ++++++++++++---- testing/test_regressions.py | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 changelog.d/20230922_200957_ronny_fix_925_turn_write_to_absolute_to_warning.md 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..47341dea 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,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) From 3277b7b9b5892355b39be5304f6c327971a1da37 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 29 Sep 2023 11:44:05 +0200 Subject: [PATCH 2/2] use less specific test to cover all python versions --- testing/test_regressions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/testing/test_regressions.py b/testing/test_regressions.py index 47341dea..e2594e89 100644 --- a/testing/test_regressions.py +++ b/testing/test_regressions.py @@ -119,6 +119,8 @@ def test_write_to_absolute_path_passes_when_subdir_of_root(tmp_path: Path) -> No subdir = tmp_path / "subdir" subdir.mkdir() with pytest.raises( - ValueError, match=".*VERSION.py' does not start with .*subdir.*" + # todo: python version specific error list + ValueError, + match=".*VERSION.py' .* .*subdir.*", ): write_version_files(replace(c, root=subdir), "1.0", v)