Tests for utils.filesystem.write_to_file

- non existent folder
- non readable file
- writable file with content

Introducing a fake_open_for_write helper function to easily use mocking
plumbery for mocking the opening and writing of text files in tests.

Renamed fake_open helper to fake_open_for_read (one helper similarly
named for read and for write)
This commit is contained in:
Raphael Marvie 2018-10-04 16:16:50 +02:00 committed by ljf
parent 0a22649b57
commit 2ed5dc8f73
2 changed files with 83 additions and 11 deletions

View file

@ -103,9 +103,13 @@ def write_to_file(file_path, data, file_mode="w"):
by append_to_file to avoid duplicating the code of this function. by append_to_file to avoid duplicating the code of this function.
""" """
assert isinstance(data, str) or isinstance(data, list) assert isinstance(data, str) or isinstance(data, list)
assert not os.path.isdir(file_path)
assert os.path.isdir(os.path.dirname(file_path))
path = os.path.dirname(file_path)
if not os.path.isdir(path):
raise MoulinetteError(errno.ENOENT,
m18n.g('file_not_exist', path=path))
# FIXME could be replaced by a writelines
# If data is a list, check elements are strings and build a single string # If data is a list, check elements are strings and build a single string
if not isinstance(data, str): if not isinstance(data, str):
for element in data: for element in data:

View file

@ -30,7 +30,7 @@ def test_read_file_raise_error_for_non_existant_file(isfile):
@mock.patch('os.path.isfile') @mock.patch('os.path.isfile')
@mock.patch('builtins.open') @mock.patch('builtins.open')
def test_read_file_raise_error_for_file_with_bad_permission(open, isfile): def test_read_file_raise_error_for_non_openable_file(open, isfile):
isfile.return_value = True # the file exists isfile.return_value = True # the file exists
open.side_effect = IOError() # it cannot be opened open.side_effect = IOError() # it cannot be opened
@ -38,6 +38,16 @@ def test_read_file_raise_error_for_file_with_bad_permission(open, isfile):
filesystem.read_file('non_openable_file.txt') filesystem.read_file('non_openable_file.txt')
@mock.patch('os.path.isfile')
@mock.patch('builtins.open')
def test_read_file_raise_error_for_non_readable_file(open, isfile):
isfile.return_value = True # the file exists
open.side_effect = Exception() # it cannot be read
with pytest.raises(MoulinetteError):
filesystem.read_file('non_openable_file.txt')
@mock.patch('os.path.isfile') @mock.patch('os.path.isfile')
@mock.patch('builtins.open') @mock.patch('builtins.open')
def test_read_file_return_file_content(open, isfile): def test_read_file_return_file_content(open, isfile):
@ -132,16 +142,74 @@ def test_read_yaml_raise_error_on_bad_content(open, isfile):
# Test writing a file # Test writing a file
######################################################################## ########################################################################
#@mock.patch('builtins.open') @mock.patch('os.path.isdir')
#def test_write_to_file_update_file_content(open): @mock.patch('builtins.open')
# file_content = 'file content' def test_write_to_file_update_file_content(open, isdir):
# open.return_value = fake_open_for_write() isdir.return_value = True
# open.return_value, fake_file = fake_open_for_write()
# filesystem.write_file('fake_file.txt') content = 'some content\n'
#
# assert content == file_content, 'read_file returned expected content' filesystem.write_to_file('fake_file.txt', content)
fake_file.write.assert_called_with(content)
@mock.patch('os.path.isdir')
@mock.patch('builtins.open')
def test_write_to_file_raise_error_for_improper_path(open, isdir):
isdir.return_value = False
content = 'some content\n'
with pytest.raises(MoulinetteError):
filesystem.write_to_file('fake_file.txt', content)
@mock.patch('os.path.isdir')
@mock.patch('builtins.open')
def test_write_to_file_raise_error_when_file_cannot_be_open(open, isdir):
isdir.return_value = True # the folder exists
open.side_effect = IOError() # it cannot be opened
content = 'some content\n'
with pytest.raises(MoulinetteError):
filesystem.write_to_file('non_openable_file.txt', content)
@mock.patch('os.path.isdir')
@mock.patch('builtins.open')
def test_write_to_file_raise_error_when_file_cannot_be_written(open, isdir):
isdir.return_value = True # the folder exists
# FIXME it could be write that raises Exception
open.side_effect = Exception() # it cannot be written
content = 'some content\n'
with pytest.raises(MoulinetteError):
filesystem.write_to_file('non_writable_file.txt', content)
def fake_open_for_write():
"""Return a mock for opening a file to be writen to as well as the fake file
This helper function is for mocking open() when used in a context manager.
@mock.patch('builtins.open')
def test(open):
open.return_value, fake_file = fake_open_for_write()
function_using_open('filename.txt', 'content')
fake_file.write.assert_called('content')
def function_using_open(filename, content):
with open(filename, 'w') as f:
content = f.write(content)
"""
fake_file = mock.Mock(write=mock.Mock())
# open is used as a context manager
# - so we fake __enter__ to return the fake file
# - so we fake __exit__ to do nothing
return (mock.Mock(
__enter__=mock.Mock(return_value=fake_file),
__exit__=mock.Mock()),
fake_file)
# #