11"""Tests of satisfying SoftwareRequirement via dependencies."""
22import os
3+ import tempfile
34from pathlib import Path
45from shutil import which
56from types import ModuleType
6- from typing import Optional
7-
7+ from typing import Optional , Tuple , Callable
8+ from getpass import getuser
89import pytest
910
1011from cwltool .context import LoadingContext
@@ -26,7 +27,15 @@ def test_biocontainers(tmp_path: Path) -> None:
2627 wflow = get_data ("tests/seqtk_seq.cwl" )
2728 job = get_data ("tests/seqtk_seq_job.json" )
2829 error_code , _ , _ = get_main_output (
29- ["--outdir" , str (tmp_path ), "--beta-use-biocontainers" , wflow , job ]
30+ [
31+ "--outdir" ,
32+ str (tmp_path / "out" ),
33+ "--beta-use-biocontainers" ,
34+ "--beta-dependencies-directory" ,
35+ str (tmp_path / "deps" ),
36+ wflow ,
37+ job ,
38+ ]
3039 )
3140
3241 assert error_code == 0
@@ -38,31 +47,84 @@ def test_biocontainers_resolution(tmp_path: Path) -> None:
3847 """Confirm expected container name for --beta-use-biocontainers."""
3948 tool = load_tool (get_data ("tests/seqtk_seq.cwl" ), LoadingContext ())
4049 assert (
41- get_container_from_software_requirements (True , tool ) == "quay.io/biocontainers/seqtk:r93--0"
50+ get_container_from_software_requirements (
51+ True , tool , container_image_cache_path = str (tmp_path )
52+ )
53+ == "quay.io/biocontainers/seqtk:r93--0"
4254 )
4355
4456
45- @pytest .mark .skipif (not deps , reason = "galaxy-tool-util is not installed" )
46- def test_bioconda (tmp_path : Path ) -> None :
57+ @pytest .fixture (scope = "session" )
58+ def bioconda_setup (request : pytest .FixtureRequest ) -> Tuple [Optional [int ], str ]:
59+ """
60+ Caches the conda environment created for seqtk_seq.cwl.
61+
62+ Respects ``--basetemp`` via code copied from
63+ :py:method:`pytest.TempPathFactory.getbasetemp`.
64+ """
65+
66+ deps_dir = request .config .cache .get ("bioconda_deps" , None )
67+ if deps_dir is not None and not Path (deps_dir ).exists ():
68+ # cache value set, but cache is gone :( ... recreate
69+ deps_dir = None
70+
71+ if deps_dir is None :
72+ given_basetemp = request .config .option .basetemp
73+ if given_basetemp is not None :
74+ basetemp = Path (os .path .abspath (str (given_basetemp ))).resolve ()
75+ deps_dir = basetemp / "bioconda"
76+ else :
77+ from_env = os .environ .get ("PYTEST_DEBUG_TEMPROOT" )
78+ temproot = Path (from_env or tempfile .gettempdir ()).resolve ()
79+ rootdir = temproot .joinpath (f"pytest-of-{ getuser () or 'unknown' } " )
80+ try :
81+ rootdir .mkdir (mode = 0o700 , exist_ok = True )
82+ except OSError :
83+ rootdir = temproot .joinpath ("pytest-of-unknown" )
84+ rootdir .mkdir (mode = 0o700 , exist_ok = True )
85+ deps_dir = rootdir / "bioconda"
86+ request .config .cache .set ("bioconda_deps" , str (deps_dir ))
87+
88+ deps_dirpath = Path (deps_dir )
89+ deps_dirpath .mkdir (exist_ok = True )
90+
4791 wflow = get_data ("tests/seqtk_seq.cwl" )
4892 job = get_data ("tests/seqtk_seq_job.json" )
4993 error_code , _ , stderr = get_main_output (
50- ["--outdir" , str (tmp_path ), "--beta-conda-dependencies" , "--debug" , wflow , job ]
94+ [
95+ "--outdir" ,
96+ str (deps_dirpath / "out" ),
97+ "--beta-conda-dependencies" ,
98+ "--beta-dependencies-directory" ,
99+ str (deps_dirpath / "deps" ),
100+ "--debug" ,
101+ wflow ,
102+ job ,
103+ ]
51104 )
105+ return error_code , stderr
52106
107+
108+ @pytest .mark .skipif (not deps , reason = "galaxy-tool-util is not installed" )
109+ def test_bioconda (bioconda_setup : Callable [[], Tuple [Optional [int ], str ]]) -> None :
110+ error_code , stderr = bioconda_setup
53111 assert error_code == 0 , stderr
54112
55113
56114@pytest .mark .skipif (not deps , reason = "galaxy-tool-util is not installed" )
57115@pytest .mark .skipif (not which ("modulecmd" ), reason = "modulecmd not installed" )
58- def test_modules (monkeypatch : pytest .MonkeyPatch ) -> None :
116+ def test_modules (monkeypatch : pytest .MonkeyPatch , tmp_path : Path ) -> None :
59117 """Do a basic smoke test using environment modules to satisfy a SoftwareRequirement."""
60118 wflow = get_data ("tests/random_lines.cwl" )
61119 job = get_data ("tests/random_lines_job.json" )
62120 monkeypatch .setenv ("MODULEPATH" , os .path .join (os .getcwd (), "tests/test_deps_env/modulefiles" ))
63121 error_code , _ , stderr = get_main_output (
64122 [
123+ "--outdir" ,
124+ str (tmp_path / "out" ),
65125 "--beta-dependency-resolvers-configuration" ,
126+ "--beta-dependencies-directory" ,
127+ str (tmp_path / "deps" ),
66128 "tests/test_deps_env_modules_resolvers_conf.yml" ,
67129 "--debug" ,
68130 wflow ,
0 commit comments