From 6cd74a99660489e907069c4184c47bbfab7628ca Mon Sep 17 00:00:00 2001 From: "Fabien C. Y. Benureau" Date: Thu, 17 May 2018 09:22:27 +0900 Subject: [PATCH] error message when editor is missing simplified a bit the ui code, to remove unecessary out-of-class functions (needed anyway because of ui.error calls). --- pubs/uis.py | 64 ++++++++++++++++++++++++------------------- tests/fake_env.py | 9 +++--- tests/test_usecase.py | 14 ++++++---- 3 files changed, 48 insertions(+), 39 deletions(-) diff --git a/pubs/uis.py b/pubs/uis.py index c2a8bfc..aae0052 100644 --- a/pubs/uis.py +++ b/pubs/uis.py @@ -43,32 +43,6 @@ def _get_local_editor(): return os.environ.get('EDITOR', 'nano') -def _editor_input(editor, initial='', suffix='.tmp'): - """Use an editor to get input""" - str_initial = initial.encode('utf-8') # TODO: make it a configuration item - with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as temp_file: - tfile_name = temp_file.name - temp_file.write(str_initial) - cmd = shlex.split(editor) # this enable editor command with option, e.g. gvim -f - cmd.append(tfile_name) - subprocess.call(cmd) - content = read_text_file(tfile_name) - os.remove(tfile_name) - return content - - -def _edit_file(editor, path_to_file, temporary=True): - if temporary: - check_file(path_to_file, fail=True) - content = read_text_file(path_to_file) - content = _editor_input(editor, content) - write_file(path_to_file, content) - else: - cmd = editor.split() # this enable editor command with option, e.g. gvim -f - cmd.append(system_path(path_to_file)) - subprocess.call(cmd) - - def get_ui(): if _ui is None: return PrintUI(config.load_default_conf()) # no editor support. (#FIXME?) @@ -223,7 +197,41 @@ class InputUI(PrintUI): return [True, False][answer] def editor_input(self, initial="", suffix='.tmp'): - return _editor_input(self.editor, initial=initial, suffix=suffix) + """Use an editor to get input""" + str_initial = initial.encode('utf-8') # TODO: make it a configuration item + with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as temp_file: + tfile_name = temp_file.name + temp_file.write(str_initial) + self._call_editor(tfile_name) + content = read_text_file(tfile_name) + os.remove(tfile_name) + return content def edit_file(self, path, temporary): - _edit_file(self.editor, path, temporary=temporary) + if temporary: + check_file(path, fail=True) + content = read_text_file(path) + content = self.editor_input(content) + write_file(path, content) + else: + self._call_editor(path) + + def _call_editor(self, path): + """Call the editor, and checks that no error were raised by the OS""" + cmd = shlex.split(self.editor) # this enable editor command with option, e.g. gvim -f + cmd.append(path) + try: + subprocess.call(cmd) + except OSError as e: + if e.errno == os.errno.ENOENT: + self.error(("Error while calling editor '{}'. The editor may " + "not be present. You can change the text editor " + "that pubs uses by setting the $EDITOR environment " + "variable, or by running `pubs conf` and setting " + "the `edit_cmd` field." + ).format(self.editor)) + # handle file not found error. + self.exit() + else: + # Something else went wrong while trying to run `wget` + self.handle_exception(e) diff --git a/tests/fake_env.py b/tests/fake_env.py index 4f8af1b..2e2a5c7 100644 --- a/tests/fake_env.py +++ b/tests/fake_env.py @@ -74,12 +74,11 @@ class FakeInput(): def as_global(self): for md in self.module_list: md.input = self - md._editor_input = self - md._edit_file = self.input_to_file - # if mdname.endswith('files'): - # md.editor_input = self + if md.__name__ == 'pubs.uis': + md.InputUI.editor_input = self + md.InputUI.edit_file = self.input_to_file - def input_to_file(self, _, path_to_file, temporary=True): + def input_to_file(self, path_to_file, temporary=True): content.write_file(path_to_file, self()) def add_input(self, inp): diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 7d98492..61d4380 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -45,9 +45,8 @@ class FakeSystemExit(Exception): "Exited with code: {}.".format(self.code), *args) -# code for fake fs - -class TestFakeInput(unittest.TestCase): +class TestInput(unittest.TestCase): + """Test that the fake input mechanisms work correctly in the tests""" def test_input(self): input = fake_env.FakeInput(['yes', 'no']) @@ -65,13 +64,16 @@ class TestFakeInput(unittest.TestCase): color.input() def test_editor_input(self): + sample_conf = conf.load_default_conf() + ui = uis.InputUI(sample_conf) + other_input = fake_env.FakeInput(['yes', 'no'], module_list=[uis, color]) other_input.as_global() - self.assertEqual(uis._editor_input(), 'yes') - self.assertEqual(uis._editor_input(), 'no') + self.assertEqual(ui.editor_input(), 'yes') + self.assertEqual(ui.editor_input(), 'no') with self.assertRaises(fake_env.FakeInput.UnexpectedInput): - color.input() + ui.editor_input() class CommandTestCase(fake_env.TestFakeFs):