From d01d2e51da45dd87f0abf98b8a856f3b3de69153 Mon Sep 17 00:00:00 2001 From: Ben Finney Date: Mon, 23 Apr 2018 16:22:09 +1000 Subject: Extract some context managers for patch objects. This allows writing less-nested statements. --- tests/commands/envelope_test.py | 12 ++++--- tests/commands/global_test.py | 77 +++++++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 34 deletions(-) (limited to 'tests') diff --git a/tests/commands/envelope_test.py b/tests/commands/envelope_test.py index ee1c0acc..30e21c03 100644 --- a/tests/commands/envelope_test.py +++ b/tests/commands/envelope_test.py @@ -230,8 +230,10 @@ class TestSignCommand(unittest.TestCase): """ env, ui = self._make_ui_mock() - with mock.patch('alot.commands.envelope.settings.get_account_by_address', - mock.Mock(return_value=mock.Mock(gpg_key=None))): + func_patcher = mock.patch( + 'alot.commands.envelope.settings.get_account_by_address', + mock.Mock(return_value=mock.Mock(gpg_key=None))) + with func_patcher: cmd = envelope.SignCommand(action='sign', keyid=None) cmd.apply(ui) @@ -245,8 +247,10 @@ class TestSignCommand(unittest.TestCase): """ env, ui = self._make_ui_mock() - with mock.patch('alot.commands.envelope.settings.get_account_by_address', - mock.Mock(return_value=mock.Mock(gpg_key='sentinel'))): + func_patcher = mock.patch( + 'alot.commands.envelope.settings.get_account_by_address', + mock.Mock(return_value=mock.Mock(gpg_key='sentinel'))) + with func_patcher: cmd = envelope.SignCommand(action='sign', keyid=None) cmd.apply(ui) diff --git a/tests/commands/global_test.py b/tests/commands/global_test.py index e3d91f8a..102d75c2 100644 --- a/tests/commands/global_test.py +++ b/tests/commands/global_test.py @@ -17,6 +17,8 @@ """Tests for global commands.""" from __future__ import absolute_import + +import contextlib import os import tempfile @@ -63,16 +65,21 @@ class TestComposeCommand(unittest.TestCase): # This whole mess is required becasue ComposeCommand.apply is waaaaay # too complicated, it needs to be split into more manageable segments. - with mock.patch('alot.commands.globals.settings.get_account_by_address', - mock.Mock(return_value=account)): - with mock.patch('alot.commands.globals.settings.get_accounts', - mock.Mock(return_value=[account])): - with mock.patch('alot.commands.globals.settings.get_addressbooks', - mock.Mock(side_effect=Stop)): - try: - yield cmd.apply(mock.Mock()) - except Stop: - pass + func_patcher_get_account_by_address = mock.patch( + 'alot.commands.globals.settings.get_account_by_address', + mock.Mock(return_value=account)) + func_patcher_get_accounts = mock.patch( + 'alot.commands.globals.settings.get_accounts', + mock.Mock(return_value=[account])) + func_patcher_get_addressbooks = mock.patch( + 'alot.commands.globals.settings.get_addressbooks', + mock.Mock(side_effect=Stop)) + with contextlib.ExitStack() as stack: + stack.enter_context(func_patcher_get_account_by_address) + stack.enter_context(func_patcher_get_accounts) + stack.enter_context(func_patcher_get_addressbooks) + with self.assertRaises(Stop): + yield cmd.apply(mock.Mock()) self.assertTrue(envelope.sign) self.assertIs(envelope.sign_key, mock.sentinel.gpg_key) @@ -85,16 +92,21 @@ class TestComposeCommand(unittest.TestCase): # This whole mess is required becasue ComposeCommand.apply is waaaaay # too complicated, it needs to be split into more manageable segments. - with mock.patch('alot.commands.globals.settings.get_account_by_address', - mock.Mock(return_value=account)): - with mock.patch('alot.commands.globals.settings.get_accounts', - mock.Mock(return_value=[account])): - with mock.patch('alot.commands.globals.settings.get_addressbooks', - mock.Mock(side_effect=Stop)): - try: - yield cmd.apply(mock.Mock()) - except Stop: - pass + func_patcher_get_account_by_address = mock.patch( + 'alot.commands.globals.settings.get_account_by_address', + mock.Mock(return_value=account)) + func_patcher_get_accounts = mock.patch( + 'alot.commands.globals.settings.get_accounts', + mock.Mock(return_value=[account])) + func_patcher_get_addressbooks = mock.patch( + 'alot.commands.globals.settings.get_addressbooks', + mock.Mock(side_effect=Stop)) + with contextlib.ExitStack() as stack: + stack.enter_context(func_patcher_get_account_by_address) + stack.enter_context(func_patcher_get_accounts) + stack.enter_context(func_patcher_get_addressbooks) + with self.assertRaises(Stop): + yield cmd.apply(mock.Mock()) self.assertFalse(envelope.sign) self.assertIs(envelope.sign_key, None) @@ -107,16 +119,21 @@ class TestComposeCommand(unittest.TestCase): # This whole mess is required becasue ComposeCommand.apply is waaaaay # too complicated, it needs to be split into more manageable segments. - with mock.patch('alot.commands.globals.settings.get_account_by_address', - mock.Mock(return_value=account)): - with mock.patch('alot.commands.globals.settings.get_accounts', - mock.Mock(return_value=[account])): - with mock.patch('alot.commands.globals.settings.get_addressbooks', - mock.Mock(side_effect=Stop)): - try: - yield cmd.apply(mock.Mock()) - except Stop: - pass + func_patcher_get_account_by_address = mock.patch( + 'alot.commands.globals.settings.get_account_by_address', + mock.Mock(return_value=account)) + func_patcher_get_accounts = mock.patch( + 'alot.commands.globals.settings.get_accounts', + mock.Mock(return_value=[account])) + func_patcher_get_addressbooks = mock.patch( + 'alot.commands.globals.settings.get_addressbooks', + mock.Mock(side_effect=Stop)) + with contextlib.ExitStack() as stack: + stack.enter_context(func_patcher_get_account_by_address) + stack.enter_context(func_patcher_get_accounts) + stack.enter_context(func_patcher_get_addressbooks) + with self.assertRaises(Stop): + yield cmd.apply(mock.Mock()) self.assertFalse(envelope.sign) self.assertIs(envelope.sign_key, None) -- cgit v1.2.3