Skip to content

Commit bdb20fa

Browse files
rupertnashmr-c
authored andcommitted
tests: use monkeypatch to ensure test enviroment changes are undone
1 parent 29b66ed commit bdb20fa

File tree

3 files changed

+43
-56
lines changed

3 files changed

+43
-56
lines changed

tests/test_ext.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
from io import StringIO
44
from pathlib import Path
5+
from typing import Any
56

67
import pytest
78

@@ -31,20 +32,14 @@ def test_listing_deep() -> None:
3132

3233

3334
@needs_docker
34-
def test_cwltool_options() -> None:
35-
try:
36-
opt = os.environ.get("CWLTOOL_OPTIONS")
37-
os.environ["CWLTOOL_OPTIONS"] = "--enable-ext"
38-
params = [
39-
get_data("tests/wf/listing_deep.cwl"),
40-
get_data("tests/listing-job.yml"),
41-
]
42-
assert main(params) == 0
43-
finally:
44-
if opt is not None:
45-
os.environ["CWLTOOL_OPTIONS"] = opt
46-
else:
47-
del os.environ["CWLTOOL_OPTIONS"]
35+
def test_cwltool_options(monkeypatch: Any) -> None:
36+
"""Check setting options via environment variable."""
37+
monkeypatch.setenv("CWLTOOL_OPTIONS", "--enable-ext")
38+
params = [
39+
get_data("tests/wf/listing_deep.cwl"),
40+
get_data("tests/listing-job.yml"),
41+
]
42+
assert main(params) == 0
4843

4944

5045
@needs_docker

tests/test_procgenerator.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
import os
2+
from typing import Any
23

34
from cwltool.main import main
45

56
from .util import get_data, windows_needs_docker
67

78

89
@windows_needs_docker
9-
def test_missing_enable_ext() -> None:
10-
# Requires --enable-ext and --enable-dev
11-
try:
12-
opt = os.environ.get("CWLTOOL_OPTIONS")
10+
def test_missing_enable_ext(monkeypatch: Any) -> None:
11+
"""Test missing enable-ext option fails.
1312
14-
if "CWLTOOL_OPTIONS" in os.environ:
15-
del os.environ["CWLTOOL_OPTIONS"]
16-
assert main([get_data("tests/wf/generator/zing.cwl"), "--zing", "zipper"]) == 1
13+
Check that a workflow that needs `--enable-ext` and
14+
`--enable-dev` fails without those options and passes with them.
15+
"""
16+
monkeypatch.delenv("CWLTOOL_OPTIONS", raising=False)
17+
assert main([get_data("tests/wf/generator/zing.cwl"), "--zing", "zipper"]) == 1
1718

18-
assert (
19-
main(
20-
[
21-
"--enable-ext",
22-
"--enable-dev",
23-
get_data("tests/wf/generator/zing.cwl"),
24-
"--zing",
25-
"zipper",
26-
]
27-
)
28-
== 0
19+
assert (
20+
main(
21+
[
22+
"--enable-ext",
23+
"--enable-dev",
24+
get_data("tests/wf/generator/zing.cwl"),
25+
"--zing",
26+
"zipper",
27+
]
2928
)
29+
== 0
30+
)
3031

31-
os.environ["CWLTOOL_OPTIONS"] = "--enable-ext --enable-dev"
32-
assert main([get_data("tests/wf/generator/zing.cwl"), "--zing", "zipper"]) == 0
33-
finally:
34-
if opt is not None:
35-
os.environ["CWLTOOL_OPTIONS"] = opt
36-
elif "CWLTOOL_OPTIONS" in os.environ:
37-
del os.environ["CWLTOOL_OPTIONS"]
32+
monkeypatch.setenv("CWLTOOL_OPTIONS", "--enable-ext --enable-dev")
33+
assert main([get_data("tests/wf/generator/zing.cwl"), "--zing", "zipper"]) == 0

tests/test_rdfprint.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import subprocess
33
import sys
4+
from typing import Any
45

56
from cwltool.main import main
67

@@ -11,21 +12,16 @@ def test_rdf_print() -> None:
1112
assert main(["--print-rdf", get_data("tests/wf/hello_single_tool.cwl")]) == 0
1213

1314

14-
def test_rdf_print_unicode() -> None:
15+
def test_rdf_print_unicode(monkeypatch: Any) -> None:
1516
"""Force ASCII encoding but load UTF file with --print-rdf."""
16-
try:
17-
lc_all = os.environ.get("LC_ALL", None)
18-
os.environ["LC_ALL"] = "C"
19-
20-
params = [
21-
sys.executable,
22-
"-m",
23-
"cwltool",
24-
"--print-rdf",
25-
get_data("tests/utf_doc_example.cwl"),
26-
]
27-
28-
assert subprocess.check_call(params) == 0
29-
finally:
30-
if lc_all:
31-
os.environ["LC_ALL"] = lc_all
17+
monkeypatch.setenv("LC_ALL", "C")
18+
19+
params = [
20+
sys.executable,
21+
"-m",
22+
"cwltool",
23+
"--print-rdf",
24+
get_data("tests/utf_doc_example.cwl"),
25+
]
26+
27+
assert subprocess.check_call(params) == 0

0 commit comments

Comments
 (0)