Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pytest-cov and test validate_file_name #34

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ test.py
.vscode
.idea
.tox
.coverage

dist/
.pdm-python

# Sphinx documentation
docs/build/
docs/build/

!tests/dump/*
160 changes: 159 additions & 1 deletion pdm.lock

Large diffs are not rendered by default.

14 changes: 1 addition & 13 deletions pydumpling/cli.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import argparse
import os.path

from .debug_dumpling import debug_dumpling, load_dumpling
from .helpers import print_traceback_and_except
from .helpers import print_traceback_and_except, validate_file_name
from .rpdb import r_post_mortem

DUMP_FILE_EXTENSION: str = ".dump"


def validate_file_name(file_name: str) -> str:
"""check file extension name and exists"""
if not file_name.endswith(DUMP_FILE_EXTENSION):
raise argparse.ArgumentTypeError("File must be .dump file")
if not os.path.exists(file_name):
raise argparse.ArgumentTypeError(f"File {file_name} not found")
return file_name


parser = argparse.ArgumentParser(
description="pydumpling cli tools",
Expand Down
14 changes: 14 additions & 0 deletions pydumpling/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import sys
import os.path
import argparse
from traceback import print_exception, print_tb

from .pydumpling import save_dumping


DUMP_FILE_EXTENSION: str = ".dump"


def print_traceback_and_except(dumpling_result):
exc_tb = dumpling_result["traceback"]
except_extra = dumpling_result.get("exc_extra")
Expand All @@ -23,3 +28,12 @@ def _hook(exc_type, exc_value, exc_tb):
original_hook(exc_type, exc_value, exc_tb) # call sys original hook

sys.excepthook = _hook


def validate_file_name(file_name: str) -> str:
"""check file extension name and exists"""
if not file_name.endswith(DUMP_FILE_EXTENSION):
raise argparse.ArgumentTypeError("File must be .dump file")
if not os.path.exists(file_name):
raise argparse.ArgumentTypeError(f"File {file_name} not found")
return file_name
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
distribution = true

[tool.pdm.scripts]
test = {composite = ["pdm install", "flake8 ./pydumpling ./tests", "pytest tests/"]}
test = {composite = ["pdm install", "flake8 ./pydumpling ./tests", "pytest --cov=pydumpling --cov-report=term-missing tests/"]}
docs = {shell = "cd docs && make html"} # build sphinx docs
docs_export = { shell = "pdm export -G doc -o docs/requirements.txt --without-hashes" } # export requirements for docs
docs_preview = {shell = 'python -m http.server -d docs\build\html'}
Expand All @@ -14,6 +14,7 @@ includes = ["pydumpling/*.py"]
test = [
"pytest-order>=1.2.0",
"flake8>=5.0.4",
"pytest-cov>=4.1.0",
]
dev = [
"tox-pdm>=0.6.1",
Expand Down
Empty file.
15 changes: 15 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest
from argparse import ArgumentTypeError

from pydumpling.helpers import validate_file_name


def test_validate_file_name():
dump_file = "./tests/dump/validate_file_name.dump"
assert validate_file_name(dump_file) == dump_file

with pytest.raises(ArgumentTypeError, match="File must be .dump file"):
validate_file_name("test.txt")

with pytest.raises(ArgumentTypeError, match="File missing.dump not found"):
validate_file_name("missing.dump")
Loading