Skip to content

Commit

Permalink
Add a test checking for CONDA-installed binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
willGraham01 committed Aug 21, 2023
1 parent e2e6ff8 commit f3b3b11
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 34 deletions.
24 changes: 0 additions & 24 deletions .github/workflows/tmate_debug.yaml

This file was deleted.

22 changes: 12 additions & 10 deletions src/brainreg/backend/niftyreg/niftyreg_binaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

_IS_WINDOWS_OS = os_system_name == "Windows"

def conda_install_path() -> Optional[Path]:
packaged_binaries_folder = (
Path(__file__).parent.parent.parent / "bin" / "nifty_reg"
)


def conda_niftyreg_path() -> Optional[Path]:
"""
If a conda install of niftyreg is available, return the directory
containing the niftyreg binaries.
Expand Down Expand Up @@ -46,7 +51,7 @@ def conda_install_path() -> Optional[Path]:
return None


_CONDA_INSTALL_PATH = conda_install_path()
_CONDA_NIFTYREG_BINARY_PATH = conda_niftyreg_path()


def get_binary(program_name: str) -> Path:
Expand All @@ -56,17 +61,14 @@ def get_binary(program_name: str) -> Path:
If niftyreg is installed via conda, use those binaries, otherwise fall
back on bundled binaries.
"""
if _CONDA_INSTALL_PATH is not None:
bin_path = _CONDA_INSTALL_PATH / program_name
if _CONDA_NIFTYREG_BINARY_PATH is not None:
bin_path = _CONDA_NIFTYREG_BINARY_PATH / program_name
else:
binaries_folder = (
Path(__file__).parent.parent.parent / "bin" / "nifty_reg"
)
bin_path = Path(binaries_folder) / os_folder_name / program_name

bin_path = packaged_binaries_folder / os_folder_name / program_name

# Append exe label to Windows executables
# It looks like subprocess is actually able to cope without the .exe appended,
# but just to be safe we'll include it on Windows OS calls
if _IS_WINDOWS_OS:
bin_path = bin_path.parent / f"{bin_path.name}.exe"
bin_path = bin_path.parent / f"{bin_path.stem}.exe"
return bin_path
Empty file.
68 changes: 68 additions & 0 deletions tests/tests/test_backend/test_niftyreg_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os
from pathlib import Path
from typing import Tuple

import pytest

from brainreg.backend.niftyreg.niftyreg_binaries import (
_CONDA_NIFTYREG_BINARY_PATH,
get_binary,
)
from brainreg.backend.niftyreg.niftyreg_binaries import (
packaged_binaries_folder,
)


def packaged_binaries_are_used() -> Tuple[bool, Path]:
"""
Return a (bool, Path) tuple which has the values:
boolean:
TRUE if the get_binary function points to one of the niftyreg binaries
that are included with the package.
FALSE otherwise.
Path:
The path to the folder containing the niftyreg binaries that were found
"""
using_binary = get_binary("reg_aladin")
return (
packaged_binaries_folder in using_binary.parents,
using_binary.parent,
)


def test_conda_with_niftyreg():
"""
Check the following:
- If we are not in a conda environment, the packaged niftyreg binaries are used by the backend
- If we are in a conda environment and niftyreg is not conda-installed, the packaged niftyreg binaries are used by the backend
- If we are in a conda environment and niftyreg IS conda-installed, the conda-installed binaries are used by the backend.
"""
if "CONDA_PREFIX" not in os.environ:
# We are not in a conda envrionment
# _CONDA_NIFTYREG_BINARY_PATH should be none
assert (
_CONDA_NIFTYREG_BINARY_PATH is None
), f"Not in a conda environment but _CONDA_NIFTYREG_BINARY_PATH is non-None: {_CONDA_NIFTYREG_BINARY_PATH}"

using_packaged_binaries, bin_folder = packaged_binaries_are_used()
assert (
using_packaged_binaries
), f"There is no CONDA_PREFIX, but non-packaged binaries in {bin_folder} are being used!"
else:
# We are in a conda environment.
# Either this environment does not have niftyreg installed, or it does.
if _CONDA_NIFTYREG_BINARY_PATH is None:
# Apparently niftyreg is not installed in this environment
# Assert the located binaries are the packaged ones
using_packaged_binaries, bin_folder = packaged_binaries_are_used()
assert (
using_packaged_binaries
), f"Conda environment without niftyreg installed exists, but non-packaged binaries in {bin_folder} are being used."
else:
# We are in a conda environment that _has_ niftyreg conda-installed
# Assert that we are using the CONDA-installed binaries
using_packaged_binaries, bin_folder = packaged_binaries_are_used()
assert (
not using_packaged_binaries,
f"Packaged binaries are being used despite appearing to be installed by CONDA at {_CONDA_NIFTYREG_BINARY_PATH}",
)

0 comments on commit f3b3b11

Please sign in to comment.