py2docfx 0.1.20.dev2245319__py3-none-any.whl → 0.1.20.dev2245492__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. py2docfx/convert_prepare/get_source.py +1 -1
  2. py2docfx/convert_prepare/tests/test_get_source.py +3 -1
  3. py2docfx/docfx_yaml/build_finished.py +1 -1
  4. py2docfx/venv/venv1/Lib/site-packages/psutil/__init__.py +39 -19
  5. py2docfx/venv/venv1/Lib/site-packages/psutil/_common.py +3 -5
  6. py2docfx/venv/venv1/Lib/site-packages/psutil/_psaix.py +1 -2
  7. py2docfx/venv/venv1/Lib/site-packages/psutil/_psbsd.py +53 -78
  8. py2docfx/venv/venv1/Lib/site-packages/psutil/_pslinux.py +55 -38
  9. py2docfx/venv/venv1/Lib/site-packages/psutil/_psosx.py +40 -12
  10. py2docfx/venv/venv1/Lib/site-packages/psutil/_psposix.py +0 -1
  11. py2docfx/venv/venv1/Lib/site-packages/psutil/_pssunos.py +1 -2
  12. py2docfx/venv/venv1/Lib/site-packages/psutil/_pswindows.py +33 -13
  13. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/__init__.py +185 -122
  14. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/__main__.py +2 -3
  15. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_bsd.py +5 -10
  16. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_connections.py +3 -4
  17. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_contracts.py +41 -45
  18. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_linux.py +35 -38
  19. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_memleaks.py +4 -8
  20. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_misc.py +6 -12
  21. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_osx.py +17 -8
  22. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_posix.py +29 -17
  23. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_process.py +74 -75
  24. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_process_all.py +11 -13
  25. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_scripts.py +2 -3
  26. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_sudo.py +117 -0
  27. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_system.py +21 -31
  28. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_testutils.py +23 -23
  29. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_unicode.py +15 -8
  30. py2docfx/venv/venv1/Lib/site-packages/psutil/tests/test_windows.py +65 -33
  31. {py2docfx-0.1.20.dev2245319.dist-info → py2docfx-0.1.20.dev2245492.dist-info}/METADATA +1 -1
  32. {py2docfx-0.1.20.dev2245319.dist-info → py2docfx-0.1.20.dev2245492.dist-info}/RECORD +34 -33
  33. {py2docfx-0.1.20.dev2245319.dist-info → py2docfx-0.1.20.dev2245492.dist-info}/WHEEL +0 -0
  34. {py2docfx-0.1.20.dev2245319.dist-info → py2docfx-0.1.20.dev2245492.dist-info}/top_level.txt +0 -0
@@ -64,7 +64,6 @@ from psutil.tests import skip_on_access_denied
64
64
  from psutil.tests import skip_on_not_implemented
65
65
  from psutil.tests import wait_for_pid
66
66
 
67
-
68
67
  # ===================================================================
69
68
  # --- psutil.Process class tests
70
69
  # ===================================================================
@@ -73,16 +72,6 @@ from psutil.tests import wait_for_pid
73
72
  class TestProcess(PsutilTestCase):
74
73
  """Tests for psutil.Process class."""
75
74
 
76
- def spawn_psproc(self, *args, **kwargs):
77
- sproc = self.spawn_testproc(*args, **kwargs)
78
- try:
79
- return psutil.Process(sproc.pid)
80
- except psutil.NoSuchProcess:
81
- self.assertPidGone(sproc.pid)
82
- raise
83
-
84
- # ---
85
-
86
75
  def test_pid(self):
87
76
  p = psutil.Process()
88
77
  assert p.pid == os.getpid()
@@ -97,7 +86,7 @@ class TestProcess(PsutilTestCase):
97
86
  assert code == signal.SIGTERM
98
87
  else:
99
88
  assert code == -signal.SIGKILL
100
- self.assertProcessGone(p)
89
+ self.assert_proc_gone(p)
101
90
 
102
91
  def test_terminate(self):
103
92
  p = self.spawn_psproc()
@@ -107,7 +96,7 @@ class TestProcess(PsutilTestCase):
107
96
  assert code == signal.SIGTERM
108
97
  else:
109
98
  assert code == -signal.SIGTERM
110
- self.assertProcessGone(p)
99
+ self.assert_proc_gone(p)
111
100
 
112
101
  def test_send_signal(self):
113
102
  sig = signal.SIGKILL if POSIX else signal.SIGTERM
@@ -118,7 +107,7 @@ class TestProcess(PsutilTestCase):
118
107
  assert code == sig
119
108
  else:
120
109
  assert code == -sig
121
- self.assertProcessGone(p)
110
+ self.assert_proc_gone(p)
122
111
 
123
112
  @pytest.mark.skipif(not POSIX, reason="not POSIX")
124
113
  def test_send_signal_mocked(self):
@@ -140,25 +129,25 @@ class TestProcess(PsutilTestCase):
140
129
  p = self.spawn_psproc(cmd)
141
130
  code = p.wait()
142
131
  assert code == 0
143
- self.assertProcessGone(p)
132
+ self.assert_proc_gone(p)
144
133
  # exit(1), implicit in case of error
145
134
  cmd = [PYTHON_EXE, "-c", "1 / 0"]
146
135
  p = self.spawn_psproc(cmd, stderr=subprocess.PIPE)
147
136
  code = p.wait()
148
137
  assert code == 1
149
- self.assertProcessGone(p)
138
+ self.assert_proc_gone(p)
150
139
  # via sys.exit()
151
140
  cmd = [PYTHON_EXE, "-c", "import sys; sys.exit(5);"]
152
141
  p = self.spawn_psproc(cmd)
153
142
  code = p.wait()
154
143
  assert code == 5
155
- self.assertProcessGone(p)
144
+ self.assert_proc_gone(p)
156
145
  # via os._exit()
157
146
  cmd = [PYTHON_EXE, "-c", "import os; os._exit(5);"]
158
147
  p = self.spawn_psproc(cmd)
159
148
  code = p.wait()
160
149
  assert code == 5
161
- self.assertProcessGone(p)
150
+ self.assert_proc_gone(p)
162
151
 
163
152
  @pytest.mark.skipif(NETBSD, reason="fails on NETBSD")
164
153
  def test_wait_stopped(self):
@@ -232,12 +221,12 @@ class TestProcess(PsutilTestCase):
232
221
  except psutil.TimeoutExpired:
233
222
  pass
234
223
  else:
235
- raise self.fail('timeout')
224
+ raise pytest.fail('timeout')
236
225
  if POSIX:
237
226
  assert code == -signal.SIGKILL
238
227
  else:
239
228
  assert code == signal.SIGTERM
240
- self.assertProcessGone(p)
229
+ self.assert_proc_gone(p)
241
230
 
242
231
  def test_cpu_percent(self):
243
232
  p = psutil.Process()
@@ -278,8 +267,8 @@ class TestProcess(PsutilTestCase):
278
267
  waste_cpu()
279
268
  a = psutil.Process().cpu_times()
280
269
  b = os.times()
281
- self.assertAlmostEqual(a.user, b.user, delta=0.1)
282
- self.assertAlmostEqual(a.system, b.system, delta=0.1)
270
+ assert abs(a.user - b.user) < 0.1
271
+ assert abs(a.system - b.system) < 0.1
283
272
 
284
273
  @pytest.mark.skipif(not HAS_PROC_CPU_NUM, reason="not supported")
285
274
  def test_cpu_num(self):
@@ -293,18 +282,8 @@ class TestProcess(PsutilTestCase):
293
282
  def test_create_time(self):
294
283
  p = self.spawn_psproc()
295
284
  now = time.time()
296
- create_time = p.create_time()
297
-
298
- # Use time.time() as base value to compare our result using a
299
- # tolerance of +/- 1 second.
300
- # It will fail if the difference between the values is > 2s.
301
- difference = abs(create_time - now)
302
- if difference > 2:
303
- raise self.fail(
304
- f"expected: {now}, found: {create_time}, difference:"
305
- f" {difference}"
306
- )
307
-
285
+ # Fail if the difference with current time is > 2s.
286
+ assert abs(p.create_time() - now) < 2
308
287
  # make sure returned value can be pretty printed with strftime
309
288
  time.strftime("%Y %m %d %H:%M:%S", time.localtime(p.create_time()))
310
289
 
@@ -518,6 +497,7 @@ class TestProcess(PsutilTestCase):
518
497
  assert hard == psutil.RLIM_INFINITY
519
498
  p.rlimit(psutil.RLIMIT_FSIZE, (soft, hard))
520
499
 
500
+ @pytest.mark.xdist_group(name="serial")
521
501
  def test_num_threads(self):
522
502
  # on certain platforms such as Linux we might test for exact
523
503
  # thread number, since we always have with 1 thread per process,
@@ -759,7 +739,6 @@ class TestProcess(PsutilTestCase):
759
739
  return
760
740
  assert ' '.join(p.cmdline()) == ' '.join(cmdline)
761
741
 
762
- @pytest.mark.skipif(PYPY, reason="broken on PYPY")
763
742
  def test_long_cmdline(self):
764
743
  cmdline = [PYTHON_EXE]
765
744
  cmdline.extend(["-v"] * 50)
@@ -767,15 +746,20 @@ class TestProcess(PsutilTestCase):
767
746
  ["-c", "import time; [time.sleep(0.1) for x in range(100)]"]
768
747
  )
769
748
  p = self.spawn_psproc(cmdline)
749
+
750
+ # XXX - flaky test: exclude the python exe which, for some
751
+ # reason, and only sometimes, on OSX appears different.
752
+ cmdline = cmdline[1:]
753
+
770
754
  if OPENBSD:
771
755
  # XXX: for some reason the test process may turn into a
772
756
  # zombie (don't know why).
773
757
  try:
774
- assert p.cmdline() == cmdline
758
+ assert p.cmdline()[1:] == cmdline
775
759
  except psutil.ZombieProcess:
776
760
  raise pytest.skip("OPENBSD: process turned into zombie")
777
761
  else:
778
- ret = p.cmdline()
762
+ ret = p.cmdline()[1:]
779
763
  if NETBSD and ret == []:
780
764
  # https://github.com/giampaolo/psutil/issues/2250
781
765
  raise pytest.skip("OPENBSD: returned EBUSY")
@@ -787,7 +771,7 @@ class TestProcess(PsutilTestCase):
787
771
  pyexe = os.path.basename(os.path.realpath(sys.executable)).lower()
788
772
  assert pyexe.startswith(name), (pyexe, name)
789
773
 
790
- @pytest.mark.skipif(PYPY, reason="unreliable on PYPY")
774
+ @retry_on_failure()
791
775
  def test_long_name(self):
792
776
  pyexe = create_py_exe(self.get_testfn(suffix=string.digits * 2))
793
777
  cmdline = [
@@ -814,25 +798,6 @@ class TestProcess(PsutilTestCase):
814
798
  else:
815
799
  assert p.name() == os.path.basename(pyexe)
816
800
 
817
- # XXX: fails too often
818
- # @pytest.mark.skipif(SUNOS, reason="broken on SUNOS")
819
- # @pytest.mark.skipif(AIX, reason="broken on AIX")
820
- # @pytest.mark.skipif(PYPY, reason="broken on PYPY")
821
- # def test_prog_w_funky_name(self):
822
- # # Test that name(), exe() and cmdline() correctly handle programs
823
- # # with funky chars such as spaces and ")", see:
824
- # # https://github.com/giampaolo/psutil/issues/628
825
- # pyexe = create_py_exe(self.get_testfn(suffix='foo bar )'))
826
- # cmdline = [
827
- # pyexe,
828
- # "-c",
829
- # "import time; [time.sleep(0.1) for x in range(100)]",
830
- # ]
831
- # p = self.spawn_psproc(cmdline)
832
- # assert p.cmdline() == cmdline
833
- # assert p.name() == os.path.basename(pyexe)
834
- # assert os.path.normcase(p.exe()) == os.path.normcase(pyexe)
835
-
836
801
  @pytest.mark.skipif(not POSIX, reason="POSIX only")
837
802
  def test_uids(self):
838
803
  p = psutil.Process()
@@ -1090,7 +1055,7 @@ class TestProcess(PsutilTestCase):
1090
1055
  ):
1091
1056
  break
1092
1057
  else:
1093
- raise self.fail(f"no file found; files={p.open_files()!r}")
1058
+ raise pytest.fail(f"no file found; files={p.open_files()!r}")
1094
1059
  assert normcase(file.path) == normcase(fileobj.name)
1095
1060
  if WINDOWS:
1096
1061
  assert file.fd == -1
@@ -1104,18 +1069,15 @@ class TestProcess(PsutilTestCase):
1104
1069
  assert fileobj.name not in p.open_files()
1105
1070
 
1106
1071
  @pytest.mark.skipif(not POSIX, reason="POSIX only")
1072
+ @pytest.mark.xdist_group(name="serial")
1107
1073
  def test_num_fds(self):
1108
1074
  p = psutil.Process()
1109
1075
  testfn = self.get_testfn()
1110
1076
  start = p.num_fds()
1111
- file = open(testfn, 'w') # noqa: SIM115
1112
- self.addCleanup(file.close)
1113
- assert p.num_fds() == start + 1
1114
- sock = socket.socket()
1115
- self.addCleanup(sock.close)
1116
- assert p.num_fds() == start + 2
1117
- file.close()
1118
- sock.close()
1077
+ with open(testfn, 'w'):
1078
+ assert p.num_fds() == start + 1
1079
+ with socket.socket():
1080
+ assert p.num_fds() == start + 2
1119
1081
  assert p.num_fds() == start
1120
1082
 
1121
1083
  @skip_on_not_implemented(only_if=LINUX)
@@ -1130,7 +1092,7 @@ class TestProcess(PsutilTestCase):
1130
1092
  after = sum(p.num_ctx_switches())
1131
1093
  if after > before:
1132
1094
  return
1133
- raise self.fail("num ctx switches still the same after 2 iterations")
1095
+ raise pytest.fail("num ctx switches still the same after 2 iterations")
1134
1096
 
1135
1097
  def test_ppid(self):
1136
1098
  p = psutil.Process()
@@ -1146,6 +1108,18 @@ class TestProcess(PsutilTestCase):
1146
1108
  lowest_pid = psutil.pids()[0]
1147
1109
  assert psutil.Process(lowest_pid).parent() is None
1148
1110
 
1111
+ def test_parent_mocked_ctime(self):
1112
+ # Make sure we get a fresh copy of the ctime before processing
1113
+ # parent().We make the assumption that the parent pid MUST have
1114
+ # a creation time < than the child. If system clock is updated
1115
+ # this assumption was broken.
1116
+ # https://github.com/giampaolo/psutil/issues/2542
1117
+ p = self.spawn_psproc()
1118
+ p.create_time() # trigger cache
1119
+ assert p._create_time
1120
+ p._create_time = 1
1121
+ assert p.parent().pid == os.getpid()
1122
+
1149
1123
  def test_parent_multi(self):
1150
1124
  parent = psutil.Process()
1151
1125
  child, grandchild = self.spawn_children_pair()
@@ -1176,6 +1150,30 @@ class TestProcess(PsutilTestCase):
1176
1150
  assert children[0].pid == child.pid
1177
1151
  assert children[0].ppid() == parent.pid
1178
1152
 
1153
+ def test_children_mocked_ctime(self):
1154
+ # Make sure we get a fresh copy of the ctime before processing
1155
+ # children(). We make the assumption that process children MUST
1156
+ # have a creation time > than the parent. If system clock is
1157
+ # updated this assumption was broken.
1158
+ # https://github.com/giampaolo/psutil/issues/2542
1159
+ parent = psutil.Process()
1160
+ parent.create_time() # trigger cache
1161
+ assert parent._create_time
1162
+ parent._create_time += 100000
1163
+
1164
+ assert not parent.children()
1165
+ assert not parent.children(recursive=True)
1166
+ # On Windows we set the flag to 0 in order to cancel out the
1167
+ # CREATE_NO_WINDOW flag (enabled by default) which creates
1168
+ # an extra "conhost.exe" child.
1169
+ child = self.spawn_psproc(creationflags=0)
1170
+ children1 = parent.children()
1171
+ children2 = parent.children(recursive=True)
1172
+ for children in (children1, children2):
1173
+ assert len(children) == 1
1174
+ assert children[0].pid == child.pid
1175
+ assert children[0].ppid() == parent.pid
1176
+
1179
1177
  def test_children_recursive(self):
1180
1178
  # Test children() against two sub processes, p1 and p2, where
1181
1179
  # p1 (our child) spawned p2 (our grandchild).
@@ -1361,7 +1359,7 @@ class TestProcess(PsutilTestCase):
1361
1359
  # NtQuerySystemInformation succeeds even if process is gone.
1362
1360
  if WINDOWS and fun_name in {'exe', 'name'}:
1363
1361
  return
1364
- raise self.fail(
1362
+ raise pytest.fail(
1365
1363
  f"{fun!r} didn't raise NSP and returned {ret!r} instead"
1366
1364
  )
1367
1365
 
@@ -1370,7 +1368,7 @@ class TestProcess(PsutilTestCase):
1370
1368
  p.wait()
1371
1369
  if WINDOWS: # XXX
1372
1370
  call_until(lambda: p.pid not in psutil.pids())
1373
- self.assertProcessGone(p)
1371
+ self.assert_proc_gone(p)
1374
1372
 
1375
1373
  ns = process_namespace(p)
1376
1374
  for fun, name in ns.iter(ns.all):
@@ -1379,7 +1377,7 @@ class TestProcess(PsutilTestCase):
1379
1377
  @pytest.mark.skipif(not POSIX, reason="POSIX only")
1380
1378
  def test_zombie_process(self):
1381
1379
  _parent, zombie = self.spawn_zombie()
1382
- self.assertProcessZombie(zombie)
1380
+ self.assert_proc_zombie(zombie)
1383
1381
 
1384
1382
  @pytest.mark.skipif(not POSIX, reason="POSIX only")
1385
1383
  def test_zombie_process_is_running_w_exc(self):
@@ -1406,7 +1404,7 @@ class TestProcess(PsutilTestCase):
1406
1404
 
1407
1405
  def test_reused_pid(self):
1408
1406
  # Emulate a case where PID has been reused by another process.
1409
- subp = self.spawn_testproc()
1407
+ subp = self.spawn_subproc()
1410
1408
  p = psutil.Process(subp.pid)
1411
1409
  p._ident = (p.pid, p.create_time() + 100)
1412
1410
 
@@ -1505,9 +1503,9 @@ class TestProcess(PsutilTestCase):
1505
1503
  for name in exclude:
1506
1504
  d.pop(name, None)
1507
1505
  return {
1508
- k.replace("\r", "").replace("\n", ""): v.replace(
1509
- "\r", ""
1510
- ).replace("\n", "")
1506
+ k.replace("\r", "").replace("\n", ""): (
1507
+ v.replace("\r", "").replace("\n", "")
1508
+ )
1511
1509
  for k, v in d.items()
1512
1510
  }
1513
1511
 
@@ -1545,7 +1543,7 @@ class TestProcess(PsutilTestCase):
1545
1543
  }
1546
1544
  """)
1547
1545
  cexe = create_c_exe(self.get_testfn(), c_code=code)
1548
- sproc = self.spawn_testproc(
1546
+ sproc = self.spawn_subproc(
1549
1547
  [cexe], stdin=subprocess.PIPE, stderr=subprocess.PIPE
1550
1548
  )
1551
1549
  p = psutil.Process(sproc.pid)
@@ -1579,6 +1577,7 @@ class TestPopen(PsutilTestCase):
1579
1577
  def tearDownClass(cls):
1580
1578
  reap_children()
1581
1579
 
1580
+ @pytest.mark.skipif(MACOS and GITHUB_ACTIONS, reason="hangs on OSX + CI")
1582
1581
  def test_misc(self):
1583
1582
  # XXX this test causes a ResourceWarning because
1584
1583
  # psutil.__subproc instance doesn't get properly freed.
@@ -38,7 +38,6 @@ from psutil.tests import is_win_secure_system_proc
38
38
  from psutil.tests import process_namespace
39
39
  from psutil.tests import pytest
40
40
 
41
-
42
41
  # Cuts the time in half, but (e.g.) on macOS the process pool stays
43
42
  # alive after join() (multiprocessing bug?), messing up other tests.
44
43
  USE_PROC_POOL = LINUX and not CI_TESTING and not PYTEST_PARALLEL
@@ -48,16 +47,16 @@ def proc_info(pid):
48
47
  tcase = PsutilTestCase()
49
48
 
50
49
  def check_exception(exc, proc, name, ppid):
51
- tcase.assertEqual(exc.pid, pid)
50
+ assert exc.pid == pid
52
51
  if exc.name is not None:
53
- tcase.assertEqual(exc.name, name)
52
+ assert exc.name == name
54
53
  if isinstance(exc, psutil.ZombieProcess):
55
- tcase.assertProcessZombie(proc)
54
+ tcase.assert_proc_zombie(proc)
56
55
  if exc.ppid is not None:
57
- tcase.assertGreaterEqual(exc.ppid, 0)
58
- tcase.assertEqual(exc.ppid, ppid)
56
+ assert exc.ppid >= 0
57
+ assert exc.ppid == ppid
59
58
  elif isinstance(exc, psutil.NoSuchProcess):
60
- tcase.assertProcessGone(proc)
59
+ tcase.assert_proc_gone(proc)
61
60
  str(exc)
62
61
  repr(exc)
63
62
 
@@ -71,12 +70,12 @@ def proc_info(pid):
71
70
  try:
72
71
  proc = psutil.Process(pid)
73
72
  except psutil.NoSuchProcess:
74
- tcase.assertPidGone(pid)
73
+ tcase.assert_pid_gone(pid)
75
74
  return {}
76
75
  try:
77
76
  d = proc.as_dict(['ppid', 'name'])
78
77
  except psutil.NoSuchProcess:
79
- tcase.assertProcessGone(proc)
78
+ tcase.assert_proc_gone(proc)
80
79
  else:
81
80
  name, ppid = d['name'], d['ppid']
82
81
  info = {'pid': proc.pid}
@@ -148,7 +147,7 @@ class TestFetchAllProcesses(PsutilTestCase):
148
147
  if value not in (0, 0.0, [], None, '', {}):
149
148
  assert value, value
150
149
  if failures:
151
- raise self.fail(''.join(failures))
150
+ raise pytest.fail(''.join(failures))
152
151
 
153
152
  def cmdline(self, ret, info):
154
153
  assert isinstance(ret, list)
@@ -203,7 +202,7 @@ class TestFetchAllProcesses(PsutilTestCase):
203
202
  else:
204
203
  raise
205
204
  # this can't be taken for granted on all platforms
206
- # self.assertGreaterEqual(ret, psutil.boot_time())
205
+ # assert ret >= psutil.boot_time())
207
206
  # make sure returned value can be pretty printed
208
207
  # with strftime
209
208
  time.strftime("%Y %m %d %H:%M:%S", time.localtime(ret))
@@ -531,5 +530,4 @@ class TestPidsRange(PsutilTestCase):
531
530
  # Process class and is querable like a PID (process
532
531
  # ID). Skip it.
533
532
  continue
534
- with self.subTest(pid=pid):
535
- check(pid)
533
+ check(pid)
@@ -31,7 +31,6 @@ from psutil.tests import import_module_by_path
31
31
  from psutil.tests import psutil
32
32
  from psutil.tests import sh
33
33
 
34
-
35
34
  INTERNAL_SCRIPTS_DIR = os.path.join(SCRIPTS_DIR, "internal")
36
35
  SETUP_PY = os.path.join(ROOT_DIR, 'setup.py')
37
36
 
@@ -77,7 +76,7 @@ class TestExampleScripts(PsutilTestCase):
77
76
  if name.endswith('.py'):
78
77
  if 'test_' + os.path.splitext(name)[0] not in meths:
79
78
  # self.assert_stdout(name)
80
- raise self.fail(
79
+ raise pytest.fail(
81
80
  "no test defined for"
82
81
  f" {os.path.join(SCRIPTS_DIR, name)!r} script"
83
82
  )
@@ -89,7 +88,7 @@ class TestExampleScripts(PsutilTestCase):
89
88
  if file.endswith('.py'):
90
89
  path = os.path.join(root, file)
91
90
  if not stat.S_IXUSR & os.stat(path)[stat.ST_MODE]:
92
- raise self.fail(f"{path!r} is not executable")
91
+ raise pytest.fail(f"{path!r} is not executable")
93
92
 
94
93
  def test_disk_usage(self):
95
94
  self.assert_stdout('disk_usage.py')
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env python3
2
+
3
+ # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
4
+ # Use of this source code is governed by a BSD-style license that can be
5
+ # found in the LICENSE file.
6
+
7
+ """Tests which are meant to be run as root.
8
+
9
+ NOTE: keep this module compatible with unittest: we want to run this
10
+ file with the unittest runner, since pytest may not be installed for
11
+ the root user.
12
+ """
13
+
14
+ import datetime
15
+ import time
16
+ import unittest
17
+
18
+ import psutil
19
+ from psutil import FREEBSD
20
+ from psutil import LINUX
21
+ from psutil import OPENBSD
22
+ from psutil import WINDOWS
23
+ from psutil.tests import CI_TESTING
24
+ from psutil.tests import PsutilTestCase
25
+
26
+
27
+ def get_systime():
28
+ if hasattr(time, "clock_gettime") and hasattr(time, "CLOCK_REALTIME"):
29
+ return time.clock_gettime(time.CLOCK_REALTIME)
30
+ return time.time()
31
+
32
+
33
+ def set_systime(secs): # secs since the epoch
34
+ if hasattr(time, "clock_settime") and hasattr(time, "CLOCK_REALTIME"):
35
+ try:
36
+ time.clock_settime(time.CLOCK_REALTIME, secs)
37
+ except PermissionError:
38
+ raise unittest.SkipTest("needs root")
39
+ elif WINDOWS:
40
+ import pywintypes
41
+ import win32api
42
+
43
+ dt = datetime.datetime.fromtimestamp(secs, datetime.timezone.utc)
44
+ try:
45
+ win32api.SetSystemTime(
46
+ dt.year,
47
+ dt.month,
48
+ dt.isoweekday() % 7,
49
+ dt.day,
50
+ dt.hour,
51
+ dt.minute,
52
+ dt.second,
53
+ int(dt.microsecond / 1000),
54
+ )
55
+ except pywintypes.error as err:
56
+ if err.winerror == 1314:
57
+ raise unittest.SkipTest("needs Administrator user")
58
+ raise
59
+ else:
60
+ raise unittest.SkipTest("setting systime not supported")
61
+
62
+
63
+ class TestUpdatedSystemTime(PsutilTestCase):
64
+ """Tests which update the system clock."""
65
+
66
+ def setUp(self):
67
+ self.time_updated = False
68
+ self.orig_time = get_systime()
69
+ self.time_started = time.monotonic()
70
+
71
+ def tearDown(self):
72
+ if self.time_updated:
73
+ extra_t = time.monotonic() - self.time_started
74
+ set_systime(self.orig_time + extra_t)
75
+
76
+ def update_systime(self):
77
+ # set system time 1 hour later
78
+ set_systime(self.orig_time + 3600)
79
+ self.time_updated = True
80
+
81
+ def test_boot_time(self):
82
+ # Test that boot_time() reflects system clock updates.
83
+ t1 = psutil.boot_time()
84
+ self.update_systime()
85
+ t2 = psutil.boot_time()
86
+ self.assertGreater(t2, t1)
87
+ diff = int(t2 - t1)
88
+ self.assertAlmostEqual(diff, 3600, delta=1)
89
+
90
+ @unittest.skipIf(WINDOWS, "broken on WINDOWS") # TODO: fix it
91
+ def test_proc_create_time(self):
92
+ # Test that Process.create_time() reflects system clock
93
+ # updates. On systems such as Linux this is added on top of the
94
+ # process monotonic time returned by the kernel.
95
+ t1 = psutil.Process().create_time()
96
+ self.update_systime()
97
+ t2 = psutil.Process().create_time()
98
+ diff = int(t2 - t1)
99
+ self.assertAlmostEqual(diff, 3600, delta=1)
100
+
101
+ @unittest.skipIf(CI_TESTING, "skipped on CI for now") # TODO: fix it
102
+ @unittest.skipIf(OPENBSD, "broken on OPENBSD") # TODO: fix it
103
+ @unittest.skipIf(FREEBSD, "broken on FREEBSD") # TODO: fix it
104
+ def test_proc_ident(self):
105
+ p1 = psutil.Process()
106
+ self.update_systime()
107
+ p2 = psutil.Process()
108
+ self.assertEqual(p1._get_ident(), p2._get_ident())
109
+ self.assertEqual(p1, p2)
110
+
111
+ @unittest.skipIf(not LINUX, "LINUX only")
112
+ def test_linux_monotonic_proc_time(self):
113
+ t1 = psutil.Process()._proc.create_time(monotonic=True)
114
+ self.update_systime()
115
+ time.sleep(0.05)
116
+ t2 = psutil.Process()._proc.create_time(monotonic=True)
117
+ self.assertEqual(t1, t2)