|
1 | 1 | import contextlib
|
2 | 2 | import distutils.spawn # pylint: disable=no-name-in-module,import-error
|
3 | 3 | import functools
|
| 4 | +import io |
4 | 5 | import os
|
5 | 6 | import subprocess
|
6 | 7 | import sys
|
7 | 8 | from pathlib import Path
|
8 |
| -from typing import Generator, List, Mapping, Optional, Tuple, Union |
| 9 | +from typing import Any, Generator, List, Mapping, Optional, Tuple, Union |
9 | 10 |
|
10 | 11 | import pytest
|
11 | 12 | from pkg_resources import Requirement, ResolutionError, resource_filename
|
12 | 13 |
|
13 | 14 | from cwltool.context import LoadingContext, RuntimeContext
|
14 | 15 | from cwltool.executors import JobExecutor
|
15 | 16 | from cwltool.factory import Factory
|
| 17 | +from cwltool.main import main |
16 | 18 | from cwltool.singularity import is_version_2_6, is_version_3_or_newer
|
17 | 19 | from cwltool.utils import onWindows, windows_default_container_id
|
18 | 20 |
|
@@ -84,19 +86,21 @@ def get_main_output(
|
84 | 86 | env: Union[
|
85 | 87 | Mapping[bytes, Union[bytes, str]], Mapping[str, Union[bytes, str]], None
|
86 | 88 | ] = None,
|
| 89 | + monkeypatch: Any = None, |
87 | 90 | ) -> Tuple[Optional[int], str, str]:
|
88 |
| - process = subprocess.Popen( |
89 |
| - [sys.executable, "-m", "cwltool"] + args, |
90 |
| - stdout=subprocess.PIPE, |
91 |
| - stderr=subprocess.PIPE, |
92 |
| - env=env, |
93 |
| - ) |
94 |
| - |
95 |
| - stdout, stderr = process.communicate() |
| 91 | + stdout = io.StringIO() |
| 92 | + stderr = io.StringIO() |
| 93 | + if env is not None: |
| 94 | + assert monkeypatch is not None |
| 95 | + monkeypatch.setattr(os, "environ", env) |
| 96 | + try: |
| 97 | + rc = main(argsl=args, stdout=stdout, stderr=stderr) |
| 98 | + except SystemExit as e: |
| 99 | + rc = e.code |
96 | 100 | return (
|
97 |
| - process.returncode, |
98 |
| - stdout.decode() if stdout else "", |
99 |
| - stderr.decode() if stderr else "", |
| 101 | + rc, |
| 102 | + stdout.getvalue(), |
| 103 | + stderr.getvalue(), |
100 | 104 | )
|
101 | 105 |
|
102 | 106 |
|
|
0 commit comments