From c345649c7d721e3bda090dece228e8b8250740e4 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Sun, 23 Oct 2022 09:54:19 +0200 Subject: [PATCH 1/3] feature: add selftest command for junifer --- junifer/api/cli.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/junifer/api/cli.py b/junifer/api/cli.py index a5888ccbd..6c1575976 100644 --- a/junifer/api/cli.py +++ b/junifer/api/cli.py @@ -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,16 @@ def wtf(long_: bool) -> None: "environment": _get_environment_information(long_=long_), } click.echo(yaml.dump(report, sort_keys=False)) + + +@cli.command() +def selftest() -> None: + """Selftest command for CLI.""" + subprocess.run( + ["pytest", "-vvv"], + stdin=subprocess.DEVNULL, + stdout=sys.stdout, + stderr=subprocess.STDOUT, + cwd=Path(__file__).parent.parent.parent.absolute(), + check=True, + ) -- 2.52.0 From e4caf171c38551b17d66353d39165a8ae2cdd216 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Mon, 24 Oct 2022 11:54:19 +0200 Subject: [PATCH 2/3] update: make junifer selftest take argument for specific sub-package tests --- junifer/api/cli.py | 71 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/junifer/api/cli.py b/junifer/api/cli.py index 6c1575976..fb8656dd0 100644 --- a/junifer/api/cli.py +++ b/junifer/api/cli.py @@ -222,13 +222,64 @@ def wtf(long_: bool) -> None: @cli.command() -def selftest() -> None: - """Selftest command for CLI.""" - subprocess.run( - ["pytest", "-vvv"], - stdin=subprocess.DEVNULL, - stdout=sys.stdout, - stderr=subprocess.STDOUT, - cwd=Path(__file__).parent.parent.parent.absolute(), - check=True, - ) +@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") -- 2.52.0 From a8007eb962f8914990a5ac7fac0f38cea5782950 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Mon, 24 Oct 2022 11:54:56 +0200 Subject: [PATCH 3/3] update: add tests for junifer selftest --- junifer/api/tests/test_cli.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/junifer/api/tests/test_cli.py b/junifer/api/tests/test_cli.py index e2259c581..8e90dc765 100644 --- a/junifer/api/tests/test_cli.py +++ b/junifer/api/tests/test_cli.py @@ -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 -- 2.52.0