1
+ """Test singularity{,-ce} & apptainer versions."""
1
2
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
+ )
9
11
12
+ from subprocess import check_output # nosec
10
13
11
- def set_dummy_check_output (name , version ):
12
- cwltool .singularity .check_output = lambda c , universal_newlines : name + " version " + version
13
14
15
+ def reset_singularity_version_cache () -> None :
16
+ """Reset the cache for testing."""
17
+ cwltool .singularity ._SINGULARITY_VERSION = None
18
+ cwltool .singularity ._SINGULARITY_FLAVOR = ""
14
19
15
20
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()."""
17
35
set_dummy_check_output ("apptainer" , "1.0.1" )
18
36
reset_singularity_version_cache ()
19
37
v = get_version ()
20
38
assert isinstance (v , tuple )
21
39
assert isinstance (v [0 ], list )
22
40
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
25
47
v_cached = get_version ()
26
48
assert v == v_cached
27
49
@@ -38,9 +60,11 @@ def test_get_version():
38
60
assert v [0 ][1 ] == 8
39
61
assert v [0 ][2 ] == 5
40
62
assert v [1 ] == "singularity"
63
+ restore_check_output ()
41
64
42
65
43
- def test_version_checks ():
66
+ def test_version_checks () -> None :
67
+ """Confirm logic in the various singularity version checks."""
44
68
set_dummy_check_output ("apptainer" , "1.0.1" )
45
69
reset_singularity_version_cache ()
46
70
assert is_apptainer_1_or_newer ()
@@ -112,3 +136,4 @@ def test_version_checks():
112
136
assert is_version_3_or_newer ()
113
137
assert is_version_3_1_or_newer ()
114
138
assert is_version_3_4_or_newer ()
139
+ restore_check_output ()
0 commit comments