-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
test_cli.py
132 lines (107 loc) · 3.58 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""Unit tests for the pypdf CLI program."""
import os
import sys
from pathlib import Path
from subprocess import check_output
try:
from contextlib import chdir
except ImportError: # Fallback when not available (< Python 3.11):
from contextlib import contextmanager
@contextmanager
def chdir(dir_path):
"""Non thread-safe context manager to change the current working directory."""
cwd = Path.cwd()
os.chdir(dir_path)
try:
yield
finally:
os.chdir(cwd)
import pytest
from pypdf import PdfReader, __version__
from pypdf.__main__ import main
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"
def test_pypdf_cli_can_be_invoked_as_a_module():
stdout = check_output(
[sys.executable, "-m", "pypdf", "--help"] # noqa: S603
).decode()
assert "usage: pypdf [-h]" in stdout
def test_pypdf_cli_version(capsys):
main(["--version"])
captured = capsys.readouterr()
assert not captured.err
assert __version__ in captured.out
def test_cat_incorrect_number_of_args(capsys, tmp_path):
with pytest.raises(SystemExit), chdir(tmp_path):
main(["cat", str(RESOURCE_ROOT / "box.pdf")])
captured = capsys.readouterr()
assert (
"error: At least two PDF documents must be provided to the cat command"
in captured.err
)
def test_cat_ok(capsys, tmp_path):
with chdir(tmp_path):
main(
[
"cat",
str(RESOURCE_ROOT / "box.pdf"),
str(RESOURCE_ROOT / "jpeg.pdf"),
"out.pdf",
]
)
reader = PdfReader("out.pdf")
assert len(reader.pages) == 2
captured = capsys.readouterr()
assert not captured.err
def test_extract_images_jpg_png(capsys, tmp_path):
with chdir(tmp_path):
main(
[
"extract-images",
str(RESOURCE_ROOT / "GeoBase_NHNC1_Data_Model_UML_EN.pdf"),
]
)
captured = capsys.readouterr()
assert not captured.err
assert captured.out.strip().split("\n") == [
"Image extracted to Image7.jpg",
"Image extracted to Image21.png",
"Image extracted to Image81.png",
]
@pytest.mark.xfail() # There is currently a bug there
def test_extract_images_monochrome(capsys, tmp_path):
with chdir(tmp_path):
main(["extract-images", str(RESOURCE_ROOT / "box.pdf")])
captured = capsys.readouterr()
assert not captured.err
assert "Image extracted" in captured.out
def test_subset_ok(capsys, tmp_path):
with chdir(tmp_path):
main(
[
"subset",
str(RESOURCE_ROOT / "GeoBase_NHNC1_Data_Model_UML_EN.pdf"),
"1",
"4",
"14-16",
]
)
reader = PdfReader("subset.GeoBase_NHNC1_Data_Model_UML_EN.pdf")
assert len(reader.pages) == 5
captured = capsys.readouterr()
assert not captured.err
@pytest.mark.parametrize(
"page_range",
["", "a", "-", "-1", "1-", "1-1-1"],
)
def test_subset_invalid_args(capsys, tmp_path, page_range):
with pytest.raises(SystemExit), chdir(tmp_path):
main(["subset", str(RESOURCE_ROOT / "jpeg.pdf"), page_range])
captured = capsys.readouterr()
assert "error: Invalid page" in captured.err
def test_subset_warn_on_missing_pages(capsys, tmp_path):
with chdir(tmp_path):
main(["subset", str(RESOURCE_ROOT / "jpeg.pdf"), "2"])
captured = capsys.readouterr()
assert "WARN" in captured.out