Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions tests/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"
SAMPLE_ROOT = PROJECT_ROOT / "sample-files"


def page_ops(pdf_path, password):
Expand Down Expand Up @@ -140,8 +139,8 @@ def text_extraction(pdf_path):
return text


def test_text_extraction(benchmark):
file_path = SAMPLE_ROOT / "009-pdflatex-geotopo/GeoTopo.pdf"
def test_text_extraction(benchmark, sample_files_dir):
file_path = sample_files_dir / "009-pdflatex-geotopo/GeoTopo.pdf"
benchmark(text_extraction, file_path)


Expand Down
40 changes: 39 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

import pytest

from pypdf import PdfReader, PdfWriter
from pypdf._page import PageObject

TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"


@pytest.fixture(scope="session")
Expand All @@ -18,3 +20,39 @@ def pdf_file_path(tmp_path_factory):
@pytest.fixture(scope="session")
def txt_file_path(tmp_path_factory):
return tmp_path_factory.mktemp("pypdf-data") / f"{uuid.uuid4()}.txt"


# Reusable PDF fixtures
@pytest.fixture
def crazyones_pdf_page_one(crazyones_pdf_reader) -> PageObject:
return crazyones_pdf_reader.pages[0]


@pytest.fixture
def crazyones_pdf_path(resources_dir) -> Path:
return resources_dir / "crazyones.pdf"


@pytest.fixture
def crazyones_pdf_reader(crazyones_pdf_path) -> PdfReader:
return PdfReader(crazyones_pdf_path)


@pytest.fixture
def crazyones_pdf_writer(crazyones_pdf_path) -> PdfWriter:
return PdfWriter(crazyones_pdf_path)


@pytest.fixture
def project_dir() -> Path:
return PROJECT_ROOT


@pytest.fixture
def resources_dir(project_dir) -> Path:
return project_dir / "resources"


@pytest.fixture
def sample_files_dir(project_dir) -> Path:
return project_dir / "sample-files"
10 changes: 3 additions & 7 deletions tests/generic/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import shutil
import subprocess
from io import BytesIO
from pathlib import Path

import pytest

Expand All @@ -24,16 +23,13 @@
)
from tests import get_data_from_url

TESTS_ROOT = Path(__file__).parent.parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
SAMPLE_ROOT = PROJECT_ROOT / "sample-files"

PDFATTACH_BINARY = shutil.which("pdfattach")


@pytest.mark.samples
@pytest.mark.skipif(PDFATTACH_BINARY is None, reason="Requires poppler-utils")
def test_embedded_file__basic(tmpdir):
clean_path = SAMPLE_ROOT / "002-trivial-libre-office-writer" / "002-trivial-libre-office-writer.pdf"
def test_embedded_file__basic(tmpdir, sample_files_dir):
clean_path = sample_files_dir / "002-trivial-libre-office-writer" / "002-trivial-libre-office-writer.pdf"
attached_path = tmpdir / "attached.pdf"
file_path = tmpdir / "test.txt"
file_path.write_binary(b"Hello World\n")
Expand Down
60 changes: 20 additions & 40 deletions tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@

from . import get_data_from_url

TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"


def test_ellipse(pdf_file_path):
def test_ellipse(pdf_file_path, crazyones_pdf_reader):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
reader = crazyones_pdf_reader
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
Expand All @@ -49,9 +44,9 @@ def test_ellipse(pdf_file_path):
writer.write(fp)


def test_text(pdf_file_path):
def test_text(pdf_file_path, resources_dir):
# Arrange
pdf_path = RESOURCE_ROOT / "outline-without-title.pdf"
pdf_path = resources_dir / "outline-without-title.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
Expand All @@ -70,10 +65,9 @@ def test_text(pdf_file_path):
writer.write(fp)


def test_free_text(pdf_file_path):
def test_free_text(pdf_file_path, crazyones_pdf_reader):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
reader = crazyones_pdf_reader
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
Expand Down Expand Up @@ -136,13 +130,10 @@ def test_annotation_dictionary():
assert a.flags == 123


def test_polygon(pdf_file_path):
def test_polygon(pdf_file_path, crazyones_pdf_page_one):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
writer.add_page(crazyones_pdf_page_one)

with pytest.raises(ValueError):
Polygon(
Expand All @@ -159,13 +150,10 @@ def test_polygon(pdf_file_path):
writer.write(fp)


def test_polyline(pdf_file_path):
def test_polyline(pdf_file_path, crazyones_pdf_page_one):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
writer.add_page(crazyones_pdf_page_one)

with pytest.raises(
ValueError,
Expand All @@ -185,13 +173,10 @@ def test_polyline(pdf_file_path):
writer.write(fp)


def test_line(pdf_file_path):
def test_line(pdf_file_path, crazyones_pdf_page_one):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
writer.add_page(crazyones_pdf_page_one)

# Act
line_annotation = Line(
Expand All @@ -207,13 +192,10 @@ def test_line(pdf_file_path):
writer.write(fp)


def test_rectangle(pdf_file_path):
def test_rectangle(pdf_file_path, crazyones_pdf_page_one):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
writer.add_page(crazyones_pdf_page_one)

# Act
square_annotation = Rectangle(
Expand All @@ -229,11 +211,9 @@ def test_rectangle(pdf_file_path):
writer.write(fp)


def test_highlight(pdf_file_path):
def test_highlight(pdf_file_path, crazyones_pdf_page_one):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
page = crazyones_pdf_page_one
writer = PdfWriter()
writer.add_page(page)

Expand Down Expand Up @@ -293,9 +273,9 @@ def test_highlight(pdf_file_path):
writer.write(fp)


def test_link(pdf_file_path):
def test_link(pdf_file_path, resources_dir):
# Arrange
pdf_path = RESOURCE_ROOT / "outline-without-title.pdf"
pdf_path = resources_dir / "outline-without-title.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
Expand Down Expand Up @@ -340,9 +320,9 @@ def test_link(pdf_file_path):
writer.write(fp)


def test_popup(caplog):
def test_popup(caplog, resources_dir):
# Arrange
pdf_path = RESOURCE_ROOT / "outline-without-title.pdf"
pdf_path = resources_dir / "outline-without-title.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
Expand Down
20 changes: 6 additions & 14 deletions tests/test_cmap.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
"""Test the pypdf_cmap module."""
from io import BytesIO
from pathlib import Path

import pytest

from pypdf import PdfReader, PdfWriter
from pypdf import PdfReader
from pypdf._cmap import get_encoding, parse_bfchar
from pypdf._codecs import charset_encoding
from pypdf._font import Font
from pypdf.generic import ArrayObject, DictionaryObject, IndirectObject, NameObject, NullObject

from . import get_data_from_url

TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"


@pytest.mark.enable_socket
@pytest.mark.slow
Expand Down Expand Up @@ -218,25 +213,22 @@ def test_eten_b5():
reader.pages[0].extract_text().startswith("1/7 \n富邦新終身壽險")


def test_missing_entries_in_cmap():
def test_missing_entries_in_cmap(crazyones_pdf_reader):
"""
Issue #2702: this issue is observed on damaged pdfs
use of this file in test has been discarded as too slow/long
we will create the same error from crazyones
"""
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
p = reader.pages[0]
p = crazyones_pdf_reader.pages[0]
p["/Resources"]["/Font"]["/F1"][NameObject("/ToUnicode")] = IndirectObject(
99999999, 0, reader
99999999, 0, crazyones_pdf_reader
)
p.extract_text()


def test_null_missing_width():
def test_null_missing_width(crazyones_pdf_writer):
"""For coverage of #2792"""
writer = PdfWriter(RESOURCE_ROOT / "crazyones.pdf")
page = writer.pages[0]
page = crazyones_pdf_writer.pages[0]
ft = page["/Resources"]["/Font"]["/F1"]
ft[NameObject("/Widths")] = ArrayObject()
ft["/FontDescriptor"][NameObject("/MissingWidth")] = NullObject()
Expand Down
9 changes: 2 additions & 7 deletions tests/test_codecs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Test LZW-related code."""
from io import BytesIO
from pathlib import Path

import pytest

Expand All @@ -10,10 +9,6 @@

from . import get_data_from_url

TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"

test_cases = [
pytest.param(b"", id="Empty input"),
pytest.param(b"A", id="Single character"),
Expand Down Expand Up @@ -68,8 +63,8 @@ def test_decode_lzw(encoded, expected_decoded):
assert actual_decoded == expected_decoded


def test_lzw_decoder_table_overflow(caplog):
path = RESOURCE_ROOT / "lzw_decoder_table_overflow.bin"
def test_lzw_decoder_table_overflow(caplog, resources_dir):
path = resources_dir / "lzw_decoder_table_overflow.bin"
codec = LzwCodec()
assert codec.decode(path.read_bytes()).startswith(
b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@'
Expand Down
21 changes: 7 additions & 14 deletions tests/test_doc_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import subprocess
from io import BytesIO
from operator import itemgetter
from pathlib import Path
from unittest import mock

import pytest
Expand All @@ -15,18 +14,14 @@
from pypdf.generic import EmbeddedFile, NameObject, NullObject, TextStringObject, ViewerPreferences
from tests import get_data_from_url

TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
SAMPLE_ROOT = PROJECT_ROOT / "sample-files"
RESOURCES_ROOT = PROJECT_ROOT / "resources"

PDFATTACH_BINARY = shutil.which("pdfattach")


@pytest.mark.samples
@pytest.mark.skipif(PDFATTACH_BINARY is None, reason="Requires poppler-utils")
def test_attachments(tmpdir):
def test_attachments(tmpdir, sample_files_dir):
# No attachments.
clean_path = SAMPLE_ROOT / "002-trivial-libre-office-writer" / "002-trivial-libre-office-writer.pdf"
clean_path = sample_files_dir / "002-trivial-libre-office-writer" / "002-trivial-libre-office-writer.pdf"
with PdfReader(clean_path) as pdf:
assert pdf._list_attachments() == []
assert list(pdf.attachment_list) == []
Expand Down Expand Up @@ -171,8 +166,8 @@ def test_byte_encoded_named_destinations():
}


def test_viewer_preferences__indirect_reference():
input_path = RESOURCES_ROOT / "git.pdf"
def test_viewer_preferences__indirect_reference(resources_dir):
input_path = resources_dir / "git.pdf"
reader = PdfReader(input_path)
assert (0, 24) not in reader.resolved_objects
viewer_preferences = reader.viewer_preferences
Expand Down Expand Up @@ -452,10 +447,8 @@ def test_outline__issue3462():
]


def test_flatten__cyclic_references():
path = RESOURCES_ROOT / "crazyones.pdf"

reader = PdfReader(path)
def test_flatten__cyclic_references(crazyones_pdf_reader):
reader = crazyones_pdf_reader
assert len(reader.pages) == 1
reader._flatten()

Expand Down
Loading
Loading