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