Skip to content

Commit c3f6551

Browse files
authored
avoid raising the result of pytest.skip/pytest.fail (#2638)
1 parent a2091b9 commit c3f6551

File tree

13 files changed

+80
-78
lines changed

13 files changed

+80
-78
lines changed

psutil/tests/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ def create_c_exe(path, c_code=None):
946946
"""Create a compiled C executable in the given location."""
947947
assert not os.path.exists(path), path
948948
if not shutil.which("gcc"):
949-
raise pytest.skip("gcc is not installed")
949+
return pytest.skip("gcc is not installed")
950950
if c_code is None:
951951
c_code = textwrap.dedent("""
952952
#include <unistd.h>
@@ -1060,7 +1060,7 @@ def assert_pid_gone(self, pid):
10601060
try:
10611061
psutil.Process(pid)
10621062
except psutil.ZombieProcess:
1063-
raise pytest.fail("wasn't supposed to raise ZombieProcess")
1063+
return pytest.fail("wasn't supposed to raise ZombieProcess")
10641064
assert cm.value.pid == pid
10651065
assert cm.value.name is None
10661066
assert not psutil.pid_exists(pid), pid
@@ -1233,13 +1233,13 @@ def _check_fds(self, fun):
12331233
f"negative diff {diff!r} (gc probably collected a"
12341234
" resource from a previous test)"
12351235
)
1236-
raise pytest.fail(msg)
1236+
return pytest.fail(msg)
12371237
if diff > 0:
12381238
type_ = "fd" if POSIX else "handle"
12391239
if diff > 1:
12401240
type_ += "s"
12411241
msg = f"{diff} unclosed {type_} after calling {fun!r}"
1242-
raise pytest.fail(msg)
1242+
return pytest.fail(msg)
12431243

12441244
def _call_ntimes(self, fun, times):
12451245
"""Get 2 distinct memory samples, before and after having
@@ -1273,14 +1273,14 @@ def _check_mem(self, fun, times, retries, tolerance):
12731273
if success:
12741274
if idx > 1:
12751275
self._log(msg)
1276-
return
1276+
return None
12771277
else:
12781278
if idx == 1:
12791279
print() # noqa: T201
12801280
self._log(msg)
12811281
times += increase
12821282
prev_mem = mem
1283-
raise pytest.fail(". ".join(messages))
1283+
return pytest.fail(". ".join(messages))
12841284

12851285
# ---
12861286

@@ -1320,7 +1320,7 @@ def call():
13201320
except exc:
13211321
pass
13221322
else:
1323-
raise pytest.fail(f"{fun} did not raise {exc}")
1323+
return pytest.fail(f"{fun} did not raise {exc}")
13241324

13251325
self.execute(call, **kwargs)
13261326

@@ -1587,7 +1587,7 @@ def wrapper(*args, **kwargs):
15871587
if only_if is not None:
15881588
if not only_if:
15891589
raise
1590-
raise pytest.skip("raises AccessDenied")
1590+
return pytest.skip("raises AccessDenied")
15911591

15921592
return wrapper
15931593

@@ -1610,7 +1610,7 @@ def wrapper(*args, **kwargs):
16101610
f"{fun.__name__!r} was skipped because it raised"
16111611
" NotImplementedError"
16121612
)
1613-
raise pytest.skip(msg)
1613+
return pytest.skip(msg)
16141614

16151615
return wrapper
16161616

psutil/tests/test_bsd.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ def df(path):
116116
assert usage.total == total
117117
# 10 MB tolerance
118118
if abs(usage.free - free) > 10 * 1024 * 1024:
119-
raise pytest.fail(f"psutil={usage.free}, df={free}")
119+
return pytest.fail(f"psutil={usage.free}, df={free}")
120120
if abs(usage.used - used) > 10 * 1024 * 1024:
121-
raise pytest.fail(f"psutil={usage.used}, df={used}")
121+
return pytest.fail(f"psutil={usage.used}, df={used}")
122122

123123
@pytest.mark.skipif(
124124
not shutil.which("sysctl"), reason="sysctl cmd not available"
@@ -268,7 +268,7 @@ def test_cpu_frequency_against_sysctl(self):
268268
try:
269269
sysctl_result = int(sysctl(sensor))
270270
except RuntimeError:
271-
raise pytest.skip("frequencies not supported by kernel")
271+
return pytest.skip("frequencies not supported by kernel")
272272
assert psutil.cpu_freq().current == sysctl_result
273273

274274
sensor = "dev.cpu.0.freq_levels"
@@ -466,7 +466,7 @@ def test_sensors_temperatures_against_sysctl(self):
466466
try:
467467
sysctl_result = int(float(sysctl(sensor)[:-1]))
468468
except RuntimeError:
469-
raise pytest.skip("temperatures not supported by kernel")
469+
return pytest.skip("temperatures not supported by kernel")
470470
assert (
471471
abs(
472472
psutil.sensors_temperatures()["coretemp"][cpu].current

psutil/tests/test_contracts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def test_cpu_count(self):
226226
@pytest.mark.skipif(not HAS_CPU_FREQ, reason="not supported")
227227
def test_cpu_freq(self):
228228
if psutil.cpu_freq() is None:
229-
raise pytest.skip("cpu_freq() returns None")
229+
return pytest.skip("cpu_freq() returns None")
230230
self.assert_ntuple_of_nums(psutil.cpu_freq(), type_=(float, int))
231231

232232
def test_disk_io_counters(self):

psutil/tests/test_linux.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def vmstat(stat):
178178
def get_free_version_info():
179179
out = sh(["free", "-V"]).strip()
180180
if 'UNKNOWN' in out:
181-
raise pytest.skip("can't determine free version")
181+
return pytest.skip("can't determine free version")
182182
return tuple(map(int, re.findall(r'\d+', out.split()[-1])))
183183

184184

@@ -237,12 +237,12 @@ def test_used(self):
237237
# This got changed in:
238238
# https://gitlab.com/procps-ng/procps/commit/
239239
# 05d751c4f076a2f0118b914c5e51cfbb4762ad8e
240-
# Newer versions of procps are using yet another way to compute used
241-
# memory.
240+
# Newer versions of procps (>=4.0.1) are using yet another way to
241+
# compute used memory.
242242
# https://gitlab.com/procps-ng/procps/commit/
243243
# 2184e90d2e7cdb582f9a5b706b47015e56707e4d
244-
if get_free_version_info() < (4, 0, 0):
245-
raise pytest.skip("free version too old")
244+
if get_free_version_info() < (4, 0, 1):
245+
return pytest.skip("free version too old")
246246
cli_value = free_physmem().used
247247
psutil_value = psutil.virtual_memory().used
248248
assert abs(cli_value - psutil_value) < TOLERANCE_SYS_MEM
@@ -258,7 +258,7 @@ def test_shared(self):
258258
free = free_physmem()
259259
free_value = free.shared
260260
if free_value == 0:
261-
raise pytest.skip("free does not support 'shared' column")
261+
return pytest.skip("free does not support 'shared' column")
262262
psutil_value = psutil.virtual_memory().shared
263263
assert (
264264
abs(free_value - psutil_value) < TOLERANCE_SYS_MEM
@@ -271,7 +271,7 @@ def test_available(self):
271271
out = sh(["free", "-b"])
272272
lines = out.split('\n')
273273
if 'available' not in lines[0]:
274-
raise pytest.skip("free does not support 'available' column")
274+
return pytest.skip("free does not support 'available' column")
275275
free_value = int(lines[1].split()[-1])
276276
psutil_value = psutil.virtual_memory().available
277277
assert abs(free_value - psutil_value) < TOLERANCE_SYS_MEM
@@ -290,12 +290,12 @@ def test_used(self):
290290
# This got changed in:
291291
# https://gitlab.com/procps-ng/procps/commit/
292292
# 05d751c4f076a2f0118b914c5e51cfbb4762ad8e
293-
# Newer versions of procps are using yet another way to compute used
294-
# memory.
293+
# Newer versions of procps (>=4.0.1) are using yet another way to
294+
# compute used memory.
295295
# https://gitlab.com/procps-ng/procps/commit/
296296
# 2184e90d2e7cdb582f9a5b706b47015e56707e4d
297-
if get_free_version_info() < (4, 0, 0):
298-
raise pytest.skip("free version too old")
297+
if get_free_version_info() < (4, 0, 1):
298+
return pytest.skip("free version too old")
299299
vmstat_value = vmstat('used memory') * 1024
300300
psutil_value = psutil.virtual_memory().used
301301
assert abs(vmstat_value - psutil_value) < TOLERANCE_SYS_MEM
@@ -595,7 +595,7 @@ def test_meminfo_against_sysinfo(self):
595595
# matches sysinfo() syscall, see:
596596
# https://github.com/giampaolo/psutil/issues/1015
597597
if not self.meminfo_has_swap_info():
598-
raise pytest.skip("/proc/meminfo has no swap metrics")
598+
return pytest.skip("/proc/meminfo has no swap metrics")
599599
with mock.patch('psutil._pslinux.cext.linux_sysinfo') as m:
600600
swap = psutil.swap_memory()
601601
assert not m.called
@@ -1028,7 +1028,7 @@ def test_flags(self):
10281028
assert ifconfig_flags == psutil_flags
10291029

10301030
if not matches_found:
1031-
raise pytest.fail("no matches were found")
1031+
return pytest.fail("no matches were found")
10321032

10331033

10341034
@pytest.mark.skipif(not LINUX, reason="LINUX only")

psutil/tests/test_misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def test__all__(self):
216216
fun.__doc__ is not None
217217
and 'deprecated' not in fun.__doc__.lower()
218218
):
219-
raise pytest.fail(
219+
return pytest.fail(
220220
f"{name!r} not in psutil.__all__"
221221
)
222222

@@ -847,7 +847,7 @@ def test_cache_clear(self):
847847
@pytest.mark.skipif(not HAS_NET_IO_COUNTERS, reason="not supported")
848848
def test_cache_clear_public_apis(self):
849849
if not psutil.disk_io_counters() or not psutil.net_io_counters():
850-
raise pytest.skip("no disks or NICs available")
850+
return pytest.skip("no disks or NICs available")
851851
psutil.disk_io_counters()
852852
psutil.net_io_counters()
853853
caches = wrap_numbers.cache_info()

psutil/tests/test_posix.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def df(device):
133133
out = sh(f"df -k {device}").strip()
134134
except RuntimeError as err:
135135
if "device busy" in str(err).lower():
136-
raise pytest.skip("df returned EBUSY")
136+
return pytest.skip("df returned EBUSY")
137137
raise
138138
line = out.split('\n')[1]
139139
fields = line.split()
@@ -337,7 +337,7 @@ def test_pids(self):
337337
difference = [x for x in pids_psutil if x not in pids_ps] + [
338338
x for x in pids_ps if x not in pids_psutil
339339
]
340-
raise pytest.fail("difference: " + str(difference))
340+
return pytest.fail("difference: " + str(difference))
341341

342342
# for some reason ifconfig -a does not report all interfaces
343343
# returned by psutil
@@ -351,7 +351,7 @@ def test_nic_names(self):
351351
if line.startswith(nic):
352352
break
353353
else:
354-
raise pytest.fail(
354+
return pytest.fail(
355355
f"couldn't find {nic} nic in 'ifconfig -a'"
356356
f" output\n{output}"
357357
)
@@ -360,7 +360,7 @@ def test_nic_names(self):
360360
def test_users(self):
361361
out = sh("who -u")
362362
if not out.strip():
363-
raise pytest.skip("no users on this system")
363+
return pytest.skip("no users on this system")
364364

365365
susers = []
366366
for line in out.splitlines():
@@ -391,7 +391,7 @@ def test_users(self):
391391
def test_users_started(self):
392392
out = sh("who -u")
393393
if not out.strip():
394-
raise pytest.skip("no users on this system")
394+
return pytest.skip("no users on this system")
395395
tstamp = None
396396
# '2023-04-11 09:31' (Linux)
397397
started = re.findall(r"\d\d\d\d-\d\d-\d\d \d\d:\d\d", out)
@@ -415,7 +415,7 @@ def test_users_started(self):
415415
started = [x.capitalize() for x in started]
416416

417417
if not tstamp:
418-
raise pytest.skip(f"cannot interpret tstamp in who output\n{out}")
418+
return pytest.skip(f"cannot interpret tstamp in who output\n{out}")
419419

420420
with self.subTest(psutil=psutil.users(), who=out):
421421
for idx, u in enumerate(psutil.users()):

0 commit comments

Comments
 (0)