|
5 | 5 | import shutil |
6 | 6 | import subprocess |
7 | 7 | import sys |
| 8 | +import textwrap |
8 | 9 |
|
9 | 10 | from datetime import date |
10 | 11 | from datetime import datetime |
|
19 | 20 | import pytest |
20 | 21 |
|
21 | 22 | import setuptools_scm._file_finders |
| 23 | +import setuptools_scm._file_finders.git |
22 | 24 |
|
23 | 25 | from setuptools_scm import Configuration |
24 | 26 | from setuptools_scm import NonNormalizedVersion |
@@ -861,3 +863,35 @@ def test_git_no_commits_uses_fallback_version(wd: WorkDir) -> None: |
861 | 863 | assert str(version_no_fallback.tag) == "0.0" |
862 | 864 | assert version_no_fallback.distance == 0 |
863 | 865 | assert version_no_fallback.dirty is True |
| 866 | + |
| 867 | + |
| 868 | +@pytest.mark.issue("https://github.com/pypa/setuptools-scm/issues/784") |
| 869 | +def test_dubious_dir( |
| 870 | + wd: WorkDir, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch |
| 871 | +) -> None: |
| 872 | + """Test that we exit clearly if we are in a unsafe directory""" |
| 873 | + wd.commit_testfile() |
| 874 | + git_wd = git.GitWorkdir(wd.cwd) |
| 875 | + |
| 876 | + def _run(*args, **kwargs) -> CompletedProcess: # type: ignore[no-untyped-def] |
| 877 | + """Fake "git rev-parse HEAD" to fail as if you do not own the git repo""" |
| 878 | + stderr = textwrap.dedent(f""" |
| 879 | + fatal: detected dubious ownership in repository at '{git_wd}' |
| 880 | + To add an exception for this directory, call: |
| 881 | +
|
| 882 | + git config --global --add safe.directory /this/is/a/fake/path |
| 883 | + """) |
| 884 | + orig_run = run |
| 885 | + if args[0] == ["git", "rev-parse", "HEAD"]: |
| 886 | + return CompletedProcess( |
| 887 | + args=[], stdout="%cI", stderr=stderr, returncode=128 |
| 888 | + ) |
| 889 | + return orig_run(*args, **kwargs) |
| 890 | + |
| 891 | + monkeypatch.setattr(setuptools_scm._file_finders.git, "_run", _run) |
| 892 | + with pytest.raises(SystemExit): |
| 893 | + git_find_files(str(wd.cwd)) |
| 894 | + |
| 895 | + assert "fatal: detected dubious ownership in repository" in " ".join( |
| 896 | + caplog.messages |
| 897 | + ) |
0 commit comments