def test_delete_directory(self, dir_function): basepath = dir_function.path directory = 'a' file_manager.create_package_directories(basepath, directory) path = os.path.join(basepath, directory) errors = file_manager.delete_directory(path) assert errors == [] assert not os.path.isdir(path)
def test_delete_directory_with_files_and_folders(self, dir_function, test_utils): basepath = dir_function.path directory = 'a' file_manager.create_package_directories(basepath, directory) file_manager.create_package_directories(os.path.join(basepath, directory), 'foo') test_utils.create_empty_file(os.path.join(basepath, directory), 'bar.txt') path = os.path.join(basepath, directory) errors = file_manager.delete_directory(path) assert errors == [] assert not os.path.isdir(path)
def create_directories(self, dir_name, file_type): errors = validate_project_element_name(dir_name, isdir=True) if not errors: base_path = self.element_directory_path(file_type) relpath = os.sep.join(dir_name.split('.')) fullpath = os.path.join(base_path, relpath) if os.path.isdir(fullpath): errors.append('A directory with that name already exists') else: file_manager.create_package_directories(base_path, relpath) return errors
def create_packages_for_element(self, element_name, file_type): """Given an file name, creates its parent directories if they don't exist. For example: given file_name=my.folder.test_name, create packages: `my/` and `my/folder/` """ base_path = self.element_directory_path(file_type) relpath = os.sep.join(element_name.split('.')[:-1]) if relpath: file_manager.create_package_directories(base_path, relpath)
def test_delete_directory_a_child_file_is_open(self, dir_function, test_utils): basepath = dir_function.path directory = 'a' file_manager.create_package_directories(basepath, directory) filename_one = test_utils.create_empty_file(os.path.join(basepath, directory), 'foo.txt') filename_two = test_utils.create_empty_file(os.path.join(basepath, directory), 'bar.txt') path = os.path.join(basepath, directory) with open(filename_one): errors = file_manager.delete_directory(path) assert errors == ['Error: PermissionError'] assert os.path.isdir(path) # open file was not removed assert os.path.isfile(filename_one) # not open file was removed assert not os.path.isfile(filename_two)
def test_rename_directory_to_parent_path(self, dir_function, test_utils): """file_manager.rename_directory should return an error When renaming a directory to a parent path. """ basepath = dir_function.path dst = 'a' src = os.sep.join([dst, 'b']) file_manager.create_package_directories(basepath, src) test_utils.create_empty_file(os.path.join(basepath, src), 'foo.txt') errors = file_manager.rename_directory(basepath, src, dst) assert errors == ['Path {} already exists'.format(dst)] assert os.path.isdir(os.path.join(basepath, src)) assert os.path.isdir(os.path.join(basepath, dst)) assert os.path.isfile(os.path.join(basepath, src, '__init__.py')) assert os.path.isfile(os.path.join(basepath, src, 'foo.txt'))
def test_rename_directory_with_files_and_folders(self, dir_function, test_utils): basepath = dir_function.path src = 'a' dst = 'b' subfolder = 'c' file_manager.create_package_directories(basepath, src) test_utils.create_empty_file(os.path.join(basepath, src), 'foo.txt') test_utils.create_empty_file(os.path.join(basepath, src, subfolder), 'bar.txt') errors = file_manager.rename_directory(basepath, src, dst) assert errors == [] assert not os.path.isdir(os.path.join(basepath, src)) assert os.path.isdir(os.path.join(basepath, dst)) assert os.path.isdir(os.path.join(basepath, dst, subfolder)) assert os.path.isfile(os.path.join(basepath, dst, '__init__.py')) assert os.path.isfile(os.path.join(basepath, dst, 'foo.txt')) assert os.path.isfile(os.path.join(basepath, dst, subfolder, 'bar.txt'))
def test_rename_directory_a_file_is_open(self, dir_function, test_utils): """A directory cannot be renamed if a child file is currently opened by another process. """ basepath = dir_function.path src = 'a' dst = 'b' file_manager.create_package_directories(basepath, src) filepath = test_utils.create_empty_file(os.path.join(basepath, src), 'foo.txt') filepath_two = test_utils.create_empty_file(os.path.join(basepath, src), 'bar.txt') with open(filepath): errors = file_manager.rename_directory(basepath, src, dst) assert errors == ['Error: PermissionError'] assert not os.path.isdir(os.path.join(basepath, dst)) assert os.path.isdir(os.path.join(basepath, src)) assert os.path.isfile(filepath) assert os.path.isfile(filepath_two)
def test_rename_directory_to_child_path(self, dir_function, test_utils): """file_manager.rename_directory should return 'Error: PermissionError' When renaming a directory to a descendant path. """ basepath = dir_function.path src = 'a' dst = os.sep.join([src, 'b']) file_manager.create_package_directories(basepath, src) test_utils.create_empty_file(os.path.join(basepath, src), 'foo.txt') errors = file_manager.rename_directory(basepath, src, dst) if os.name == 'nt': assert errors == ['Error: PermissionError'] else: assert errors == ['An error occurred while renaming folder'] assert os.path.isdir(os.path.join(basepath, src)) assert not os.path.isdir(os.path.join(basepath, dst)) assert os.path.isfile(os.path.join(basepath, src, '__init__.py')) assert os.path.isfile(os.path.join(basepath, src, 'foo.txt'))
def test_rename_directory(self, dir_function): basepath = dir_function.path src = 'a' dst = 'b' file_manager.create_package_directories(basepath, src) errors = file_manager.rename_directory(basepath, src, dst) assert errors == [] assert not os.path.isdir(os.path.join(basepath, src)) assert os.path.isdir(os.path.join(basepath, dst)) assert os.path.isfile(os.path.join(basepath, dst, '__init__.py')) src = 'q' dst = os.sep.join(['w', 'e']) file_manager.create_package_directories(basepath, src) errors = file_manager.rename_directory(basepath, src, dst) assert errors == [] assert not os.path.isdir(os.path.join(basepath, src)) assert os.path.isdir(os.path.join(basepath, dst)) assert os.path.isfile(os.path.join(basepath, dst, '__init__.py'))
def test_create_package_directories(self, dir_function): basepath = dir_function.path # one folder level relpath = os.path.join('a') file_manager.create_package_directories(basepath, relpath) assert os.path.isdir(os.path.join(basepath, 'a')) assert os.path.isfile(os.path.join(basepath, 'a', '__init__.py')) # two folder levels relpath = os.path.join('b', 'c') file_manager.create_package_directories(basepath, relpath) assert os.path.isdir(os.path.join(basepath, 'b', 'c')) assert os.path.isfile(os.path.join(basepath, 'b', '__init__.py')) assert os.path.isfile(os.path.join(basepath, 'b', 'c', '__init__.py')) # folder only, no file in path relpath = os.path.join('d', 'e', 'f') + os.sep file_manager.create_package_directories(basepath, relpath) assert os.path.isdir(os.path.join(basepath, 'd', 'e', 'f')) assert os.path.isfile(os.path.join(basepath, 'd', 'e', 'f', '__init__.py'))
def test_rename_directory_dst_exists(self, dir_function, test_utils): """It should return an error when dst is an existing file or folder""" basepath = dir_function.path # dst is file src = 'a' dst = 'b' file_manager.create_package_directories(basepath, src) test_utils.create_empty_file(basepath, dst) errors = file_manager.rename_directory(basepath, src, dst) assert errors == ['Path {} already exists'.format(dst)] assert os.path.isdir(os.path.join(basepath, src)) assert os.path.isfile(os.path.join(basepath, dst)) # dst is directory src = 'c' dst = 'd' file_manager.create_package_directories(basepath, src) file_manager.create_package_directories(basepath, dst) errors = file_manager.rename_directory(basepath, src, dst) assert errors == ['Path {} already exists'.format(dst)] assert os.path.isdir(os.path.join(basepath, src)) assert os.path.isdir(os.path.join(basepath, dst))