diff --git a/django_migration_linter/utils.py b/django_migration_linter/utils.py index e358593..e4cddbc 100644 --- a/django_migration_linter/utils.py +++ b/django_migration_linter/utils.py @@ -42,14 +42,28 @@ def find_project_settings_module(path): if file_name == "settings.py": return ( os.path.join(root.replace(path, ""), file_name) - .replace("/", ".") + .replace(os.sep, ".") .rstrip(".py") ) def split_path(path): - a, b = os.path.split(path) - return (split_path(a) if (len(a) > 0 and a != "/") else []) + ([b] if b else []) + decomposed_path = [] + while 1: + head, tail = os.path.split(path) + if head == path: # sentinel for absolute paths + decomposed_path.insert(0, head) + break + elif tail == path: # sentinel for relative paths + decomposed_path.insert(0, tail) + break + else: + path = head + decomposed_path.insert(0, tail) + + if not decomposed_path[-1]: + decomposed_path = decomposed_path[:-1] + return decomposed_path def split_migration_path(migration_path): diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 6998413..62202f7 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -44,25 +44,27 @@ class UtilityFunctionTest(unittest.TestCase): actual = find_project_settings_module(fixtures.CORRECT_PROJECT) self.assertEqual(actual, expected) + +class SplitPathTest(unittest.TestCase): def test_split_path(self): - splitted = split_path('foo/bar/fuz.py') - self.assertEqual(len(splitted), 3) - self.assertEqual(splitted[0], 'foo') - self.assertEqual(splitted[1], 'bar') - self.assertEqual(splitted[2], 'fuz.py') + split = split_path('foo/bar/fuz.py') + self.assertEqual(split, ["foo", "bar", "fuz.py"]) def test_split_full_path(self): - splitted = split_path('/foo/bar/fuz.py') - self.assertEqual(len(splitted), 3) - self.assertEqual(splitted[0], 'foo') - self.assertEqual(splitted[1], 'bar') - self.assertEqual(splitted[2], 'fuz.py') + split = split_path('/foo/bar/fuz.py') + self.assertEqual(split, ["/", "foo", "bar", "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') + split = split_path('/foo/bar') + self.assertEqual(split, ["/", "foo", "bar"]) + + def test_split_folder_path_trailing_slash(self): + split = split_path('/foo/bar/') + self.assertEqual(split, ["/", "foo", "bar"]) + + def test_split_folder_path_trailing_slashes(self): + split = split_path('/foo/bar///') + self.assertEqual(split, ["/", "foo", "bar"]) def test_split_migration_long_path(self): input_path = 'apps/the_app/migrations/0001_stuff.py'