Skip to content

Commit de7c159

Browse files
Merge pull request #505 from RonnyPfannschmidt/fix-337-relative-to-directory
fix #337: if relative_to is given as folder instead of file
2 parents 404094f + 91d7ed7 commit de7c159

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ v5.0.0
44

55
Breaking changes:
66
* fix #339: strict errors on missing scms when parsing a scm dir to avoid false version lookups
7+
* fix #337: if relative_to is a directory instead of a file,
8+
consider it as direct target instead of the containing folder and print a warning
79

810
Bugfixes:
911

src/setuptools_scm/config.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,24 @@ def _check_tag_regex(value):
2727

2828

2929
def _check_absolute_root(root, relative_to):
30+
trace("l", repr(locals()))
3031
if relative_to:
3132
if os.path.isabs(root) and not root.startswith(relative_to):
3233
warnings.warn(
3334
"absolute root path '%s' overrides relative_to '%s'"
3435
% (root, relative_to)
3536
)
36-
root = os.path.join(os.path.dirname(relative_to), root)
37+
if os.path.isdir(relative_to):
38+
warnings.warn(
39+
"relative_to is expected to be a file,"
40+
" its the directory %r\n"
41+
"assuming the parent directory was passed" % (relative_to,)
42+
)
43+
trace("dir", relative_to)
44+
root = os.path.join(relative_to, root)
45+
else:
46+
trace("file", relative_to)
47+
root = os.path.join(os.path.dirname(relative_to), root)
3748
return os.path.abspath(root)
3849

3950

@@ -94,6 +105,7 @@ def relative_to(self, value):
94105
self._absolute_root = _check_absolute_root(self._root, value)
95106
self._relative_to = value
96107
trace("root", repr(self._absolute_root))
108+
trace("relative_to", repr(value))
97109

98110
@property
99111
def root(self):
@@ -104,6 +116,7 @@ def root(self, value):
104116
self._absolute_root = _check_absolute_root(value, self._relative_to)
105117
self._root = value
106118
trace("root", repr(self._absolute_root))
119+
trace("relative_to", repr(self._relative_to))
107120

108121
@property
109122
def tag_regex(self):

testing/test_basic_api.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,14 @@ def test_pretended(version, monkeypatch):
9090
assert setuptools_scm.get_version() == version
9191

9292

93-
def test_root_relative_to(monkeypatch, tmpdir):
94-
assert_root(monkeypatch, tmpdir.join("alt").strpath)
95-
__file__ = tmpdir.join("module/file.py").strpath
96-
setuptools_scm.get_version(root="../alt", relative_to=__file__)
93+
def test_root_relative_to(monkeypatch, tmp_path):
94+
assert_root(monkeypatch, str(tmp_path / "alt"))
95+
module = tmp_path / "module/file.py"
96+
module.parent.mkdir()
97+
module.touch()
98+
setuptools_scm.get_version(root="../alt", relative_to=str(module))
99+
with pytest.warns(UserWarning, match="relative_to is expected to be a file.*"):
100+
setuptools_scm.get_version(root="../alt", relative_to=str(module.parent))
97101

98102

99103
def test_dump_version(tmpdir):

0 commit comments

Comments
 (0)