From d839a0b1013873e27eae334a21b56fa57cd5e178 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Wed, 4 Oct 2023 03:31:18 -0400 Subject: [PATCH] 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. --- mypyc/lib-rt/setup.py | 29 ++++++++++++++++++++- mypyc/test/test_external.py | 50 ++++++++++++++++++------------------- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/mypyc/lib-rt/setup.py b/mypyc/lib-rt/setup.py index a31b705cd723..ef81b794c9bd 100644 --- a/mypyc/lib-rt/setup.py +++ b/mypyc/lib-rt/setup.py @@ -5,7 +5,10 @@ from __future__ import annotations +import os +import subprocess import sys +from distutils.command.build_ext import build_ext from distutils.core import Extension, setup from typing import Any @@ -17,6 +20,30 @@ kwargs = {} compile_args = ["--std=c++11"] + +class build_ext_custom(build_ext): + def get_library_names(self): + return ["gtest"] + + def run(self): + gtest_dir = os.path.abspath( + os.path.join(os.path.dirname(__file__), "..", "external", "googletest") + ) + + os.makedirs(self.build_temp, exist_ok=True) + + # Build Google Test, the C++ framework we use for testing C code. + # The source code for Google Test is copied to this repository. + subprocess.check_call( + ["make", "-f", os.path.join(gtest_dir, "make", "Makefile"), f"GTEST_DIR={gtest_dir}"], + cwd=self.build_temp, + ) + + self.library_dirs = [self.build_temp] + + return build_ext.run(self) + + setup( name="test_capi", version="0.1", @@ -34,10 +61,10 @@ ], depends=["CPy.h", "mypyc_util.h", "pythonsupport.h"], extra_compile_args=["-Wno-unused-function", "-Wno-sign-compare"] + compile_args, - library_dirs=["../external/googletest/make"], libraries=["gtest"], include_dirs=["../external/googletest", "../external/googletest/include"], **kwargs, ) ], + cmdclass={"build_ext": build_ext_custom}, ) diff --git a/mypyc/test/test_external.py b/mypyc/test/test_external.py index 6deabd81255e..22eb8019133c 100644 --- a/mypyc/test/test_external.py +++ b/mypyc/test/test_external.py @@ -5,6 +5,7 @@ import os import subprocess import sys +import tempfile import unittest base_dir = os.path.join(os.path.dirname(__file__), "..", "..") @@ -16,34 +17,33 @@ class TestExternal(unittest.TestCase): @unittest.skipIf(sys.platform.startswith("win"), "rt tests don't work on windows") def test_c_unit_test(self) -> None: """Run C unit tests in a subprocess.""" - # Build Google Test, the C++ framework we use for testing C code. - # The source code for Google Test is copied to this repository. cppflags: list[str] = [] env = os.environ.copy() if sys.platform == "darwin": cppflags += ["-mmacosx-version-min=10.10", "-stdlib=libc++"] env["CPPFLAGS"] = " ".join(cppflags) - subprocess.check_call( - ["make", "libgtest.a"], - env=env, - cwd=os.path.join(base_dir, "mypyc", "external", "googletest", "make"), - ) # Build Python wrapper for C unit tests. - env = os.environ.copy() - env["CPPFLAGS"] = " ".join(cppflags) - status = subprocess.check_call( - [sys.executable, "setup.py", "build_ext", "--inplace"], - env=env, - cwd=os.path.join(base_dir, "mypyc", "lib-rt"), - ) - # Run C unit tests. - env = os.environ.copy() - if "GTEST_COLOR" not in os.environ: - env["GTEST_COLOR"] = "yes" # Use fancy colors - status = subprocess.call( - [sys.executable, "-c", "import sys, test_capi; sys.exit(test_capi.run_tests())"], - env=env, - cwd=os.path.join(base_dir, "mypyc", "lib-rt"), - ) - if status != 0: - raise AssertionError("make test: C unit test failure") + + with tempfile.TemporaryDirectory() as tmpdir: + status = subprocess.check_call( + [ + sys.executable, + "setup.py", + "build_ext", + f"--build-lib={tmpdir}", + f"--build-temp={tmpdir}", + ], + env=env, + cwd=os.path.join(base_dir, "mypyc", "lib-rt"), + ) + # Run C unit tests. + env = os.environ.copy() + if "GTEST_COLOR" not in os.environ: + env["GTEST_COLOR"] = "yes" # Use fancy colors + status = subprocess.call( + [sys.executable, "-c", "import sys, test_capi; sys.exit(test_capi.run_tests())"], + env=env, + cwd=tmpdir, + ) + if status != 0: + raise AssertionError("make test: C unit test failure")