Skip to content

Commit

Permalink
Fix: Fix find signing key tests if a global signing key is set
Browse files Browse the repository at this point in the history
Run the tests within a temporary git repository and stash the global
signing key for the find not signing key test.
  • Loading branch information
bjoernricks committed Sep 1, 2022
1 parent 59cc23a commit a7c9b25
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 additions & 41 deletions tests/release/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@
get_project_name,
update_version,
)
from pontos.testing import temp_git_repository


class TestHelperFunctions(unittest.TestCase):
def setUp(self):
self.shell_cmd_runner = lambda x: subprocess.run(
self.shell_cmd_runner = lambda x, cwd=None: subprocess.run(
x,
shell=True,
check=True,
errors="utf-8", # use utf-8 encoding for error output
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=cwd,
)
self.tmpdir = Path(tempfile.gettempdir()) / "testrepo"
self.tmpdir.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -76,53 +78,55 @@ def test_get_project_name(self):

def test_find_signing_key(self):
terminal = MagicMock()
# save possibly set git signing key from user temporarily
try:
saved_key = self.shell_cmd_runner(
"git config user.signingkey"
).stdout.strip()
except subprocess.CalledProcessError:
saved_key = None

self.shell_cmd_runner(
"git config user.signingkey "
"1234567890ABCEDEF1234567890ABCEDEF123456"
)
with temp_git_repository():
self.shell_cmd_runner(
"git config user.signingkey "
"1234567890ABCEDEF1234567890ABCEDEF123456"
)

signing_key = find_signing_key(
terminal, shell_cmd_runner=self.shell_cmd_runner
)
self.assertEqual(
signing_key, "1234567890ABCEDEF1234567890ABCEDEF123456"
)

# reset the previously saved signing key ...
if saved_key is not None:
self.shell_cmd_runner(f"git config user.signingkey {saved_key}")
signing_key = find_signing_key(
terminal, shell_cmd_runner=self.shell_cmd_runner
)
self.assertEqual(
signing_key, "1234567890ABCEDEF1234567890ABCEDEF123456"
)

def test_find_no_signing_key(self):
terminal = MagicMock()
# save possibly set git signing key from user temporarily
try:
saved_key = self.shell_cmd_runner(
"git config user.signingkey"
).stdout.strip()
except subprocess.CalledProcessError:
saved_key = None
saved_key = None

try:
self.shell_cmd_runner("git config --unset user.signingkey")
except subprocess.CalledProcessError as e:
self.assertEqual(e.returncode, 5)

signing_key = find_signing_key(
terminal, shell_cmd_runner=self.shell_cmd_runner
)
self.assertEqual(signing_key, "")

# reset the previously saved signing key ...
if saved_key is not None:
self.shell_cmd_runner(f"git config user.signingkey {saved_key}")
# save possibly set git signing key from user temporarily
try:
saved_key = self.shell_cmd_runner(
"git config --global user.signingkey",
cwd=Path.home(),
).stdout.strip()
except subprocess.CalledProcessError:
saved_key = None

try:
self.shell_cmd_runner(
"git config --global --unset user.signingkey",
cwd=Path.home(),
)
except subprocess.CalledProcessError as e:
self.assertEqual(e.returncode, 5)

with temp_git_repository():
signing_key = find_signing_key(
terminal, shell_cmd_runner=self.shell_cmd_runner
)
self.assertEqual(signing_key, "")

finally:
# reset the previously saved signing key ...
if saved_key is not None:
self.shell_cmd_runner(
f'git config --global user.signingkey "{saved_key}"',
cwd=Path.home(),
)

def test_update_version_not_found(self):
terminal = MagicMock()
Expand Down

0 comments on commit a7c9b25

Please sign in to comment.