• 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
9
10if support.check_sanitizer(address=True, memory=True):
11    # bpo-46633: Skip the test because it is too slow when Python is built
12    # with ASAN/MSAN: between 5 and 20 minutes on GitHub Actions.
13    raise unittest.SkipTest("test too slow on ASAN/MSAN build")
14
15
16basepath = os.path.normpath(
17        os.path.dirname(                 # <src/install dir>
18            os.path.dirname(                # Lib
19                os.path.dirname(                # test
20                    os.path.dirname(__file__)))))    # test_tools
21
22toolsdir = os.path.join(basepath, 'Tools')
23scriptsdir = os.path.join(toolsdir, 'scripts')
24
25def skip_if_missing(tool=None):
26    if tool:
27        tooldir = os.path.join(toolsdir, tool)
28    else:
29        tool = 'scripts'
30        tooldir = scriptsdir
31    if not os.path.isdir(tooldir):
32        raise unittest.SkipTest(f'{tool} directory could not be found')
33
34@contextlib.contextmanager
35def imports_under_tool(name, *subdirs):
36    tooldir = os.path.join(toolsdir, name, *subdirs)
37    with import_helper.DirsOnSysPath(tooldir) as cm:
38        yield cm
39
40def import_tool(toolname):
41    with import_helper.DirsOnSysPath(scriptsdir):
42        return importlib.import_module(toolname)
43
44def load_tests(*args):
45    return support.load_package_tests(os.path.dirname(__file__), *args)
46