Skip to content

Commit 8495431

Browse files
stxue1mr-c
authored andcommitted
Restructure code to ensure coverage
1 parent a8c8342 commit 8495431

File tree

3 files changed

+146
-27
lines changed

3 files changed

+146
-27
lines changed

cwltool/command_line_tool.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -877,26 +877,15 @@ def remove_prefix(s: str, prefix: str) -> str:
877877
keydict[cls] = r
878878

879879
# If there are environmental variables to preserve, add it to the key
880-
env_def = cast(CWLObjectType, keydict.get("EnvVarRequirement", {}))
880+
env_def = dict(cast(Mapping[str, str], keydict.get("EnvVarRequirement", {})))
881881
env_requirement, _ = self.get_requirement("EnvVarRequirement")
882882
if env_requirement:
883883
for req in cast(list[CWLObjectType], env_requirement["envDef"]):
884884
env_name = cast(str, req["envName"])
885885
env_value = cast(str, req["envValue"])
886886
env_def[env_name] = env_value
887887
if runtimeContext.preserve_environment is not None:
888-
# This could either be a list or a dictionary
889-
if isinstance(runtimeContext.preserve_environment, dict):
890-
for name, value in runtimeContext.preserve_environment.items():
891-
env_def[name] = value
892-
elif isinstance(runtimeContext.preserve_environment, list):
893-
for key in runtimeContext.preserve_environment:
894-
try:
895-
env_def[key] = os.environ[key]
896-
except KeyError:
897-
_logger.warning(
898-
f"Attempting to preserve environment variable {key!r} which is not present"
899-
)
888+
env_def = JobBase.extract_environment(runtimeContext, env_def)
900889

901890
if env_def:
902891
keydict["EnvVarRequirement"] = env_def

cwltool/job.py

+26-12
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,31 @@ def _preserve_environment_on_containers_warning(
469469
# By default, don't do anything; ContainerCommandLineJob below
470470
# will issue a warning.
471471

472+
@staticmethod
473+
def extract_environment(
474+
runtimeContext: RuntimeContext, envVarReq: Mapping[str, str]
475+
) -> dict[str, str]:
476+
"""
477+
Extract environment variables that should be preserved
478+
"""
479+
# Start empty
480+
env: dict[str, str] = {}
481+
# Preserve any env vars
482+
if runtimeContext.preserve_entire_environment:
483+
env.update(os.environ)
484+
elif runtimeContext.preserve_environment:
485+
for key in runtimeContext.preserve_environment:
486+
try:
487+
env[key] = os.environ[key]
488+
except KeyError:
489+
_logger.warning(
490+
f"Attempting to preserve environment variable {key!r} which is not present"
491+
)
492+
# Apply EnvVarRequirement
493+
env.update()
494+
495+
return env
496+
472497
def prepare_environment(
473498
self, runtimeContext: RuntimeContext, envVarReq: Mapping[str, str]
474499
) -> None:
@@ -481,26 +506,15 @@ def prepare_environment(
481506
"""
482507
# Start empty
483508
env: dict[str, str] = {}
484-
485-
# Preserve any env vars
486509
if runtimeContext.preserve_entire_environment:
487510
self._preserve_environment_on_containers_warning()
488-
env.update(os.environ)
489511
elif runtimeContext.preserve_environment:
490512
self._preserve_environment_on_containers_warning(runtimeContext.preserve_environment)
491-
for key in runtimeContext.preserve_environment:
492-
try:
493-
env[key] = os.environ[key]
494-
except KeyError:
495-
_logger.warning(
496-
f"Attempting to preserve environment variable {key!r} which is not present"
497-
)
498513

499514
# Set required env vars
500515
env.update(self._required_env())
501516

502-
# Apply EnvVarRequirement
503-
env.update(envVarReq)
517+
env.update(self.extract_environment(runtimeContext, envVarReq))
504518

505519
# Set on ourselves
506520
self.environment = env

tests/test_examples.py

+118-2
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,121 @@ def test_secondary_files_v1_0(tmp_path: Path, factor: str) -> None:
12321232
assert "completed success" in stderr
12331233
assert error_code == 0
12341234

1235+
1236+
@needs_docker
1237+
@pytest.mark.parametrize("factor", test_factors)
1238+
def test_wf_without_container(tmp_path: Path, factor: str) -> None:
1239+
"""Confirm that we can run a workflow without a container."""
1240+
test_file = "hello-workflow.cwl"
1241+
cache_dir = str(tmp_path / "cwltool_cache")
1242+
commands = factor.split()
1243+
commands.extend(
1244+
[
1245+
"--cachedir",
1246+
cache_dir,
1247+
"--outdir",
1248+
str(tmp_path / "outdir"),
1249+
get_data("tests/wf/" + test_file),
1250+
"--usermessage",
1251+
"hello",
1252+
]
1253+
)
1254+
error_code, _, stderr = get_main_output(commands)
1255+
1256+
stderr = re.sub(r"\s\s+", " ", stderr)
1257+
assert "completed success" in stderr
1258+
assert error_code == 0
1259+
1260+
1261+
@needs_docker
1262+
@pytest.mark.parametrize("factor", test_factors)
1263+
def test_issue_740_fixed(tmp_path: Path, factor: str) -> None:
1264+
"""Confirm that re-running a particular workflow with caching succeeds."""
1265+
test_file = "cache_test_workflow.cwl"
1266+
cache_dir = str(tmp_path / "cwltool_cache")
1267+
commands = factor.split()
1268+
commands.extend(["--cachedir", cache_dir, get_data("tests/wf/" + test_file)])
1269+
error_code, _, stderr = get_main_output(commands)
1270+
1271+
stderr = re.sub(r"\s\s+", " ", stderr)
1272+
assert "completed success" in stderr
1273+
assert error_code == 0
1274+
1275+
commands = factor.split()
1276+
commands.extend(["--cachedir", cache_dir, get_data("tests/wf/" + test_file)])
1277+
error_code, _, stderr = get_main_output(commands)
1278+
1279+
stderr = re.sub(r"\s\s+", " ", stderr)
1280+
assert "Output of job will be cached in" not in stderr
1281+
assert error_code == 0, stderr
1282+
1283+
1284+
@needs_docker
1285+
@pytest.mark.parametrize("factor", test_factors)
1286+
def test_cache_relative_paths(tmp_path: Path, factor: str) -> None:
1287+
"""Confirm that re-running a particular workflow with caching succeeds."""
1288+
test_file = "secondary-files.cwl"
1289+
test_job_file = "secondary-files-job.yml"
1290+
cache_dir = str(tmp_path / "cwltool_cache")
1291+
commands = factor.split()
1292+
commands.extend(
1293+
[
1294+
"--out",
1295+
str(tmp_path / "out"),
1296+
"--cachedir",
1297+
cache_dir,
1298+
get_data(f"tests/{test_file}"),
1299+
get_data(f"tests/{test_job_file}"),
1300+
]
1301+
)
1302+
error_code, _, stderr = get_main_output(commands)
1303+
1304+
stderr = re.sub(r"\s\s+", " ", stderr)
1305+
assert "completed success" in stderr
1306+
assert error_code == 0
1307+
1308+
commands = factor.split()
1309+
commands.extend(
1310+
[
1311+
"--out",
1312+
str(tmp_path / "out2"),
1313+
"--cachedir",
1314+
cache_dir,
1315+
get_data(f"tests/{test_file}"),
1316+
get_data(f"tests/{test_job_file}"),
1317+
]
1318+
)
1319+
error_code, _, stderr = get_main_output(commands)
1320+
1321+
stderr = re.sub(r"\s\s+", " ", stderr)
1322+
assert "Output of job will be cached in" not in stderr
1323+
assert error_code == 0, stderr
1324+
1325+
assert (tmp_path / "cwltool_cache" / "27903451fc1ee10c148a0bdeb845b2cf").exists()
1326+
1327+
1328+
@pytest.mark.parametrize("factor", test_factors)
1329+
def test_cache_default_literal_file(tmp_path: Path, factor: str) -> None:
1330+
"""Confirm that running a CLT with a default literal file with caching succeeds."""
1331+
test_file = "tests/wf/extract_region_specs.cwl"
1332+
cache_dir = str(tmp_path / "cwltool_cache")
1333+
commands = factor.split()
1334+
commands.extend(
1335+
[
1336+
"--out",
1337+
str(tmp_path / "out"),
1338+
"--cachedir",
1339+
cache_dir,
1340+
get_data(test_file),
1341+
]
1342+
)
1343+
error_code, _, stderr = get_main_output(commands)
1344+
1345+
stderr = re.sub(r"\s\s+", " ", stderr)
1346+
assert "completed success" in stderr
1347+
assert error_code == 0
1348+
1349+
12351350
@pytest.mark.parametrize("factor", test_factors)
12361351
def test_cache_environment_variable(tmp_path: Path, factor: str) -> None:
12371352
"""Ensure that changing the environment variables will result in different cache keys"""
@@ -1246,7 +1361,7 @@ def test_cache_environment_variable(tmp_path: Path, factor: str) -> None:
12461361
"--outdir",
12471362
str(tmp_path / "outdir"),
12481363
get_data("tests/" + test_file),
1249-
get_data(f"tests/{test_job_file}")
1364+
get_data(f"tests/{test_job_file}"),
12501365
]
12511366
)
12521367
error_code, _, stderr = get_main_output(commands)
@@ -1268,7 +1383,7 @@ def test_cache_environment_variable(tmp_path: Path, factor: str) -> None:
12681383
"--outdir",
12691384
str(tmp_path / "outdir"),
12701385
get_data("tests/" + test_file),
1271-
get_data(f"tests/{test_job_file}")
1386+
get_data(f"tests/{test_job_file}"),
12721387
]
12731388
)
12741389

@@ -1278,6 +1393,7 @@ def test_cache_environment_variable(tmp_path: Path, factor: str) -> None:
12781393
assert "Output of job will be cached in" in stderr
12791394
assert error_code == 0, stderr
12801395

1396+
12811397
def test_write_summary(tmp_path: Path) -> None:
12821398
"""Test --write-summary."""
12831399
commands = [

0 commit comments

Comments
 (0)