Skip to content

Commit efda9e3

Browse files
rupertnashmr-c
authored andcommitted
tests: no manual os.chdir; use util.working_directory context manager
1 parent bdb20fa commit efda9e3

File tree

3 files changed

+144
-144
lines changed

3 files changed

+144
-144
lines changed

tests/test_examples.py

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
get_windows_safe_factory,
3535
needs_docker,
3636
windows_needs_docker,
37+
working_directory,
3738
)
3839

3940
sys.argv = [""]
@@ -1068,16 +1069,16 @@ def test_no_js_console(factor: str) -> None:
10681069
def test_cid_file_dir(tmp_path: Path, factor: str) -> None:
10691070
"""Test --cidfile-dir option works."""
10701071
test_file = "cache_test_workflow.cwl"
1071-
cwd = Path.cwd()
1072-
os.chdir(tmp_path)
1073-
commands = factor.split()
1074-
commands.extend(["--cidfile-dir", str(tmp_path), get_data("tests/wf/" + test_file)])
1075-
error_code, stdout, stderr = get_main_output(commands)
1076-
assert "completed success" in stderr
1077-
assert error_code == 0
1078-
cidfiles_count = sum(1 for _ in tmp_path.glob("**/*"))
1079-
assert cidfiles_count == 2
1080-
os.chdir(cwd)
1072+
with working_directory(tmp_path):
1073+
commands = factor.split()
1074+
commands.extend(
1075+
["--cidfile-dir", str(tmp_path), get_data("tests/wf/" + test_file)]
1076+
)
1077+
error_code, stdout, stderr = get_main_output(commands)
1078+
assert "completed success" in stderr
1079+
assert error_code == 0
1080+
cidfiles_count = sum(1 for _ in tmp_path.glob("**/*"))
1081+
assert cidfiles_count == 2
10811082

10821083

10831084
@needs_docker
@@ -1121,22 +1122,20 @@ def test_cid_file_non_existing_dir(tmp_path: Path, factor: str) -> None:
11211122
def test_cid_file_w_prefix(tmp_path: Path, factor: str) -> None:
11221123
"""Test that --cidfile-prefix works."""
11231124
test_file = "cache_test_workflow.cwl"
1124-
cwd = Path.cwd()
1125-
os.chdir(tmp_path)
1126-
try:
1127-
commands = factor.split()
1128-
commands.extend(
1129-
[
1130-
"--record-container-id",
1131-
"--cidfile-prefix=pytestcid",
1132-
get_data("tests/wf/" + test_file),
1133-
]
1134-
)
1135-
error_code, stdout, stderr = get_main_output(commands)
1136-
finally:
1137-
listing = tmp_path.iterdir()
1138-
os.chdir(cwd)
1139-
cidfiles_count = sum(1 for _ in tmp_path.glob("**/pytestcid*"))
1125+
with working_directory(tmp_path):
1126+
try:
1127+
commands = factor.split()
1128+
commands.extend(
1129+
[
1130+
"--record-container-id",
1131+
"--cidfile-prefix=pytestcid",
1132+
get_data("tests/wf/" + test_file),
1133+
]
1134+
)
1135+
error_code, stdout, stderr = get_main_output(commands)
1136+
finally:
1137+
listing = tmp_path.iterdir()
1138+
cidfiles_count = sum(1 for _ in tmp_path.glob("**/pytestcid*"))
11401139
assert "completed success" in stderr
11411140
assert error_code == 0
11421141
assert cidfiles_count == 2, "{}/n{}".format(list(listing), stderr)
@@ -1397,20 +1396,20 @@ def test_v1_0_arg_empty_prefix_separate_false() -> None:
13971396
def test_scatter_output_filenames(tmp_path: Path) -> None:
13981397
"""If a scatter step produces identically named output then confirm that the final output is renamed correctly."""
13991398
cwd = Path.cwd()
1400-
os.chdir(tmp_path)
1401-
rtc = RuntimeContext()
1402-
rtc.outdir = str(cwd)
1403-
factory = cwltool.factory.Factory(runtime_context=rtc)
1404-
output_names = ["output.txt", "output.txt_2", "output.txt_3"]
1405-
scatter_workflow = factory.make(get_data("tests/scatter_numbers.cwl"))
1406-
result = scatter_workflow(range=3)
1407-
assert isinstance(result, dict)
1408-
assert "output" in result
1409-
1410-
locations = sorted([element["location"] for element in result["output"]])
1399+
with working_directory(tmp_path):
1400+
rtc = RuntimeContext()
1401+
rtc.outdir = str(cwd)
1402+
factory = cwltool.factory.Factory(runtime_context=rtc)
1403+
output_names = ["output.txt", "output.txt_2", "output.txt_3"]
1404+
scatter_workflow = factory.make(get_data("tests/scatter_numbers.cwl"))
1405+
result = scatter_workflow(range=3)
1406+
assert isinstance(result, dict)
1407+
assert "output" in result
1408+
1409+
locations = sorted([element["location"] for element in result["output"]])
14111410

1412-
assert (
1413-
locations[0].endswith("output.txt")
1414-
and locations[1].endswith("output.txt_2")
1415-
and locations[2].endswith("output.txt_3")
1416-
), f"Locations {locations} do not end with {output_names}"
1411+
assert (
1412+
locations[0].endswith("output.txt")
1413+
and locations[1].endswith("output.txt_2")
1414+
and locations[2].endswith("output.txt_3")
1415+
), f"Locations {locations} do not end with {output_names}"

tests/test_singularity.py

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@
2121
def test_singularity_pullfolder(tmp_path: Path) -> None:
2222
workdir = tmp_path / "working_dir_new"
2323
workdir.mkdir()
24-
os.chdir(workdir)
25-
pullfolder = tmp_path / "pullfolder"
26-
pullfolder.mkdir()
27-
env = os.environ.copy()
28-
env["SINGULARITY_PULLFOLDER"] = str(pullfolder)
29-
result_code, stdout, stderr = get_main_output(
30-
[
31-
"--singularity",
32-
get_data("tests/sing_pullfolder_test.cwl"),
33-
"--message",
34-
"hello",
35-
],
36-
env=env,
37-
)
38-
print(stdout)
39-
print(stderr)
40-
assert result_code == 0
41-
image = pullfolder / "debian.img"
42-
assert image.exists()
24+
with working_directory(workdir):
25+
pullfolder = tmp_path / "pullfolder"
26+
pullfolder.mkdir()
27+
env = os.environ.copy()
28+
env["SINGULARITY_PULLFOLDER"] = str(pullfolder)
29+
result_code, stdout, stderr = get_main_output(
30+
[
31+
"--singularity",
32+
get_data("tests/sing_pullfolder_test.cwl"),
33+
"--message",
34+
"hello",
35+
],
36+
env=env,
37+
)
38+
print(stdout)
39+
print(stderr)
40+
assert result_code == 0
41+
image = pullfolder / "debian.img"
42+
assert image.exists()
4343

4444

4545
@needs_singularity
@@ -97,32 +97,37 @@ def test_singularity_incorrect_image_pull() -> None:
9797
def test_singularity_local(tmp_path: Path) -> None:
9898
workdir = tmp_path / "working_dir"
9999
workdir.mkdir()
100-
os.chdir(workdir)
101-
result_code, stdout, stderr = get_main_output(
102-
[
103-
"--singularity",
104-
get_data("tests/sing_pullfolder_test.cwl"),
105-
"--message",
106-
"hello",
107-
]
108-
)
109-
assert result_code == 0
100+
with working_directory(workdir):
101+
result_code, stdout, stderr = get_main_output(
102+
[
103+
"--singularity",
104+
get_data("tests/sing_pullfolder_test.cwl"),
105+
"--message",
106+
"hello",
107+
]
108+
)
109+
assert result_code == 0
110110

111111

112112
@needs_singularity_2_6
113113
def test_singularity_docker_image_id_in_tool(tmp_path: Path) -> None:
114114
workdir = tmp_path / "working_dir"
115115
workdir.mkdir()
116-
os.chdir(workdir)
117-
result_code, stdout, stderr = get_main_output(
118-
[
119-
"--singularity",
120-
get_data("tests/sing_pullfolder_test.cwl"),
121-
"--message",
122-
"hello",
123-
]
124-
)
125-
result_code1, stdout, stderr = get_main_output(
126-
["--singularity", get_data("tests/debian_image_id.cwl"), "--message", "hello"]
127-
)
128-
assert result_code1 == 0
116+
with working_directory(workdir):
117+
result_code, stdout, stderr = get_main_output(
118+
[
119+
"--singularity",
120+
get_data("tests/sing_pullfolder_test.cwl"),
121+
"--message",
122+
"hello",
123+
]
124+
)
125+
result_code1, stdout, stderr = get_main_output(
126+
[
127+
"--singularity",
128+
get_data("tests/debian_image_id.cwl"),
129+
"--message",
130+
"hello",
131+
]
132+
)
133+
assert result_code1 == 0

tests/test_udocker.py

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99
from _pytest.tmpdir import TempPathFactory
1010

11-
from .util import get_data, get_main_output
11+
from .util import get_data, get_main_output, working_directory
1212

1313
LINUX = sys.platform in ("linux", "linux2")
1414

@@ -19,59 +19,57 @@ def udocker(tmp_path_factory: TempPathFactory) -> str:
1919
test_cwd = os.getcwd()
2020
test_environ = copy.copy(os.environ)
2121
docker_install_dir = str(tmp_path_factory.mktemp("udocker"))
22-
os.chdir(docker_install_dir)
23-
24-
url = "https://raw.githubusercontent.com/jorge-lip/udocker-builds/master/tarballs/udocker-1.1.4.tar.gz"
25-
install_cmds = [
26-
["curl", url, "-o", "./udocker-tarball.tgz"],
27-
["tar", "xzvf", "udocker-tarball.tgz", "udocker"],
28-
[
29-
"bash",
30-
"-c",
31-
"UDOCKER_TARBALL={}/udocker-tarball.tgz ./udocker install".format(
32-
docker_install_dir
33-
),
34-
],
35-
]
36-
37-
test_environ["UDOCKER_DIR"] = os.path.join(docker_install_dir, ".udocker")
38-
test_environ["HOME"] = docker_install_dir
39-
40-
results = []
41-
for _ in range(3):
42-
results = [subprocess.call(cmds, env=test_environ) for cmds in install_cmds]
43-
if sum(results) == 0:
44-
break
45-
subprocess.call(["rm", "./udocker"])
46-
47-
assert sum(results) == 0
48-
49-
udocker_path = os.path.join(docker_install_dir, "udocker")
50-
os.chdir(test_cwd)
22+
with working_directory(docker_install_dir):
23+
24+
url = "https://raw.githubusercontent.com/jorge-lip/udocker-builds/master/tarballs/udocker-1.1.4.tar.gz"
25+
install_cmds = [
26+
["curl", url, "-o", "./udocker-tarball.tgz"],
27+
["tar", "xzvf", "udocker-tarball.tgz", "udocker"],
28+
[
29+
"bash",
30+
"-c",
31+
"UDOCKER_TARBALL={}/udocker-tarball.tgz ./udocker install".format(
32+
docker_install_dir
33+
),
34+
],
35+
]
36+
37+
test_environ["UDOCKER_DIR"] = os.path.join(docker_install_dir, ".udocker")
38+
test_environ["HOME"] = docker_install_dir
39+
40+
results = []
41+
for _ in range(3):
42+
results = [subprocess.call(cmds, env=test_environ) for cmds in install_cmds]
43+
if sum(results) == 0:
44+
break
45+
subprocess.call(["rm", "./udocker"])
46+
47+
assert sum(results) == 0
48+
49+
udocker_path = os.path.join(docker_install_dir, "udocker")
50+
5151
return udocker_path
5252

5353

5454
@pytest.mark.skipif(not LINUX, reason="LINUX only")
5555
def test_udocker_usage_should_not_write_cid_file(udocker: str, tmp_path: Path) -> None:
5656
"""Confirm that no cidfile is made when udocker is used."""
57-
cwd = Path.cwd()
58-
os.chdir(tmp_path)
59-
60-
test_file = "tests/wf/wc-tool.cwl"
61-
job_file = "tests/wf/wc-job.json"
62-
error_code, stdout, stderr = get_main_output(
63-
[
64-
"--debug",
65-
"--default-container",
66-
"debian",
67-
"--user-space-docker-cmd=" + udocker,
68-
get_data(test_file),
69-
get_data(job_file),
70-
]
71-
)
7257

73-
cidfiles_count = sum(1 for _ in tmp_path.glob("*.cid"))
74-
os.chdir(cwd)
58+
with working_directory(tmp_path):
59+
test_file = "tests/wf/wc-tool.cwl"
60+
job_file = "tests/wf/wc-job.json"
61+
error_code, stdout, stderr = get_main_output(
62+
[
63+
"--debug",
64+
"--default-container",
65+
"debian",
66+
"--user-space-docker-cmd=" + udocker,
67+
get_data(test_file),
68+
get_data(job_file),
69+
]
70+
)
71+
72+
cidfiles_count = sum(1 for _ in tmp_path.glob("*.cid"))
7573

7674
assert "completed success" in stderr, stderr
7775
assert cidfiles_count == 0
@@ -83,19 +81,17 @@ def test_udocker_usage_should_not_write_cid_file(udocker: str, tmp_path: Path) -
8381
)
8482
def test_udocker_should_display_memory_usage(udocker: str, tmp_path: Path) -> None:
8583
"""Confirm that memory ussage is logged even with udocker."""
86-
cwd = Path.cwd()
87-
os.chdir(tmp_path)
88-
error_code, stdout, stderr = get_main_output(
89-
[
90-
"--enable-ext",
91-
"--default-container=debian",
92-
"--user-space-docker-cmd=" + udocker,
93-
get_data("tests/wf/timelimit.cwl"),
94-
"--sleep_time",
95-
"10",
96-
]
97-
)
98-
os.chdir(cwd)
84+
with working_directory(tmp_path):
85+
error_code, stdout, stderr = get_main_output(
86+
[
87+
"--enable-ext",
88+
"--default-container=debian",
89+
"--user-space-docker-cmd=" + udocker,
90+
get_data("tests/wf/timelimit.cwl"),
91+
"--sleep_time",
92+
"10",
93+
]
94+
)
9995

10096
assert "completed success" in stderr, stderr
10197
assert "Max memory" in stderr, stderr

0 commit comments

Comments
 (0)