diff options
author | Matthieu Darbois <mayeut@users.noreply.github.com> | 2022-10-05 23:15:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-05 23:15:37 +0200 |
commit | 7271ec7e0fc4e346a339cdc2e1640a52e92845f4 (patch) | |
tree | ee277bc8aea70f7ffaf48f3f291cb8b3582c99b3 /psutil/tests | |
parent | 052c1e2ddbd712c201786b7cc9983a4284d3a6c8 (diff) | |
download | psutil-7271ec7e0fc4e346a339cdc2e1640a52e92845f4.tar.gz |
fix: disk usage report on macOS 12+ (#2152)
Diffstat (limited to 'psutil/tests')
-rw-r--r-- | psutil/tests/__init__.py | 34 | ||||
-rwxr-xr-x | psutil/tests/test_process.py | 5 | ||||
-rwxr-xr-x | psutil/tests/test_system.py | 7 |
3 files changed, 43 insertions, 3 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 6ddafc97..a7da8d23 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -18,6 +18,7 @@ import functools import gc import inspect import os +import platform import random import re import select @@ -47,6 +48,7 @@ from psutil import POSIX from psutil import SUNOS from psutil import WINDOWS from psutil._common import bytes2human +from psutil._common import memoize from psutil._common import print_color from psutil._common import supports_ipv6 from psutil._compat import PY3 @@ -84,7 +86,8 @@ __all__ = [ "HAS_CPU_AFFINITY", "HAS_CPU_FREQ", "HAS_ENVIRON", "HAS_PROC_IO_COUNTERS", "HAS_IONICE", "HAS_MEMORY_MAPS", "HAS_PROC_CPU_NUM", "HAS_RLIMIT", "HAS_SENSORS_BATTERY", "HAS_BATTERY", "HAS_SENSORS_FANS", - "HAS_SENSORS_TEMPERATURES", "HAS_MEMORY_FULL_INFO", + "HAS_SENSORS_TEMPERATURES", "HAS_MEMORY_FULL_INFO", "MACOS_11PLUS", + "MACOS_12PLUS", # subprocesses 'pyrun', 'terminate', 'reap_children', 'spawn_testproc', 'spawn_zombie', 'spawn_children_pair', @@ -129,6 +132,35 @@ CI_TESTING = APPVEYOR or GITHUB_ACTIONS IS_64BIT = sys.maxsize > 2 ** 32 +@memoize +def macos_version(): + version_str = platform.mac_ver()[0] + version = tuple(map(int, version_str.split(".")[:2])) + if version == (10, 16): + # When built against an older macOS SDK, Python will report + # macOS 10.16 instead of the real version. + version_str = subprocess.check_output( + [ + sys.executable, + "-sS", + "-c", + "import platform; print(platform.mac_ver()[0])", + ], + env={"SYSTEM_VERSION_COMPAT": "0"}, + universal_newlines=True, + ) + version = tuple(map(int, version_str.split(".")[:2])) + return version + + +if MACOS: + MACOS_11PLUS = macos_version() > (10, 15) + MACOS_12PLUS = macos_version() >= (12, 0) +else: + MACOS_11PLUS = False + MACOS_12PLUS = False + + # --- configurable defaults # how many times retry_on_failure() decorator will retry diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index b2c3b2c7..26869e98 100755 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -49,6 +49,7 @@ from psutil.tests import HAS_PROC_CPU_NUM from psutil.tests import HAS_PROC_IO_COUNTERS from psutil.tests import HAS_RLIMIT from psutil.tests import HAS_THREADS +from psutil.tests import MACOS_11PLUS from psutil.tests import PYPY from psutil.tests import PYTHON_EXE from psutil.tests import PsutilTestCase @@ -1426,6 +1427,10 @@ class TestProcess(PsutilTestCase): @unittest.skipIf(not HAS_ENVIRON, "not supported") @unittest.skipIf(not POSIX, "POSIX only") + @unittest.skipIf( + MACOS_11PLUS, + "macOS 11+ can't get another process environment, issue #2084" + ) def test_weird_environ(self): # environment variables can contain values without an equals sign code = textwrap.dedent(""" diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py index d6b7a21a..753249bc 100755 --- a/psutil/tests/test_system.py +++ b/psutil/tests/test_system.py @@ -44,6 +44,7 @@ from psutil.tests import HAS_SENSORS_BATTERY from psutil.tests import HAS_SENSORS_FANS from psutil.tests import HAS_SENSORS_TEMPERATURES from psutil.tests import IS_64BIT +from psutil.tests import MACOS_12PLUS from psutil.tests import PYPY from psutil.tests import UNICODE_SUFFIX from psutil.tests import PsutilTestCase @@ -561,8 +562,10 @@ class TestDiskAPIs(PsutilTestCase): self.assertEqual(usage.total, shutil_usage.total) self.assertAlmostEqual(usage.free, shutil_usage.free, delta=tolerance) - self.assertAlmostEqual(usage.used, shutil_usage.used, - delta=tolerance) + if not MACOS_12PLUS: + # see https://github.com/giampaolo/psutil/issues/2147 + self.assertAlmostEqual(usage.used, shutil_usage.used, + delta=tolerance) # if path does not exist OSError ENOENT is expected across # all platforms |