Skip to content

Commit

Permalink
🐛 Fix "list" to not explode when no migrations directories are given
Browse files Browse the repository at this point in the history
  • Loading branch information
karenc committed Mar 14, 2016
1 parent 8e71d04 commit f139e6a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
28 changes: 26 additions & 2 deletions dbmigrator/tests/test_cli.py
Expand Up @@ -10,16 +10,22 @@
import unittest

import pkg_resources
import psycopg2

from .testing import captured_output
from . import testing


class MainTestCase(unittest.TestCase):
def tearDown(self):
with psycopg2.connect(testing.db_connection_string) as db_conn:
with db_conn.cursor() as cursor:
cursor.execute('DROP TABLE IF EXISTS schema_migrations')

def test_version(self):
from ..cli import main

version = pkg_resources.get_distribution('db-migrator').version
with captured_output() as (out, err):
with testing.captured_output() as (out, err):
self.assertRaises(SystemExit, main, ['-V'])

stdout = out.getvalue()
Expand All @@ -30,3 +36,21 @@ def test_version(self):
else:
self.assertEqual(stdout, '')
self.assertEqual(stderr.strip(), version)

def test_list_no_migrations_directory(self):
from ..cli import main

cmd = ['--db-connection-string', testing.db_connection_string]
main(cmd + ['init'])
with testing.captured_output() as (out, err):
main(cmd + ['list'])

stdout = out.getvalue()
stderr = err.getvalue()

self.assertEqual(stdout, """\
name | is applied | date applied
----------------------------------------------------------------------\n""")
self.assertEqual(stderr, """\
context undefined, using current directory name "['db-migrator']"
migrations directory undefined\n""")
7 changes: 7 additions & 0 deletions dbmigrator/tests/test_utils.py
Expand Up @@ -147,6 +147,13 @@ def test_get_migrations(self):
('20160228210326', 'initial_data'),
('20160228212456', 'cool_stuff')])

def test_get_migrations_no_migrations_directories(self):
from ..utils import get_migrations

migrations = get_migrations([])

self.assertEqual(list(migrations), [])

def test_get_pending_migrations(self):
from ..utils import get_pending_migrations

Expand Down
2 changes: 1 addition & 1 deletion dbmigrator/utils.py
Expand Up @@ -94,7 +94,7 @@ def get_migrations(migration_directories, import_modules=False, reverse=False):
paths = [os.path.join(md, '*.py') for md in migration_directories]
python_files = functools.reduce(
lambda a, b: a + b,
[glob.glob(path) for path in paths])
[glob.glob(path) for path in paths], [])
for path in sorted(python_files,
key=lambda path: os.path.basename(path),
reverse=reverse):
Expand Down

0 comments on commit f139e6a

Please sign in to comment.