dirbrowser: move parent dir logic to own function

This commit is contained in:
Daniel Schadt 2016-03-24 18:52:49 +01:00
parent 7fe4c7e06d
commit 5e73a2ea37
2 changed files with 36 additions and 1 deletions

View File

@ -68,6 +68,18 @@ def is_root(directory):
return os.path.dirname(directory) == directory
def parent_dir(directory):
"""Return the parent directory for the given directory.
Args:
directory: The path to the directory.
Return:
The path to the parent directory.
"""
return os.path.normpath(os.path.join(directory, os.pardir))
def dirbrowser_html(path):
"""Get the directory browser web page.
@ -82,7 +94,7 @@ def dirbrowser_html(path):
if is_root(path):
parent = None
else:
parent = os.path.normpath(os.path.join(path, '..'))
parent = parent_dir(path)
try:
all_files = os.listdir(path)

View File

@ -77,6 +77,29 @@ class TestIsRoot:
assert filescheme.is_root(directory) == is_root
class TestParentDir:
@pytest.mark.windows
@pytest.mark.parametrize('directory, parent', [
('C:\\foo\\bar', 'C:\\foo'),
('C:\\foo', 'C:\\'),
('C:\\foo\\', 'C:\\'),
('C:\\', 'C:\\'),
])
def test_windows(self, directory, parent):
assert filescheme.parent_dir(directory) == parent
@pytest.mark.posix
@pytest.mark.parametrize('directory, parent', [
('/home/foo', '/home'),
('/home', '/'),
('/home/', '/'),
('/', '/'),
])
def test_posix(self, directory, parent):
assert filescheme.parent_dir(directory) == parent
def _file_url(path):
"""Return a file:// url (as string) for the given LocalPath.