Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Git test when global gitconfig signs all commits #35

Closed
wants to merge 1 commit into from
Closed

Fix Git test when global gitconfig signs all commits #35

wants to merge 1 commit into from

Conversation

fpeter8
Copy link

@fpeter8 fpeter8 commented Apr 29, 2024

No description provided.

@jaraco
Copy link
Owner

jaraco commented Jul 28, 2024

I have git signing enabled in my environment, and I don't have issues. Can you describe what behavior this fixes? Is the issue that you're prompted to enter a password to unlock the signing key?

I think it would be better for tests to run in isolation of the user's config, rather than piecemeal reset default configs. I remember doing this recently by monkeypatching the HOME variable (git-repo commit). That commit references the pytest-home plugin, so that's probably the best approach for this project.

@jaraco jaraco closed this in 49c9d80 Jul 28, 2024
@fpeter8
Copy link
Author

fpeter8 commented Aug 1, 2024

sorry for not responding for so long, I forgot about this. I don't know what's the difference between our setups, but on my machine the tests outright fail. My only guess is that the GPG key is not associated with the mock email address, that's why gpg refuses to sign immediately.

Here is the test log for reference:

============================================== test session starts ==============================================
platform linux -- Python 3.12.4, pytest-8.3.2, pluggy-1.5.0
rootdir: /home/fpeter/.cache/yay/python-jaraco.vcs/src/jaraco.vcs-2.3.0
configfile: pytest.ini
plugins: mypy-0.10.3, enabler-3.1.1, typeguard-4.3.0, ruff-0.3.2, cov-5.0.0
collected 66 items                                                                                              

conftest.py ....                                                                                          [  6%]
docs/conf.py ...                                                                                          [ 10%]
jaraco/vcs/base.py ...                                                                                    [ 15%]
jaraco/vcs/cmd.py ...F                                                                                    [ 21%]
jaraco/vcs/library.py ...                                                                                 [ 25%]
jaraco/vcs/reentry.py ...                                                                                 [ 30%]
jaraco/vcs/subprocess.py ...                                                                              [ 34%]
tests/__init__.py ...                                                                                     [ 39%]
tests/test_git.py ....E..E                                                                                [ 51%]
tests/test_managers.py .....                                                                              [ 59%]
tests/test_mercurial.py ....ssssssssssss                                                                  [ 83%]
tests/test_reentry.py ...........                                                                         [100%]

==================================================== ERRORS =====================================================
__________________________________ ERROR at setup of TestTags.test_single_tag ___________________________________

temp_work_dir = PosixPath('/tmp/pytest-of-fpeter/pytest-0/test_single_tag0')

    @pytest.fixture
    def git_repo(temp_work_dir):
        repo = vcs.Git()
        _ensure_present(repo)
        repo._invoke('init')
        repo._invoke('config', 'user.email', 'vip@example.com')
        repo._invoke('config', 'user.name', 'Important User')
        jaraco.path.build(source_tree)
        repo._invoke('add', '.')
>       repo._invoke('commit', '-m', 'committed')

/home/fpeter/.cache/yay/python-jaraco.vcs/src/jaraco.vcs-2.3.0/conftest.py:55: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = Git(.), params = ('commit', '-m', 'committed'), cmd = ['git', 'commit', '-m', 'committed']
proc = <Popen: returncode: 128 args: ['git', 'commit', '-m', 'committed']>, stdout = b''

    def _invoke(self, *params):
        """
        Invoke self.exe as a subprocess
        """
        cmd = [self.exe] + list(params)
        proc = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            cwd=self.location,
            env=self.env,
        )
        stdout, stderr = proc.communicate()
        if not proc.returncode == 0:
>           raise RuntimeError(stderr.strip() or stdout.strip())
E           RuntimeError: b'error: gpg failed to sign the data:\ngpg: skipped "Important User <vip@example.com>": No secret key\n[GNUPG:] INV_SGNR 9 Important User <vip@example.com>\n[GNUPG:] FAILURE sign 17\ngpg: signing failed: No secret key\n\nfatal: failed to write commit object'

/home/fpeter/.cache/yay/python-jaraco.vcs/src/jaraco.vcs-2.3.0/jaraco/vcs/subprocess.py:24: RuntimeError
_______________________ ERROR at setup of TestRevisionTimestamp.test_tagged_rev_timestamp _______________________

temp_work_dir = PosixPath('/tmp/pytest-of-fpeter/pytest-0/test_tagged_rev_timestamp0')

    @pytest.fixture
    def git_repo(temp_work_dir):
        repo = vcs.Git()
        _ensure_present(repo)
        repo._invoke('init')
        repo._invoke('config', 'user.email', 'vip@example.com')
        repo._invoke('config', 'user.name', 'Important User')
        jaraco.path.build(source_tree)
        repo._invoke('add', '.')
>       repo._invoke('commit', '-m', 'committed')

/home/fpeter/.cache/yay/python-jaraco.vcs/src/jaraco.vcs-2.3.0/conftest.py:55: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = Git(.), params = ('commit', '-m', 'committed'), cmd = ['git', 'commit', '-m', 'committed']
proc = <Popen: returncode: 128 args: ['git', 'commit', '-m', 'committed']>, stdout = b''

    def _invoke(self, *params):
        """
        Invoke self.exe as a subprocess
        """
        cmd = [self.exe] + list(params)
        proc = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            cwd=self.location,
            env=self.env,
        )
        stdout, stderr = proc.communicate()
        if not proc.returncode == 0:
>           raise RuntimeError(stderr.strip() or stdout.strip())
E           RuntimeError: b'error: gpg failed to sign the data:\ngpg: skipped "Important User <vip@example.com>": No secret key\n[GNUPG:] INV_SGNR 9 Important User <vip@example.com>\n[GNUPG:] FAILURE sign 17\ngpg: signing failed: No secret key\n\nfatal: failed to write commit object'

/home/fpeter/.cache/yay/python-jaraco.vcs/src/jaraco.vcs-2.3.0/jaraco/vcs/subprocess.py:24: RuntimeError
=================================================== FAILURES ====================================================
_______________________________________ [doctest] jaraco.vcs.cmd.Git.age ________________________________________
212 
213         >>> repo = getfixture('git_repo')
UNEXPECTED EXCEPTION: RuntimeError(b'error: gpg failed to sign the data:\ngpg: skipped "Important User <vip@example.com>": No secret key\n[GNUPG:] INV_SGNR 9 Important User <vip@example.com>\n[GNUPG:] FAILURE sign 17\ngpg: signing failed: No secret key\n\nfatal: failed to write commit object')
Traceback (most recent call last):
  File "/usr/lib/python3.12/doctest.py", line 1368, in __run
    exec(compile(example.source, filename, "single",
  File "<doctest jaraco.vcs.cmd.Git.age[0]>", line 1, in <module>
  File "/usr/lib/python3.12/site-packages/_pytest/fixtures.py", line 531, in getfixturevalue
    fixturedef = self._get_active_fixturedef(argname)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/_pytest/fixtures.py", line 616, in _get_active_fixturedef
    fixturedef.execute(request=subrequest)
  File "/usr/lib/python3.12/site-packages/_pytest/fixtures.py", line 1090, in execute
    result = ihook.pytest_fixture_setup(fixturedef=self, request=request)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/pluggy/_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/pluggy/_callers.py", line 139, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "/usr/lib/python3.12/site-packages/pluggy/_callers.py", line 122, in _multicall
    teardown.throw(exception)  # type: ignore[union-attr]
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/_pytest/setuponly.py", line 36, in pytest_fixture_setup
    return (yield)
            ^^^^^
  File "/usr/lib/python3.12/site-packages/pluggy/_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/_pytest/fixtures.py", line 1139, in pytest_fixture_setup
    result = call_fixture_func(fixturefunc, request, kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/_pytest/fixtures.py", line 897, in call_fixture_func
    fixture_result = fixturefunc(**kwargs)
                     ^^^^^^^^^^^^^^^^^^^^^
  File "/home/fpeter/.cache/yay/python-jaraco.vcs/src/jaraco.vcs-2.3.0/conftest.py", line 55, in git_repo
    repo._invoke('commit', '-m', 'committed')
  File "/home/fpeter/.cache/yay/python-jaraco.vcs/src/jaraco.vcs-2.3.0/jaraco/vcs/subprocess.py", line 24, in _invoke
    raise RuntimeError(stderr.strip() or stdout.strip())
RuntimeError: b'error: gpg failed to sign the data:\ngpg: skipped "Important User <vip@example.com>": No secret key\n[GNUPG:] INV_SGNR 9 Important User <vip@example.com>\n[GNUPG:] FAILURE sign 17\ngpg: signing failed: No secret key\n\nfatal: failed to write commit object'
/home/fpeter/.cache/yay/python-jaraco.vcs/src/jaraco.vcs-2.3.0/jaraco/vcs/cmd.py:213: UnexpectedException

---------- coverage: platform linux, python 3.12.4-final-0 -----------
Name                       Stmts   Miss  Cover   Missing
--------------------------------------------------------
conftest.py                   39     10    74%   37-43, 56-58
docs/conf.py                  10      0   100%
jaraco/vcs/__init__.py         4      0   100%
jaraco/vcs/base.py            67     20    70%   33, 70, 73, 80, 86, 93-97, 104, 108, 114-120, 123-124, 127, 130, 136
jaraco/vcs/cmd.py            128     76    41%   22, 42-45, 51-52, 55-60, 67-69, 80, 86-97, 104-110, 116-123, 128-129, 136-137, 140-141, 144-148, 151, 162-165, 168-169, 175-176, 179-187, 193, 200-202, 205-206, 209, 217-227
jaraco/vcs/library.py         12      5    58%   18-22
jaraco/vcs/reentry.py         51      1    98%   17
jaraco/vcs/subprocess.py      16      0   100%
tests/__init__.py              0      0   100%
tests/test_git.py             35      8    77%   31-35, 50-52
tests/test_managers.py        12      0   100%
tests/test_mercurial.py       96     56    42%   23-27, 51-52, 55-56, 59-61, 73-76, 80, 84-86, 89-98, 104-107, 113-116, 124-134, 141-149, 156-158, 164-166
tests/test_reentry.py         60      0   100%
--------------------------------------------------------
TOTAL                        530    176    67%

jaraco added a commit that referenced this pull request Aug 1, 2024
jaraco added a commit that referenced this pull request Aug 1, 2024
@jaraco
Copy link
Owner

jaraco commented Aug 1, 2024

I also don't have a key for the "Important User". I only have a key for myself. And when I run the tests, the commits are signed by me (and not the committer).

My guess is that your git config is more strict about what commits it will sign.

I thought that the 2.3 release had fixed this issue, but I did some more investigation and it seems that my use of pytestmark wasn't working as intended. The 2.3.1 has additional checks to ensure that tests are isolated from the user's config. Please have a look and let me know if that doesn't fix the issue.

@fpeter8
Copy link
Author

fpeter8 commented Aug 1, 2024

the new version fixed the problem, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants