From 82eac2165d3a8bc20abea52d32a1037be8472b69 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Tue, 15 Mar 2022 09:52:31 +0100 Subject: Add a mixin with the ability to open a file in an external editor --- tests/test_mixins.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/test_mixins.py (limited to 'tests/test_mixins.py') diff --git a/tests/test_mixins.py b/tests/test_mixins.py new file mode 100644 index 0000000..1e2f91d --- /dev/null +++ b/tests/test_mixins.py @@ -0,0 +1,37 @@ +import unittest + +import hazwaz + + +class testEditorMixin(unittest.TestCase): + def test_open_with_cat_existing_file(self): + subcmd = hazwaz.mixins.ExternalEditorMixin() + subcmd.editors = [("cat", "cat")] + # TODO: suppress this output in the tests (we can't use + # contextlib.redirect_stdout because that doesn't redirect the + # stdout used by subprocess. + res = subcmd.edit_file_in_external_editor("/bin/fgrep") + self.assertTrue(res) + + def test_open_with_cat_missing_file(self): + subcmd = hazwaz.mixins.ExternalEditorMixin() + subcmd.editors = [("cat", "cat")] + # TODO: suppress this output in the tests (we can't use + # contextlib.redirect_stderr because that doesn't redirect the + # stderr used by subprocess. + res = subcmd.edit_file_in_external_editor("no_such_file") + self.assertFalse(res) + + def test_open_with_non_existing_editor(self): + subcmd = hazwaz.mixins.ExternalEditorMixin() + subcmd.editors = [("no_such_command", "no_such_command")] + with self.assertLogs() as cm: + subcmd.edit_file_in_external_editor("no_such_file") + self.assertIn( + "Could not open file no_such_file with no_such_command", + cm.output[0] + ) + + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.3