Skip to content

Commit

Permalink
Adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cimon Lucas (LCM) committed Oct 10, 2023
1 parent 470ecab commit d5c7465
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 128 deletions.
24 changes: 11 additions & 13 deletions .github/workflows/github-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,22 @@ 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

- name: Install pdfly
run: |
pip install .
run: pip install .

- name: Test with black
- name: Lint with black
run: black --check .

- name: Test with flake8
run: |
flake8 . --exclude build
- name: Lint with flake8
run: flake8 . --exclude build

- name: Test with mypy
run: |
mypy . --ignore-missing-imports --exclude build
- name: Lint with mypy
run: mypy . --ignore-missing-imports --exclude build

- name: Run tests
run: pytest -vv
12 changes: 6 additions & 6 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 @@ -22,27 +22,27 @@ repos:
# hooks:
# - id: mypy
- repo: https://github.com/psf/black
rev: 23.1.0
rev: 23.9.1
hooks:
- id: black
args: [--target-version, py36]
- repo: https://github.com/asottile/blacken-docs
rev: 1.13.0
rev: 1.16.0
hooks:
- id: blacken-docs
additional_dependencies: [black==22.1.0]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.254'
rev: 'v0.0.292'
hooks:
- id: ruff
args: [--fix]
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py36-plus]
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 6.1.0
hooks:
- id: flake8
args: ["--ignore", "E,W,F"]
39 changes: 26 additions & 13 deletions pdfly/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@
# see https://github.com/py-pdf/pypdf/LICENSE


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

from pypdf import PdfMerger, parse_filename_page_ranges
Expand All @@ -54,29 +53,43 @@
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]):
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(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
47 changes: 16 additions & 31 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,65 +8,50 @@ 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.5
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.1
pathspec==0.11.2
# via black
platformdirs==3.9.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
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
1 change: 1 addition & 0 deletions requirements/dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ black
pip-tools
pre-commit
pytest-cov
pytest-timeout
twine
wheel
Loading

0 comments on commit d5c7465

Please sign in to comment.