Skip to content

Commit

Permalink
tests: avoid leaving artifacts in the source tree (#16201)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eli-schwartz authored Oct 4, 2023
1 parent 96803e0 commit d839a0b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
29 changes: 28 additions & 1 deletion mypyc/lib-rt/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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",
Expand All @@ -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},
)
50 changes: 25 additions & 25 deletions mypyc/test/test_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import subprocess
import sys
import tempfile
import unittest

base_dir = os.path.join(os.path.dirname(__file__), "..", "..")
Expand All @@ -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")

0 comments on commit d839a0b

Please sign in to comment.