7061: Wrap User...._progress in a context manager
ensure end_progress is called when exception is raised svn: r23092
This commit is contained in:
parent
fffeb1c0b4
commit
dd8263a6e8
@ -32,9 +32,9 @@ import sys
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
if sys.version_info < (3,3):
|
if sys.version_info < (3,3):
|
||||||
from mock import Mock, NonCallableMock
|
from mock import Mock, patch
|
||||||
else:
|
else:
|
||||||
from unittest.mock import Mock, NonCallableMock
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
MOCKING = True
|
MOCKING = True
|
||||||
|
|
||||||
@ -159,6 +159,8 @@ class TestUser_progress(unittest.TestCase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.user = user.User()
|
self.user = user.User()
|
||||||
self.user._fileout = Mock(spec=sys.stderr)
|
self.user._fileout = Mock(spec=sys.stderr)
|
||||||
|
|
||||||
|
def test_can_step_using_with(self):
|
||||||
# Collect baseline output from the old-style interface (begin/step/end)
|
# Collect baseline output from the old-style interface (begin/step/end)
|
||||||
self._progress_begin_step_end()
|
self._progress_begin_step_end()
|
||||||
self.expected_output = list(self.user._fileout.method_calls)
|
self.expected_output = list(self.user._fileout.method_calls)
|
||||||
@ -167,16 +169,22 @@ class TestUser_progress(unittest.TestCase):
|
|||||||
len(self.user._fileout.method_calls) == 0,
|
len(self.user._fileout.method_calls) == 0,
|
||||||
list(self.user._fileout.method_calls))
|
list(self.user._fileout.method_calls))
|
||||||
|
|
||||||
def test(self):
|
|
||||||
with self.user.progress("Foo", "Bar", 0) as step:
|
with self.user.progress("Foo", "Bar", 0) as step:
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
step()
|
step()
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
# Output using `with' differs from one with `progress_...'
|
# Output using `with' differs from one with `progress_...'
|
||||||
self.assertEqual(self.expected_output,
|
self.assertEqual(self.expected_output,
|
||||||
list(self.user._fileout.method_calls))
|
list(self.user._fileout.method_calls))
|
||||||
|
|
||||||
|
def test_ends_progress_upon_exception_in_with(self):
|
||||||
|
with patch('gramps.cli.user.User.end_progress') as MockEP:
|
||||||
|
try:
|
||||||
|
with self.user.progress("Foo", "Bar", 0) as step:
|
||||||
|
raise Exception()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
self.assertTrue(MockEP.called)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
@ -89,7 +89,11 @@ class User():
|
|||||||
@contextmanager
|
@contextmanager
|
||||||
def progress(self, *args, **kwargs):
|
def progress(self, *args, **kwargs):
|
||||||
self.begin_progress(*args, **kwargs)
|
self.begin_progress(*args, **kwargs)
|
||||||
|
try:
|
||||||
yield self.step_progress
|
yield self.step_progress
|
||||||
|
except:
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
self.end_progress()
|
self.end_progress()
|
||||||
|
|
||||||
def prompt(self, title, message, accept_label, reject_label):
|
def prompt(self, title, message, accept_label, reject_label):
|
||||||
|
Loading…
Reference in New Issue
Block a user