From 417b78cd738b93a610d4ef34b4067c383f3ecfd5 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Tue, 8 Dec 2020 17:26:29 -0600 Subject: [PATCH] Allow overriding of assets --- src/pgwui_common/assets.py | 17 +++++++++++++--- src/pgwui_common/exceptions.py | 14 +++++++++++++ tests/test_assets.py | 36 ++++++++++++++++++++++++++++++---- tests/test_exceptions.py | 12 ++++++++++++ 4 files changed, 72 insertions(+), 7 deletions(-) diff --git a/src/pgwui_common/assets.py b/src/pgwui_common/assets.py index 248d37f..39ab1b6 100644 --- a/src/pgwui_common/assets.py +++ b/src/pgwui_common/assets.py @@ -23,6 +23,9 @@ ''' import logging +import pyramid.exceptions + +from . import exceptions as ex # Logging log = logging.getLogger(__name__) @@ -31,8 +34,16 @@ log = logging.getLogger(__name__) def override_assets(config, settings): pgwui = settings['pgwui'] if 'override_assets' not in pgwui: - return + return [] + errors = [] for asset, replacement in pgwui['override_assets'].items(): - config.override_asset(asset, replacement) - log.debug(f'Overriding asset ({asset}) with ({replacement})') + try: + config.override_asset(asset, replacement) + except (pyramid.exceptions.ConfigurationError, + ModuleNotFoundError) as exp: + errors.append(ex.BadAssetOverrideError(asset, replacement, exp)) + else: + log.debug(f'Overriding asset ({asset}) with ({replacement})') + + return errors diff --git a/src/pgwui_common/exceptions.py b/src/pgwui_common/exceptions.py index cd69831..80152e7 100644 --- a/src/pgwui_common/exceptions.py +++ b/src/pgwui_common/exceptions.py @@ -125,6 +125,20 @@ class NotBooleanSettingError(Error): .format(key)) +class BadAssetOverrideError(Error): + def __init__(self, asset, new_asset, exp): + super().__init__( + f'The asset ({asset}) cannot be overridden with ({new_asset}):' + f' {exp}') + + +class BadSettingError(Error): + def __init__(self, exp): + super().__init__( + f'Bad settings caused an error: {exp}' + '\nHint: Overriding non-existant assets can cause this problem') + + class BadPathError(Error): pass diff --git a/tests/test_assets.py b/tests/test_assets.py index 9650edf..9948319 100644 --- a/tests/test_assets.py +++ b/tests/test_assets.py @@ -22,7 +22,9 @@ import pytest import logging +import pyramid.exceptions import pgwui_common.assets as assets +import pgwui_common.exceptions as common_ex from pgwui_testing import testing @@ -35,7 +37,7 @@ pytestmark = pytest.mark.unittest mock_override_asset = testing.instance_method_mock_fixture('override_asset') -# override_asset() +# override_assets() @pytest.mark.parametrize( ('overrides', 'call_count'), [ @@ -43,9 +45,10 @@ mock_override_asset = testing.instance_method_mock_fixture('override_asset') ({'some_asset': 'new_asset'}, 1), ({'some_asset': 'new_asset', 'other_asset': 'other new asset'}, 2)]) -def test_override_asset( +def test_override_assets_success( caplog, pyramid_config, mock_override_asset, overrides, call_count): - '''override_asset() is called for each override + '''override_asset() is called and a debug message logged + for each good override ''' caplog.set_level(logging.DEBUG) mocked_override_asset = mock_override_asset(pyramid_config) @@ -54,7 +57,32 @@ def test_override_asset( settings = {'override_assets': overrides} else: settings = {} - assets.override_assets(pyramid_config, {'pgwui': settings}) + result = assets.override_assets(pyramid_config, {'pgwui': settings}) + assert result == [] assert mocked_override_asset.call_count == call_count assert len(caplog.record_tuples) == call_count + + +@pytest.mark.parametrize( + ('exception'), [ + (pyramid.exceptions.ConfigurationError,), + (ModuleNotFoundError,)]) +def test_override_assets_failure( + caplog, pyramid_config, mock_override_asset, exception): + '''There is no logging when the override_asset call fails, + but an error is returned + ''' + caplog.set_level(logging.DEBUG) + mocked_override_asset = mock_override_asset(pyramid_config) + mocked_override_asset.side_effect = exception + + result = assets.override_assets(pyramid_config, + {'pgwui': + {'override_assets': + {'asset': 'new'}}}) + assert len(result) == 1 + assert isinstance(result[0], common_ex.BadAssetOverrideError) + + logs = caplog.record_tuples + assert len(logs) == 0 diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 575412c..43ad7b8 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -24,6 +24,18 @@ import pytest import pgwui_common.exceptions as common_ex +# Unit tests + +@pytest.mark.unittest +def test_badsettingerror(): + '''Takes an argument''' + # Thing is, this exception is only used by PGWUI_Server + # when exiting the configuration context raises a configuration + # execution error. + assert isinstance(common_ex.BadSettingError('foo'), + common_ex.Error) + + # Functional tests @pytest.mark.integrationtest -- 2.34.1