Skip to content

Commit

Permalink
test: monkeypatch get_or_merge_config instead api
Browse files Browse the repository at this point in the history
  • Loading branch information
mbargull committed Nov 29, 2022
1 parent 8f6bfb4 commit efe302f
Show file tree
Hide file tree
Showing 17 changed files with 15 additions and 70 deletions.
53 changes: 13 additions & 40 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Copyright (C) 2014 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
import inspect
import os
import sys
from collections import defaultdict

import pytest

import conda_build.api
import conda_build.config
from conda_build.config import (
Config,
get_or_merge_config,
_src_cache_root_default,
conda_pkg_format_default,
enable_static_default,
Expand Down Expand Up @@ -123,52 +123,25 @@ def boolify(v):
return result


@pytest.fixture(scope="function")
def api_default_testing_config(testing_config, request):
"""Monkeypatch conda_build.api functions to use config=testing_config default
@pytest.fixture(scope="function", autouse=True)
def default_testing_config(testing_config, monkeypatch, request):
"""Monkeypatch get_or_merge_config to use testing_config by default
This requests fixture testing_config, thus implicitly testing_workdir, too.
"""

# Allow single tests to disable this fixture even if outer scope adds it.
if "no_api_default_testing_config" in request.keywords:
if "no_default_testing_config" in request.keywords:
return

arg_name = "config"
new_default = testing_config

saved_defaults = {}
saved_kwdefaults = {}
def get_or_merge_testing_config(config, variant=None, **kwargs):
return get_or_merge_config(config or testing_config, variant, **kwargs)

module = conda_build.api
for _, member in inspect.getmembers(module, inspect.isfunction):
if inspect.getmodule(member) is not module:
continue
arg_spec = inspect.getfullargspec(member)
defaults = member.__defaults__
kwdefaults = member.__kwdefaults__
try:
index = arg_spec.args[-len(defaults):].index(arg_name)
except (TypeError, ValueError):
pass
else:
saved_defaults[member] = defaults
member.__defaults__ = defaults[:index] + (new_default,) + defaults[index + 1:]
try:
kwdefaults[arg_name]
except (TypeError, KeyError):
pass
else:
saved_kwdefaults[member] = kwdefaults
member.__kwdefaults__ = {**kwdefaults, arg_name: new_default}

def restore_defaults():
for func, defaults in saved_defaults.items():
func.__defaults__ = defaults
for func, kwdefaults in saved_kwdefaults.items():
func.__kwdefaults__ = kwdefaults

request.addfinalizer(restore_defaults)
monkeypatch.setatr(
conda_build.config,
"get_or_merge_config",
get_or_merge_testing_config,
)


@pytest.fixture(scope="function")
Expand Down
2 changes: 0 additions & 2 deletions tests/test_api_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@

from .utils import is_valid_dir, metadata_dir, fail_dir, add_mangling

pytestmark = pytest.mark.usefixtures("api_default_testing_config")

# define a few commonly used recipes - use os.path.join(metadata_dir, recipe) elsewhere
empty_sections = os.path.join(metadata_dir, "empty_sections")

Expand Down
2 changes: 0 additions & 2 deletions tests/test_api_build_conda_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

from .utils import metadata_dir

pytestmark = pytest.mark.usefixtures("api_default_testing_config")


@pytest.mark.parametrize("pkg_format,pkg_ext", [(None, ".tar.bz2"), ("2", ".conda")])
def test_conda_pkg_format(
Expand Down
2 changes: 0 additions & 2 deletions tests/test_api_build_dll_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

from .utils import thisdir

pytestmark = pytest.mark.usefixtures("api_default_testing_config")


@pytest.fixture()
def recipe():
Expand Down
2 changes: 0 additions & 2 deletions tests/test_api_build_go_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

from .utils import thisdir

pytestmark = pytest.mark.usefixtures("api_default_testing_config")


@pytest.fixture()
def recipe():
Expand Down
2 changes: 2 additions & 0 deletions tests/test_api_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from inspect import getfullargspec as getargspec

pytestmark = pytest.mark.no_default_testing_config


def test_api_config():
assert hasattr(api, 'Config')
Expand Down
2 changes: 0 additions & 2 deletions tests/test_api_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

from .utils import metadata_dir, assert_package_consistency

pytestmark = pytest.mark.usefixtures("api_default_testing_config")


def test_convert_wheel_raises():
with pytest.raises(RuntimeError) as exc:
Expand Down
2 changes: 0 additions & 2 deletions tests/test_api_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

from .utils import metadata_dir, thisdir, on_win

pytestmark = pytest.mark.usefixtures("api_default_testing_config")

recipe_path = os.path.join(metadata_dir, "_debug_pkg")
ambiguous_recipe_path = os.path.join(metadata_dir, "_debug_pkg_multiple_outputs")
tarball_path = os.path.join(thisdir, "archives", "test_debug_pkg-1.0-0.tar.bz2")
Expand Down
2 changes: 0 additions & 2 deletions tests/test_api_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

from .utils import metadata_dir, thisdir

pytestmark = pytest.mark.usefixtures("api_default_testing_config")


def test_render_need_download(testing_workdir, testing_config):
# first, test that the download/render system renders all it can,
Expand Down
2 changes: 0 additions & 2 deletions tests/test_api_skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import conda_build.os_utils.external as external
from conda_build.utils import on_win

pytestmark = pytest.mark.usefixtures("api_default_testing_config")

thisdir = os.path.dirname(os.path.realpath(__file__))

repo_packages = [('', 'pypi', 'pip', '8.1.2'),
Expand Down
2 changes: 0 additions & 2 deletions tests/test_api_skeleton_cran.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
from conda_build.skeletons.cran import CRAN_BUILD_SH_SOURCE, CRAN_META
from conda_build.utils import ensure_list

pytestmark = pytest.mark.usefixtures("api_default_testing_config")


# CRAN packages to test license_file entry.
# (package, license_id, license_family, license_files)
Expand Down
2 changes: 0 additions & 2 deletions tests/test_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from conda_build import api
from .utils import metadata_dir

pytestmark = pytest.mark.usefixtures("api_default_testing_config")


@pytest.mark.sanity
def test_recipe_test(testing_workdir, testing_config):
Expand Down
2 changes: 0 additions & 2 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from conda_build.utils import DEFAULT_SUBDIRS
from conda_build.metadata import _hash_dependencies

pytestmark = pytest.mark.usefixtures("api_default_testing_config")


def test_uses_vcs_in_metadata(testing_workdir, testing_metadata):
testing_metadata._meta_path = os.path.join(testing_workdir, 'meta.yaml')
Expand Down
2 changes: 0 additions & 2 deletions tests/test_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

from .utils import add_mangling, metadata_dir

pytestmark = pytest.mark.usefixtures("api_default_testing_config")


def test_compile_missing_pyc(testing_workdir):
good_files = ['f1.py', 'f3.py']
Expand Down
2 changes: 0 additions & 2 deletions tests/test_published_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from conda_build.utils import check_call_env
from .utils import metadata_dir, is_valid_dir

pytestmark = pytest.mark.usefixtures("api_default_testing_config")

published_examples = os.path.join(os.path.dirname(metadata_dir), 'published_code')


Expand Down
2 changes: 0 additions & 2 deletions tests/test_subpackages.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

from .utils import subpackage_dir, is_valid_dir

pytestmark = pytest.mark.usefixtures("api_default_testing_config")


@pytest.fixture(params=[dirname for dirname in os.listdir(subpackage_dir)
if is_valid_dir(subpackage_dir, dirname)])
Expand Down
2 changes: 0 additions & 2 deletions tests/test_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from conda_build import api, exceptions, variants
from conda_build.utils import package_has_file

pytestmark = pytest.mark.usefixtures("api_default_testing_config")

thisdir = os.path.dirname(__file__)
recipe_dir = os.path.join(thisdir, 'test-recipes', 'variants')

Expand Down

0 comments on commit efe302f

Please sign in to comment.