Skip to content

Commit 084f60b

Browse files
bhelgsLee-W
authored andcommitted
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 2c8d5b6 commit 084f60b

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

tests/conftest.py

+29-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
import tempfile
34

45
import pytest
56

@@ -24,34 +25,39 @@ def tmp_commitizen_project(tmp_git_project):
2425
yield tmp_git_project
2526

2627

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}")
28+
def _get_gpg_keyid(signer_mail):
29+
_new_key = cmd.run(f"gpg --list-secret-keys {signer_mail}")
4630
_m = re.search(
4731
rf"[a-zA-Z0-9 \[\]-_]*{os.linesep}[ ]*([0-9A-Za-z]*){os.linesep}[{os.linesep}a-zA-Z0-9 \[\]-_<>@]*",
4832
_new_key.out,
4933
)
34+
return _m.group(1) if _m else None
35+
5036

51-
if _m:
52-
_key_id = _m.group(1)
53-
cmd.run("git config commit.gpgsign true")
54-
cmd.run(f"git config user.signingkey {_key_id}")
37+
@pytest.fixture(scope="function")
38+
def tmp_commitizen_project_with_gpg(tmp_commitizen_project):
39+
signer = "GitHub Action"
40+
signer_mail = "action@github.com"
41+
42+
# create a temporary GPGHOME to store a temporary keyring.
43+
# Home path must be less than 104 characters
44+
gpg_home = tempfile.TemporaryDirectory(suffix="_cz")
45+
os.environ["GNUPGHOME"] = gpg_home.name # tempdir = temp keyring
46+
47+
# create a key (a keyring will be generated within GPUPGHOME)
48+
c = cmd.run(
49+
f"gpg --batch --yes --debug-quick-random --passphrase '' --quick-gen-key '{signer} {signer_mail}'"
50+
)
51+
if c.return_code != 0:
52+
raise Exception(f"gpg keygen failed with err: '{c.err}'")
53+
key_id = _get_gpg_keyid(signer_mail)
54+
assert key_id
55+
56+
# configure git
57+
cmd.run("git config commit.gpgsign true")
58+
cmd.run(f"git config user.name {signer}")
59+
cmd.run(f"git config user.email {signer_mail}")
60+
cmd.run(f"git config user.signingkey {key_id}")
5561

5662
yield tmp_commitizen_project
5763

tests/utils.py

+7-3
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)