diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2023-03-29 18:00:10 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2023-03-29 19:01:43 +0100 |
commit | 72e3796c9e89946fd820fda96912dba2519e0828 (patch) | |
tree | 6fc8b6f81ebc72f233618781b24311c4d20e94f3 | |
parent | 10425e109b4160e309fa8f32eabd13576b3ab5c7 (diff) | |
download | paste-72e3796c9e89946fd820fda96912dba2519e0828.tar.gz paste-72e3796c9e89946fd820fda96912dba2519e0828.tar.xz paste-72e3796c9e89946fd820fda96912dba2519e0828.zip |
very basic cli test
-rw-r--r-- | paste/__main__.py | 16 | ||||
-rw-r--r-- | tests/test_cli.py | 12 |
2 files changed, 20 insertions, 8 deletions
diff --git a/paste/__main__.py b/paste/__main__.py index 6e684c4..7260516 100644 --- a/paste/__main__.py +++ b/paste/__main__.py @@ -1,9 +1,9 @@ +import sys from base64 import b64decode, b64encode from collections.abc import Iterator from contextlib import contextmanager from datetime import datetime, timezone from os import getenv -from sys import argv, stderr from wsgiref.simple_server import make_server from . import DB_PATH, application, db, store @@ -14,7 +14,7 @@ PROGRAM_NAME = "paste" def print_usage() -> None: print( f"Usage: {PROGRAM_NAME} [-h|serve|new-token|list-tokens|delete-token]", - file=stderr, + file=sys.stderr, ) @@ -36,7 +36,7 @@ Environment: PASTE_HOST The HTTP server host (default: localhost) PASTE_PORT The HTTP server port (default: 8080) PASTE_DB The path to the sqlite3 database (default: {DB_PATH})""", - file=stderr, + file=sys.stderr, ) @@ -49,7 +49,7 @@ def open_auth() -> Iterator[store.Auth]: yield store.Auth(conn) -def main(): +def main(argv=sys.argv): if len(argv) <= 1 or len(argv) == 2 and argv[1] == "serve": host = getenv("PASTE_HOST", "localhost") port = int(getenv("PASTE_PORT", "8080")) @@ -80,20 +80,20 @@ def main(): except ValueError: auth.delete_token_hash(bytes.fromhex(token)) except ValueError: - print("Malformed token", file=stderr) + print("Malformed token", file=sys.stderr) exit(1) except KeyError: - print("Token not found", file=stderr) + print("Token not found", file=sys.stderr) exit(1) elif argv[1] == "verify-token": with open_auth() as auth: try: if not auth.check_token(b64decode(argv[2])): - print("Token not found", file=stderr) + print("Token not found", file=sys.stderr) exit(1) print("Found") except ValueError: - print("Malformed token", file=stderr) + print("Malformed token", file=sys.stderr) if __name__ == "__main__": diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..6f3beb2 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,12 @@ +from paste.__main__ import main + + +def test_cli(monkeypatch, capfd): + monkeypatch.setattr("paste.__main__.db", None) + monkeypatch.setattr("paste.__main__.application", None) + monkeypatch.setattr("paste.__main__.store", None) + monkeypatch.setenv("PASTE_DB", "test_value") + + main(["paste", "--help"]) + _, err = capfd.readouterr() + assert "paste" in err |