Skip to content

Commit

Permalink
fix pypa#549: when CommandNotFound is triggered, try the fallbacks
Browse files Browse the repository at this point in the history
supersedes pypa#783
  • Loading branch information
RonnyPfannschmidt committed Sep 13, 2023
1 parent de08279 commit 978564b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -38,6 +38,7 @@ features
* support passing log levels to SETUPTOOLS_SCM_DEBUG
* support using rich.logging as console log handler if installed
* fix #527: type annotation in default version template
* fix #549: use fallbacks when scm search raises CommandNotFoundError

bugfixes
--------
Expand Down
32 changes: 20 additions & 12 deletions src/setuptools_scm/_get_version.py
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import re
import warnings
from pathlib import Path
Expand All @@ -9,27 +10,34 @@

from . import _config
from . import _entrypoints
from . import _run_cmd
from . import _types as _t
from ._config import Configuration
from ._overrides import _read_pretended_version_for
from ._version_cls import _validate_version_cls
from .version import format_version as _format_version
from .version import ScmVersion

_log = logging.getLogger(__name__)


def parse_scm_version(config: Configuration) -> ScmVersion | None:
if config.parse is not None:
parse_result = config.parse(config.absolute_root, config=config)
if parse_result is not None and not isinstance(parse_result, ScmVersion):
raise TypeError(
f"version parse result was {str!r}\n"
"please return a parsed version (ScmVersion)"
)
return parse_result
else:
entrypoint = "setuptools_scm.parse_scm"
root = config.absolute_root
return _entrypoints.version_from_entrypoint(config, entrypoint, root)
try:
if config.parse is not None:
parse_result = config.parse(config.absolute_root, config=config)
if parse_result is not None and not isinstance(parse_result, ScmVersion):
raise TypeError(
f"version parse result was {str!r}\n"
"please return a parsed version (ScmVersion)"
)
return parse_result
else:
entrypoint = "setuptools_scm.parse_scm"
root = config.absolute_root
return _entrypoints.version_from_entrypoint(config, entrypoint, root)
except _run_cmd.CommandNotFoundError as e:
_log.exception("command %s not found while parsing the scm, using fallbacks", e)
return None


def parse_fallback_version(config: Configuration) -> ScmVersion | None:
Expand Down
4 changes: 4 additions & 0 deletions testing/test_git.py
Expand Up @@ -94,9 +94,13 @@ def test_root_search_parent_directories(

def test_git_gone(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("PATH", str(wd.cwd / "not-existing"))

wd.write("pyproject.toml", "[tool.setuptools_scm]")
with pytest.raises(CommandNotFoundError, match=r"git"):
git.parse(wd.cwd, Configuration(), git.DEFAULT_DESCRIBE)

assert wd.get_version(fallback_version="1.0") == "1.0"


@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/298")
@pytest.mark.issue(403)
Expand Down
3 changes: 3 additions & 0 deletions testing/test_mercurial.py
Expand Up @@ -56,9 +56,12 @@ def test_archival_to_version(expected: str, data: dict[str, str]) -> None:
def test_hg_gone(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("PATH", str(wd.cwd / "not-existing"))
config = Configuration()
wd.write("pyproject.toml", "[tool.setuptools_scm]")
with pytest.raises(CommandNotFoundError, match=r"hg"):
parse(wd.cwd, config=config)

assert wd.get_version(fallback_version="1.0") == "1.0"


def test_find_files_stop_at_root_hg(
wd: WorkDir, monkeypatch: pytest.MonkeyPatch
Expand Down

0 comments on commit 978564b

Please sign in to comment.