Skip to content

Commit

Permalink
Mock in Python 3.12 finds more errors in mock syntax. (#9734)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellert committed Jul 21, 2023
1 parent 68d812e commit ddd4b31
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions certbot-nginx/certbot_nginx/_internal/tests/parser_obj_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,23 @@ def test_is_block(self):
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):
Expand All @@ -91,13 +94,15 @@ def test_parse_raw_no_match(self, parsing_hooks):
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):
Expand Down
2 changes: 1 addition & 1 deletion certbot/certbot/_internal/tests/renewupdater_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_renew_deployer(self):
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):
Expand Down

0 comments on commit ddd4b31

Please sign in to comment.