[ENH] Implement junifer selftest to allow the users to test the library themselves #9
2 changed files with 84 additions and 1 deletions
|
|
@ -5,6 +5,9 @@
|
||||||
# License: AGPL
|
# License: AGPL
|
||||||
|
|
||||||
import pathlib
|
import pathlib
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
from typing import Dict, List, Union
|
from typing import Dict, List, Union
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
@ -216,3 +219,67 @@ def wtf(long_: bool) -> None:
|
||||||
"environment": _get_environment_information(long_=long_),
|
"environment": _get_environment_information(long_=long_),
|
||||||
}
|
}
|
||||||
click.echo(yaml.dump(report, sort_keys=False))
|
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")
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import pytest
|
||||||
import yaml
|
import yaml
|
||||||
from click.testing import CliRunner
|
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
|
# Create click test runner
|
||||||
|
|
@ -84,3 +84,19 @@ def test_wtf_long() -> None:
|
||||||
wtf_result = runner.invoke(wtf, "--long")
|
wtf_result = runner.invoke(wtf, "--long")
|
||||||
# Check
|
# Check
|
||||||
assert wtf_result.exit_code == 0
|
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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue