summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBen Finney <ben+python@benfinney.id.au>2018-04-23 16:22:09 +1000
committerBen Finney <ben@benfinney.id.au>2018-04-23 19:54:45 +1000
commitd01d2e51da45dd87f0abf98b8a856f3b3de69153 (patch)
treecd5f57004d51c5cc670ca95dc8949a6faa85b437 /tests
parent2b766949a7f2dd6025c9af3290d01f531130ed2d (diff)
Extract some context managers for patch objects.
This allows writing less-nested statements.
Diffstat (limited to 'tests')
-rw-r--r--tests/commands/envelope_test.py12
-rw-r--r--tests/commands/global_test.py77
2 files changed, 55 insertions, 34 deletions
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)