def get_directory_contents(self, directory): """ Get the contents of a directory while handling errors that may occur """ contents = [] try: contents = utils.get_directory_contents(directory) except PermissionError: self.errors.add(error.PermissionDenied(self.subcmd, directory)) except FileNotFoundError: self.errors.add( error.NoSuchFileOrDirectory(self.subcmd, directory)) except NotADirectoryError: self.errors.add(error.NoSuchDirectory(self.subcmd, directory)) return contents
def _collect_actions(self, source, dest): """ Concrete method to collect required actions to perform a stow sub-command """ if self.ignore.should_ignore(source): self.ignore.ignore(source) return if not StowInput(self.errors, self.subcmd).is_valid_collection_input( source, dest): return sources = self.get_directory_contents(source) for subsources in sources: if self.ignore.should_ignore(subsources): self.ignore.ignore(subsources) continue dest_path = dest / pathlib.Path(subsources.name) does_dest_path_exist = False try: does_dest_path_exist = dest_path.exists() except PermissionError: self.errors.add(error.PermissionDenied(self.subcmd, dest_path)) return if does_dest_path_exist: self._collect_actions_existing_dest(subsources, dest_path) elif dest_path.is_symlink(): self.errors.add( error.ConflictsWithExistingLink(self.subcmd, subsources, dest_path)) elif not dest_path.parent.exists() and not self.is_unfolding: self.errors.add( error.NoSuchDirectory(self.subcmd, dest_path.parent)) else: self._are_other(subsources, dest_path)
def _is_valid_source(self, source): """ Check if the source argument is valid """ result = True if not source.is_dir(): self.errors.add(error.NoSuchDirectory(self.subcmd, source)) result = False else: if not utils.is_directory_readable(source): self.errors.add( error.InsufficientPermissionsToSubcmdFrom( self.subcmd, source)) result = False if not utils.is_directory_executable(source): self.errors.add( error.InsufficientPermissionsToSubcmdFrom( self.subcmd, source)) result = False return result
def test_stow_with_file_as_source(file_a, dest): message = str(error.NoSuchDirectory(subcmd=SUBCMD, file=file_a)) with pytest.raises(error.NoSuchDirectory, match=message): dploy.stow([file_a], dest)
def test_stow_with_non_existant_source(dest): non_existant_source = 'source' message = str( error.NoSuchDirectory(subcmd=SUBCMD, file=non_existant_source)) with pytest.raises(error.NoSuchDirectory, match=message): dploy.stow([non_existant_source], dest)
def test_unstow_with_file_as_source(file_a, dest): with pytest.raises(NotADirectoryError) as e: dploy.unstow([file_a], dest) assert error.NoSuchDirectory(subcmd=SUBCMD, file=file_a).msg in str(e.value)
def test_unstow_with_non_existant_source(dest): source = 'source' with pytest.raises(NotADirectoryError) as e: dploy.unstow([source], dest) assert error.NoSuchDirectory(subcmd=SUBCMD, file=source).msg in str(e.value)