From 009153fbf4c99ba2efd7b4d6c039e7e330d7a50e Mon Sep 17 00:00:00 2001 From: David Wobrock Date: Sat, 19 Jan 2019 17:21:12 +0100 Subject: [PATCH] handle linting a project in pwd When calling "django-migration-linter ." be able to handle it (especially with cache) --- django_migration_linter/cache.py | 2 +- django_migration_linter/migration_linter.py | 2 +- django_migration_linter/utils.py | 2 +- tests/functional/test_cmd_line_call.py | 21 +++++++++++++++++++++ tests/unit/test_utils.py | 6 ++++++ 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/django_migration_linter/cache.py b/django_migration_linter/cache.py index 909d45f..892e2df 100644 --- a/django_migration_linter/cache.py +++ b/django_migration_linter/cache.py @@ -22,7 +22,7 @@ from .utils import split_path class Cache(dict): def __init__(self, django_folder, cache_path): self.django_folder = django_folder - project_name = split_path(django_folder)[-2] + project_name = split_path(django_folder)[-1] self.filename = os.path.join(cache_path, "{0}.pickle".format(project_name)) if not os.path.exists(os.path.dirname(self.filename)): diff --git a/django_migration_linter/migration_linter.py b/django_migration_linter/migration_linter.py index 808be46..f9c0ab1 100644 --- a/django_migration_linter/migration_linter.py +++ b/django_migration_linter/migration_linter.py @@ -45,7 +45,7 @@ class MigrationLinter(object): ) # Store parameters and options - self.django_path = project_path + self.django_path = os.path.abspath(project_path) self.ignore_name_contains = kwargs.get("ignore_name_contains", None) self.ignore_name = kwargs.get("ignore_name", None) or tuple() self.include_apps = kwargs.get("include_apps", None) diff --git a/django_migration_linter/utils.py b/django_migration_linter/utils.py index 0f50c26..e358593 100644 --- a/django_migration_linter/utils.py +++ b/django_migration_linter/utils.py @@ -49,7 +49,7 @@ def find_project_settings_module(path): def split_path(path): a, b = os.path.split(path) - return (split_path(a) if (len(a) > 0 and a != "/") else []) + [b] + return (split_path(a) if (len(a) > 0 and a != "/") else []) + ([b] if b else []) def split_migration_path(migration_path): diff --git a/tests/functional/test_cmd_line_call.py b/tests/functional/test_cmd_line_call.py index 055d210..1077375 100644 --- a/tests/functional/test_cmd_line_call.py +++ b/tests/functional/test_cmd_line_call.py @@ -220,3 +220,24 @@ class CallLinterFromCommandLineTest(unittest.TestCase): self.assertEqual(len(lines), 3) self.assertTrue(lines[0].endswith('OK')) self.assertTrue(lines[1].startswith('*** Summary')) + + def test_call_from_within_project(self): + cmd = 'cd {0} && {1} --no-cache .'.format( + fixtures.CORRECT_PROJECT, + self.linter_exec) + + process = Popen( + cmd, shell=True, stdout=PIPE, stderr=PIPE) + process.wait() + self.assertEqual(process.returncode, 0) + lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines())) + self.assertEqual(len(lines), 6) + self.assertEqual( + sorted(lines[:4]), + sorted([ + "(test_app1, 0001_initial)... OK", + "(test_app1, 0002_a_new_null_field)... OK", + "(test_app2, 0001_foo)... OK", + "(test_app3, 0001_initial)... OK", + ]) + ) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index b997288..6998413 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -58,6 +58,12 @@ class UtilityFunctionTest(unittest.TestCase): self.assertEqual(splitted[1], 'bar') self.assertEqual(splitted[2], 'fuz.py') + def test_split_folder_path(self): + splitted = split_path('/foo/bar') + self.assertEqual(len(splitted), 2) + self.assertEqual(splitted[0], 'foo') + self.assertEqual(splitted[1], 'bar') + def test_split_migration_long_path(self): input_path = 'apps/the_app/migrations/0001_stuff.py' app, mig = split_migration_path(input_path)