mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Improving style of utils.test_filesystem
- documenting the use of fake_open - improving readability of read_file tests
This commit is contained in:
parent
4a11d3a66a
commit
cb8da5f21e
1 changed files with 19 additions and 7 deletions
|
@ -22,24 +22,26 @@ from moulinette.utils import filesystem
|
||||||
|
|
||||||
@mock.patch('os.path.isfile')
|
@mock.patch('os.path.isfile')
|
||||||
def test_read_file_raise_error_for_non_existant_file(isfile):
|
def test_read_file_raise_error_for_non_existant_file(isfile):
|
||||||
isfile.return_value = False
|
isfile.return_value = False # the file does not exist
|
||||||
|
|
||||||
with pytest.raises(MoulinetteError):
|
with pytest.raises(MoulinetteError):
|
||||||
filesystem.read_file('non existent file')
|
filesystem.read_file('non_existent_file.txt')
|
||||||
|
|
||||||
|
|
||||||
|
@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):
|
def test_read_file_raise_error_for_file_with_bad_permission(open, isfile):
|
||||||
|
isfile.return_value = True # the file exists
|
||||||
open.side_effect = IOError()
|
open.side_effect = IOError()
|
||||||
|
|
||||||
with pytest.raises(MoulinetteError):
|
with pytest.raises(MoulinetteError):
|
||||||
filesystem.read_file('non openable file')
|
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):
|
||||||
isfile.return_value = True
|
isfile.return_value = True # the file exists
|
||||||
file_content = 'file content'
|
file_content = 'file content'
|
||||||
open.return_value = fake_open(file_content)
|
open.return_value = fake_open(file_content)
|
||||||
|
|
||||||
|
@ -51,11 +53,21 @@ def test_read_file_return_file_content(open, isfile):
|
||||||
def fake_open(content):
|
def fake_open(content):
|
||||||
"""Return a mock for opening a file with given content
|
"""Return a mock for opening a file with given content
|
||||||
|
|
||||||
This function is for mocking open() when used in a context manager.
|
This helper function is for mocking open() when used in a context manager.
|
||||||
|
|
||||||
|
@mock.patch('builtins.open')
|
||||||
|
def test(open):
|
||||||
|
open.return_value = fake_open('content')
|
||||||
|
function_using_open('filename.txt')
|
||||||
|
...
|
||||||
|
|
||||||
|
def function_using_open(filename):
|
||||||
|
with open(filename, 'r') as f:
|
||||||
|
content = f.read()
|
||||||
"""
|
"""
|
||||||
fake_file = mock.Mock(read=mock.Mock(return_value=content))
|
fake_file = mock.Mock(read=mock.Mock(return_value=content))
|
||||||
# open is used as a context manager
|
# open is used as a context manager
|
||||||
# - so we fake __enter__ to return the file
|
# - so we fake __enter__ to return the fake file
|
||||||
# - so we fake __exit__ to do nothing
|
# - so we fake __exit__ to do nothing
|
||||||
return mock.Mock(
|
return mock.Mock(
|
||||||
__enter__=mock.Mock(return_value=fake_file),
|
__enter__=mock.Mock(return_value=fake_file),
|
||||||
|
|
Loading…
Add table
Reference in a new issue