Skip to content

Commit

Permalink
TST: Adding unit tests (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C authored Oct 11, 2023
1 parent 6c50ae1 commit 0f786e8
Show file tree
Hide file tree
Showing 18 changed files with 366 additions and 154 deletions.
34 changes: 15 additions & 19 deletions .github/workflows/github-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,15 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Upgrade pip
run: |
python -m pip install --upgrade pip
run: python -m pip install --upgrade pip
- name: Install requirements (Python 3)
run: |
pip install -r requirements/ci.txt
run: pip install -r requirements/ci.txt -r requirements/dev.txt
- name: Install pdfly
run: |
pip install .
run: pip install .

- name: Test with black
run: black --check . --exclude sample-files
- name: Run tests
run: pytest -vv

- name: Test with mypy
run: |
mypy . --ignore-missing-imports --exclude build
codestyle:
name: Check code style issues
runs-on: ubuntu-20.04
Expand All @@ -65,19 +58,22 @@ jobs:
with:
path: '**/tests/pdf_cache/*'
key: cache-downloaded-files

- name: Upgrade pip
run: |
python -m pip install --upgrade pip
run: python -m pip install --upgrade pip
- name: Install requirements
run: |
pip install -r requirements/ci.txt
run: pip install -r requirements/ci.txt
- name: Install pdfly
run: |
pip install .
run: pip install .

- name: Lint with black
run: black --check --extend-exclude sample-files .
- name: Lint with mypy
run: mypy . --ignore-missing-imports --exclude build
- name: Test with ruff
run: |
echo `ruff --version`
ruff .
ruff pdfly/
package:
name: Build & verify package
Expand Down
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pre-commit run --all-files
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-ast
- id: check-byte-order-marker
Expand All @@ -28,12 +28,13 @@ repos:
- id: blacken-docs
additional_dependencies: [black==22.1.0]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.290
rev: v0.0.292
hooks:
- id: ruff
args: ['--fix']
exclude: "tests/"
- repo: https://github.com/asottile/pyupgrade
rev: v3.12.0
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py36-plus]
Expand Down
49 changes: 35 additions & 14 deletions pdfly/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,61 @@


import os
import sys
import traceback
from pathlib import Path
from sys import exit, stderr, stdout
from typing import List
from typing import List, Tuple

from pypdf import PdfMerger, parse_filename_page_ranges
from pypdf import PageRange, PdfMerger, parse_filename_page_ranges


def main(
filename: Path, fn_pgrgs: List[str], output: Path, verbose: bool
) -> None:
fn_pgrgs_l = list(fn_pgrgs)
fn_pgrgs_l.insert(0, str(filename))
filename_page_ranges = parse_filename_page_ranges(fn_pgrgs_l) # type: ignore
filename_page_ranges = parse_filepaths_and_pagerange_args(
filename, fn_pgrgs
)
if output:
output_fh = open(output, "wb")
else:
stdout.flush()
output_fh = os.fdopen(stdout.fileno(), "wb")
sys.stdout.flush()
output_fh = os.fdopen(sys.stdout.fileno(), "wb")

merger = PdfMerger()
in_fs = {}
try:
for filename, page_range in filename_page_ranges: # type: ignore
if verbose:
print(filename, page_range, file=stderr)
print(filename, page_range, file=sys.stderr)
if filename not in in_fs:
in_fs[filename] = open(filename, "rb")
merger.append(in_fs[filename], pages=page_range)
merger.write(output_fh)
except Exception:
print(traceback.format_exc(), file=stderr)
print(f"Error while reading {filename}", file=stderr)
exit(1)
merger.write(output_fh)
output_fh.close()
print(traceback.format_exc(), file=sys.stderr)
print(f"Error while reading {filename}", file=sys.stderr)
sys.exit(1)
finally:
output_fh.close()
# In 3.0, input files must stay open until output is written.
# Not closing the in_fs because this script exits now.


def parse_filepaths_and_pagerange_args(
filename: Path, fn_pgrgs: List[str]
) -> List[Tuple[Path, PageRange]]:
fn_pgrgs_l = list(fn_pgrgs)
fn_pgrgs_l.insert(0, str(filename))
filename_page_ranges, invalid_filepaths = [], []
for filename, page_range in parse_filename_page_ranges(fn_pgrgs_l): # type: ignore
if Path(filename).is_file():
filename_page_ranges.append((filename, page_range))
else:
invalid_filepaths.append(str(filename))
if invalid_filepaths:
print(
f"Invalid file path or page range provided: {' '.join(invalid_filepaths)}",
file=sys.stderr,
)
sys.exit(2)
return filename_page_ranges
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ pdfly = "pdfly.cli:entry_point"

[tool.pytest.ini_options]
addopts = "--disable-socket --doctest-modules --cov=. --cov-report html:tests/reports/coverage-html --cov-report term-missing --ignore=docs/ --durations=3 --timeout=30"
filterwarnings = ["error"]
doctest_encoding = "utf-8"
testpaths = ["tests"]

Expand Down
45 changes: 15 additions & 30 deletions requirements/ci.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with Python 3.7
# This file is autogenerated by pip-compile with Python 3.8
# by the following command:
#
# pip-compile requirements/ci.in
Expand All @@ -8,67 +8,52 @@ astor==0.8.1
# via flake8-simplify
attrs==23.1.0
# via flake8-bugbear
black==23.3.0
black==23.9.1
# via -r requirements/ci.in
click==8.1.7
# via black
flake8==5.0.4
flake8==6.1.0
# via
# -r requirements/ci.in
# flake8-bugbear
# flake8-comprehensions
# flake8-isort
# flake8-simplify
flake8-bugbear==23.3.12
flake8-bugbear==23.9.16
# via -r requirements/ci.in
flake8-comprehensions==3.13.0
flake8-comprehensions==3.14.0
# via -r requirements/ci.in
flake8-isort==6.0.0
flake8-isort==6.1.0
# via -r requirements/ci.in
flake8-simplify==0.20.0
flake8-simplify==0.21.0
# via -r requirements/ci.in
importlib-metadata==4.2.0
# via
# attrs
# click
# flake8
# flake8-comprehensions
# flake8-simplify
isort==5.11.5
isort==5.12.0
# via flake8-isort
mccabe==0.7.0
# via flake8
mypy==1.4.1
mypy==1.5.1
# via -r requirements/ci.in
mypy-extensions==1.0.0
# via
# black
# mypy
packaging==23.1
packaging==23.2
# via black
pathspec==0.11.2
# via black
platformdirs==3.10.0
platformdirs==3.11.0
# via black
pycodestyle==2.9.1
pycodestyle==2.11.0
# via flake8
pyflakes==2.5.0
pyflakes==3.1.0
# via flake8
ruff==0.0.287
ruff==0.0.292
# via -r requirements/ci.in
tomli==2.0.1
# via
# black
# mypy
typed-ast==1.5.5
# via
# black
# mypy
typing-extensions==4.7.1
typing-extensions==4.8.0
# via
# black
# importlib-metadata
# mypy
# platformdirs
zipp==3.15.0
# via importlib-metadata
3 changes: 3 additions & 0 deletions requirements/dev.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
black
pip-tools
pre-commit
pytest
pytest-cov
pytest-socket
pytest-timeout
twine
wheel
Loading

0 comments on commit 0f786e8

Please sign in to comment.