From 51094a1a18e75c9d1544989e86b5a1914c28b144 Mon Sep 17 00:00:00 2001 From: scito Date: Thu, 29 Dec 2022 23:17:31 +0100 Subject: [PATCH] use PathLike type instead of str | Path --- .github/workflows/ci.yml | 4 ++-- Pipfile | 2 ++ extract_otp_secret_keys.py | 3 ++- requirements-dev.txt | 1 + upgrade_deps.sh | 4 ++-- utils.py | 13 +++++++++---- 6 files changed, 18 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ccaf9cb..a097568 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,5 +49,5 @@ jobs: mypy --install-types --non-interactive *.py mypy --strict *.py if: matrix.python-version == '3.x' - - name: Test with pytest - run: pytest + - name: Test with pytest (with code coverage) + run: pytest --cov=test_extract_otp_secret_keys_pytest diff --git a/Pipfile b/Pipfile index e5dd662..d428d9d 100644 --- a/Pipfile +++ b/Pipfile @@ -12,6 +12,8 @@ opencv-python = "*" [dev-packages] pytest = "*" +pytest-mock = "*" +pytest-cov = "*" wheel = "*" flake8 = "*" pylint = "*" diff --git a/extract_otp_secret_keys.py b/extract_otp_secret_keys.py index 711c350..6410054 100644 --- a/extract_otp_secret_keys.py +++ b/extract_otp_secret_keys.py @@ -76,10 +76,11 @@ Exception: {e}""") except ImportError: qreader_available = False +# TODO Workaround for PYTHON < 3.10: Union[int, None] used instead of int | None + # Types Args = argparse.Namespace OtpUrl = str -# Workaround for PYTHON < 3.10: use Union[int, None] instead of int | None Otp = TypedDict('Otp', {'name': str, 'secret': str, 'issuer': str, 'type': str, 'counter': Union[int, None], 'url': OtpUrl}) Otps = list[Otp] OtpUrls = list[OtpUrl] diff --git a/requirements-dev.txt b/requirements-dev.txt index 4372796..573f275 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,5 +3,6 @@ pytest flake8 pylint pytest-mock +pytest-cov mypy types-protobuf diff --git a/upgrade_deps.sh b/upgrade_deps.sh index b2ed5b1..68bbe8d 100755 --- a/upgrade_deps.sh +++ b/upgrade_deps.sh @@ -238,11 +238,11 @@ eval "$cmd" # Test -cmd="pytest" +cmd="pytest --cov=test_extract_otp_secret_keys_pytest" if $interactive ; then askContinueYn "$cmd"; else echo -e "${cyan}$cmd${reset}";fi eval "$cmd" -cmd="$PIPENV run pytest" +cmd="$PIPENV run pytest --cov=test_extract_otp_secret_keys_pytest" if $interactive ; then askContinueYn "$cmd"; else echo -e "${cyan}$cmd${reset}";fi eval "$cmd" diff --git a/utils.py b/utils.py index 9c60695..84b39f0 100644 --- a/utils.py +++ b/utils.py @@ -22,7 +22,12 @@ import re import shutil import sys import pathlib -from typing import BinaryIO, Any +from typing import BinaryIO, Any, Union + + +# Types +# PYTHON < 3.10: Workaround for str | pathlib.Path +PathLike = Union[str, pathlib.Path] # Ref. https://stackoverflow.com/a/16571630 @@ -50,11 +55,11 @@ with Capturing() as output: sys.stderr = self._stderr -def file_exits(file: str | pathlib.Path) -> bool: +def file_exits(file: PathLike) -> bool: return os.path.isfile(file) -def remove_file(file: str | pathlib.Path) -> None: +def remove_file(file: PathLike) -> None: if file_exits(file): os.remove(file) @@ -63,7 +68,7 @@ def remove_files(glob_pattern: str) -> None: os.remove(f) -def remove_dir_with_files(dir: str | pathlib.Path) -> None: +def remove_dir_with_files(dir: PathLike) -> None: if os.path.exists(dir): shutil.rmtree(dir)