mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Testing utils.filesystem.rm
- removing existing file with proper permissions - removing existing folder with proper permissions - non existant or no permissions prevent removing file - non existant or no permissions prevent removing folder Fixed potential OSError handling from shutil.rmtree
This commit is contained in:
parent
b385cd93f4
commit
9d82e3ac28
3 changed files with 98 additions and 8 deletions
|
@ -1,9 +1,12 @@
|
|||
# encoding: utf-8
|
||||
|
||||
import os
|
||||
import yaml
|
||||
import errno
|
||||
import shutil
|
||||
import json
|
||||
import grp
|
||||
|
||||
from pwd import getpwnam
|
||||
|
||||
from moulinette import m18n
|
||||
|
@ -278,13 +281,13 @@ def rm(path, recursive=False, force=False):
|
|||
- force -- Ignore nonexistent files
|
||||
|
||||
"""
|
||||
if recursive and os.path.isdir(path):
|
||||
shutil.rmtree(path, ignore_errors=force)
|
||||
else:
|
||||
try:
|
||||
try:
|
||||
if recursive and os.path.isdir(path):
|
||||
shutil.rmtree(path, ignore_errors=force)
|
||||
else:
|
||||
os.remove(path)
|
||||
except OSError as e:
|
||||
if not force:
|
||||
raise MoulinetteError(errno.EIO,
|
||||
m18n.g('error_removing',
|
||||
except OSError as e:
|
||||
if not force:
|
||||
raise MoulinetteError(errno.EIO,
|
||||
m18n.g('error_removing',
|
||||
path=path, error=str(e)))
|
||||
|
|
|
@ -21,3 +21,7 @@
|
|||
|
||||
# renamed multiprocessing.process.Process to BaseProcess
|
||||
|
||||
# writing tests for utils (only ones available) using mocks
|
||||
|
||||
the goal is to be able to run moulinette tests on any environment just
|
||||
after a git clone
|
||||
|
|
|
@ -2,6 +2,11 @@
|
|||
|
||||
"""
|
||||
Testing moulinette utils filesystem
|
||||
|
||||
|
||||
WARNING These tests have been written based on the actual implementation and
|
||||
thus are dependant on this implementation. This is fragile but allow for a
|
||||
first instroduction of tests before doing refactorings.
|
||||
"""
|
||||
|
||||
import mock
|
||||
|
@ -276,6 +281,84 @@ def fake_open_for_write():
|
|||
fake_file)
|
||||
|
||||
|
||||
########################################################################
|
||||
# Test file remove
|
||||
########################################################################
|
||||
|
||||
#
|
||||
# Removing a file
|
||||
|
||||
@mock.patch('os.remove')
|
||||
def test_rm_remove_file_if_it_exists(remove):
|
||||
filename = 'file.txt'
|
||||
|
||||
filesystem.rm(filename)
|
||||
|
||||
remove.assert_called_with(filename)
|
||||
|
||||
|
||||
@mock.patch('os.remove')
|
||||
def test_rm_cannot_remove_non_existing_file(remove):
|
||||
filename = 'do_not_exist.txt'
|
||||
remove.side_effect = FileNotFoundError()
|
||||
|
||||
with pytest.raises(MoulinetteError):
|
||||
filesystem.rm(filename)
|
||||
|
||||
|
||||
@mock.patch('os.remove')
|
||||
def test_rm_cannot_remove_file_without_permission(remove):
|
||||
filename = 'not_mine.txt'
|
||||
remove.side_effect = PermissionError()
|
||||
|
||||
with pytest.raises(MoulinetteError):
|
||||
filesystem.rm(filename)
|
||||
|
||||
|
||||
@mock.patch('os.remove')
|
||||
def test_rm_cannot_remove_folder(remove):
|
||||
filename = './folder'
|
||||
remove.side_effect = IsADirectoryError()
|
||||
|
||||
with pytest.raises(MoulinetteError):
|
||||
filesystem.rm(filename)
|
||||
|
||||
#
|
||||
# Removing a folder
|
||||
|
||||
@mock.patch('os.path.isdir')
|
||||
@mock.patch('shutil.rmtree')
|
||||
def test_rm_remove_folder_if_it_exists(rmtree, isdir):
|
||||
isdir.return_value = True
|
||||
foldername = 'folder'
|
||||
|
||||
filesystem.rm(foldername, recursive=True)
|
||||
|
||||
rmtree.assert_called_with(foldername, ignore_errors=False)
|
||||
|
||||
|
||||
@mock.patch('os.path.isdir')
|
||||
@mock.patch('os.remove')
|
||||
def test_rm_cannot_remove_non_existing_folder(remove, isdir):
|
||||
isdir.return_value = False
|
||||
foldername = 'do_not_exist'
|
||||
remove.side_effect = FileNotFoundError()
|
||||
|
||||
with pytest.raises(MoulinetteError):
|
||||
filesystem.rm(foldername, recursive=True)
|
||||
|
||||
|
||||
@mock.patch('os.path.isdir')
|
||||
@mock.patch('shutil.rmtree')
|
||||
def test_rm_cannot_remove_folder_without_permission(rmtree, isdir):
|
||||
isdir.return_value = True
|
||||
foldername = 'not_mine'
|
||||
rmtree.side_effect = PermissionError()
|
||||
|
||||
with pytest.raises(MoulinetteError):
|
||||
filesystem.rm(foldername, recursive=True)
|
||||
|
||||
|
||||
################################################################################
|
||||
## Test file remove #
|
||||
################################################################################
|
||||
|
|
Loading…
Add table
Reference in a new issue