Skip to content

Commit 1f69442

Browse files
committed
Restructure code to ensure coverage
1 parent 282332a commit 1f69442

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

cwltool/command_line_tool.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -888,26 +888,15 @@ def remove_prefix(s: str, prefix: str) -> str:
888888
keydict[cls] = r
889889

890890
# If there are environmental variables to preserve, add it to the key
891-
env_def = cast(CWLObjectType, keydict.get("EnvVarRequirement", {}))
891+
env_def = dict(cast(Mapping[str, str], keydict.get("EnvVarRequirement", {})))
892892
env_requirement, _ = self.get_requirement("EnvVarRequirement")
893893
if env_requirement:
894894
for req in cast(list[CWLObjectType], env_requirement["envDef"]):
895895
env_name = cast(str, req["envName"])
896896
env_value = cast(str, req["envValue"])
897897
env_def[env_name] = env_value
898898
if runtimeContext.preserve_environment is not None:
899-
# This could either be a list or a dictionary
900-
if isinstance(runtimeContext.preserve_environment, dict):
901-
for name, value in runtimeContext.preserve_environment.items():
902-
env_def[name] = value
903-
elif isinstance(runtimeContext.preserve_environment, list):
904-
for key in runtimeContext.preserve_environment:
905-
try:
906-
env_def[key] = os.environ[key]
907-
except KeyError:
908-
_logger.warning(
909-
f"Attempting to preserve environment variable {key!r} which is not present"
910-
)
899+
env_def = JobBase.extract_environment(runtimeContext, env_def)
911900

912901
if env_def:
913902
keydict["EnvVarRequirement"] = env_def

cwltool/job.py

Lines changed: 26 additions & 12 deletions
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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ def test_cache_default_literal_file(tmp_path: Path, factor: str) -> None:
13461346
assert "completed success" in stderr
13471347
assert error_code == 0
13481348

1349+
13491350
@pytest.mark.parametrize("factor", test_factors)
13501351
def test_cache_environment_variable(tmp_path: Path, factor: str) -> None:
13511352
"""Ensure that changing the environment variables will result in different cache keys"""
@@ -1360,7 +1361,7 @@ def test_cache_environment_variable(tmp_path: Path, factor: str) -> None:
13601361
"--outdir",
13611362
str(tmp_path / "outdir"),
13621363
get_data("tests/" + test_file),
1363-
get_data(f"tests/{test_job_file}")
1364+
get_data(f"tests/{test_job_file}"),
13641365
]
13651366
)
13661367
error_code, _, stderr = get_main_output(commands)
@@ -1382,7 +1383,7 @@ def test_cache_environment_variable(tmp_path: Path, factor: str) -> None:
13821383
"--outdir",
13831384
str(tmp_path / "outdir"),
13841385
get_data("tests/" + test_file),
1385-
get_data(f"tests/{test_job_file}")
1386+
get_data(f"tests/{test_job_file}"),
13861387
]
13871388
)
13881389

@@ -1392,6 +1393,7 @@ def test_cache_environment_variable(tmp_path: Path, factor: str) -> None:
13921393
assert "Output of job will be cached in" in stderr
13931394
assert error_code == 0, stderr
13941395

1396+
13951397
def test_write_summary(tmp_path: Path) -> None:
13961398
"""Test --write-summary."""
13971399
commands = [

0 commit comments

Comments
 (0)