From 8545a85cbf6a49235577e35d58712b47d1a56c15 Mon Sep 17 00:00:00 2001 From: Yuta Hayashibe Date: Sun, 9 Oct 2022 23:56:39 +0900 Subject: [PATCH] Add test --- Makefile | 4 ++- poetry.lock | 84 ++++++++++++++++++++++++++++++++++++++++++++++- pyproject.toml | 1 + tests/test_cli.py | 28 ++++++++++++++++ whispering/cli.py | 30 ++++++++++++----- 5 files changed, 136 insertions(+), 11 deletions(-) create mode 100644 tests/test_cli.py diff --git a/Makefile b/Makefile index 2214929..2ba06cf 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,8 @@ isort: find $(TARGET_DIRS) | grep '\.py$$' | xargs isort --diff | diff /dev/null - pydocstyle: find $(TARGET_DIRS) | grep -v tests | xargs pydocstyle --ignore=D100,D101,D102,D103,D104,D105,D107,D203,D212 +pytest: + pytest yamllint: find . \( -name node_modules -o -name .venv \) -prune -o -type f -name '*.yml' -print \ @@ -21,7 +23,7 @@ yamllint: version_check: git tag | python ./scripts/check_version.py --toml pyproject.toml -i README.md --tags /dev/stdin -lint_python: flake8 black isort pydocstyle version_check +lint_python: flake8 black isort pydocstyle version_check pytest pyright: diff --git a/poetry.lock b/poetry.lock index 3f2b478..2d19eb1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,3 +1,17 @@ +[[package]] +name = "attrs" +version = "22.1.0" +description = "Classes Without Boilerplate" +category = "dev" +optional = false +python-versions = ">=3.5" + +[package.extras] +dev = ["cloudpickle", "coverage[toml] (>=5.0.2)", "furo", "hypothesis", "mypy (>=0.900,!=0.940)", "pre-commit", "pympler", "pytest (>=4.3.0)", "pytest-mypy-plugins", "sphinx", "sphinx-notfound-page", "zope.interface"] +docs = ["furo", "sphinx", "sphinx-notfound-page", "zope.interface"] +tests = ["cloudpickle", "coverage[toml] (>=5.0.2)", "hypothesis", "mypy (>=0.900,!=0.940)", "pympler", "pytest (>=4.3.0)", "pytest-mypy-plugins", "zope.interface"] +tests_no_zope = ["cloudpickle", "coverage[toml] (>=5.0.2)", "hypothesis", "mypy (>=0.900,!=0.940)", "pympler", "pytest (>=4.3.0)", "pytest-mypy-plugins"] + [[package]] name = "black" version = "22.8.0" @@ -150,6 +164,14 @@ category = "main" optional = false python-versions = ">=3.5" +[[package]] +name = "iniconfig" +version = "1.1.1" +description = "iniconfig: brain-dead simple config-ini parsing" +category = "dev" +optional = false +python-versions = "*" + [[package]] name = "isort" version = "5.10.1" @@ -227,6 +249,26 @@ python-versions = ">=3.7" docs = ["furo (>=2021.7.5b38)", "proselint (>=0.10.2)", "sphinx (>=4)", "sphinx-autodoc-typehints (>=1.12)"] test = ["appdirs (==1.4.4)", "pytest (>=6)", "pytest-cov (>=2.7)", "pytest-mock (>=3.6)"] +[[package]] +name = "pluggy" +version = "1.0.0" +description = "plugin and hook calling mechanisms for python" +category = "dev" +optional = false +python-versions = ">=3.6" + +[package.extras] +dev = ["pre-commit", "tox"] +testing = ["pytest", "pytest-benchmark"] + +[[package]] +name = "py" +version = "1.11.0" +description = "library with cross-python path, ini-parsing, io, code, log facilities" +category = "dev" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" + [[package]] name = "pycodestyle" version = "2.9.1" @@ -305,6 +347,26 @@ cffi = ">=0.6" [package.extras] numpy = ["numpy"] +[[package]] +name = "pytest" +version = "7.1.3" +description = "pytest: simple powerful testing with Python" +category = "dev" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +attrs = ">=19.2.0" +colorama = {version = "*", markers = "sys_platform == \"win32\""} +iniconfig = "*" +packaging = "*" +pluggy = ">=0.12,<2.0" +py = ">=1.8.2" +tomli = ">=1.0.0" + +[package.extras] +testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"] + [[package]] name = "PyYAML" version = "6.0" @@ -547,9 +609,13 @@ resolved_reference = "d18e9ea5dd2ca57c697e8e55f9e654f06ede25d0" [metadata] lock-version = "1.1" python-versions = ">=3.8,<3.11" -content-hash = "b37661ab8440224a082f728829d750084d1a80471292218fe3f05b515ebd6db2" +content-hash = "4be376481f536b81281e132b4b7a3a01d42b7639c3702b7ef9cc4a99908e51ab" [metadata.files] +attrs = [ + {file = "attrs-22.1.0-py2.py3-none-any.whl", hash = "sha256:86efa402f67bf2df34f51a335487cf46b1ec130d02b8d39fd248abfd30da551c"}, + {file = "attrs-22.1.0.tar.gz", hash = "sha256:29adc2665447e5191d0e7c568fde78b21f9672d344281d0c6e1ab085429b22b6"}, +] black = [ {file = "black-22.8.0-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:ce957f1d6b78a8a231b18e0dd2d94a33d2ba738cd88a7fe64f53f659eea49fdd"}, {file = "black-22.8.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:5107ea36b2b61917956d018bd25129baf9ad1125e39324a9b18248d362156a27"}, @@ -680,6 +746,10 @@ idna = [ {file = "idna-3.4-py3-none-any.whl", hash = "sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2"}, {file = "idna-3.4.tar.gz", hash = "sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4"}, ] +iniconfig = [ + {file = "iniconfig-1.1.1-py2.py3-none-any.whl", hash = "sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3"}, + {file = "iniconfig-1.1.1.tar.gz", hash = "sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32"}, +] isort = [ {file = "isort-5.10.1-py3-none-any.whl", hash = "sha256:6f62d78e2f89b4500b080fe3a81690850cd254227f27f75c3a0c491a1f351ba7"}, {file = "isort-5.10.1.tar.gz", hash = "sha256:e8443a5e7a020e9d7f97f1d7d9cd17c88bcb3bc7e218bf9cf5095fe550be2951"}, @@ -738,6 +808,14 @@ platformdirs = [ {file = "platformdirs-2.5.2-py3-none-any.whl", hash = "sha256:027d8e83a2d7de06bbac4e5ef7e023c02b863d7ea5d079477e722bb41ab25788"}, {file = "platformdirs-2.5.2.tar.gz", hash = "sha256:58c8abb07dcb441e6ee4b11d8df0ac856038f944ab98b7be6b27b2a3c7feef19"}, ] +pluggy = [ + {file = "pluggy-1.0.0-py2.py3-none-any.whl", hash = "sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3"}, + {file = "pluggy-1.0.0.tar.gz", hash = "sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159"}, +] +py = [ + {file = "py-1.11.0-py2.py3-none-any.whl", hash = "sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378"}, + {file = "py-1.11.0.tar.gz", hash = "sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719"}, +] pycodestyle = [ {file = "pycodestyle-2.9.1-py2.py3-none-any.whl", hash = "sha256:d1735fc58b418fd7c5f658d28d943854f8a849b01a5d0a1e6f3f3fdd0166804b"}, {file = "pycodestyle-2.9.1.tar.gz", hash = "sha256:2c9607871d58c76354b697b42f5d57e1ada7d261c261efac224b664affdc5785"}, @@ -803,6 +881,10 @@ PySoundFile = [ {file = "PySoundFile-0.9.0.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win_amd64.whl", hash = "sha256:d92afd505d395523200d5b7f217e409bae4639c90cc61e90832a57a5a0fb484a"}, {file = "PySoundFile-0.9.0.post1.tar.gz", hash = "sha256:43dd46a2afc0484c26930a7e59eef9365cee81bce7a4aadc5699f788f60d32c3"}, ] +pytest = [ + {file = "pytest-7.1.3-py3-none-any.whl", hash = "sha256:1377bda3466d70b55e3f5cecfa55bb7cfcf219c7964629b967c37cf0bda818b7"}, + {file = "pytest-7.1.3.tar.gz", hash = "sha256:4f365fec2dff9c1162f834d9f18af1ba13062db0c708bf7b946f8a5c76180c39"}, +] PyYAML = [ {file = "PyYAML-6.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d4db7c7aef085872ef65a8fd7d6d09a14ae91f691dec3e87ee5ee0539d516f53"}, {file = "PyYAML-6.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:9df7ed3b3d2e0ecfe09e14741b857df43adb5a3ddadc919a2d94fbdf78fea53c"}, diff --git a/pyproject.toml b/pyproject.toml index 65d465b..b5c2c26 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ isort = ">=5.10.1" flake8 = ">=5.0.4" pydocstyle = ">=6.1.1" toml = "^0.10.2" +pytest = "^7.1.3" [build-system] requires = ["poetry-core"] diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..a0702be --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + + +import sys +from unittest.mock import patch + +from whispering.cli import get_opts, is_valid_arg + + +def test_options(): + + invalid_args = [ + "--mode server --mic 0", + "--mode server --mic 1", + "--mode server --beam_size 3", + "--mode server --temperature 0", + "--mode server --allow-padding", + "--mode server --no-vad", + "--mode server --num_block 3", + "--mode mic --host 0.0.0.0", + "--mode mic --port 8000", + ] + + for invalid_arg in invalid_args: + with patch.object(sys, "argv", [""] + invalid_arg.split()): + opts = get_opts() + ok = is_valid_arg(opts) + assert ok is False, f"{invalid_arg} should be invalid" diff --git a/whispering/cli.py b/whispering/cli.py index 2f53a49..2d2f529 100644 --- a/whispering/cli.py +++ b/whispering/cli.py @@ -117,7 +117,6 @@ def get_opts() -> argparse.Namespace: group_ws = parser.add_argument_group("WebSocket options") group_ws.add_argument( "--host", - default="0.0.0.0", help="host of websocker server", ) group_ws.add_argument( @@ -231,17 +230,28 @@ def show_devices(): print(f"{i}: {device['name']}") -def check_invalid_arg(opts): - ngs = [] +def is_valid_arg(opts) -> bool: + keys = [] if opts.mode == Mode.server.value: - ngs = [ + keys = [ "mic", + "beam_size", + "temperature", "allow_padding", + "no-vad", ] - for ng in ngs: - if vars(opts).get(ng) not in {None, False}: - sys.stderr.write(f"{ng} is not accepted option for {opts.mode} mode\n") - sys.exit(1) + elif opts.mode == Mode.mic.value: + keys = [ + "host", + "port", + ] + + for key in keys: + _val = vars(opts).get(key) + if _val is not None and _val is not False: + sys.stderr.write(f"{key} is not accepted option for {opts.mode} mode\n") + return False + return True def main() -> None: @@ -262,7 +272,9 @@ def main() -> None: ): opts.mode = Mode.server.value - check_invalid_arg(opts) + if not is_valid_arg(opts): + sys.exit(1) + if opts.mode == Mode.client.value: assert opts.language is None assert opts.model is None