|
33 | 33 | import unittest.mock |
34 | 34 | import warnings |
35 | 35 | from collections import UserDict, UserList, UserString, namedtuple |
| 36 | +from typing import Callable |
36 | 37 |
|
37 | 38 | import TestCmd |
38 | 39 |
|
|
75 | 76 | to_bytes, |
76 | 77 | to_str, |
77 | 78 | wait_for_process_to_die, |
| 79 | + _wait_for_process_to_die_non_psutil, |
78 | 80 | ) |
79 | 81 | from SCons.Util.envs import is_valid_construction_var |
80 | 82 | from SCons.Util.hashes import ( |
|
84 | 86 | _set_allowed_viable_default_hashes, |
85 | 87 | ) |
86 | 88 |
|
| 89 | +try: |
| 90 | + import psutil |
| 91 | + has_psutil = True |
| 92 | +except ImportError: |
| 93 | + has_psutil = False |
| 94 | + |
| 95 | + |
87 | 96 | # These Util classes have no unit tests. Some don't make sense to test? |
88 | 97 | # DisplayEngine, Delegate, MethodWrapper, UniqueList, Unbuffered, Null, NullSeq |
89 | 98 |
|
@@ -849,26 +858,20 @@ def test_intern(self) -> None: |
849 | 858 | s4 = silent_intern("spam") |
850 | 859 | assert id(s1) == id(s4) |
851 | 860 |
|
852 | | - def test_wait_for_process_to_die_success(self) -> None: |
853 | | - cmd = [sys.executable, "-c", ""] |
854 | | - p = subprocess.Popen(cmd) |
855 | | - p.wait() |
856 | | - wait_for_process_to_die(p.pid, timeout=10.0) |
| 861 | + @unittest.skipUnless(has_psutil, "requires psutil") |
| 862 | + def test_wait_for_process_to_die_success_psutil(self) -> None: |
| 863 | + self._test_wait_for_process(wait_for_process_to_die) |
857 | 864 |
|
858 | | - def test_wait_for_process_to_die_timeout(self) -> None: |
859 | | - # Run a python script that will keep running until we close it's stdin |
860 | | - cmd = [sys.executable, "-c", "import sys; data = sys.stdin.read()"] |
861 | | - p = subprocess.Popen(cmd, stdin=subprocess.PIPE) |
| 865 | + def test_wait_for_process_to_die_success_non_psutil(self) -> None: |
| 866 | + self._test_wait_for_process(_wait_for_process_to_die_non_psutil) |
862 | 867 |
|
863 | | - # wait_for_process_to_die() should time out while the process is running |
864 | | - with self.assertRaises(TimeoutError): |
865 | | - wait_for_process_to_die(p.pid, timeout=0.2) |
866 | | - |
867 | | - p.stdin.close() |
| 868 | + def _test_wait_for_process( |
| 869 | + self, wait_fn: Callable[[int], None] |
| 870 | + ) -> None: |
| 871 | + cmd = [sys.executable, "-c", ""] |
| 872 | + p = subprocess.Popen(cmd) |
868 | 873 | p.wait() |
869 | | - |
870 | | - # wait_for_process_to_die() should complete normally now |
871 | | - wait_for_process_to_die(p.pid, timeout=10.0) |
| 874 | + wait_fn(p.pid) |
872 | 875 |
|
873 | 876 |
|
874 | 877 | class HashTestCase(unittest.TestCase): |
|
0 commit comments