aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lesana/command.py110
-rw-r--r--tests/test_commands.py56
2 files changed, 111 insertions, 55 deletions
diff --git a/lesana/command.py b/lesana/command.py
index 7ca50cf..0054be7 100644
--- a/lesana/command.py
+++ b/lesana/command.py
@@ -9,58 +9,6 @@ import ruamel.yaml
from . import Collection, Entry, TemplatingError
-def edit_file_in_external_editor(filepath):
- # First we try to use $EDITOR
- editor = os.environ.get('EDITOR')
- if editor:
- try:
- subprocess.call([editor, filepath])
- except FileNotFoundError as e:
- if editor in str(e):
- logging.info(
- 'Could not open file {} with $EDITOR (currently {})'
- .format(
- filepath, editor
- )
- )
- else:
- logging.warning("Could not open file {}".format(filepath))
- return False
- else:
- return True
- # then we try to use sensible-editor (which should be available on
- # debian and derivatives)
- try:
- subprocess.call(['sensible-editor', filepath])
- except FileNotFoundError as e:
- if 'sensible-editor' in e.strerror:
- logging.debug(
- "Could not open file {} with editor: sensible-editor".format(
- filepath
- )
- )
- else:
- logging.warning("Could not open file {}".format(filepath))
- return False
- else:
- return True
- # and finally we fallback to vi, because ed is the standard editor,
- # but that would be way too cruel, and vi is also in posix
- try:
- subprocess.call(['vi', filepath])
- except FileNotFoundError as e:
- if 'vi' in e.strerror:
- logging.warning(
- "Could not open file {} with any known editor".format(filepath)
- )
- return False
- else:
- logging.warning("Could not open file {}".format(filepath))
- return False
- else:
- return True
-
-
def _get_first_docstring_line(obj):
try:
return obj.__doc__.split('\n')[1].strip()
@@ -98,6 +46,58 @@ class Command:
self.args = args
self.main()
+ def edit_file_in_external_editor(self, filepath):
+ # First we try to use $EDITOR
+ editor = os.environ.get('EDITOR')
+ if editor:
+ try:
+ subprocess.call([editor, filepath])
+ except FileNotFoundError as e:
+ if editor in str(e):
+ logging.info(
+ 'Could not open file {} with $EDITOR (currently {})'
+ .format(
+ filepath, editor
+ )
+ )
+ else:
+ logging.warning("Could not open file {}".format(filepath))
+ return False
+ else:
+ return True
+ # then we try to use sensible-editor (which should be available on
+ # debian and derivatives)
+ try:
+ subprocess.call(['sensible-editor', filepath])
+ except FileNotFoundError as e:
+ if 'sensible-editor' in e.strerror:
+ logging.debug(
+ "Could not open file {} with editor: sensible-editor"
+ .format(filepath)
+ )
+ else:
+ logging.warning("Could not open file {}".format(filepath))
+ return False
+ else:
+ return True
+ # and finally we fallback to vi, because ed is the standard editor,
+ # but that would be way too cruel, and vi is also in posix
+ try:
+ subprocess.call(['vi', filepath])
+ except FileNotFoundError as e:
+ if 'vi' in e.strerror:
+ logging.warning(
+ "Could not open file {} with any known editor".format(
+ filepath
+ )
+ )
+ return False
+ else:
+ logging.warning("Could not open file {}".format(filepath))
+ return False
+ else:
+ return True
+
class New(Command):
"""
@@ -123,7 +123,7 @@ class New(Command):
new_entry = self.entry_class(collection)
collection.save_entries([new_entry])
filepath = os.path.join(collection.itemdir, new_entry.fname)
- if edit_file_in_external_editor(filepath):
+ if self.edit_file_in_external_editor(filepath):
collection.update_cache([filepath])
if self.args.git:
collection.git_add_files([filepath])
@@ -166,7 +166,7 @@ class Edit(Command):
collection.save_entries([entry])
# and then edit the updated file
filepath = os.path.join(collection.itemdir, entry.fname)
- if edit_file_in_external_editor(filepath):
+ if self.edit_file_in_external_editor(filepath):
collection.update_cache([filepath])
if self.args.git:
collection.git_add_files([filepath])
@@ -388,7 +388,7 @@ class Init(Command):
self.collection_class.init(
self.args.collection,
git_enabled=self.args.git,
- edit_file=edit_file_in_external_editor,
+ edit_file=self.edit_file_in_external_editor,
)
diff --git a/tests/test_commands.py b/tests/test_commands.py
new file mode 100644
index 0000000..c948d69
--- /dev/null
+++ b/tests/test_commands.py
@@ -0,0 +1,56 @@
+import contextlib
+import io
+import tempfile
+import unittest
+
+from lesana import command
+
+
+class Args:
+ def __init__(self, args):
+ self.args = args
+
+ def __getattribute__(self, k):
+ try:
+ return super().__getattribute__(k)
+ except AttributeError:
+ try:
+ return self.args[k]
+ except KeyError as e:
+ raise AttributeError(e)
+
+
+class testCommands(unittest.TestCase):
+ def setUp(self):
+ self.collection_dir = tempfile.TemporaryDirectory()
+
+ def tearDown(self):
+ pass
+
+ def _edit_file(self, filepath):
+ return True
+
+ def _run_command(self, cmd, args):
+ stream = {
+ 'stdout': io.StringIO(),
+ 'stderr': io.StringIO(),
+ }
+ cmd.edit_file_in_external_editor = self._edit_file
+ cmd.args = Args(args)
+ with contextlib.redirect_stdout(stream['stdout']):
+ with contextlib.redirect_stderr(stream['stderr']):
+ cmd.main()
+ return stream
+
+ def test_init(self):
+ args = {
+ 'collection': self.collection_dir.name,
+ 'git': True,
+ }
+ streams = self._run_command(command.Init(), args)
+ self.assertEqual(streams['stdout'].getvalue(), '')
+ self.assertEqual(streams['stderr'].getvalue(), '')
+
+
+if __name__ == '__main__':
+ unittest.main()