Skip to content

Commit 5ecfb98

Browse files
committed
Fix bugs in test rm file
1 parent 4349375 commit 5ecfb98

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

tests/test_rm.py

+36-22
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1+
"""Tests for the `rm` command."""
2+
3+
from pathlib import Path
4+
from typing import List
5+
16
import pytest
7+
from _pytest.capture import CaptureFixture
28
from pypdf import PdfReader
39

410
from .conftest import RESOURCES_ROOT, chdir, run_cli
11+
from .test_cat import extract_embedded_images
512

613

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:
817
with chdir(tmp_path):
918
exit_code = run_cli(["rm", str(RESOURCES_ROOT / "box.pdf")])
1019
assert exit_code == 2
1120
captured = capsys.readouterr()
1221
assert "Missing argument" in captured.err
1322

1423

15-
def test_rm_subset_ok(capsys, tmp_path):
24+
def test_rm_subset_ok(capsys: CaptureFixture, tmp_path: Path) -> None:
1625
with chdir(tmp_path):
1726
exit_code = run_cli(
1827
[
@@ -37,7 +46,9 @@ def test_rm_subset_ok(capsys, tmp_path):
3746
"page_range",
3847
["a", "-", "1-", "1-1-1", "1:1:1:1"],
3948
)
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:
4152
with chdir(tmp_path):
4253
exit_code = run_cli(
4354
[
@@ -53,8 +64,9 @@ def test_rm_subset_invalid_args(capsys, tmp_path, page_range):
5364
assert "Invalid file path or page range provided" in captured.err
5465

5566

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:
5870
with chdir(tmp_path):
5971
exit_code = run_cli(
6072
[
@@ -67,11 +79,12 @@ def test_rm_subset_warn_on_missing_pages(capsys, tmp_path):
6779
)
6880
captured = capsys.readouterr()
6981
assert exit_code == 0, captured
70-
assert "WARN" in captured.out
82+
assert "WARN" in captured.err
7183

7284

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:
7588
exit_code = run_cli(
7689
[
7790
"rm",
@@ -101,15 +114,12 @@ def test_rm_subset_ensure_reduced_size(tmp_path, two_pages_pdf_filepath):
101114
assert len(embedded_images) == 1
102115

103116

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:
113123
with chdir(tmp_path):
114124
output_pdf_path = tmp_path / "out.pdf"
115125

@@ -135,8 +145,7 @@ def test_rm_combine_files(pdf_file_100, pdf_file_abc, tmp_path, capsys):
135145
# Extract text from the original and modified PDFs
136146
extracted_pages = []
137147
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]
140149

141150
# Compare the extracted text
142151
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):
187196
("::-1", []),
188197
],
189198
)
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:
191206
with chdir(tmp_path):
192207
output_pdf_path = tmp_path / "out.pdf"
193208

@@ -208,8 +223,7 @@ def test_rm_commands(pdf_file_100, capsys, tmp_path, page_range, expected):
208223
# Extract text from the original and modified PDFs
209224
extracted_pages = []
210225
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]
213227

214228
# Compare the extracted text
215229
assert extracted_pages == expected

0 commit comments

Comments
 (0)