1
1
import os
2
+ import pathlib
2
3
import re
4
+ import shutil
3
5
4
6
import pytest
5
7
@@ -24,36 +26,45 @@ def tmp_commitizen_project(tmp_git_project):
24
26
yield tmp_git_project
25
27
26
28
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 } " )
46
31
_m = re .search (
47
32
rf"[a-zA-Z0-9 \[\]-_]*{ os .linesep } [ ]*([0-9A-Za-z]*){ os .linesep } [{ os .linesep } a-zA-Z0-9 \[\]-_<>@]*" ,
48
33
_new_key .out ,
49
34
)
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
50
59
51
- if _m :
52
- _key_id = _m .group (1 )
53
60
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 } " )
55
64
56
- yield tmp_commitizen_project
65
+ yield tmp_commitizen_project
66
+ finally :
67
+ shutil .rmtree (gpg_home )
57
68
58
69
59
70
@pytest .fixture ()
0 commit comments