Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- '3.12'
- '3.13'
- '3.14'
- '3.15.0-beta - 3.15.0'
os: [ubuntu-latest]
include:
- python-version: '3.7'
Expand Down
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ Changelog
Internal
--------

+ `#26`_: add two more test cases.
+ `#26`_, `#27`_: review test suite.

.. _#26: https://github.com/RKrahl/auto-patch/pull/26
.. _#27: https://github.com/RKrahl/auto-patch/pull/27


.. _changes-1_2_0:
Expand Down
24 changes: 21 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ class mock_subprocess_run:
It fakes all invocations of the zypper binary.
"""
def __init__(self, results):
self.expected_calls = len(results)
self.calls = 0
self.results_iter = iter(results)

def __call__(self, cmd, stdout=None, **kwargs):
self.calls += 1
zypp_res = next(self.results_iter)
assert Path(cmd[0]).name == "zypper"
args = zypper_arg_parser.parse_args(args=cmd[1:])
Expand All @@ -50,6 +53,9 @@ def __call__(self, cmd, stdout=None, **kwargs):
stderr=zypp_res.stderr)
return proc

def check_num_calls(self):
return self.calls == self.expected_calls

class mock_smtp(contextlib.AbstractContextManager):
"""A mock replacement for smtplib.SMTP.
Instead of sending the mail, it pickles it to a well known file.
Expand All @@ -69,12 +75,24 @@ def invoke_auto_patch(zypper_results):
"""Patch the current Python intepreter and execute auto-patch.py.
This function is supposed to be the target of a Process.
"""
import logging
import subprocess
import smtplib
subprocess.run = mock_subprocess_run(zypper_results)
import sys
mock_run = mock_subprocess_run(zypper_results)
subprocess.run = mock_run
smtplib.SMTP = mock_smtp
with auto_patch_path.open("rt") as script:
exec(script.read(), dict(__name__="__main__"))
log = logging.getLogger(__name__)
with auto_patch_path.open("rt") as f:
auto_patch = f.read()
try:
exec(auto_patch, dict(__name__="__main__"))
except SystemExit:
if not mock_run.check_num_calls():
log.critical("premature exit of auto-patch script")
sys.exit(-1)
else:
raise

class ZypperResult:
"""Represent the result of one mock zypper call in AutoPatchCaller.
Expand Down