Fix fake input behavior in tests on unexpected input.

Because of the mechanism for catching exceptions in pubs, the
UnexpectedInput exception raised by FakeInput never reached the catch
statement in the CommandTestCase and raised a FakeSystemExit instead.

This commit monkey-patches the exception handler in the ui at the same
time as the patching of the input functions to ignore UnexptectedInput
at the ui level.
main
Olivier Mangin 7 years ago
parent 2f48f37199
commit d8dc386a18
No known key found for this signature in database
GPG Key ID: D72FEC1C3120A884

@ -77,6 +77,16 @@ class FakeInput():
if md.__name__ == 'pubs.uis':
md.InputUI.editor_input = self
md.InputUI.edit_file = self.input_to_file
# Do not catch UnexpectedInput
original_handler = md.InputUI.handle_exception
def handler(ui, exc):
if isinstance(exc, self.UnexpectedInput):
raise
else:
original_handler(ui, exc)
md.InputUI.handle_exception = handler
def input_to_file(self, path_to_file, temporary=True):
content.write_file(path_to_file, self())

@ -131,7 +131,7 @@ class CommandTestCase(fake_env.TestFakeFs):
outs.append(color.undye(actual_out))
else:
pubs_cmd.execute(actual_cmd.split())
except fake_env.FakeInput.UnexpectedInput as e:
except fake_env.FakeInput.UnexpectedInput:
self.fail('Unexpected input asked by command: {}.'.format(
actual_cmd))
return outs
@ -200,7 +200,7 @@ class TestAlone(CommandTestCase):
def test_alone_misses_command(self):
with self.assertRaises(FakeSystemExit) as cm:
self.execute_cmds(['pubs'])
self.assertEqual(cm.exception.code, 2)
self.assertEqual(cm.exception.code, 2)
def test_alone_prints_help(self):
# capturing the output of `pubs --help` is difficult because argparse
@ -682,13 +682,11 @@ class TestUsecase(DataCommandTestCase):
self.assertFileContentEqual(os.path.expanduser('~/.pubsrc'),
str_fixtures.sample_conf)
def test_editor_abort(self):
def test_editor_aborts(self):
with self.assertRaises(FakeSystemExit):
cmds = ['pubs init',
('pubs add', ['abc', 'n']),
('pubs add', ['abc', 'y', 'abc', 'n']),
'pubs add data/pagerank.bib',
('pubs edit Page99', ['', 'a']),
('pubs edit Page99', ['', 'n']),
]
self.execute_cmds(cmds)

Loading…
Cancel
Save