From f3b3b116daecc4f88032c13bcb88531d2b82e1f2 Mon Sep 17 00:00:00 2001 From: willGraham01 <1willgraham@gmail.com> Date: Mon, 21 Aug 2023 11:48:48 +0100 Subject: [PATCH] Add a test checking for CONDA-installed binaries --- .github/workflows/tmate_debug.yaml | 24 ------- .../backend/niftyreg/niftyreg_binaries.py | 22 +++--- tests/tests/test_backend/__init__.py | 0 .../test_backend/test_niftyreg_detection.py | 68 +++++++++++++++++++ 4 files changed, 80 insertions(+), 34 deletions(-) delete mode 100644 .github/workflows/tmate_debug.yaml create mode 100644 tests/tests/test_backend/__init__.py create mode 100644 tests/tests/test_backend/test_niftyreg_detection.py diff --git a/.github/workflows/tmate_debug.yaml b/.github/workflows/tmate_debug.yaml deleted file mode 100644 index 34bb669..0000000 --- a/.github/workflows/tmate_debug.yaml +++ /dev/null @@ -1,24 +0,0 @@ -name: Windows Tmate Debug -on: - workflow_dispatch: - -jobs: - build: - runs-on: windows-latest - steps: - - uses: actions/checkout@v3 - - - uses: mamba-org/setup-micromamba@v1 - with: - init-shell: >- - bash - powershell - environment-name: br-debug - create-args: >- - python=3.9 - niftyreg - - # Enable tmate debugging of manually-triggered workflows - - name: Setup tmate session - uses: mxschmitt/action-tmate@v3 - timeout-minutes: 30 diff --git a/src/brainreg/backend/niftyreg/niftyreg_binaries.py b/src/brainreg/backend/niftyreg/niftyreg_binaries.py index 24d2665..0230c6e 100644 --- a/src/brainreg/backend/niftyreg/niftyreg_binaries.py +++ b/src/brainreg/backend/niftyreg/niftyreg_binaries.py @@ -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. @@ -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: @@ -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 diff --git a/tests/tests/test_backend/__init__.py b/tests/tests/test_backend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tests/test_backend/test_niftyreg_detection.py b/tests/tests/test_backend/test_niftyreg_detection.py new file mode 100644 index 0000000..ef38fa7 --- /dev/null +++ b/tests/tests/test_backend/test_niftyreg_detection.py @@ -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}", + )