[ENH] Implement junifer selftest to allow the users to test the library themselves #9

Merged
synchon merged 3 commits from feature/selftest-cmd into main 2022-10-25 06:57:12 +00:00
2 changed files with 84 additions and 1 deletions

View file

@ -5,6 +5,9 @@
# License: AGPL
import pathlib
import subprocess
import sys
from pathlib import Path
from typing import Dict, List, Union
import click
@ -216,3 +219,67 @@ def wtf(long_: bool) -> None:
"environment": _get_environment_information(long_=long_),
}
click.echo(yaml.dump(report, sort_keys=False))
@cli.command()
@click.argument("subpkg", type=str)
def selftest(subpkg: str) -> None:
"""Selftest command for CLI.
Parameters
----------
subpkg : {"all", "api", "configs", "data", "datagrabber", "datareader",
"markers", "pipeline", "preprocess", "storage", "testing", "utils",
"stats"}
The sub-package to run tests for.
Raises
------
click.BadArgumentUsage
If `subpkg` is invalid.
"""
sub_packages = [
"all",
"api",
"configs",
"data",
"datagrabber",
"datareader",
"markers",
"pipeline",
"preprocess",
"storage",
"testing",
"tests",
"utils",
]
if subpkg not in sub_packages:
raise click.BadArgumentUsage(
f"Invalid value for argument `subpkg`: {subpkg}. "
f"Should be one of {sub_packages}"
)
if subpkg == "all":
completed_process = subprocess.run(
["pytest", "-vvv"],
stdin=subprocess.DEVNULL,
stdout=sys.stdout,
stderr=subprocess.STDOUT,
cwd=Path(__file__).parent.parent.parent.absolute(),
check=False,
)
else:
completed_process = subprocess.run(
["pytest", f"junifer/{subpkg}", "-vvv"],
stdin=subprocess.DEVNULL,
stdout=sys.stdout,
stderr=subprocess.STDOUT,
cwd=Path(__file__).parent.parent.parent.absolute(),
check=False,
)
if completed_process.returncode == 0:
click.secho("Successful.", fg="green")
else:
click.secho("Failure.", fg="red")

View file

@ -11,7 +11,7 @@ import pytest
import yaml
from click.testing import CliRunner
from junifer.api.cli import collect, run, wtf
from junifer.api.cli import collect, run, selftest, wtf
# Create click test runner
@ -84,3 +84,19 @@ def test_wtf_long() -> None:
wtf_result = runner.invoke(wtf, "--long")
# Check
assert wtf_result.exit_code == 0
def test_selftest_invalid_arg() -> None:
"""Test selftest failure for invalid argument value."""
# Invoke selftest command
selftest_result = runner.invoke(selftest, "abyss")
# Check
assert selftest_result.exit_code == 2
def test_selftest() -> None:
"""Test selftest."""
# Invoke selftest command
selftest_result = runner.invoke(selftest, "tests")
# Check; will result in 1 due to I/O descriptor manipulation
assert selftest_result.exit_code == 1