aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2022-08-02 09:15:20 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2022-08-02 09:17:47 +0200
commitb614ed52a4aaf7678938448ef67f645a15ff2592 (patch)
treeadb7666bf3de85b6746e5bc5cb87923c7213a341
parentdc736d9ad85df6c1ed177b3616e4fe9fa6b6b6f4 (diff)
Add an example of testing a command with the ExternamEditorMixin
-rw-r--r--hazwaz/mixins.py11
-rw-r--r--tests/test_mixins.py23
2 files changed, 34 insertions, 0 deletions
diff --git a/hazwaz/mixins.py b/hazwaz/mixins.py
index 25cc347..5a02238 100644
--- a/hazwaz/mixins.py
+++ b/hazwaz/mixins.py
@@ -7,6 +7,17 @@ class ExternalEditorMixin:
"""
Add facilities to open a file in an external editor to a Command.
"""
+
+ #: A list of editors to try.
+ #:
+ #: Defaults to the value of ``$EDITOR``, followed by
+ #: ``sensible-editor``, followed by ``vi`` as a last resort.
+ #:
+ #: Each editor should be a tuple ``(<executable>, <name>)``, where
+ #: ``<name>`` is printed in case of errors.
+ #:
+ #: To write unittests that use this mixin, you can override this
+ #: attribute with ``[("true"), ("true")]``.
editors = [
(os.environ.get("EDITOR"), "$EDITOR (set to {editor})"),
("sensible-editor", "sensible-editor"),
diff --git a/tests/test_mixins.py b/tests/test_mixins.py
index 1e2f91d..44ae165 100644
--- a/tests/test_mixins.py
+++ b/tests/test_mixins.py
@@ -1,3 +1,4 @@
+import tempfile
import unittest
import hazwaz
@@ -33,5 +34,27 @@ class testEditorMixin(unittest.TestCase):
)
+class MyCommand(hazwaz.MainCommand, hazwaz.mixins.ExternalEditorMixin):
+ """
+ A command that edits a file
+ """
+
+ def main(self):
+ my_file = tempfile.NamedTemporaryFile()
+ self.edit_file_in_external_editor(my_file.name)
+ my_file.close()
+
+
+class testCommandWithMixin(hazwaz.unittest.HazwazTestCase):
+ def test_run(self):
+ cmd = MyCommand()
+ cmd.editors = [("true", "true")]
+ stream = self.run_with_argv(cmd, [
+ "mycommand",
+ ])
+ self.assertEqual(stream["stdout"].getvalue(), "")
+ self.assertEqual(stream["stderr"].getvalue(), "")
+
+
if __name__ == '__main__':
unittest.main()