Test patched which method

This commit is contained in:
Philip Ridout 2017-09-18 07:20:06 +01:00
parent dbcf4d2f78
commit d801ca9b09
4 changed files with 97 additions and 81 deletions

View File

@ -95,19 +95,17 @@ def rmtree(*args, **kwargs):
args, kwargs = replace_params(args, kwargs, ((0, 'path', path_to_str),))
return shutil.rmtree(*args, **kwargs)
# TODO: Test and tidy
def which(*args, **kwargs):
"""
Wraps :func:shutil.rmtree` so that we can accept Path objects.
Wraps :func:shutil.which` so that it return a Path objects.
:param openlp.core.common.path.Path path: Takes a Path object which is then converted to a str object
:return: Passes the return from :func:`shutil.rmtree` back
:rtype: None
:rtype: openlp.core.common.Path
See the following link for more information on the other parameters:
https://docs.python.org/3/library/shutil.html#shutil.rmtree
https://docs.python.org/3/library/shutil.html#shutil.which
"""
file_name = shutil.which(*args, **kwargs)
if file_name:
return str_to_path(file_name)

View File

@ -376,7 +376,10 @@ class ServiceManager(OpenLPMixin, RegistryMixin, QtWidgets.QWidget, Ui_ServiceMa
self._file_name = path_to_str(file_path)
self.main_window.set_service_modified(self.is_modified(), self.short_file_name())
Settings().setValue('servicemanager/last file', file_path)
self._save_lite = file_path.suffix() == '.oszl'
if file_path and file_path.suffix() == '.oszl':
self._save_lite = True
else:
self._save_lite = False
def file_name(self):
"""

View File

@ -223,10 +223,9 @@ class ImpressDocument(PresentationDocument):
if desktop is None:
self.controller.start_process()
desktop = self.controller.get_com_desktop()
url = self.file_path.as_uri()
else:
desktop = self.controller.get_uno_desktop()
url = uno.systemPathToFileUrl(str(self.file_path))
url = self.file_path.as_uri()
if desktop is None:
return False
self.desktop = desktop
@ -253,10 +252,7 @@ class ImpressDocument(PresentationDocument):
if self.check_thumbnails():
return
temp_folder_path = self.get_temp_folder()
if is_win():
thumb_dir_url = temp_folder_path.as_uri()
else:
thumb_dir_url = uno.systemPathToFileUrl(str(temp_folder_path))
thumb_dir_url = temp_folder_path.as_uri()
properties = []
properties.append(self.create_property('FilterName', 'impress_png_Export'))
properties = tuple(properties)

View File

@ -3,15 +3,15 @@ from unittest import TestCase
from unittest.mock import ANY, MagicMock, patch
from openlp.core.common.path import Path
from openlp.core.lib import shutilpatches
from openlp.core.lib.shutil import copy, copyfile, copytree, rmtree, which
class TestShutilPatches(TestCase):
class TestShutil(TestCase):
"""
Tests for the :mod:`openlp.core.lib.shutil` module
"""
def test_pcopy(self):
def test_copy(self):
"""
Test :func:`copy`
"""
@ -19,133 +19,152 @@ class TestShutilPatches(TestCase):
with patch('openlp.core.lib.shutil.shutil.copy', return_value=os.path.join('destination', 'test', 'path')) \
as mocked_shutil_copy:
# WHEN: Calling shutilpatches.copy with the src and dst parameters as Path object types
result = shutilpatches.copy(Path('source', 'test', 'path'), Path('destination', 'test', 'path'))
# WHEN: Calling :func:`copy` with the src and dst parameters as Path object types
result = copy(Path('source', 'test', 'path'), Path('destination', 'test', 'path'))
# THEN: `shutil.copy` should have been called with the str equivalents of the Path objects.
# `shutilpatches.copy` should return the str type result of calling `shutil.copy` as a Path
# object.
# THEN: :func:`shutil.copy` should have been called with the str equivalents of the Path objects.
# :func:`copy` should return the str type result of calling :func:`shutil.copy` as a Path object.
mocked_shutil_copy.assert_called_once_with(os.path.join('source', 'test', 'path'),
os.path.join('destination', 'test', 'path'))
self.assertEqual(result, Path('destination', 'test', 'path'))
def test_pcopy_follow_optional_params(self):
def test_copy_follow_optional_params(self):
"""
Test :func:`copy` when follow_symlinks is set to false
"""
# GIVEN: A mocked `shutil.copy`
with patch('openlp.core.lib.shutil.shutil.copy', return_value='') as mocked_shutil_copy:
# WHEN: Calling shutilpatches.copy with `follow_symlinks` set to False
shutilpatches.copy(Path('source', 'test', 'path'),
Path('destination', 'test', 'path'),
follow_symlinks=False)
# WHEN: Calling :func:`copy` with :param:`follow_symlinks` set to False
copy(Path('source', 'test', 'path'), Path('destination', 'test', 'path'), follow_symlinks=False)
# THEN: `shutil.copy` should have been called with follow_symlinks is set to false
# THEN: :func:`shutil.copy` should have been called with :param:`follow_symlinks` set to false
mocked_shutil_copy.assert_called_once_with(ANY, ANY, follow_symlinks=False)
def test_pcopyfile(self):
def test_copyfile(self):
"""
Test :func:`copyfile`
"""
# GIVEN: A mocked `shutil.copyfile` which returns a test path as a string
with patch('openlp.core.lib.shutil.shutil.copyfile', return_value=os.path.join('destination', 'test', 'path')) \
as mocked_shutil_copyfile:
# GIVEN: A mocked :func:`shutil.copyfile` which returns a test path as a string
with patch('openlp.core.lib.shutil.shutil.copyfile',
return_value=os.path.join('destination', 'test', 'path')) as mocked_shutil_copyfile:
# WHEN: Calling shutilpatches.copyfile with the src and dst parameters as Path object types
result = shutilpatches.copyfile(Path('source', 'test', 'path'), Path('destination', 'test', 'path'))
# WHEN: Calling :func:`copyfile` with the src and dst parameters as Path object types
result = copyfile(Path('source', 'test', 'path'), Path('destination', 'test', 'path'))
# THEN: `shutil.copyfile` should have been called with the str equivalents of the Path objects.
# `shutilpatches.copyfile` should return the str type result of calling `shutil.copyfile` as a Path
# THEN: :func:`shutil.copyfile` should have been called with the str equivalents of the Path objects.
# :func:`copyfile` should return the str type result of calling :func:`shutil.copyfile` as a Path
# object.
mocked_shutil_copyfile.assert_called_once_with(os.path.join('source', 'test', 'path'),
os.path.join('destination', 'test', 'path'))
self.assertEqual(result, Path('destination', 'test', 'path'))
def test_pcopyfile_optional_params(self):
def test_copyfile_optional_params(self):
"""
Test :func:`copyfile` when follow_symlinks is set to false
"""
# GIVEN: A mocked `shutil.copyfile`
# GIVEN: A mocked :func:`shutil.copyfile`
with patch('openlp.core.lib.shutil.shutil.copyfile', return_value='') as mocked_shutil_copyfile:
# WHEN: Calling shutilpatches.copyfile with `follow_symlinks` set to False
shutilpatches.copyfile(Path('source', 'test', 'path'),
Path('destination', 'test', 'path'),
follow_symlinks=False)
# WHEN: Calling :func:`copyfile` with :param:`follow_symlinks` set to False
copyfile(Path('source', 'test', 'path'), Path('destination', 'test', 'path'), follow_symlinks=False)
# THEN: `shutil.copyfile` should have been called with the optional parameters, with out any of the values
# being modified
# THEN: :func:`shutil.copyfile` should have been called with the optional parameters, with out any of the
# values being modified
mocked_shutil_copyfile.assert_called_once_with(ANY, ANY, follow_symlinks=False)
def test_pcopytree(self):
def test_copytree(self):
"""
Test :func:`copytree`
"""
# GIVEN: A mocked `shutil.copytree` which returns a test path as a string
with patch('openlp.core.lib.shutil.shutil.copytree', return_value=os.path.join('destination', 'test', 'path')) \
as mocked_shutil_copytree:
# GIVEN: A mocked :func:`shutil.copytree` which returns a test path as a string
with patch('openlp.core.lib.shutil.shutil.copytree',
return_value=os.path.join('destination', 'test', 'path')) as mocked_shutil_copytree:
# WHEN: Calling shutilpatches.copytree with the src and dst parameters as Path object types
result = shutilpatches.copytree(Path('source', 'test', 'path'), Path('destination', 'test', 'path'))
# WHEN: Calling :func:`copytree` with the src and dst parameters as Path object types
result = copytree(Path('source', 'test', 'path'), Path('destination', 'test', 'path'))
# THEN: `shutil.copytree` should have been called with the str equivalents of the Path objects.
# `shutilpatches.copytree` should return the str type result of calling `shutil.copytree` as a Path
# object.
# THEN: :func:`shutil.copytree` should have been called with the str equivalents of the Path objects.
# :func:`patches.copytree` should return the str type result of calling :func:`shutil.copytree` as a
# Path object.
mocked_shutil_copytree.assert_called_once_with(os.path.join('source', 'test', 'path'),
os.path.join('destination', 'test', 'path'))
self.assertEqual(result, Path('destination', 'test', 'path'))
def test_pcopytree_optional_params(self):
def test_copytree_optional_params(self):
"""
Test :func:`copytree` when optional parameters are passed
"""
# GIVEN: A mocked `shutil.copytree`
# GIVEN: A mocked :func:`shutil.copytree`
with patch('openlp.core.lib.shutil.shutil.copytree', return_value='') as mocked_shutil_copytree:
mocked_ignore = MagicMock()
mocked_copy_function = MagicMock()
# WHEN: Calling shutilpatches.copytree with the optional parameters set
shutilpatches.copytree(Path('source', 'test', 'path'),
Path('destination', 'test', 'path'),
symlinks=True,
ignore=mocked_ignore,
copy_function=mocked_copy_function,
ignore_dangling_symlinks=True)
# WHEN: Calling :func:`copytree` with the optional parameters set
copytree(Path('source', 'test', 'path'), Path('destination', 'test', 'path'), symlinks=True,
ignore=mocked_ignore, copy_function=mocked_copy_function, ignore_dangling_symlinks=True)
# THEN: `shutil.copytree` should have been called with the optional parameters, with out any of the values
# being modified
mocked_shutil_copytree.assert_called_once_with(ANY, ANY,
symlinks=True,
ignore=mocked_ignore,
# THEN: :func:`shutil.copytree` should have been called with the optional parameters, with out any of the
# values being modified
mocked_shutil_copytree.assert_called_once_with(ANY, ANY, symlinks=True, ignore=mocked_ignore,
copy_function=mocked_copy_function,
ignore_dangling_symlinks=True)
def test_prmtree(self):
def test_rmtree(self):
"""
Test :func:`rmtree`
"""
# GIVEN: A mocked `shutil.rmtree`
with patch('openlp.core.lib.shutil.shutil.rmtree', return_value=None) as mocked_rmtree:
# GIVEN: A mocked :func:`shutil.rmtree`
with patch('openlp.core.lib.shutil.shutil.rmtree', return_value=None) as mocked_shutil_rmtree:
# WHEN: Calling shutilpatches.rmtree with the path parameter as Path object type
result = shutilpatches.rmtree(Path('test', 'path'))
# WHEN: Calling :func:`rmtree` with the path parameter as Path object type
result = rmtree(Path('test', 'path'))
# THEN: `shutil.rmtree` should have been called with the str equivalents of the Path object.
mocked_rmtree.assert_called_once_with(os.path.join('test', 'path'))
# THEN: :func:`shutil.rmtree` should have been called with the str equivalents of the Path object.
mocked_shutil_rmtree.assert_called_once_with(os.path.join('test', 'path'))
self.assertIsNone(result)
def test_prmtree_optional_params(self):
def test_rmtree_optional_params(self):
"""
Test :func:`rmtree` when optional parameters are passed
Test :func:`rmtree` when optional parameters are passed
"""
# GIVEN: A mocked `shutil.rmtree`
# GIVEN: A mocked :func:`shutil.rmtree`
with patch('openlp.core.lib.shutil.shutil.rmtree', return_value='') as mocked_shutil_rmtree:
mocked_on_error = MagicMock()
# WHEN: Calling shutilpatches.rmtree with `ignore_errors` set to True and `onerror` set to a mocked object
shutilpatches.rmtree(Path('test', 'path'), ignore_errors=True, onerror=mocked_on_error)
# WHEN: Calling :func:`rmtree` with :param:`ignore_errors` set to True and `onerror` set to a mocked object
rmtree(Path('test', 'path'), ignore_errors=True, onerror=mocked_on_error)
# THEN: `shutil.rmtree` should have been called with the optional parameters, with out any of the values
# being modified
# THEN: :func:`shutil.rmtree` should have been called with the optional parameters, with out any of the
# values being modified
mocked_shutil_rmtree.assert_called_once_with(ANY, ignore_errors=True, onerror=mocked_on_error)
def test_which_no_command(self):
"""
Test :func:`which` when the command is not found.
"""
# GIVEN: A mocked :func:``shutil.which` when the command is not found.
with patch('openlp.core.lib.shutil.shutil.which', return_value=None) as mocked_shutil_which:
# WHEN: Calling :func:`which` with a command that does not exist.
result = which('no_command')
# THEN: :func:`shutil.which` should have been called with the command, and :func:`which` should return None.
mocked_shutil_which.assert_called_once_with('no_command')
self.assertIsNone(result)
def test_which_command(self):
"""
Test :func:`which` when a command has been found.
"""
# GIVEN: A mocked :func:`shutil.which` when the command is found.
with patch('openlp.core.lib.shutil.shutil.which',
return_value=os.path.join('path', 'to', 'command')) as mocked_shutil_which:
# WHEN: Calling :func:`which` with a command that exists.
result = which('command')
# THEN: :func:`shutil.which` should have been called with the command, and :func:`which` should return a
# Path object equivalent of the command path.
mocked_shutil_which.assert_called_once_with('command')
self.assertEqual(result, Path('path', 'to', 'command'))