55import os
66import subprocess
77import sys
8+ import tempfile
89import unittest
910
1011base_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