def test_resolve_returns_expected_result_for_nominal_case(self): empty_dir = tempfile.mkdtemp() dir_containing_file = tempfile.mkdtemp() frag1 = "somefile" self.__create_empty_file(os.path.join(dir_containing_file, frag1)) ret = multipath.resolve([empty_dir, dir_containing_file], frag1) self.assertEqual(ret, os.path.join(dir_containing_file, frag1))
def resolve(*paths): """ Joins `paths` onto each dir in overlay_dirs + workspace dir using `os.path.join` until one of the join results is found to exist and returns that existent result :param paths: Path components to join :return: The first path to be resolved that exists :raises ValueError: If no overlay dirs are set *and* the workspace dir cannot be established :raises FileNotFoundError: If no path could be resolved """ return multipath.resolve(__get_dirs(), *paths)
def test_resolve_returns_first_existent_path_even_if_other_paths_have_the_subpath( self): empty_dir = tempfile.mkdtemp() first_dir_with_file = tempfile.mkdtemp() second_dir_with_file = tempfile.mkdtemp() path = "somefile" self.__create_empty_file(os.path.join(first_dir_with_file, path)) self.__create_empty_file(os.path.join(second_dir_with_file, path)) ret = multipath.resolve( [empty_dir, first_dir_with_file, second_dir_with_file], path) self.assertEqual(ret, os.path.join(first_dir_with_file, path))
def copy_file(self, source, destination): """ Copy a file from `source` to `destination`. :param source: Path to source file. Relative paths are resolved relative to `source_roots` until a path exists. :param destination: Destination path. Relative paths are resolved relative to `destination_root` :raises FileNotFoundError if `source` does not exist :raises FileExistsError if `destination` already exists """ source_path = multipath.resolve(self.source_roots(), source) destination_path = os.path.join(self.destination_root(), destination) if os.path.exists(destination_path): raise FileExistsError( "{destination_path}: already exists: cannot copy {source_path} to this location" .format(destination_path=destination_path, source_path=source_path)) shutil.copy(source_path, destination_path) self.__print_creation(destination)
def test_resolve_raises_ValueError_if_dirs_is_empty(self): with self.assertRaises(ValueError): multipath.resolve([], "doesnt", "matter")