Create and run a subprocess. Thin wrapper around `subprocess.run`, intended for testing. Will mark fork() failures on Cygwin as expected failures: not a success, but not indicating a problem with the code either. Parameters ---------- args : list of str env : dict
(command, env=None, timeout=60, stdout=None,
stderr=None, check=False, text=True,
capture_output=False, **kwargs)
| 52 | |
| 53 | |
| 54 | def subprocess_run_for_testing(command, env=None, timeout=60, stdout=None, |
| 55 | stderr=None, check=False, text=True, |
| 56 | capture_output=False, **kwargs): |
| 57 | """ |
| 58 | Create and run a subprocess. |
| 59 | |
| 60 | Thin wrapper around `subprocess.run`, intended for testing. Will |
| 61 | mark fork() failures on Cygwin as expected failures: not a |
| 62 | success, but not indicating a problem with the code either. |
| 63 | |
| 64 | Parameters |
| 65 | ---------- |
| 66 | args : list of str |
| 67 | env : dict[str, str] |
| 68 | timeout : float |
| 69 | stdout, stderr |
| 70 | check : bool |
| 71 | text : bool |
| 72 | Also called ``universal_newlines`` in subprocess. I chose this |
| 73 | name since the main effect is returning bytes (`False`) vs. str |
| 74 | (`True`), though it also tries to normalize newlines across |
| 75 | platforms. |
| 76 | capture_output : bool |
| 77 | Set stdout and stderr to subprocess.PIPE |
| 78 | |
| 79 | Returns |
| 80 | ------- |
| 81 | proc : subprocess.Popen |
| 82 | |
| 83 | See Also |
| 84 | -------- |
| 85 | subprocess.run |
| 86 | |
| 87 | Raises |
| 88 | ------ |
| 89 | pytest.skip |
| 90 | If running on emscripten, which does not support subprocesses. |
| 91 | pytest.xfail |
| 92 | If platform is Cygwin and subprocess reports a fork() failure. |
| 93 | """ |
| 94 | if sys.platform == 'emscripten': |
| 95 | import pytest |
| 96 | pytest.skip('emscripten does not support subprocesses') |
| 97 | if capture_output: |
| 98 | stdout = stderr = subprocess.PIPE |
| 99 | # Add CREATE_NO_WINDOW flag on Windows to prevent console window overhead |
| 100 | # This is added in an attempt to fix flaky timeouts of subprocesses on Windows |
| 101 | if sys.platform == 'win32': |
| 102 | if 'creationflags' not in kwargs: |
| 103 | kwargs['creationflags'] = subprocess.CREATE_NO_WINDOW |
| 104 | else: |
| 105 | kwargs['creationflags'] |= subprocess.CREATE_NO_WINDOW |
| 106 | try: |
| 107 | proc = subprocess.run( |
| 108 | command, env=env, |
| 109 | timeout=timeout, check=check, |
| 110 | stdout=stdout, stderr=stderr, |
| 111 | text=text, **kwargs |
searching dependent graphs…