提交 9f539d1c 编写于 作者: C Cleber Rosa

selftests: add common method for checking presence of Python modules

There are at least two places in which the same functionality is
present.  Let's make it a single utility function instead.
Signed-off-by: NCleber Rosa <crosa@redhat.com>
上级 ca6f4683
import os
import pkg_resources
import sys
import unittest
......@@ -33,3 +34,19 @@ def recent_mock():
elif sys.version_info[0] == 3:
return sys.version_info[1] >= 6
return True
def python_module_available(module_name):
'''
Checks if a given Python module is available
:param module_name: the name of the module
:type module_name: str
:returns: if the Python module is available in the system
:rtype: bool
'''
try:
pkg_resources.require(module_name)
return True
except pkg_resources.DistributionNotFound:
return False
......@@ -12,7 +12,6 @@ import xml.dom.minidom
import zipfile
import unittest
import psutil
import pkg_resources
try:
from io import BytesIO
......@@ -35,7 +34,7 @@ from avocado.utils import process
from avocado.utils import script
from avocado.utils import path as utils_path
from .. import AVOCADO, BASEDIR
from .. import AVOCADO, BASEDIR, python_module_available
LOCAL_IMPORT_TEST_CONTENTS = '''
......@@ -145,14 +144,6 @@ READ_BINARY = probe_binary('read')
SLEEP_BINARY = probe_binary('sleep')
def html_capable():
try:
pkg_resources.require('avocado-framework-plugin-result-html')
return True
except pkg_resources.DistributionNotFound:
return False
class RunnerOperationTest(unittest.TestCase):
def setUp(self):
......@@ -782,7 +773,7 @@ class RunnerSimpleTest(unittest.TestCase):
"sysinfo", "pre",
"echo \'________\'")))
if html_capable():
if python_module_available('avocado-framework-plugin-result-html'):
with open(os.path.join(self.tmpdir, "latest",
"results.html")) as html_res:
html_results = html_res.read()
......@@ -1156,7 +1147,7 @@ class PluginsTest(AbsPluginsTest, unittest.TestCase):
result_plugins = ["json", "xunit", "zip_archive"]
result_outputs = ["results.json", "results.xml"]
if html_capable():
if python_module_available('avocado-framework-plugin-result-html'):
result_plugins.append("html")
result_outputs.append("results.html")
......
......@@ -5,26 +5,19 @@ __author__ = 'Lucas Meneghel Rodrigues <lmr@redhat.com>'
import gc
import os
import pkg_resources
import subprocess
import sys
import unittest
from avocado.core import data_dir
from selftests import python_module_available
CHECK_TMP_DIRS = os.path.abspath(os.path.join(os.path.dirname(__file__),
"check_tmp_dirs"))
def plugin_available(plugin_name):
try:
pkg_resources.require(plugin_name)
return True
except pkg_resources.DistributionNotFound:
return False
def test_suite():
suite = unittest.TestSuite()
loader = unittest.TestLoader()
......@@ -44,7 +37,7 @@ def test_suite():
('avocado-framework-plugin-result-html',
'html'))
for plugin_name, plugin_dir in plugins:
if plugin_available(plugin_name):
if python_module_available(plugin_name):
path = os.path.join(basedir, 'optional_plugins',
plugin_dir, 'tests')
suite.addTests(loader.discover(start_dir=path, top_level_dir=path))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册