From 3a8a7711dca164d641d18d5c326ebc59e9283666 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Mon, 30 Mar 2020 16:59:09 -0400 Subject: [PATCH] ImportErrors thrown if sleekxmpp was missing fixed --- .../plugins/NotifyXMPP/SleekXmppAdapter.py | 27 +++++++++++++++++-- apprise/plugins/NotifyXMPP/__init__.py | 18 ++----------- test/test_xmpp_plugin.py | 10 +++++-- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/apprise/plugins/NotifyXMPP/SleekXmppAdapter.py b/apprise/plugins/NotifyXMPP/SleekXmppAdapter.py index 86650995..a28e9ce5 100644 --- a/apprise/plugins/NotifyXMPP/SleekXmppAdapter.py +++ b/apprise/plugins/NotifyXMPP/SleekXmppAdapter.py @@ -2,13 +2,27 @@ import ssl from os.path import isfile -import sleekxmpp import logging +# Default our global support flag +SLEEKXMPP_SUPPORT_AVAILABLE = False + +try: + # Import sleekxmpp if available + import sleekxmpp + + SLEEKXMPP_SUPPORT_AVAILABLE = True + +except ImportError: + # No problem; we just simply can't support this plugin because we're + # either using Linux, or simply do not have sleekxmpp installed. + pass + + class SleekXmppAdapter(object): """ - Wrapper to SleekXmpp + Wrapper to sleekxmpp """ @@ -44,6 +58,15 @@ class SleekXmppAdapter(object): "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", ] + # This entry is a bit hacky, but it allows us to unit-test this library + # in an environment that simply doesn't have the sleekxmpp package + # available to us. + # + # If anyone is seeing this had knows a better way of testing this + # outside of what is defined in test/test_xmpp_plugin.py, please + # let me know! :) + _enabled = SLEEKXMPP_SUPPORT_AVAILABLE + def __init__(self, host=None, port=None, secure=False, verify_certificate=True, xep=None, jid=None, password=None, body=None, targets=None, before_message=None, logger=None): diff --git a/apprise/plugins/NotifyXMPP/__init__.py b/apprise/plugins/NotifyXMPP/__init__.py index 41539796..a1cd0073 100644 --- a/apprise/plugins/NotifyXMPP/__init__.py +++ b/apprise/plugins/NotifyXMPP/__init__.py @@ -30,25 +30,11 @@ from ...URLBase import PrivacyMode from ...common import NotifyType from ...utils import parse_list from ...AppriseLocale import gettext_lazy as _ +from .SleekXmppAdapter import SleekXmppAdapter # xep string parser XEP_PARSE_RE = re.compile('^[^1-9]*(?P[1-9][0-9]{0,3})$') -# Default our global support flag -NOTIFY_XMPP_SUPPORT_ENABLED = False - -try: - # Import sleekxmpp if available - - from .SleekXmppAdapter import SleekXmppAdapter - - NOTIFY_XMPP_SUPPORT_ENABLED = True - -except ImportError: - # No problem; we just simply can't support this plugin because we're - # either using Linux, or simply do not have sleekxmpp installed. - pass - class NotifyXMPP(NotifyBase): """ @@ -86,7 +72,7 @@ class NotifyXMPP(NotifyBase): # If anyone is seeing this had knows a better way of testing this # outside of what is defined in test/test_xmpp_plugin.py, please # let me know! :) - _enabled = NOTIFY_XMPP_SUPPORT_ENABLED + _enabled = SleekXmppAdapter._enabled # Define object templates templates = ( diff --git a/test/test_xmpp_plugin.py b/test/test_xmpp_plugin.py index 90b181c0..1abd8dbb 100644 --- a/test/test_xmpp_plugin.py +++ b/test/test_xmpp_plugin.py @@ -29,7 +29,6 @@ import sys import ssl import mock import pytest - import apprise try: @@ -43,11 +42,14 @@ except ImportError: # Python v2.7 pass + # Disable logging for a cleaner testing output import logging logging.disable(logging.CRITICAL) +@pytest.mark.skipif( + 'sleekxmpp' not in sys.modules, reason="requires sleekxmpp") def test_xmpp_plugin_import_error(tmpdir): """ API: NotifyXMPP Plugin() Import Error @@ -111,13 +113,15 @@ def test_xmpp_plugin_import_error(tmpdir): sys.path.remove(str(suite)) # Reload the libraries we care about - reload(sys.modules['apprise.plugins.NotifyXMPP']) reload(sys.modules['apprise.plugins.NotifyXMPP.SleekXmppAdapter']) + reload(sys.modules['apprise.plugins.NotifyXMPP']) reload(sys.modules['apprise.plugins']) reload(sys.modules['apprise.Apprise']) reload(sys.modules['apprise']) +@pytest.mark.skipif( + 'sleekxmpp' not in sys.modules, reason="requires sleekxmpp") def test_xmpp_plugin(tmpdir): """ API: NotifyXMPP Plugin() @@ -318,6 +322,8 @@ def test_xmpp_plugin(tmpdir): ca_backup +@pytest.mark.skipif( + 'sleekxmpp' not in sys.modules, reason="requires sleekxmpp") def test_sleekxmpp_callbacks(): """ API: NotifyXMPP Plugin() Sleekxmpp callback tests