From d8dc386a182285d64fdd21d1123bcdab4b94d81a Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Wed, 25 Jul 2018 19:53:55 +0200 Subject: [PATCH] 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. --- tests/fake_env.py | 10 ++++++++++ tests/test_usecase.py | 10 ++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/fake_env.py b/tests/fake_env.py index 2e2a5c7..2bc9a51 100644 --- a/tests/fake_env.py +++ b/tests/fake_env.py @@ -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()) diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 4f3d805..bdc1de5 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -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)