Debian Codegraph @

mypyc/test/test_external.py

raw
1 """Test cases that run tests as subprocesses."""
2
3 from __future__ import annotations
4
5 import os
6 import subprocess
7 import sys
8 import tempfile
9 import unittest
10 from distutils import ccompiler, sysconfig
11 from typing import Any
12
13 from mypyc.build import get_cflags, include_dir
14
15 from .config import PREFIX
16
17 EXCLUDED_LIB_RT_COMPILE_FILES = ["static_data.c"]
18
19
20 class TestExternal(unittest.TestCase):
21 def test_lib_rt_c_files_compile_individually(self) -> None:
22 """Compile each top-level lib-rt C file as its own translation unit."""
23 lib_rt_dir = include_dir()
24 source_names = sorted(
25 name
26 for name in os.listdir(lib_rt_dir)
27 if name.endswith(".c")
28 and os.path.isfile(os.path.join(lib_rt_dir, name))
29 and name not in EXCLUDED_LIB_RT_COMPILE_FILES
30 )
31 compiler: Any = ccompiler.new_compiler()
32 sysconfig.customize_compiler(compiler)
33
34 include_dirs = [lib_rt_dir]
35 for plat_specific in (False, True):
36 path = sysconfig.get_python_inc(plat_specific=plat_specific)
37 if path and path not in include_dirs:
38 include_dirs.append(path)
39
40 with tempfile.TemporaryDirectory() as tmpdir:
41 for experimental_features in (False, True):
42 cflags = get_cflags(
43 compiler_type=compiler.compiler_type,
44 opt_level="0",
45 experimental_features=experimental_features,
46 )
47 output_dir = os.path.join(
48 tmpdir, "experimental" if experimental_features else "default"
49 )
50
51 for source_name in source_names:
52 source_path = os.path.join(lib_rt_dir, source_name)
53 with self.subTest(source=source_name, experimental=experimental_features):
54 try:
55 compiler.compile(
56 [source_path],
57 output_dir=output_dir,
58 include_dirs=include_dirs,
59 extra_postargs=cflags,
60 )
61 except Exception as err:
62 raise AssertionError(
63 f"failed to compile {source_name} "
64 f"(experimental={experimental_features})"
65 ) from err
66
67 # TODO: Get this to work on Windows.
68 # (Or don't. It is probably not a good use of time.)
69 @unittest.skipIf(sys.platform.startswith("win"), "rt tests don't work on windows")
70 def test_c_unit_test(self) -> None:
71 """Run C unit tests in a subprocess."""
72 cppflags: list[str] = []
73 env = os.environ.copy()
74 if sys.platform == "darwin":
75 cppflags += ["-O0", "-mmacosx-version-min=10.10", "-stdlib=libc++"]
76 elif sys.platform == "linux":
77 cppflags += ["-O0"]
78 env["CPPFLAGS"] = " ".join(cppflags)
79 # Build Python wrapper for C unit tests.
80
81 with tempfile.TemporaryDirectory() as tmpdir:
82 status = subprocess.check_call(
83 [
84 sys.executable,
85 "setup.py",
86 "build_ext",
87 f"--build-lib={tmpdir}",
88 f"--build-temp={tmpdir}",
89 "--run-capi-tests",
90 ],
91 env=env,
92 cwd=os.path.join(PREFIX, "mypyc", "lib-rt"),
93 )
94 # Run C unit tests.
95 env = os.environ.copy()
96 if "GTEST_COLOR" not in os.environ:
97 env["GTEST_COLOR"] = "yes" # Use fancy colors
98 status = subprocess.call(
99 [sys.executable, "-c", "import sys, test_capi; sys.exit(test_capi.run_tests())"],
100 env=env,
101 cwd=tmpdir,
102 )
103 if status != 0:
104 raise AssertionError("make test: C unit test failure")