1
+ """Tests for the `rm` command."""
2
+
3
+ from pathlib import Path
4
+ from typing import List
5
+
1
6
import pytest
7
+ from _pytest .capture import CaptureFixture
2
8
from pypdf import PdfReader
3
9
4
10
from .conftest import RESOURCES_ROOT , chdir , run_cli
11
+ from .test_cat import extract_embedded_images
5
12
6
13
7
- def test_rm_incorrect_number_of_args (capsys , tmp_path ):
14
+ def test_rm_incorrect_number_of_args (
15
+ capsys : CaptureFixture , tmp_path : Path
16
+ ) -> None :
8
17
with chdir (tmp_path ):
9
18
exit_code = run_cli (["rm" , str (RESOURCES_ROOT / "box.pdf" )])
10
19
assert exit_code == 2
11
20
captured = capsys .readouterr ()
12
21
assert "Missing argument" in captured .err
13
22
14
23
15
- def test_rm_subset_ok (capsys , tmp_path ) :
24
+ def test_rm_subset_ok (capsys : CaptureFixture , tmp_path : Path ) -> None :
16
25
with chdir (tmp_path ):
17
26
exit_code = run_cli (
18
27
[
@@ -37,7 +46,9 @@ def test_rm_subset_ok(capsys, tmp_path):
37
46
"page_range" ,
38
47
["a" , "-" , "1-" , "1-1-1" , "1:1:1:1" ],
39
48
)
40
- def test_rm_subset_invalid_args (capsys , tmp_path , page_range ):
49
+ def test_rm_subset_invalid_args (
50
+ capsys : CaptureFixture , tmp_path : Path , page_range : str
51
+ ) -> None :
41
52
with chdir (tmp_path ):
42
53
exit_code = run_cli (
43
54
[
@@ -53,8 +64,9 @@ def test_rm_subset_invalid_args(capsys, tmp_path, page_range):
53
64
assert "Invalid file path or page range provided" in captured .err
54
65
55
66
56
- @pytest .mark .skip (reason = "This check is not implemented yet" )
57
- def test_rm_subset_warn_on_missing_pages (capsys , tmp_path ):
67
+ def test_rm_subset_warn_on_missing_pages (
68
+ capsys : CaptureFixture , tmp_path : Path
69
+ ) -> None :
58
70
with chdir (tmp_path ):
59
71
exit_code = run_cli (
60
72
[
@@ -67,11 +79,12 @@ def test_rm_subset_warn_on_missing_pages(capsys, tmp_path):
67
79
)
68
80
captured = capsys .readouterr ()
69
81
assert exit_code == 0 , captured
70
- assert "WARN" in captured .out
82
+ assert "WARN" in captured .err
71
83
72
84
73
- @pytest .mark .xfail ()
74
- def test_rm_subset_ensure_reduced_size (tmp_path , two_pages_pdf_filepath ):
85
+ def test_rm_subset_ensure_reduced_size (
86
+ tmp_path : Path , two_pages_pdf_filepath : Path
87
+ ) -> None :
75
88
exit_code = run_cli (
76
89
[
77
90
"rm" ,
@@ -101,15 +114,12 @@ def test_rm_subset_ensure_reduced_size(tmp_path, two_pages_pdf_filepath):
101
114
assert len (embedded_images ) == 1
102
115
103
116
104
- def extract_embedded_images (pdf_filepath ):
105
- images = []
106
- reader = PdfReader (pdf_filepath )
107
- for page in reader .pages :
108
- images .extend (page .images )
109
- return images
110
-
111
-
112
- def test_rm_combine_files (pdf_file_100 , pdf_file_abc , tmp_path , capsys ):
117
+ def test_rm_combine_files (
118
+ pdf_file_100 : Path ,
119
+ pdf_file_abc : Path ,
120
+ tmp_path : Path ,
121
+ capsys : CaptureFixture ,
122
+ ) -> None :
113
123
with chdir (tmp_path ):
114
124
output_pdf_path = tmp_path / "out.pdf"
115
125
@@ -135,8 +145,7 @@ def test_rm_combine_files(pdf_file_100, pdf_file_abc, tmp_path, capsys):
135
145
# Extract text from the original and modified PDFs
136
146
extracted_pages = []
137
147
reader = PdfReader (output_pdf_path )
138
- for page in reader .pages :
139
- extracted_pages .append (page .extract_text ())
148
+ extracted_pages = [page .extract_text () for page in reader .pages ]
140
149
141
150
# Compare the extracted text
142
151
l1 = [str (el ) for el in list (range (0 , 10 , 2 )) + list (range (10 , 100 ))]
@@ -187,7 +196,13 @@ def test_rm_combine_files(pdf_file_100, pdf_file_abc, tmp_path, capsys):
187
196
("::-1" , []),
188
197
],
189
198
)
190
- def test_rm_commands (pdf_file_100 , capsys , tmp_path , page_range , expected ):
199
+ def test_rm_commands (
200
+ pdf_file_100 : Path ,
201
+ capsys : CaptureFixture ,
202
+ tmp_path : Path ,
203
+ page_range : str ,
204
+ expected : List [str ],
205
+ ) -> None :
191
206
with chdir (tmp_path ):
192
207
output_pdf_path = tmp_path / "out.pdf"
193
208
@@ -208,8 +223,7 @@ def test_rm_commands(pdf_file_100, capsys, tmp_path, page_range, expected):
208
223
# Extract text from the original and modified PDFs
209
224
extracted_pages = []
210
225
reader = PdfReader (output_pdf_path )
211
- for page in reader .pages :
212
- extracted_pages .append (page .extract_text ())
226
+ extracted_pages = [page .extract_text () for page in reader .pages ]
213
227
214
228
# Compare the extracted text
215
229
assert extracted_pages == expected
0 commit comments