Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
/ pycosio Public archive

Commit

Permalink
mkdir, makesdir tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JGoutin committed Oct 4, 2018
1 parent eee6061 commit 8ae08a9
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 25 deletions.
6 changes: 5 additions & 1 deletion pycosio/_core/functions_os_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ def isdir(path):
Returns:
bool: True if directory exists.
"""
return get_instance(path).isdir(path)
system = get_instance(path)

# User may use directory path without trailing '/'
# like on standard file systems
return system.isdir(system.ensure_dir_path(path))


@equivalent_to(os.path.isfile)
Expand Down
8 changes: 6 additions & 2 deletions pycosio/_core/io_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,12 @@ def ensure_dir_path(self, path, relative=False):
else:
rel_path = path

# Locator
if self.is_locator(rel_path, relative=True):
path = path.rstrip('/')
elif rel_path and rel_path[-1] != '/':
path += '/'

# Directory
elif rel_path:
path = path.rstrip('/') + '/'
# else: root
return path
115 changes: 93 additions & 22 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def cos_function(path, *args, **kwargs):
def test_equivalent_functions():
"""Tests functions using pycosio._core.functions_core.equivalent_to"""
from pycosio._core.storage_manager import MOUNTED
import pycosio._core.functions_os_path as std
import pycosio._core.functions_os_path as std_os_path
import pycosio._core.functions_os as std_os
from pycosio._core.io_system import SystemBase

# Mock system

Expand All @@ -74,8 +76,11 @@ def test_equivalent_functions():
dummy_path = root + relative
excepted_path = dummy_path
result = 'result'
dirs_exists = set()
dir_created = []
check_ending_slash = True

class System:
class System(SystemBase):
"""dummy system"""

@staticmethod
Expand All @@ -85,8 +90,34 @@ def relpath(path):
assert path.startswith(excepted_path)
return path.split(root)[1].strip('/')

system = System()
MOUNTED[root] = dict(system_cached=system)
@staticmethod
def isdir(path=None, *_, **__):
"""Checks arguments and returns fake result"""
if check_ending_slash:
assert path[-1] == '/'
return path in dirs_exists

@staticmethod
def _make_dir(*_, **__):
"""Do nothing"""
dir_created.append(1)

@staticmethod
def _head(*_, **__):
"""Do nothing"""

@staticmethod
def _get_roots(*_, **__):
"""Do nothing"""
return root,

@staticmethod
def _get_client(*_, **__):
"""Do nothing"""

@staticmethod
def get_client_kwargs(*_, **__):
"""Do nothing"""

# Tests
try:
Expand All @@ -97,53 +128,93 @@ def basic_function(path):
assert path == excepted_path
return result

for name in ('exists', 'getsize', 'getmtime', 'isdir', 'isfile'):
for name in ('exists', 'getsize', 'getmtime', 'isfile'):
system = System()
MOUNTED[root] = dict(system_cached=system)
setattr(system, name, basic_function)
assert getattr(std, name)(dummy_path) == result
assert getattr(std_os_path, name)(dummy_path) == result

MOUNTED[root] = dict(system_cached=System())

# relpath
assert std.relpath(dummy_path) == relative
assert std.relpath(dummy_path, start='dir1/') == 'dir2/dir3'
assert std_os_path.relpath(dummy_path) == relative
assert std_os_path.relpath(dummy_path, start='dir1/') == 'dir2/dir3'

# ismount
assert std.ismount(dummy_path) is False
assert std_os_path.ismount(dummy_path) is False
excepted_path = root
assert std.ismount(root) is True
assert std_os_path.ismount(root) is True

# isabs
excepted_path = dummy_path
assert std.isabs(dummy_path) is True
assert std_os_path.isabs(dummy_path) is True
excepted_path = relative
assert std.isabs(excepted_path) is False
assert std_os_path.isabs(excepted_path) is False

# splitdrive
excepted_path = dummy_path
assert std.splitdrive(dummy_path) == (root, relative)
assert std_os_path.splitdrive(dummy_path) == (root, relative)
old_root = root
old_relative = relative
relative = 'dir2/dir3'
root += 'dir1'
assert std.splitdrive(dummy_path) == (root, '/' + relative)
assert std_os_path.splitdrive(dummy_path) == (root, '/' + relative)
root = old_root
relative = old_relative

# samefile
if version_info[0] == 2 and platform().startswith('Windows'):
with pytest.raises(NotImplementedError):
std.samefile(__file__, __file__)
std_os_path.samefile(__file__, __file__)
else:
assert std.samefile(__file__, __file__)
assert std_os_path.samefile(__file__, __file__)

assert std.samefile(dummy_path, dummy_path)
assert not std.samefile(dummy_path, relative)
assert not std.samefile(relative, dummy_path)
assert std.samefile(dummy_path, dummy_path + '/')
assert not std.samefile(dummy_path, dummy_path + '/dir4')
assert std_os_path.samefile(dummy_path, dummy_path)
assert not std_os_path.samefile(dummy_path, relative)
assert not std_os_path.samefile(relative, dummy_path)
assert std_os_path.samefile(dummy_path, dummy_path + '/')
assert not std_os_path.samefile(dummy_path, dummy_path + '/dir4')

root2 = 'dummy2://'
MOUNTED[root2] = dict(system_cached=System())
excepted_path = ''
assert not std.samefile(root2 + relative, dummy_path)
assert not std_os_path.samefile(root2 + relative, dummy_path)

# isdir
dirs_exists.add('dummy://locator/dir1/')
assert std_os_path.isdir('dummy://locator/dir1/')
assert std_os_path.isdir('dummy://locator/dir1')

# makesdir
assert not dir_created
std_os.makedirs('dummy://locator/dir1', exist_ok=True)
assert dir_created

dir_created = []
with pytest.raises(OSError):
std_os.makedirs('dummy://locator/dir1', exist_ok=False)
assert not dir_created

# mkdir
dirs_exists.add('dummy://locator/')
dirs_exists.add('dummy://')

with pytest.raises(OSError):
std_os.mkdir('dummy://locator/dir_not_exists/dir1')
assert not dir_created

std_os.mkdir('dummy://locator/dir1/dir2')
assert dir_created

dir_created = []
check_ending_slash = False
with pytest.raises(OSError):
std_os.mkdir('dummy://locator/dir1')
assert not dir_created

dir_created = []
std_os.mkdir('dummy://locator2/')
assert dir_created

# Clean up
finally:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_io_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def _make_dir(self, client_kwargs):
system.make_dir('root://locator')
system.make_dir('root://locator/path')
system.make_dir('root://locator/path/')
system.make_dir('locator', relative=True)
system.make_dir('locator/path', relative=True)
system.make_dir('locator/path/', relative=True)

# Test empty header
header = {}
Expand Down

0 comments on commit 8ae08a9

Please sign in to comment.