]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Replaced hand-crafted code with python-editor.
authorMichel Albert <michel@albert.lu>
Thu, 16 Jul 2015 06:06:01 +0000 (08:06 +0200)
committerMichel Albert <michel@albert.lu>
Thu, 16 Jul 2015 06:09:06 +0000 (08:09 +0200)
alembic/command.py
alembic/config.py
alembic/util/__init__.py
alembic/util/os_helpers.py [deleted file]
setup.py
tests/test_command.py
tests/test_os_helpers.py [deleted file]

index 644a1691a8c894daf57c978097d5e8e0102f54e0..37ba4afbce27d07c533ab4e92bbbf436f0e9f4cb 100644 (file)
@@ -5,6 +5,8 @@ from .runtime.environment import EnvironmentContext
 from . import util
 from . import autogenerate as autogen
 
+import editor
+
 
 def list_templates(config):
     """List available templates"""
@@ -363,6 +365,6 @@ def edit(config):
     head = next(revisions)
 
     try:
-        util.open_in_editor(head.path)
-    except OSError as exc:
+        editor.edit(head.path)
+    except Exception as exc:
         raise util.CommandError('Error executing editor (%s)' % (exc,))
index bd0ea4f5b74dfc99642a6da2e6053b3b3387a1d2..b3fc36fc71b23e1977f02698e92687f942812b4d 100644 (file)
@@ -351,7 +351,7 @@ class CommandLine(object):
                         action="store",
                         help="Specify a revision range; "
                         "format is [start]:[end]")
-                ),
+                )
             }
             positional_help = {
                 'directory': "location of scripts directory",
index 0fe9f3c53e26c8028f518cbded9da57d96d88405..bd7196ce4749dc3e4930d8687b5f4aad1deb270b 100644 (file)
@@ -9,8 +9,6 @@ from .pyfiles import (  # noqa
 from .sqla_compat import (  # noqa
     sqla_07, sqla_079, sqla_08, sqla_083, sqla_084, sqla_09, sqla_092,
     sqla_094, sqla_094, sqla_099, sqla_100, sqla_105)
-from .os_helpers import ( # noqa
-    open_in_editor)
 
 
 class CommandError(Exception):
diff --git a/alembic/util/os_helpers.py b/alembic/util/os_helpers.py
deleted file mode 100644 (file)
index ccbfa11..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-from os.path import join, exists
-from subprocess import check_call
-import os
-
-
-def open_in_editor(filename, environ=None):
-    '''
-    Opens the given file in a text editor. If the environment vaiable ``EDITOR``
-    is set, this is taken as preference.
-
-    Otherwise, a list of commonly installed editors is tried.
-
-    If no editor matches, an :py:exc:`OSError` is raised.
-
-    :param filename: The filename to open. Will be passed  verbatim to the
-        editor command.
-    :param environ: An optional drop-in replacement for ``os.environ``. Used
-        mainly for testing.
-    '''
-
-    environ = environ or os.environ
-
-    # Look for an editor. Prefer the user's choice by env-var, fall back to most
-    # commonly installed editor (nano/vim)
-    candidates = [
-        '/usr/bin/sensible-editor',
-        '/usr/bin/nano',
-        '/usr/bin/vim',
-    ]
-
-    if 'EDITOR' in environ:
-        user_choice = environ['EDITOR']
-        if '/' not in user_choice:
-            # Assuming this is on the PATH, we need to determine it's absolute
-            # location. Otherwise, ``check_call`` will fail
-            for path in environ.get('PATH', '').split(os.pathsep):
-                if exists(join(path, user_choice)):
-                    user_choice = join(path, user_choice)
-                    break
-        candidates.insert(0, user_choice)
-
-    for path in candidates:
-        if exists(path):
-            editor = path
-            break
-    else:
-        raise OSError('No suitable editor found. Please set the '
-                      '"EDITOR" environment variable')
-    check_call([editor, filename])
index 932f217406fe74c6002678205e182370f5ae1df3..132a58b5371a592cceaef420b7d1a178a3e4e0df 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -13,6 +13,7 @@ readme = os.path.join(os.path.dirname(__file__), 'README.rst')
 requires = [
     'SQLAlchemy>=0.7.6',
     'Mako',
+    'python-editor',
 ]
 
 # Hack to prevent "TypeError: 'NoneType' object is not callable" error
index ffb659b799e244fbae70f7e983d17ca1b4969341..f375367265c43b1b1cd9a530f02672dd26777ab6 100644 (file)
@@ -1,5 +1,4 @@
 from alembic import command
-from mock import patch
 from io import TextIOWrapper, BytesIO
 from alembic.script import ScriptDirectory
 from alembic.testing.fixtures import TestBase, capture_context_buffer
@@ -9,6 +8,11 @@ from alembic.testing.env import staging_env, _sqlite_testing_config, \
 from alembic.testing import eq_, assert_raises_message
 from alembic import util
 
+try:
+    from mock import patch
+except ImportError:
+    from unittest.mock import patch
+
 
 class HistoryTest(TestBase):
 
@@ -386,36 +390,21 @@ class EditTest(TestBase):
     def teardown_class(cls):
         clear_staging_env()
 
-    def test_edit_with_user_editor(self):
-        expected_call_arg = '%s/scripts/versions/%s_revision_c.py' % (
-            EditTest.cfg.config_args['here'],
-            EditTest.c
-        )
-
-        with patch('alembic.util.os_helpers.check_call') as check_call, \
-                patch('alembic.util.os_helpers.exists') as exists:
-            exists.side_effect = lambda fname: fname == '/usr/bin/vim'
-            command.edit(self.cfg)
-            check_call.assert_called_with(['/usr/bin/vim', expected_call_arg])
-
-    def test_edit_with_default_editor(self):
+    def test_edit_latest(self):
         expected_call_arg = '%s/scripts/versions/%s_revision_c.py' % (
             EditTest.cfg.config_args['here'],
             EditTest.c
         )
 
-        with patch('alembic.util.os_helpers.check_call') as check_call, \
-                patch('alembic.util.os_helpers.exists') as exists:
-            exists.side_effect = lambda fname: fname == '/usr/bin/vim'
+        with patch('alembic.command.editor.edit') as edit:
             command.edit(self.cfg)
-            check_call.assert_called_with(['/usr/bin/vim', expected_call_arg])
+            edit.assert_called_with(expected_call_arg)
 
     def test_edit_with_missing_editor(self):
-        with patch('alembic.util.os_helpers.check_call'), \
-                patch('alembic.util.os_helpers.exists') as exists:
-            exists.return_value = False
+        with patch('alembic.command.editor.edit') as edit:
+            edit.side_effect = OSError('file not found')
             assert_raises_message(
                 util.CommandError,
-                'EDITOR',
+                'file not found',
                 command.edit,
                 self.cfg)
diff --git a/tests/test_os_helpers.py b/tests/test_os_helpers.py
deleted file mode 100644 (file)
index 220e114..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-from alembic import util
-from alembic.testing import assert_raises_message
-from alembic.testing.fixtures import TestBase
-
-try:
-    from unittest.mock import patch
-except ImportError:
-    from mock import patch  # noqa
-
-
-class TestHelpers(TestBase):
-
-    def test_edit_with_user_editor(self):
-        test_environ = {
-            'EDITOR': 'myvim',
-            'PATH': '/usr/bin'
-        }
-
-        with patch('alembic.util.os_helpers.check_call') as check_call, \
-                patch('alembic.util.os_helpers.exists') as exists:
-            exists.side_effect = lambda fname: fname == '/usr/bin/myvim'
-            util.open_in_editor('myfile', test_environ)
-            check_call.assert_called_with(['/usr/bin/myvim', 'myfile'])
-
-    def test_edit_with_default_editor(self):
-        test_environ = {}
-
-        with patch('alembic.util.os_helpers.check_call') as check_call, \
-                patch('alembic.util.os_helpers.exists') as exists:
-            exists.side_effect = lambda fname: fname == '/usr/bin/vim'
-            util.open_in_editor('myfile', test_environ)
-            check_call.assert_called_with(['/usr/bin/vim', 'myfile'])
-
-    def test_edit_with_missing_editor(self):
-        test_environ = {}
-        with patch('alembic.util.os_helpers.check_call'), \
-                patch('alembic.util.os_helpers.exists') as exists:
-            exists.return_value = False
-            assert_raises_message(
-                OSError,
-                'EDITOR',
-                util.open_in_editor,
-                'myfile',
-                test_environ)