From 8e4099d9f063ceb4ee3da5845562c5b934f83544 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Fri, 21 Oct 2022 11:34:55 -0700 Subject: [PATCH] add windows test for free physical mem #2074 --- psutil/tests/test_windows.py | 39 ++++++++++++++++++++++++++++++++++++ scripts/internal/winmake.py | 4 ++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/psutil/tests/test_windows.py b/psutil/tests/test_windows.py index 3bf458079..cb2642d19 100755 --- a/psutil/tests/test_windows.py +++ b/psutil/tests/test_windows.py @@ -23,6 +23,7 @@ import psutil from psutil import WINDOWS from psutil._compat import FileNotFoundError +from psutil._compat import which from psutil._compat import super from psutil.tests import APPVEYOR from psutil.tests import GITHUB_ACTIONS @@ -31,6 +32,7 @@ from psutil.tests import PY3 from psutil.tests import PYPY from psutil.tests import TOLERANCE_DISK_USAGE +from psutil.tests import TOLERANCE_SYS_MEM from psutil.tests import PsutilTestCase from psutil.tests import mock from psutil.tests import retry_on_failure @@ -62,6 +64,37 @@ class WindowsTestCase(PsutilTestCase): pass +def powershell(cmd): + """Currently not used, but avalable just in case. Usage: + + >>> powershell( + "Get-CIMInstance Win32_PageFileUsage | Select AllocatedBaseSize") + """ + if not which("powershell.exe"): + raise unittest.SkipTest("powershell.exe not available") + cmdline = \ + 'powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive ' + \ + '-NoProfile -WindowStyle Hidden -Command "%s"' % cmd + return sh(cmdline) + + +def wmic(path, what, converter=int): + """Currently not used, but avalable just in case. Usage: + + >>> wmic("Win32_OperatingSystem", "FreePhysicalMemory") + 2134124534 + """ + out = sh("wmic path %s get %s" % (path, what)).strip() + data = "".join(out.splitlines()[1:]).strip() # get rid of the header + if converter is not None: + if "," in what: + return tuple([converter(x) for x in data.split()]) + else: + return converter(data) + else: + return data + + # =================================================================== # System APIs # =================================================================== @@ -123,6 +156,12 @@ def test_total_phymem(self): self.assertEqual(int(w.TotalPhysicalMemory), psutil.virtual_memory().total) + def test_free_phymem(self): + w = wmi.WMI().Win32_PerfRawData_PerfOS_Memory()[0] + self.assertAlmostEqual( + int(w.AvailableBytes), psutil.virtual_memory().free, + delta=TOLERANCE_SYS_MEM) + # @unittest.skipIf(wmi is None, "wmi module is not installed") # def test__UPTIME(self): # # _UPTIME constant is not public but it is used internally diff --git a/scripts/internal/winmake.py b/scripts/internal/winmake.py index bb8800519..466887c2c 100755 --- a/scripts/internal/winmake.py +++ b/scripts/internal/winmake.py @@ -383,7 +383,7 @@ def setup_dev_env(): sh("%s -m pip install -U %s" % (PYTHON, " ".join(DEPS))) -def check_flake8(): +def flake8(): """Run flake8 against all py files""" py_files = subprocess.check_output("git ls-files") if PY3: @@ -565,7 +565,7 @@ def main(): sp.add_parser('install', help="build + install in develop/edit mode") sp.add_parser('install-git-hooks', help="install GIT pre-commit hook") sp.add_parser('install-pip', help="install pip") - sp.add_parser('check_flake8', help="run flake8 against all py files") + sp.add_parser('flake8', help="run flake8 against all py files") sp.add_parser('print-access-denied', help="print AD exceptions") sp.add_parser('print-api-speed', help="benchmark all API calls") sp.add_parser('setup-dev-env', help="install deps")