|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +from pypdf import PdfReader |
| 4 | + |
| 5 | +from .conftest import RESOURCES_ROOT, chdir, run_cli |
| 6 | + |
| 7 | + |
| 8 | +def test_up2_fewer_args(capsys, tmp_path): |
| 9 | + with chdir(tmp_path): |
| 10 | + exit_code = run_cli(["2-up", str(RESOURCES_ROOT / "box.pdf")]) |
| 11 | + assert exit_code == 2 |
| 12 | + captured = capsys.readouterr() |
| 13 | + assert "Missing argument" in captured.err |
| 14 | + |
| 15 | + |
| 16 | +def test_up2_extra_args(capsys, tmp_path): |
| 17 | + with chdir(tmp_path): |
| 18 | + exit_code = run_cli( |
| 19 | + [ |
| 20 | + "2-up", |
| 21 | + str(RESOURCES_ROOT / "box.pdf"), |
| 22 | + "./out.pdf", |
| 23 | + "./out2.pdf", |
| 24 | + ] |
| 25 | + ) |
| 26 | + |
| 27 | + assert exit_code == 2 |
| 28 | + captured = capsys.readouterr() |
| 29 | + assert "unexpected extra argument" in captured.err |
| 30 | + |
| 31 | + with chdir(tmp_path): |
| 32 | + assert not os.path.exists("out.pdf"), f"'out.pdf' should not exist." |
| 33 | + assert not os.path.exists("out2.pdf"), f"'out2.pdf' should not exist." |
| 34 | + |
| 35 | + |
| 36 | +def test_up2_8page_file(capsys, tmp_path): |
| 37 | + # Act |
| 38 | + with chdir(tmp_path): |
| 39 | + exit_code = run_cli( |
| 40 | + [ |
| 41 | + "2-up", |
| 42 | + str(RESOURCES_ROOT / "input8.pdf"), |
| 43 | + "./out.pdf", |
| 44 | + ] |
| 45 | + ) |
| 46 | + captured = capsys.readouterr() |
| 47 | + |
| 48 | + # Assert |
| 49 | + assert exit_code == 0, captured |
| 50 | + assert not captured.err |
| 51 | + in_reader = PdfReader(RESOURCES_ROOT / "input8.pdf") |
| 52 | + out_reader = PdfReader(tmp_path / "./out.pdf") |
| 53 | + |
| 54 | + assert len(in_reader.pages) == 8 |
| 55 | + assert len(out_reader.pages) == 4 |
| 56 | + |
| 57 | + in_width = in_reader.pages[0].mediabox.width |
| 58 | + in_height = in_reader.pages[0].mediabox.height |
| 59 | + out_width = out_reader.pages[0].mediabox.width |
| 60 | + out_height = out_reader.pages[0].mediabox.height |
| 61 | + |
| 62 | + assert out_width == 2 * in_width # PR #78 |
| 63 | + assert out_height == out_height |
0 commit comments