Skip to content

Commit

Permalink
fix: test_package for .tar.bz2 and .conda (#983)
Browse files Browse the repository at this point in the history
We used some custom code to extract test information from `.tar.bz2`
packages which would need adjustments to handle `.conda` packages -- but
nowadays we can just use `conda-package-streaming` for this instead, as
done here.

---------

Signed-off-by: Marcel Bargull <marcel.bargull@udo.edu>
  • Loading branch information
mbargull authored May 8, 2024
1 parent 6b605c0 commit c683290
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
1 change: 1 addition & 0 deletions bioconda_utils/bioconda_utils-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ conda=24.3.*
conda-libmamba-solver=24.1.*
conda-build=24.3.*
conda-index=0.4.*
conda-package-streaming=0.9.*
mamba=1.5.*
boa=0.17.*

Expand Down
6 changes: 4 additions & 2 deletions bioconda_utils/pkg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from conda_build.metadata import MetaData
from conda_index.index import update_index
from conda_package_streaming.package_streaming import stream_conda_info

logger = logging.getLogger(__name__)

Expand All @@ -23,8 +24,9 @@
def get_tests(path):
"Extract tests from a built package"
tmp = tempfile.mkdtemp()
t = tarfile.open(path)
t.extractall(tmp)
for tar, member in stream_conda_info(path):
if member.name.startswith("info/recipe/"):
tar.extract(member, tmp)
input_dir = os.path.join(tmp, 'info', 'recipe')

tests = [
Expand Down
15 changes: 7 additions & 8 deletions bioconda_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,19 +408,18 @@ def sandboxed_env(env):
the existing `os.environ` or the provided **env** that match
ENV_VAR_WHITELIST globs.
"""
os_environ = os.environ
orig = os_environ.copy()
env = dict(env)
orig = os.environ.copy()

_env = {k: v for k, v in orig.items() if allowed_env_var(k)}
_env.update({k: str(v) for k, v in env.items() if allowed_env_var(k)})

os.environ = _env

try:
os_environ.clear()
os_environ.update({k: v for k, v in orig.items() if allowed_env_var(k)})
os_environ.update({k: str(v) for k, v in env.items() if allowed_env_var(k)})
yield
finally:
os.environ.clear()
os.environ.update(orig)
os_environ.clear()
os_environ.update(orig)


def load_all_meta(recipe, config=None, finalize=True):
Expand Down
74 changes: 73 additions & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import sys
import subprocess as sp
import pytest
Expand All @@ -10,9 +11,10 @@
import tarfile
import logging
import shutil
from pathlib import Path
from textwrap import dedent

from conda_build import metadata
from conda_build import api, metadata

from bioconda_utils import __version__
from bioconda_utils import utils
Expand Down Expand Up @@ -1246,3 +1248,73 @@ def test_skip_unsatisfiable_pin_compatible(config_fixture):
)
assert build_result
assert len(utils.load_all_meta(r.recipe_dirs["two"])) == 1


@pytest.mark.parametrize('mulled_test', PARAMS, ids=IDS)
@pytest.mark.parametrize('pkg_format', ["1", "2"])
def test_pkg_test_conda_package_format(
config_fixture, pkg_format, mulled_test, tmp_path, monkeypatch
):
"""
Running a mulled-build test with .tar.bz2/.conda package formats
"""
# ("1" is .tar.bz2 and "2" is .conda)
try:
from conda_build.conda_interface import cc_conda_build
except ImportError:
pass
else:
monkeypatch.setitem(cc_conda_build, "pkg_format", pkg_format)
from conda.base.context import context
monkeypatch.setitem(context.conda_build, "pkg_format", pkg_format)
condarc = Path(tmp_path, ".condarc")
condarc.write_text(f"conda_build:\n pkg_format: {pkg_format}\n")
monkeypatch.setenv("CONDARC", str(condarc))
monkeypatch.setattr(utils, "ENV_VAR_WHITELIST", ["CONDARC", *utils.ENV_VAR_WHITELIST])

r = Recipes(
f"""
one:
meta.yaml: |
package:
name: one
version: 1.1
build:
script:
- touch "${{PREFIX}}/one-file"
test:
commands:
- test -f "${{PREFIX}}/one-file"
""",
from_string=True,
)
r.write_recipes()
docker_builder = None
if mulled_test:
# Override conda_build.pkg_format in build_script_template.
build_script_template = re.sub(
"^(conda config.*)",
f"conda config --set conda_build.pkg_format {pkg_format}\n\\1",
docker_utils.BUILD_SCRIPT_TEMPLATE,
count=1,
flags=re.M,
)
docker_builder = docker_utils.RecipeBuilder(
use_host_conda_bld=True,
docker_base_image=DOCKER_BASE_IMAGE,
build_script_template=build_script_template,
)
build_result = build.build_recipes(
r.basedir,
config_fixture,
r.recipe_dirnames,
docker_builder=docker_builder,
mulled_test=mulled_test,
)
assert build_result

for recipe_dir in r.recipe_dirnames:
for pkg_file in utils.built_package_paths(recipe_dir):
assert pkg_file.endswith({"1": ".tar.bz2", "2": ".conda"}[pkg_format])
assert os.path.exists(pkg_file)
ensure_missing(pkg_file)

0 comments on commit c683290

Please sign in to comment.