diff options
-rw-r--r-- | alembic/command.py | 6 | ||||
-rw-r--r-- | alembic/config.py | 2 | ||||
-rw-r--r-- | alembic/util/__init__.py | 2 | ||||
-rw-r--r-- | alembic/util/os_helpers.py | 49 | ||||
-rw-r--r-- | setup.py | 1 | ||||
-rw-r--r-- | tests/test_command.py | 33 | ||||
-rw-r--r-- | tests/test_os_helpers.py | 44 |
7 files changed, 17 insertions, 120 deletions
diff --git a/alembic/command.py b/alembic/command.py index 644a169..37ba4af 100644 --- a/alembic/command.py +++ b/alembic/command.py @@ -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,)) diff --git a/alembic/config.py b/alembic/config.py index bd0ea4f..b3fc36f 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -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", diff --git a/alembic/util/__init__.py b/alembic/util/__init__.py index 0fe9f3c..bd7196c 100644 --- a/alembic/util/__init__.py +++ b/alembic/util/__init__.py @@ -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 index ccbfa11..0000000 --- a/alembic/util/os_helpers.py +++ /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]) @@ -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 diff --git a/tests/test_command.py b/tests/test_command.py index ffb659b..f375367 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -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 index 220e114..0000000 --- a/tests/test_os_helpers.py +++ /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) |