Skip to content

Commit

Permalink
Run Pytests across all supported Python versions (PennLINC#279)
Browse files Browse the repository at this point in the history
* Try this.

* Update config.yml

* Try to work around parallel approach.

* Update config.yml

* Update config.yml

* Update config.yml

* Update config.yml

* Update config.yml

* Update config.yml

* Update config.yml

* Update config.yml

* Update config.yml

* Add merge_coverage job.

* Update config.yml

* Update config.yml

* Whoops

* Update config.yml

* Update config.yml

* Update config.yml

* Update pyproject.toml

* update

* Skip the copilot tests.

* Update config.yml

* Update pyproject.toml
  • Loading branch information
tsalo authored Feb 1, 2024
1 parent 2b8ad8d commit c2e6cb8
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 32 deletions.
80 changes: 49 additions & 31 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
version: 2.1
orbs:
codecov: codecov/[email protected]

jobs:
set_up_conda:
run_pytests:
parameters:
python_version:
type: string
default: "3.8"
machine:
image: ubuntu-2004:202201-02
working_directory: /home/circleci/src/CuBIDS
steps:
- checkout:
path: /home/circleci/src/CuBIDS

- run:
name: install miniconda
name: Test CuBIDS
command: |
export MINICONDA=/tmp/miniconda
export PATH="$MINICONDA/bin:$PATH"
Expand All @@ -17,7 +25,9 @@ jobs:
conda config --set always_yes yes
conda update conda
conda info -a
conda create -n cubids python=3.9 pip
export PATH=/tmp/miniconda/bin:$PATH
conda create -n cubids python=<< parameters.python_version >> pip
source activate cubids
conda install -c conda-forge -y datalad
Expand All @@ -26,39 +36,40 @@ jobs:
npm install -g yarn && \
npm install -g bids-validator
# Install CuBIDS
pip install -e .[tests]
# Run tests
pytest --cov-append --cov-report term-missing --cov=cubids cubids
mkdir /home/circleci/src/coverage
mv /home/circleci/src/CuBIDS/.coverage /home/circleci/src/coverage/.coverage.<< parameters.python_version >>
- persist_to_workspace:
root: /tmp
root: /home/circleci/src/coverage
paths:
- miniconda
- .coverage.<< parameters.python_version >>

run_pytests:
machine:
image: ubuntu-2004:202201-02
working_directory: /home/circleci/src/CuBIDS
merge_coverage:
docker:
- image: continuumio/miniconda3
steps:
- checkout:
path: /home/circleci/src/CuBIDS

- attach_workspace:
at: /tmp

at: /home/circleci/src/coverage
- run:
name: Test CuBIDS
name: Merge coverage files
command: |
export PATH=/tmp/miniconda/bin:$PATH
source activate cubids
# Install CuBIDS
pip install -e .[tests]
# Reinstall bids-validator
sudo apt update
sudo apt install nodejs npm
npm install -g yarn && \
npm install -g bids-validator
conda create -n cov python=3.9 pip coverage
source activate cov
# Run tests
pytest cubids
cd /home/circleci/src/coverage/
echo "Merge coverage files"
coverage combine --data-file=/home/circleci/src/coverage/.coverage
echo "Generate XML report"
coverage xml --data-file /home/circleci/src/coverage/.coverage -o /home/circleci/src/coverage/coverage.xml
echo "Upload to Codecov"
- codecov/upload:
file: /home/circleci/src/coverage/coverage.xml

deployable:
docker:
Expand All @@ -84,20 +95,27 @@ jobs:
command: python -m twine upload -u __token__ -p ${PYPI_TOKEN} dist/cubids*

workflows:
version: 2
build_test_deploy:
jobs:
- set_up_conda:
- run_pytests:
# Define the matrix for Python versions
matrix:
parameters:
python_version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
filters:
tags:
only: /.*/

- run_pytests:
- merge_coverage:
requires:
- set_up_conda
- run_pytests
filters:
tags:
only: /.*/
ignore: /.*/

- deployable:
requires:
Expand Down
2 changes: 2 additions & 0 deletions cubids/tests/coverage.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[coverage:run]
parallel = true
82 changes: 82 additions & 0 deletions cubids/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
This file contains unit tests for the command-line interface (CLI) of the CuBIDS package.
The tests cover the following functions:
- _path_exists: Tests whether a given path exists or not.
- _is_file: Tests whether a given path is a file or a directory.
- _get_parser: Tests the creation and configuration of the argument parser.
- _main: Tests the main function of the CLI.
Each test case includes assertions to verify the expected behavior of the corresponding function.
Author: [Your Name]
Date: [Current Date]
"""

import argparse
import pytest

from cubids.cli import _path_exists, _is_file, _get_parser, _main


def _test_path_exists():
"""Test whether a given path exists or not.
This function tests the `_path_exists` function by providing a path that exists
and a path that does not exist.
It asserts that the function returns the expected path when the path exists,
and raises an `argparse.ArgumentTypeError` when the path does not exist.
"""
assert _path_exists("/path/to/existing/file", None) == "/path/to/existing/file"

with pytest.raises(argparse.ArgumentTypeError):
_path_exists("/path/to/nonexistent/file", None)


def _test_is_file():
"""Test whether a given path is a file or a directory.
This function tests the `_is_file` function by providing a path that is a file
and a path that is a directory.
It asserts that the function returns the expected path when the path is a file,
and raises an `argparse.ArgumentTypeError` when the path is a directory.
"""
assert _is_file("/path/to/file.txt", None) == "/path/to/file.txt"

with pytest.raises(argparse.ArgumentTypeError):
_is_file("/path/to/directory", None)


def _test_get_parser():
"""Test the creation and configuration of the argument parser.
This function tests the `_get_parser` function by asserting that the returned object is an
instance of `argparse.ArgumentParser`, and that it has the expected `prog` and `description`
attributes.
Additional assertions can be added to test the configuration of the parser.
"""
parser = _get_parser()
assert isinstance(parser, argparse.ArgumentParser)
assert parser.prog == "cubids"
assert parser.description == "Console script for cubids"
# Add more assertions for the parser configuration


def _test_main():
"""Test the main function of the CLI.
This function tests the `_main` function by providing different sets of arguments.
It asserts that the function returns the expected exit code (0 or 1) based on the provided
arguments.
"""
# Test the main function with valid arguments
argv = ["validate", "/path/to/dataset"]
assert _main(argv) == 0

# Test the main function with invalid arguments
argv = ["invalid-command", "/path/to/dataset"]
assert _main(argv) == 1

# Test the main function with missing arguments
argv = []
assert _main(argv) == 1
Loading

0 comments on commit c2e6cb8

Please sign in to comment.