Skip to content

Commit 8dde785

Browse files
committed
cleanups, docs, & make format
1 parent 2d3a001 commit 8dde785

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

cwltool/singularity.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@
2929
# Can be singularity, singularity-ce or apptainer
3030
_SINGULARITY_FLAVOR: str = ""
3131

32-
def reset_singularity_version_cache():
33-
"""
34-
Needed for resetting the cache for testing.
35-
"""
36-
global _SINGULARITY_VERSION # pylint: disable=global-statement
37-
global _SINGULARITY_FLAVOR # pylint: disable=global-statement
38-
_SINGULARITY_VERSION = None
39-
_SINGULARITY_FLAVOR = ""
40-
4132

4233
def get_version() -> Tuple[List[int], str]:
4334
"""
@@ -74,7 +65,7 @@ def get_version() -> Tuple[List[int], str]:
7465

7566
def is_apptainer_1_or_newer() -> bool:
7667
"""
77-
Is this the apptainer singularity distribution in version 1.0 or higher?
68+
Check if apptainer singularity distribution is version 1.0 or higher.
7869
7970
Apptainer v1.0.0 is compatible with SingularityCE 3.9.5.
8071
See: https://github.com/apptainer/apptainer/releases
@@ -87,7 +78,7 @@ def is_apptainer_1_or_newer() -> bool:
8778

8879
def is_version_2_6() -> bool:
8980
"""
90-
Is this exactly version 2.6?
81+
Check if this singularity version is exactly version 2.6.
9182
9283
Also returns False if the flavor is not singularity or singularity-ce.
9384
"""
@@ -98,13 +89,15 @@ def is_version_2_6() -> bool:
9889

9990

10091
def is_version_3_or_newer() -> bool:
92+
"""Check if this version is singularity version 3 or newer or equivalent."""
10193
if is_apptainer_1_or_newer():
10294
return True # this is equivalent to singularity-ce > 3.9.5
10395
v = get_version()
10496
return v[0][0] >= 3
10597

10698

10799
def is_version_3_1_or_newer() -> bool:
100+
"""Check if this version is singularity version 3.1 or newer or equivalent."""
108101
if is_apptainer_1_or_newer():
109102
return True # this is equivalent to singularity-ce > 3.9.5
110103
v = get_version()

tests/test_environment.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ def PWD(v: str, env: Env) -> bool:
131131
}
132132

133133
# Singularity variables appear to be in flux somewhat.
134-
version = get_version().split(".")
135-
vmajor = int(version[0])
134+
version = get_version()[0]
135+
vmajor = version[0]
136136
assert vmajor == 3, "Tests only work for Singularity 3"
137-
vminor = int(version[1])
137+
vminor = version[1]
138138
sing_vars: EnvChecks = {
139139
"SINGULARITY_CONTAINER": None,
140140
"SINGULARITY_NAME": None,

tests/test_singularity_versions.py

+38-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
1+
"""Test singularity{,-ce} & apptainer versions."""
12
import cwltool.singularity
2-
from cwltool.singularity import (reset_singularity_version_cache,
3-
get_version,
4-
is_apptainer_1_or_newer,
5-
is_version_2_6,
6-
is_version_3_or_newer,
7-
is_version_3_1_or_newer,
8-
is_version_3_4_or_newer)
3+
from cwltool.singularity import (
4+
get_version,
5+
is_apptainer_1_or_newer,
6+
is_version_2_6,
7+
is_version_3_or_newer,
8+
is_version_3_1_or_newer,
9+
is_version_3_4_or_newer,
10+
)
911

12+
from subprocess import check_output # nosec
1013

11-
def set_dummy_check_output(name, version):
12-
cwltool.singularity.check_output = lambda c, universal_newlines: name + " version " + version
1314

15+
def reset_singularity_version_cache() -> None:
16+
"""Reset the cache for testing."""
17+
cwltool.singularity._SINGULARITY_VERSION = None
18+
cwltool.singularity._SINGULARITY_FLAVOR = ""
1419

1520

16-
def test_get_version():
21+
def set_dummy_check_output(name: str, version: str) -> None:
22+
"""Mock out subprocess.check_output."""
23+
cwltool.singularity.check_output = ( # type: ignore[attr-defined]
24+
lambda c, universal_newlines: name + " version " + version
25+
)
26+
27+
28+
def restore_check_output() -> None:
29+
"""Undo the mock of subprocess.check_output."""
30+
cwltool.singularity.check_output = check_output # type: ignore[attr-defined]
31+
32+
33+
def test_get_version() -> None:
34+
"""Confirm expected types of singularity.get_version()."""
1735
set_dummy_check_output("apptainer", "1.0.1")
1836
reset_singularity_version_cache()
1937
v = get_version()
2038
assert isinstance(v, tuple)
2139
assert isinstance(v[0], list)
2240
assert isinstance(v[1], str)
23-
assert cwltool.singularity._SINGULARITY_VERSION is not None # pylint: disable=protected-access
24-
assert len(cwltool.singularity._SINGULARITY_FLAVOR) > 0 # pylint: disable=protected-access
41+
assert (
42+
cwltool.singularity._SINGULARITY_VERSION is not None
43+
) # pylint: disable=protected-access
44+
assert (
45+
len(cwltool.singularity._SINGULARITY_FLAVOR) > 0
46+
) # pylint: disable=protected-access
2547
v_cached = get_version()
2648
assert v == v_cached
2749

@@ -38,9 +60,11 @@ def test_get_version():
3860
assert v[0][1] == 8
3961
assert v[0][2] == 5
4062
assert v[1] == "singularity"
63+
restore_check_output()
4164

4265

43-
def test_version_checks():
66+
def test_version_checks() -> None:
67+
"""Confirm logic in the various singularity version checks."""
4468
set_dummy_check_output("apptainer", "1.0.1")
4569
reset_singularity_version_cache()
4670
assert is_apptainer_1_or_newer()
@@ -112,3 +136,4 @@ def test_version_checks():
112136
assert is_version_3_or_newer()
113137
assert is_version_3_1_or_newer()
114138
assert is_version_3_4_or_newer()
139+
restore_check_output()

0 commit comments

Comments
 (0)