Skip to content

Commit

Permalink
Merge pull request #505 from RonnyPfannschmidt/fix-337-relative-to-di…
Browse files Browse the repository at this point in the history
…rectory

fix #337: if relative_to is given as folder instead of file
  • Loading branch information
RonnyPfannschmidt authored Dec 11, 2020
2 parents 404094f + 91d7ed7 commit de7c159
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ v5.0.0

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

Bugfixes:

Expand Down
15 changes: 14 additions & 1 deletion src/setuptools_scm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,24 @@ def _check_tag_regex(value):


def _check_absolute_root(root, relative_to):
trace("l", repr(locals()))
if relative_to:
if os.path.isabs(root) and not root.startswith(relative_to):
warnings.warn(
"absolute root path '%s' overrides relative_to '%s'"
% (root, relative_to)
)
root = os.path.join(os.path.dirname(relative_to), root)
if os.path.isdir(relative_to):
warnings.warn(
"relative_to is expected to be a file,"
" its the directory %r\n"
"assuming the parent directory was passed" % (relative_to,)
)
trace("dir", relative_to)
root = os.path.join(relative_to, root)
else:
trace("file", relative_to)
root = os.path.join(os.path.dirname(relative_to), root)
return os.path.abspath(root)


Expand Down Expand Up @@ -94,6 +105,7 @@ def relative_to(self, value):
self._absolute_root = _check_absolute_root(self._root, value)
self._relative_to = value
trace("root", repr(self._absolute_root))
trace("relative_to", repr(value))

@property
def root(self):
Expand All @@ -104,6 +116,7 @@ def root(self, value):
self._absolute_root = _check_absolute_root(value, self._relative_to)
self._root = value
trace("root", repr(self._absolute_root))
trace("relative_to", repr(self._relative_to))

@property
def tag_regex(self):
Expand Down
12 changes: 8 additions & 4 deletions testing/test_basic_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ def test_pretended(version, monkeypatch):
assert setuptools_scm.get_version() == version


def test_root_relative_to(monkeypatch, tmpdir):
assert_root(monkeypatch, tmpdir.join("alt").strpath)
__file__ = tmpdir.join("module/file.py").strpath
setuptools_scm.get_version(root="../alt", relative_to=__file__)
def test_root_relative_to(monkeypatch, tmp_path):
assert_root(monkeypatch, str(tmp_path / "alt"))
module = tmp_path / "module/file.py"
module.parent.mkdir()
module.touch()
setuptools_scm.get_version(root="../alt", relative_to=str(module))
with pytest.warns(UserWarning, match="relative_to is expected to be a file.*"):
setuptools_scm.get_version(root="../alt", relative_to=str(module.parent))


def test_dump_version(tmpdir):
Expand Down

0 comments on commit de7c159

Please sign in to comment.