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:
Raphael Marvie 2018-10-05 10:25:18 +02:00 committed by ljf
parent b385cd93f4
commit 9d82e3ac28
3 changed files with 98 additions and 8 deletions

View file

@ -1,9 +1,12 @@
# encoding: utf-8
import os import os
import yaml import yaml
import errno import errno
import shutil import shutil
import json import json
import grp import grp
from pwd import getpwnam from pwd import getpwnam
from moulinette import m18n from moulinette import m18n
@ -278,10 +281,10 @@ def rm(path, recursive=False, force=False):
- force -- Ignore nonexistent files - force -- Ignore nonexistent files
""" """
try:
if recursive and os.path.isdir(path): if recursive and os.path.isdir(path):
shutil.rmtree(path, ignore_errors=force) shutil.rmtree(path, ignore_errors=force)
else: else:
try:
os.remove(path) os.remove(path)
except OSError as e: except OSError as e:
if not force: if not force:

View file

@ -21,3 +21,7 @@
# renamed multiprocessing.process.Process to BaseProcess # 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

View file

@ -2,6 +2,11 @@
""" """
Testing moulinette utils filesystem 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 import mock
@ -276,6 +281,84 @@ def fake_open_for_write():
fake_file) 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 # ## Test file remove #
################################################################################ ################################################################################