Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add --format Option for Custom Page Sizes in x2pdf Command #65

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Test unit returned as a separate command
mulla028 committed Oct 31, 2024
commit 0cb1d6e70b046d8c5fc4b818df801d9b5f0ebd19
42 changes: 41 additions & 1 deletion tests/test_x2pdf.py
Original file line number Diff line number Diff line change
@@ -28,4 +28,44 @@ def test_x2pdf(capsys, tmp_path: Path) -> None:
captured = capsys.readouterr()
assert exit_code == 0, captured
assert captured.out == ""
assert output.exists()
assert output.exists()

def test_x2pdf_with_format(capsys, tmp_path: Path) -> None:
# Arrange
output = tmp_path / "out.pdf"
assert not output.exists()

formats_to_test = [
"Letter",
"A4-portrait",
"A4-landscape",
"210x297",
"invalid-format"
]

for format_option in formats_to_test:
# Act
exit_code = run_cli(
[
"x2pdf",
"sample-files/003-pdflatex-image/page-0-Im1.jpg",
"--output",
str(output),
"--format",
format_option,
]
)

# Assert
captured = capsys.readouterr()

# For valid formats, we expect a successful exit code and the output file to exist
if format_option != "invalid-format":
assert exit_code == 0, captured
assert captured.out == ""
assert output.exists()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be interesting to also validate the resulting pages dimensions.

This can be checked with pdfly pagemeta $pdf_filepath $page_index

Or using the underlying pypdf library: PdfReader(pdf: Path).mediabox/.cropbox/.artbox/.bleedbox

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you did not handle that feedback comment @mulla028 🙂

else:
# For an invalid format, we expect a non-zero exit code (indicating failure)
assert exit_code != 0
assert "Invalid format" in captured.err # Check for expected error message
output.unlink(missing_ok=True) # Clean up for the next test iteration