Skip to content

Commit

Permalink
Keep working
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo committed May 8, 2024
1 parent a8d18fc commit 3a3c285
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build:
apt_packages:
- graphviz
tools:
python: "3.10"
python: "3.11"

sphinx:
configuration: docs/conf.py
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ authors = [{name = "The NiPreps Developers", email = "[email protected]"}]
classifiers = [
"Development Status :: 2 - Pre-Alpha",
"Programming Language :: Python",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dependencies = [
"fmriprep",
Expand Down
2 changes: 1 addition & 1 deletion src/fmripost_aroma/data/tests/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ templateflow_version = "0.4.2"
version = "20.0.1"

[execution]
bids_dir = "ds000005/"
bids_dir = "/data"
bids_description_hash = "5d42e27751bbc884eca87cb4e62b9a0cca0cd86f8e578747fe89b77e6c5b21e5"
boilerplate_only = false
fs_license_file = "/opt/freesurfer/license.txt"
Expand Down
84 changes: 84 additions & 0 deletions src/fmripost_aroma/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""Fixtures for the CircleCI tests."""

import base64
import os

import pytest


def pytest_addoption(parser):
"""Collect pytest parameters for running tests."""
parser.addoption(
"--working_dir",
action="store",
default=(
"/usr/local/miniconda/lib/python3.11/site-packages/fmripost_aroma/fmripost_aroma/"
"tests/data/test_data/run_pytests/work"
),
)
parser.addoption(
"--data_dir",
action="store",
default=(
"/usr/local/miniconda/lib/python3.11/site-packages/fmripost_aroma/fmripost_aroma/"
"tests/data/test_data"
),
)
parser.addoption(
"--output_dir",
action="store",
default=(
"/usr/local/miniconda/lib/python3.11/site-packages/fmripost_aroma/fmripost_aroma/"
"tests/data/test_data/run_pytests/out"
),
)


# Set up the commandline options as fixtures
@pytest.fixture(scope="session")
def data_dir(request):
"""Grab data directory."""
return request.config.getoption("--data_dir")


@pytest.fixture(scope="session")
def working_dir(request):
"""Grab working directory."""
workdir = request.config.getoption("--working_dir")
os.makedirs(workdir, exist_ok=True)
return workdir


@pytest.fixture(scope="session")
def output_dir(request):
"""Grab output directory."""
outdir = request.config.getoption("--output_dir")
os.makedirs(outdir, exist_ok=True)
return outdir


@pytest.fixture(scope="session", autouse=True)
def fslicense(working_dir):
"""Set the FreeSurfer license as an environment variable."""
FS_LICENSE = os.path.join(working_dir, "license.txt")
os.environ["FS_LICENSE"] = FS_LICENSE
LICENSE_CODE = (
"bWF0dGhldy5jaWVzbGFrQHBzeWNoLnVjc2IuZWR1CjIwNzA2CipDZmVWZEg1VVQ4clkKRlNCWVouVWtlVElDdwo="
)
with open(FS_LICENSE, "w") as f:
f.write(base64.b64decode(LICENSE_CODE).decode())


@pytest.fixture(scope="session")
def base_config():
from fmripost_aroma.tests.tests import mock_config

return mock_config


@pytest.fixture(scope="session")
def base_parser():
from argparse import ArgumentParser

parser = ArgumentParser(description="Test parser")
return parser
96 changes: 96 additions & 0 deletions src/fmripost_aroma/tests/run_local_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
"""Run tests locally by calling Docker."""
import argparse
import os
import subprocess


def _get_parser():
"""Parse command line inputs for tests.
Returns
-------
parser.parse_args() : argparse dict
"""
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
"-k",
dest="test_regex",
metavar="PATTERN",
type=str,
help="Test pattern.",
required=False,
default=None,
)
parser.add_argument(
"-m",
dest="test_mark",
metavar="LABEL",
type=str,
help="Test mark label.",
required=False,
default=None,
)
return parser


def run_command(command, env=None):
"""Run a given shell command with certain environment variables set.
Keep this out of the real xcp_d code so that devs don't need to install ASLPrep to run tests.
"""
merged_env = os.environ
if env:
merged_env.update(env)

process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
env=merged_env,
)
while True:
line = process.stdout.readline()
line = str(line, "utf-8")[:-1]
print(line)
if line == "" and process.poll() is not None:
break

if process.returncode != 0:
raise RuntimeError(
f"Non zero return code: {process.returncode}\n" f"{command}\n\n{process.stdout.read()}"
)


def run_tests(test_regex, test_mark):
"""Run the tests."""
local_patch = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
mounted_code = "/usr/local/miniconda/lib/python3.11/site-packages/fmripost_aroma"
run_str = "docker run --rm -ti "
run_str += f"-v {local_patch}:{mounted_code} "
run_str += "--entrypoint pytest "
run_str += "nipreps/fmripost_aroma:unstable "
run_str += (
f"{mounted_code}/fmripost_aroma "
f"--data_dir={mounted_code}/fmripost_aroma/tests/test_data "
f"--output_dir={mounted_code}/fmripost_aroma/tests/pytests/out "
f"--working_dir={mounted_code}/fmripost_aroma/tests/pytests/work "
)
if test_regex:
run_str += f"-k {test_regex} "
elif test_mark:
run_str += f"-rP -o log_cli=true -m {test_mark} "

run_command(run_str)


def _main(argv=None):
"""Run the tests."""
options = _get_parser().parse_args(argv)
kwargs = vars(options)
run_tests(**kwargs)


if __name__ == "__main__":
_main()
File renamed without changes.
70 changes: 0 additions & 70 deletions src/fmripost_aroma/workflows/tests/__init__.py

This file was deleted.

0 comments on commit 3a3c285

Please sign in to comment.