| 1 |
"""Support functions for testing scripts in the Tools directory.""" |
| 2 |
import contextlib |
| 3 |
import importlib |
| 4 |
import os.path |
| 5 |
import unittest |
| 6 |
from test import support |
| 7 |
from test.support import import_helper |
| 8 |
|
| 9 |
|
| 10 |
if not support.has_subprocess_support: |
| 11 |
raise unittest.SkipTest("test module requires subprocess") |
| 12 |
|
| 13 |
|
| 14 |
basepath = os.path.normpath( |
| 15 |
os.path.dirname( # <src/install dir> |
| 16 |
os.path.dirname( # Lib |
| 17 |
os.path.dirname( # test |
| 18 |
os.path.dirname(__file__))))) # test_tools |
| 19 |
|
| 20 |
toolsdir = os.path.join(basepath, 'Tools') |
| 21 |
scriptsdir = os.path.join(toolsdir, 'scripts') |
| 22 |
|
| 23 |
def skip_if_missing(tool=None): |
| 24 |
if tool: |
| 25 |
tooldir = os.path.join(toolsdir, tool) |
| 26 |
else: |
| 27 |
tool = 'scripts' |
| 28 |
tooldir = scriptsdir |
| 29 |
if not os.path.isdir(tooldir): |
| 30 |
raise unittest.SkipTest(f'{tool} directory could not be found') |
| 31 |
|
| 32 |
@contextlib.contextmanager |
| 33 |
def imports_under_tool(name, *subdirs): |
| 34 |
tooldir = os.path.join(toolsdir, name, *subdirs) |
| 35 |
with import_helper.DirsOnSysPath(tooldir) as cm: |
| 36 |
yield cm |
| 37 |
|
| 38 |
def import_tool(toolname): |
| 39 |
with import_helper.DirsOnSysPath(scriptsdir): |
| 40 |
return importlib.import_module(toolname) |
| 41 |
|
| 42 |
def load_tests(*args): |
| 43 |
return support.load_package_tests(os.path.dirname(__file__), *args) |