Merge branch 'more-permission-errors' into 'master'

Work around a permission error

See merge request openlp/openlp!466
This commit is contained in:
Tim Bentley 2022-07-17 13:31:39 +00:00
commit f0dd979158
5 changed files with 66 additions and 11 deletions

View File

@ -263,10 +263,13 @@ def sha256_file_hash(filename):
hash_obj = hashlib.sha256()
if not filename.exists():
return None
with filename.open('rb') as f:
for chunk in iter(lambda: f.read(65536), b''):
hash_obj.update(chunk)
return hash_obj.hexdigest()
try:
with filename.open('rb') as f:
for chunk in iter(lambda: f.read(65536), b''):
hash_obj.update(chunk)
return hash_obj.hexdigest()
except PermissionError:
return None
def qmd5_hash(salt=None, data=None):

View File

@ -24,7 +24,6 @@ This class contains the core default settings.
import datetime
import json
import logging
from openlp.core.ui.style import UiThemes
import os
from enum import IntEnum
from pathlib import Path
@ -38,6 +37,7 @@ from openlp.core.common.enum import AlertLocation, BibleSearch, CustomSearch, Im
from openlp.core.common.json import OpenLPJSONDecoder, OpenLPJSONEncoder, is_serializable
from openlp.core.common.path import files_to_paths, str_to_path
from openlp.core.common.platform import is_linux, is_win
from openlp.core.ui.style import UiThemes
log = logging.getLogger(__name__)

View File

@ -152,7 +152,9 @@ class PresentationDocument(object):
folder = self._sha256_file_hash
else:
self._sha256_file_hash = sha256_file_hash(self.file_path)
folder = self._sha256_file_hash
# If the sha256_file_hash() function encounters an error, it will return None, so use the
# filename as the thumbnail folder if the result is None (or falsey)
folder = self._sha256_file_hash or self.file_path.name
else:
folder = self.file_path.name
return Path(self.controller.thumbnail_folder, folder)

View File

@ -356,10 +356,10 @@ def test_sha256_file_hash():
ppt_path = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'test.ppt')
filename = Path(ppt_path)
# WHEN: Given a known salt+data
# WHEN: Generating a hash for the file
result = sha256_file_hash(filename)
# THEN: Validate return has is same
# THEN: Validate that the hash is correct
assert result == 'be6d7bdca25d1662d7faa1f856bfc224646dbad3b65ebff800d9ae70537968f9'
@ -371,10 +371,25 @@ def test_sha256_file_hash_no_exist():
mocked_path = MagicMock()
mocked_path.exists.return_value = False
# WHEN: Given a known salt+data
# WHEN: A hash is generated on a non-existent file
result = sha256_file_hash(mocked_path)
# THEN: Validate return has is same
# THEN: the result should be None
assert result is None
def test_sha256_file_hash_permission_error():
"""
Test SHA256 file hash when there is a permission error
"""
# GIVEN: A mocked Path object
mocked_path = MagicMock()
mocked_path.open.side_effect = PermissionError
# WHEN: Generating a hash for the file
result = sha256_file_hash(mocked_path)
# THEN: The result should be None
assert result is None

View File

@ -26,7 +26,8 @@ from pathlib import Path
from unittest.mock import call, patch
from openlp.core.common import settings
from openlp.core.common.settings import Settings, media_players_conv
from openlp.core.common.settings import Settings, media_players_conv, upgrade_dark_theme_to_ui_theme
from openlp.core.ui.style import UiThemes
def test_media_players_conv():
@ -84,6 +85,20 @@ def test_get_default_value():
assert result == 'baa'
def test_get_default_value_with_group():
"""Test that the default value for a setting is returned"""
# GIVEN: A Settings class with a default value
Settings.__default_settings__['test/moo'] = 'baa'
# WHEN: get_default_value() is called
settings = Settings()
settings.beginGroup('test')
result = settings.get_default_value('moo')
# THEN: The correct default value should be returned
assert result == 'baa'
def test_settings_override():
"""Test the Settings creation and its override usage"""
# GIVEN: an override for the settings
@ -300,3 +315,23 @@ def test_convert_value_setting_bool_str():
# THEN: The result should be False
assert result is False, 'The result should be False'
def test_upgrade_dark_theme_to_ui_theme_true():
"""Test that the upgrade_dark_theme_to_ui_theme function returns UiTheme.QDarkStyle for True"""
# GIVEN: The upgrade_dark_theme_to_ui_theme function
# WHEN: upgrade_dark_theme_to_ui_theme is called with True
result = upgrade_dark_theme_to_ui_theme(True)
# THEN: UiTheme.QDarkStyle should be returned
assert result == UiThemes.QDarkStyle
def test_upgrade_dark_theme_to_ui_theme_false():
"""Test that the upgrade_dark_theme_to_ui_theme function returns UiTheme.Automatic for False"""
# GIVEN: The upgrade_dark_theme_to_ui_theme function
# WHEN: upgrade_dark_theme_to_ui_theme is called with False
result = upgrade_dark_theme_to_ui_theme(False)
# THEN: UiTheme.QDarkStyle should be returned
assert result == UiThemes.Automatic