1+ """Test singularity{,-ce} & apptainer versions."""
12import 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