Skip to content

Commit 9bab971

Browse files
committed
test(commands): stores gpg keyring in a temporary directory
GNUPGHOME must be less than 104 characters in order for the gpg-agent to run.
1 parent 2ff9f15 commit 9bab971

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

tests/conftest.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
2+
import pathlib
23
import re
4+
import shutil
35

46
import pytest
57

@@ -24,36 +26,45 @@ def tmp_commitizen_project(tmp_git_project):
2426
yield tmp_git_project
2527

2628

27-
@pytest.fixture(scope="function")
28-
def tmp_commitizen_project_with_gpg(tmp_commitizen_project):
29-
_gpg_file = tmp_commitizen_project.join("gpg_setup")
30-
_signer_mail = "action@github.com"
31-
with open(_gpg_file, "w", newline="") as f:
32-
f.write("Key-Type: RSA" + os.linesep)
33-
f.write("Key-Length: 2048" + os.linesep)
34-
f.write("Subkey-Type: RSA" + os.linesep)
35-
f.write("Subkey-Length: 2048" + os.linesep)
36-
f.write("Name-Real: GitHub Action" + os.linesep)
37-
f.write("Name-Comment: with stupid passphrase" + os.linesep)
38-
f.write(f"Name-Email: {_signer_mail}" + os.linesep)
39-
f.write("Expire-Date: 1" + os.linesep)
40-
41-
cmd.run(
42-
f"gpg --batch --passphrase '' --pinentry-mode loopback --generate-key {_gpg_file}"
43-
)
44-
45-
_new_key = cmd.run(f"gpg --list-secret-keys {_signer_mail}")
29+
def _get_gpg_keyid(signer_mail):
30+
_new_key = cmd.run(f"gpg --list-secret-keys {signer_mail}")
4631
_m = re.search(
4732
rf"[a-zA-Z0-9 \[\]-_]*{os.linesep}[ ]*([0-9A-Za-z]*){os.linesep}[{os.linesep}a-zA-Z0-9 \[\]-_<>@]*",
4833
_new_key.out,
4934
)
35+
return _m.group(1) if _m else None
36+
37+
38+
@pytest.fixture(scope="function")
39+
def tmp_commitizen_project_with_gpg(tmp_commitizen_project):
40+
_signer = "GitHub Action"
41+
_signer_mail = "action@github.com"
42+
43+
# create a temporary GPGHOME to store a temporary keyring.
44+
# Home path must be less than 104 characters
45+
gpg_home = pathlib.Path.home() / f".gnupg-commitizen-unittest-{os.getpid()}"
46+
try:
47+
gpg_home.mkdir(mode=0o700, exist_ok=True)
48+
os.environ["GNUPGHOME"] = str(gpg_home) # tempdir = temp keyring
49+
key_id = _get_gpg_keyid(_signer_mail)
50+
if not key_id:
51+
c = cmd.run(
52+
f"gpg --batch --yes --debug-quick-random --passphrase '' --quick-gen-key '{_signer} {_signer_mail}'"
53+
)
54+
if c.return_code != 0:
55+
raise Exception(f"gpg keygen failed with err: '{c.err}'")
56+
key_id = _get_gpg_keyid(_signer_mail)
57+
58+
assert key_id
5059

51-
if _m:
52-
_key_id = _m.group(1)
5360
cmd.run("git config commit.gpgsign true")
54-
cmd.run(f"git config user.signingkey {_key_id}")
61+
cmd.run(f"git config user.name {_signer}")
62+
cmd.run(f"git config user.email {_signer_mail}")
63+
cmd.run(f"git config user.signingkey {key_id}")
5564

56-
yield tmp_commitizen_project
65+
yield tmp_commitizen_project
66+
finally:
67+
shutil.rmtree(gpg_home)
5768

5869

5970
@pytest.fixture()

tests/utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44
from typing import Optional
55

6-
from commitizen import cmd, git
6+
from commitizen import cmd, exceptions, git
77

88

99
class FakeCommand:
@@ -18,8 +18,12 @@ def create_file_and_commit(message: str, filename: Optional[str] = None):
1818
filename = str(uuid.uuid4())
1919

2020
Path(f"./{filename}").touch()
21-
cmd.run("git add .")
22-
git.commit(message)
21+
c = cmd.run("git add .")
22+
if c.return_code != 0:
23+
raise exceptions.CommitError(c.err)
24+
c = git.commit(message)
25+
if c.return_code != 0:
26+
raise exceptions.CommitError(c.err)
2327

2428

2529
def wait_for_tag():

0 commit comments

Comments
 (0)