Skip to content

Commit

Permalink
add windows test for free physical mem giampaolo#2074
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo authored and dbwiddis committed Oct 21, 2022
1 parent 39d8355 commit 4b758df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
39 changes: 39 additions & 0 deletions psutil/tests/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
# ===================================================================
Expand Down Expand Up @@ -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)

def test_total_swapmem(self):
if (psutil.swap_memory().total > 0):
w = wmi.WMI().Win32_PerfRawData_PerfOS_Memory()[0]
Expand Down
4 changes: 2 additions & 2 deletions scripts/internal/winmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 4b758df

Please sign in to comment.