test_external.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """Test cases that run tests as subprocesses."""
  2. from __future__ import annotations
  3. import os
  4. import subprocess
  5. import sys
  6. import unittest
  7. base_dir = os.path.join(os.path.dirname(__file__), "..", "..")
  8. class TestExternal(unittest.TestCase):
  9. # TODO: Get this to work on Windows.
  10. # (Or don't. It is probably not a good use of time.)
  11. @unittest.skipIf(sys.platform.startswith("win"), "rt tests don't work on windows")
  12. def test_c_unit_test(self) -> None:
  13. """Run C unit tests in a subprocess."""
  14. # Build Google Test, the C++ framework we use for testing C code.
  15. # The source code for Google Test is copied to this repository.
  16. cppflags: list[str] = []
  17. env = os.environ.copy()
  18. if sys.platform == "darwin":
  19. cppflags += ["-mmacosx-version-min=10.10", "-stdlib=libc++"]
  20. env["CPPFLAGS"] = " ".join(cppflags)
  21. subprocess.check_call(
  22. ["make", "libgtest.a"],
  23. env=env,
  24. cwd=os.path.join(base_dir, "mypyc", "external", "googletest", "make"),
  25. )
  26. # Build Python wrapper for C unit tests.
  27. env = os.environ.copy()
  28. env["CPPFLAGS"] = " ".join(cppflags)
  29. status = subprocess.check_call(
  30. [sys.executable, "setup.py", "build_ext", "--inplace"],
  31. env=env,
  32. cwd=os.path.join(base_dir, "mypyc", "lib-rt"),
  33. )
  34. # Run C unit tests.
  35. env = os.environ.copy()
  36. if "GTEST_COLOR" not in os.environ:
  37. env["GTEST_COLOR"] = "yes" # Use fancy colors
  38. status = subprocess.call(
  39. [sys.executable, "-c", "import sys, test_capi; sys.exit(test_capi.run_tests())"],
  40. env=env,
  41. cwd=os.path.join(base_dir, "mypyc", "lib-rt"),
  42. )
  43. if status != 0:
  44. raise AssertionError("make test: C unit test failure")