Skip to content

Commit 978564b

Browse files
fix #549: when CommandNotFound is triggered, try the fallbacks
supersedes #783
1 parent de08279 commit 978564b

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ features
3838
* support passing log levels to SETUPTOOLS_SCM_DEBUG
3939
* support using rich.logging as console log handler if installed
4040
* fix #527: type annotation in default version template
41+
* fix #549: use fallbacks when scm search raises CommandNotFoundError
4142

4243
bugfixes
4344
--------

src/setuptools_scm/_get_version.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import logging
34
import re
45
import warnings
56
from pathlib import Path
@@ -9,27 +10,34 @@
910

1011
from . import _config
1112
from . import _entrypoints
13+
from . import _run_cmd
1214
from . import _types as _t
1315
from ._config import Configuration
1416
from ._overrides import _read_pretended_version_for
1517
from ._version_cls import _validate_version_cls
1618
from .version import format_version as _format_version
1719
from .version import ScmVersion
1820

21+
_log = logging.getLogger(__name__)
22+
1923

2024
def parse_scm_version(config: Configuration) -> ScmVersion | None:
21-
if config.parse is not None:
22-
parse_result = config.parse(config.absolute_root, config=config)
23-
if parse_result is not None and not isinstance(parse_result, ScmVersion):
24-
raise TypeError(
25-
f"version parse result was {str!r}\n"
26-
"please return a parsed version (ScmVersion)"
27-
)
28-
return parse_result
29-
else:
30-
entrypoint = "setuptools_scm.parse_scm"
31-
root = config.absolute_root
32-
return _entrypoints.version_from_entrypoint(config, entrypoint, root)
25+
try:
26+
if config.parse is not None:
27+
parse_result = config.parse(config.absolute_root, config=config)
28+
if parse_result is not None and not isinstance(parse_result, ScmVersion):
29+
raise TypeError(
30+
f"version parse result was {str!r}\n"
31+
"please return a parsed version (ScmVersion)"
32+
)
33+
return parse_result
34+
else:
35+
entrypoint = "setuptools_scm.parse_scm"
36+
root = config.absolute_root
37+
return _entrypoints.version_from_entrypoint(config, entrypoint, root)
38+
except _run_cmd.CommandNotFoundError as e:
39+
_log.exception("command %s not found while parsing the scm, using fallbacks", e)
40+
return None
3341

3442

3543
def parse_fallback_version(config: Configuration) -> ScmVersion | None:

testing/test_git.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,13 @@ def test_root_search_parent_directories(
9494

9595
def test_git_gone(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> None:
9696
monkeypatch.setenv("PATH", str(wd.cwd / "not-existing"))
97+
98+
wd.write("pyproject.toml", "[tool.setuptools_scm]")
9799
with pytest.raises(CommandNotFoundError, match=r"git"):
98100
git.parse(wd.cwd, Configuration(), git.DEFAULT_DESCRIBE)
99101

102+
assert wd.get_version(fallback_version="1.0") == "1.0"
103+
100104

101105
@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/298")
102106
@pytest.mark.issue(403)

testing/test_mercurial.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ def test_archival_to_version(expected: str, data: dict[str, str]) -> None:
5656
def test_hg_gone(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> None:
5757
monkeypatch.setenv("PATH", str(wd.cwd / "not-existing"))
5858
config = Configuration()
59+
wd.write("pyproject.toml", "[tool.setuptools_scm]")
5960
with pytest.raises(CommandNotFoundError, match=r"hg"):
6061
parse(wd.cwd, config=config)
6162

63+
assert wd.get_version(fallback_version="1.0") == "1.0"
64+
6265

6366
def test_find_files_stop_at_root_hg(
6467
wd: WorkDir, monkeypatch: pytest.MonkeyPatch

0 commit comments

Comments
 (0)