From ddd4b31b1c0bc397f04a9c96176157ab5ae639ee Mon Sep 17 00:00:00 2001 From: Mattias Ellert Date: Sat, 22 Jul 2023 01:44:48 +0200 Subject: [PATCH] Mock in Python 3.12 finds more errors in mock syntax. (#9734) --- .../_internal/tests/parser_obj_test.py | 23 +++++++++++-------- .../_internal/tests/renewupdater_test.py | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/certbot-nginx/certbot_nginx/_internal/tests/parser_obj_test.py b/certbot-nginx/certbot_nginx/_internal/tests/parser_obj_test.py index 710b63fc4..05bc86dbc 100644 --- a/certbot-nginx/certbot_nginx/_internal/tests/parser_obj_test.py +++ b/certbot-nginx/certbot_nginx/_internal/tests/parser_obj_test.py @@ -64,20 +64,23 @@ class ParsingHooksTest(unittest.TestCase): assert Block.should_parse([['block_name'], [['many'], ['statements'], 'here']]) assert Block.should_parse([['if', ' ', '(whatever)'], ['hi']]) - def test_parse_raw(self): + @mock.patch("certbot_nginx._internal.parser_obj.Parsable.parsing_hooks") + def test_parse_raw(self, parsing_hooks): fake_parser1 = mock.Mock() fake_parser1.should_parse = lambda x: True fake_parser2 = mock.Mock() - fake_parser2.should_parse = lambda x: False + fake_parser2.should_parse = lambda x: True + parsing_hooks.return_value = (fake_parser1, fake_parser2,) # First encountered "match" should parse. parse_raw([]) - fake_parser1.called_once() - fake_parser2.not_called() + fake_parser1().parse.assert_called_once() + fake_parser2().parse.assert_not_called() fake_parser1.reset_mock() # "match" that returns False shouldn't parse. + fake_parser1.should_parse = lambda x: False parse_raw([]) - fake_parser1.not_called() - fake_parser2.called_once() + fake_parser1().parse.assert_not_called() + fake_parser2().parse.assert_called_once() @mock.patch("certbot_nginx._internal.parser_obj.Parsable.parsing_hooks") def test_parse_raw_no_match(self, parsing_hooks): @@ -91,13 +94,15 @@ class ParsingHooksTest(unittest.TestCase): with pytest.raises(errors.MisconfigurationError): parse_raw([]) - def test_parse_raw_passes_add_spaces(self): + @mock.patch("certbot_nginx._internal.parser_obj.Parsable.parsing_hooks") + def test_parse_raw_passes_add_spaces(self, parsing_hooks): fake_parser1 = mock.Mock() fake_parser1.should_parse = lambda x: True + parsing_hooks.return_value = (fake_parser1,) parse_raw([]) - fake_parser1.parse.called_with([None]) + fake_parser1().parse.assert_called_with([], False) parse_raw([], add_spaces=True) - fake_parser1.parse.called_with([None, True]) + fake_parser1().parse.assert_called_with([], True) class SentenceTest(unittest.TestCase): diff --git a/certbot/certbot/_internal/tests/renewupdater_test.py b/certbot/certbot/_internal/tests/renewupdater_test.py index eab07cd34..22caae349 100644 --- a/certbot/certbot/_internal/tests/renewupdater_test.py +++ b/certbot/certbot/_internal/tests/renewupdater_test.py @@ -47,7 +47,7 @@ class RenewUpdaterTest(test_util.ConfigTestCase): lineage = mock.MagicMock() mock_deployer = self.renew_deployer updater.run_renewal_deployer(self.config, lineage, mock_deployer) - assert mock_deployer.renew_deploy.called_with(lineage) + mock_deployer.renew_deploy.assert_called_with(lineage) @mock.patch("certbot._internal.updater.logger.debug") def test_updater_skip_dry_run(self, mock_log):