Skip to content

Commit 10c2cb6

Browse files
authored
make unit tests run in-process for speed (#1392)
1 parent 322d16c commit 10c2cb6

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

tests/test_content_type.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
from typing import Any
2+
13
import pydot # type: ignore
24

35
from .util import get_main_output
46

57

6-
def test_content_types() -> None:
8+
def test_content_types(caplog: Any) -> None:
79
for test_file in ("js_output.cwl", "js_output_workflow.cwl"):
810
commands = [
911
"https://raw.githubusercontent.com/common-workflow-language/common-workflow-language/main/v1.0/v1.0/test-cwl-out2.cwl",
1012
"https://github.com/common-workflow-language/common-workflow-language/blob/main/v1.0/v1.0/empty.json",
1113
]
1214
error_code, _, stderr = get_main_output(commands)
1315

14-
assert "got content-type of 'text/html'" in stderr
16+
found = False
17+
for record in caplog.records:
18+
if (
19+
record.name == "salad"
20+
and "got content-type of 'text/html'" in record.message
21+
):
22+
found = True
23+
break
24+
assert found
1525
assert error_code == 1, stderr

tests/util.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import contextlib
22
import distutils.spawn # pylint: disable=no-name-in-module,import-error
33
import functools
4+
import io
45
import os
56
import subprocess
67
import sys
78
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
910

1011
import pytest
1112
from pkg_resources import Requirement, ResolutionError, resource_filename
1213

1314
from cwltool.context import LoadingContext, RuntimeContext
1415
from cwltool.executors import JobExecutor
1516
from cwltool.factory import Factory
17+
from cwltool.main import main
1618
from cwltool.singularity import is_version_2_6, is_version_3_or_newer
1719
from cwltool.utils import onWindows, windows_default_container_id
1820

@@ -84,19 +86,21 @@ def get_main_output(
8486
env: Union[
8587
Mapping[bytes, Union[bytes, str]], Mapping[str, Union[bytes, str]], None
8688
] = None,
89+
monkeypatch: Any = None,
8790
) -> 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
96100
return (
97-
process.returncode,
98-
stdout.decode() if stdout else "",
99-
stderr.decode() if stderr else "",
101+
rc,
102+
stdout.getvalue(),
103+
stderr.getvalue(),
100104
)
101105

102106

0 commit comments

Comments
 (0)