• 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 not support.has_subprocess_support:
11    raise unittest.SkipTest("test module requires subprocess")
12
13
14basepath = 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
20toolsdir = os.path.join(basepath, 'Tools')
21scriptsdir = os.path.join(toolsdir, 'scripts')
22
23def 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
33def 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
38def import_tool(toolname):
39    with import_helper.DirsOnSysPath(scriptsdir):
40        return importlib.import_module(toolname)
41
42def load_tests(*args):
43    return support.load_package_tests(os.path.dirname(__file__), *args)
44