Skip to content

Commit 05c6567

Browse files
committed
gh-151089: Generalise and improve test skipping in test_ctypes/test_find.py
In FindLibraryLinux, check for gcc and ld in setUpClass and then skip the tests that need those commands to be present.
1 parent 30f0ccf commit 05c6567

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

Lib/test/test_ctypes/test_find.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,30 @@ def test_shell_injection(self):
7878
@unittest.skipUnless(sys.platform.startswith('linux'),
7979
'Test only valid for Linux')
8080
class FindLibraryLinux(unittest.TestCase):
81+
@classmethod
82+
def setUpClass(cls):
83+
import subprocess
84+
85+
try:
86+
subprocess.check_output(['gcc', '--version'])
87+
cls.has_gcc = True
88+
except (FileNotFoundError, subprocess.CalledProcessError):
89+
cls.has_gcc = False
90+
91+
try:
92+
subprocess.check_output(['ld', '--version'])
93+
cls.has_ld = True
94+
except (FileNotFoundError, subprocess.CalledProcessError):
95+
cls.has_ld = False
96+
8197
@thread_unsafe('uses setenv')
8298
def test_find_on_libpath(self):
99+
if not self.has_gcc:
100+
self.skipTest("gcc not available")
101+
83102
import subprocess
84103
import tempfile
85104

86-
try:
87-
p = subprocess.Popen(['gcc', '--version'], stdout=subprocess.PIPE,
88-
stderr=subprocess.DEVNULL)
89-
out, _ = p.communicate()
90-
except OSError:
91-
raise unittest.SkipTest('gcc, needed for test, not available')
92105
with tempfile.TemporaryDirectory() as d:
93106
# create an empty temporary file
94107
srcname = os.path.join(d, 'dummy.c')
@@ -118,11 +131,17 @@ def test_find_on_libpath(self):
118131
self.assertEqual(find_library(libname), 'lib%s.so' % libname)
119132

120133
def test_find_library_with_gcc(self):
134+
if not self.has_gcc:
135+
self.skipTest("gcc not available")
136+
121137
with unittest.mock.patch("ctypes.util._findSoname_ldconfig", lambda *args: None), \
122138
unittest.mock.patch("ctypes.util._findLib_ld", lambda *args: None):
123139
self.assertNotEqual(find_library('c'), None)
124140

125141
def test_find_library_with_ld(self):
142+
if not self.has_ld:
143+
self.skipTest("ld not available")
144+
126145
with unittest.mock.patch("ctypes.util._findSoname_ldconfig", lambda *args: None), \
127146
unittest.mock.patch("ctypes.util._findLib_gcc", lambda *args: None):
128147
self.assertNotEqual(find_library('c'), None)

0 commit comments

Comments
 (0)