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