Skip to content

Commit d839a0b

Browse files
authored
tests: avoid leaving artifacts in the source tree (#16201)
When running the mypy unittests, most of the time any output files are produced into a temporary directory and cleaned up. In one case, it wasn't. Fix this for test_capi.
1 parent 96803e0 commit d839a0b

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

mypyc/lib-rt/setup.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
from __future__ import annotations
77

8+
import os
9+
import subprocess
810
import sys
11+
from distutils.command.build_ext import build_ext
912
from distutils.core import Extension, setup
1013
from typing import Any
1114

@@ -17,6 +20,30 @@
1720
kwargs = {}
1821
compile_args = ["--std=c++11"]
1922

23+
24+
class build_ext_custom(build_ext):
25+
def get_library_names(self):
26+
return ["gtest"]
27+
28+
def run(self):
29+
gtest_dir = os.path.abspath(
30+
os.path.join(os.path.dirname(__file__), "..", "external", "googletest")
31+
)
32+
33+
os.makedirs(self.build_temp, exist_ok=True)
34+
35+
# Build Google Test, the C++ framework we use for testing C code.
36+
# The source code for Google Test is copied to this repository.
37+
subprocess.check_call(
38+
["make", "-f", os.path.join(gtest_dir, "make", "Makefile"), f"GTEST_DIR={gtest_dir}"],
39+
cwd=self.build_temp,
40+
)
41+
42+
self.library_dirs = [self.build_temp]
43+
44+
return build_ext.run(self)
45+
46+
2047
setup(
2148
name="test_capi",
2249
version="0.1",
@@ -34,10 +61,10 @@
3461
],
3562
depends=["CPy.h", "mypyc_util.h", "pythonsupport.h"],
3663
extra_compile_args=["-Wno-unused-function", "-Wno-sign-compare"] + compile_args,
37-
library_dirs=["../external/googletest/make"],
3864
libraries=["gtest"],
3965
include_dirs=["../external/googletest", "../external/googletest/include"],
4066
**kwargs,
4167
)
4268
],
69+
cmdclass={"build_ext": build_ext_custom},
4370
)

mypyc/test/test_external.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import subprocess
77
import sys
8+
import tempfile
89
import unittest
910

1011
base_dir = os.path.join(os.path.dirname(__file__), "..", "..")
@@ -16,34 +17,33 @@ class TestExternal(unittest.TestCase):
1617
@unittest.skipIf(sys.platform.startswith("win"), "rt tests don't work on windows")
1718
def test_c_unit_test(self) -> None:
1819
"""Run C unit tests in a subprocess."""
19-
# Build Google Test, the C++ framework we use for testing C code.
20-
# The source code for Google Test is copied to this repository.
2120
cppflags: list[str] = []
2221
env = os.environ.copy()
2322
if sys.platform == "darwin":
2423
cppflags += ["-mmacosx-version-min=10.10", "-stdlib=libc++"]
2524
env["CPPFLAGS"] = " ".join(cppflags)
26-
subprocess.check_call(
27-
["make", "libgtest.a"],
28-
env=env,
29-
cwd=os.path.join(base_dir, "mypyc", "external", "googletest", "make"),
30-
)
3125
# Build Python wrapper for C unit tests.
32-
env = os.environ.copy()
33-
env["CPPFLAGS"] = " ".join(cppflags)
34-
status = subprocess.check_call(
35-
[sys.executable, "setup.py", "build_ext", "--inplace"],
36-
env=env,
37-
cwd=os.path.join(base_dir, "mypyc", "lib-rt"),
38-
)
39-
# Run C unit tests.
40-
env = os.environ.copy()
41-
if "GTEST_COLOR" not in os.environ:
42-
env["GTEST_COLOR"] = "yes" # Use fancy colors
43-
status = subprocess.call(
44-
[sys.executable, "-c", "import sys, test_capi; sys.exit(test_capi.run_tests())"],
45-
env=env,
46-
cwd=os.path.join(base_dir, "mypyc", "lib-rt"),
47-
)
48-
if status != 0:
49-
raise AssertionError("make test: C unit test failure")
26+
27+
with tempfile.TemporaryDirectory() as tmpdir:
28+
status = subprocess.check_call(
29+
[
30+
sys.executable,
31+
"setup.py",
32+
"build_ext",
33+
f"--build-lib={tmpdir}",
34+
f"--build-temp={tmpdir}",
35+
],
36+
env=env,
37+
cwd=os.path.join(base_dir, "mypyc", "lib-rt"),
38+
)
39+
# Run C unit tests.
40+
env = os.environ.copy()
41+
if "GTEST_COLOR" not in os.environ:
42+
env["GTEST_COLOR"] = "yes" # Use fancy colors
43+
status = subprocess.call(
44+
[sys.executable, "-c", "import sys, test_capi; sys.exit(test_capi.run_tests())"],
45+
env=env,
46+
cwd=tmpdir,
47+
)
48+
if status != 0:
49+
raise AssertionError("make test: C unit test failure")

0 commit comments

Comments
 (0)