Fix split path function

So it also works on windows
and does not return empty stuff
and update tests
This commit is contained in:
David-Wobrock authored and David Wobrock committed 2019-02-24 15:55:05 +01:00
1 parent c403754564
commit f5a587c2be
2 files changed
+33 -17

No files matched your search

+17 -3
View File
@@ -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):
+16 -14
View File
@@ -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'