From fdde8f81ae161e92d42aea7f063d2a0ca199d2e1 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 6 Dec 2020 18:24:55 +0100 Subject: [PATCH] [breaking] fix #339: error when targetted scm parsing fails for missing command --- CHANGELOG.rst | 8 +++++++- src/setuptools_scm/git.py | 5 ++--- src/setuptools_scm/hg.py | 5 ++--- src/setuptools_scm/utils.py | 11 ++++++++--- testing/test_git.py | 14 +++++++++----- testing/test_integration.py | 6 +----- testing/test_mercurial.py | 14 +++++++++----- 7 files changed, 38 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c5703578..83666ae7 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,12 @@ -v4.2.0 +v5.0.0 ====== + +Breaking changes: +* fix #339: strict errors on missing scms when parsing a scm dir to avoid false version lookups + +Bugfixes: + * fix #352: add support for generally ignoring specific vcs roots * fix #471: better error for version bump failing on complex but accepted tag * fix #479: raise indicative error when tags carry non-parsable information diff --git a/src/setuptools_scm/git.py b/src/setuptools_scm/git.py index 76be436c..4fd5d49f 100644 --- a/src/setuptools_scm/git.py +++ b/src/setuptools_scm/git.py @@ -1,5 +1,5 @@ from .config import Configuration -from .utils import do_ex, trace, has_command +from .utils import do_ex, trace, require_command from .version import meta from os.path import isfile, join @@ -92,8 +92,7 @@ def parse( if not config: config = Configuration(root=root) - if not has_command("git"): - return + require_command("git") wd = GitWorkdir.from_potential_worktree(config.absolute_root) if wd is None: diff --git a/src/setuptools_scm/hg.py b/src/setuptools_scm/hg.py index d699d45c..2ac91414 100644 --- a/src/setuptools_scm/hg.py +++ b/src/setuptools_scm/hg.py @@ -1,6 +1,6 @@ import os from .config import Configuration -from .utils import do, trace, data_from_mime, has_command +from .utils import do, trace, data_from_mime, require_command from .version import meta, tags_to_versions @@ -36,8 +36,7 @@ def parse(root, config=None): if not config: config = Configuration(root=root) - if not has_command("hg"): - return + require_command("hg") identity_data = do("hg id -i -b -t", config.absolute_root).split() if not identity_data: return diff --git a/src/setuptools_scm/utils.py b/src/setuptools_scm/utils.py index 15ca83e0..413e98ac 100644 --- a/src/setuptools_scm/utils.py +++ b/src/setuptools_scm/utils.py @@ -132,7 +132,7 @@ def function_has_arg(fn, argname): return argname in argspec -def has_command(name): +def has_command(name, warn=True): try: p = _popen_pipes([name, "help"], ".") except OSError: @@ -141,6 +141,11 @@ def has_command(name): else: p.communicate() res = not p.returncode - if not res: - warnings.warn("%r was not found" % name) + if not res and warn: + warnings.warn("%r was not found" % name, category=RuntimeWarning) return res + + +def require_command(name): + if not has_command(name, warn=False): + raise EnvironmentError("%r was not found" % name) diff --git a/testing/test_git.py b/testing/test_git.py index 6f5246cf..1b57fed1 100644 --- a/testing/test_git.py +++ b/testing/test_git.py @@ -7,7 +7,6 @@ from datetime import datetime from os.path import join as opj from setuptools_scm.file_finder_git import git_find_files -import warnings skip_if_win_27 = pytest.mark.skipif( @@ -16,10 +15,9 @@ ) -with warnings.catch_warnings(): - warnings.filterwarnings("ignore") - if not has_command("git"): - pytestmark = pytest.mark.skip(reason="git executable not found") +pytestmark = pytest.mark.skipif( + not has_command("git", warn=False), reason="git executable not found" +) @pytest.fixture @@ -59,6 +57,12 @@ def test_root_relative_to(tmpdir, wd, monkeypatch): assert res == "0.1.dev0" +def test_git_gone(wd, monkeypatch): + monkeypatch.setenv("PATH", str(wd.cwd / "not-existing")) + with pytest.raises(EnvironmentError, match="'git' was not found"): + git.parse(str(wd.cwd), git.DEFAULT_DESCRIBE) + + @pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/298") @pytest.mark.issue(403) def test_file_finder_no_history(wd, caplog): diff --git a/testing/test_integration.py b/testing/test_integration.py index 45648975..446aac0a 100644 --- a/testing/test_integration.py +++ b/testing/test_integration.py @@ -8,11 +8,7 @@ @pytest.fixture def wd(wd): - try: - wd("git init") - except OSError: - pytest.skip("git executable not found") - + wd("git init") wd("git config user.email test@example.com") wd('git config user.name "a test"') wd.add_command = "git add ." diff --git a/testing/test_mercurial.py b/testing/test_mercurial.py index 815ca00a..265e2074 100644 --- a/testing/test_mercurial.py +++ b/testing/test_mercurial.py @@ -4,13 +4,11 @@ from setuptools_scm.config import Configuration from setuptools_scm.utils import has_command import pytest -import warnings -with warnings.catch_warnings(): - warnings.filterwarnings("ignore") - if not has_command("hg"): - pytestmark = pytest.mark.skip(reason="hg executable not found") +pytestmark = pytest.mark.skipif( + not has_command("hg", warn=False), reason="hg executable not found" +) @pytest.fixture @@ -46,6 +44,12 @@ def test_archival_to_version(expected, data): ) +def test_hg_gone(wd, monkeypatch): + monkeypatch.setenv("PATH", str(wd.cwd / "not-existing")) + with pytest.raises(EnvironmentError, match="'hg' was not found"): + parse(str(wd.cwd)) + + def test_find_files_stop_at_root_hg(wd, monkeypatch): wd.commit_testfile() project = wd.cwd / "project"