Skip to content

Commit

Permalink
Mock out calls to conda-lock for test_solve_lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
peytondmurray committed Jun 12, 2024
1 parent 87f8ffc commit c689447
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions conda-store-server/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import subprocess
import sys

from unittest import mock

import pytest
import yaml
import yarl

from fastapi.responses import RedirectResponse
Expand All @@ -21,6 +24,7 @@
server,
utils,
)
from conda_store_server.action import generate_lockfile
from conda_store_server.server.auth import DummyAuthentication


Expand Down Expand Up @@ -70,14 +74,54 @@ def test_function(context):
"simple_specification_with_pip",
],
)
def test_solve_lockfile(conda_store, specification, request):
@mock.patch.object(generate_lockfile, "yaml", wraps=yaml)
@mock.patch("conda_store_server.action.generate_lockfile.logged_command")
@mock.patch("conda_store_server.action.generate_lockfile.run_lock")
def test_solve_lockfile(
mock_run_lock,
mock_logged_command,
mock_yaml,
conda_store,
specification,
request,
):
"""Test that the call to conda_lock.run_lock is formed correctly.
Mock out logged_command, which is used to call `conda info`; this is an
extremely slow call, and we aren't testing any aspect of it here.
Mock out the yaml package, it is used to load the lockfile normally
generated by conda-lock.
"""

# Dump dummy data to the expected lockfile output location
def run_lock_side_effect(lockfile_path, **kwargs):
with open(lockfile_path, "w") as f:
yaml.dump({"foo": "bar"}, f)

mock_run_lock.side_effect = run_lock_side_effect

platforms = [conda_utils.conda_platform()]
specification = request.getfixturevalue(specification)
context = action.action_solve_lockfile(
if specification.variables is None:
cuda_version = None
else:
cuda_version = specification.variables.get("CONDA_OVERRIDE_CUDA")

generate_lockfile.action_solve_lockfile(
conda_command=conda_store.conda_command,
specification=specification,
platforms=[conda_utils.conda_platform()],
platforms=platforms,
)
assert len(context.result["package"]) != 0

# Check that the call to `conda_lock` is correctly formed
mock_run_lock.assert_called_once()
call_args = mock_run_lock.call_args_list[0][1]
assert str(call_args["environment_files"][0]).endswith("environment.yaml")
assert call_args["platforms"] == platforms
assert str(call_args["lockfile_path"]).endswith("conda-lock.yaml")
assert call_args["conda_exe"] == conda_store.conda_command
assert call_args["with_cuda"] == cuda_version


def test_solve_lockfile_valid_conda_flags(conda_store, simple_specification):
Expand Down

0 comments on commit c689447

Please sign in to comment.