Skip to content

Commit

Permalink
Improve validation error messages (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
mportesdev authored Feb 27, 2025
1 parent 99f3ea6 commit 7b777d2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 17 deletions.
40 changes: 40 additions & 0 deletions changelog.d/20250212_155835_michalportes1_error_messages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Removed
- A bullet item for the Removed category.
-->
<!--
### Added
- A bullet item for the Added category.
-->
### Changed

- More descriptive validation error messages

<!--
### Deprecated
- A bullet item for the Deprecated category.
-->
<!--
### Fixed
- A bullet item for the Fixed category.
-->
<!--
### Security
- A bullet item for the Security category.
-->
6 changes: 4 additions & 2 deletions src/pictureshow/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ def _validate_page_size(page_size, landscape):
f' please use one of: {", ".join(PAGE_SIZES)}'
) from err

page_size_error = PageSizeError('two positive numbers expected')
page_size_error = PageSizeError(
f'expected two positive numbers, got {page_size!r}'
)
try:
page_width, page_height = page_size
if page_width <= 0 or page_height <= 0:
Expand All @@ -170,7 +172,7 @@ def _validate_color(color):

@staticmethod
def _validate_layout(layout):
layout_error = LayoutError('two positive integers expected')
layout_error = LayoutError(f'expected two positive integers, got {layout!r}')
try:
if isinstance(layout, str):
layout = tuple(int(s) for s in DELIMITER.split(layout))
Expand Down
24 changes: 14 additions & 10 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,26 @@ def test_multiple_pictures_layout(self, capsys, new_pdf, layout, num_pages, page
assert f'Saved 6 pictures ({pages}) to ' in std_out
assert_pdf(new_pdf, num_pages=num_pages)

@pytest.mark.parametrize(
'layout',
(
pytest.param('1', id='invalid format'),
pytest.param('0x1', id='invalid value'),
),
)
def test_invalid_layout_throws_error(self, capsys, new_pdf, layout):
argv = f'{PIC_FILE} -o {new_pdf} -l {layout}'.split()
def test_invalid_layout_format_raises_error(self, capsys, new_pdf):
argv = f'{PIC_FILE} -o {new_pdf} -l 1'.split()
with pytest.raises(SystemExit) as exc:
main(argv)
exit_code = exc.value.args[0]
std_err = capsys.readouterr().err

assert exit_code == 2
assert "LayoutError: expected two positive integers, got '1'" in std_err
assert not new_pdf.exists()

def test_invalid_layout_value_raises_error(self, capsys, new_pdf):
argv = f'{PIC_FILE} -o {new_pdf} -l 0x1'.split()
with pytest.raises(SystemExit) as exc:
main(argv)
exit_code = exc.value.args[0]
std_err = capsys.readouterr().err

assert exit_code == 2
assert 'LayoutError: two positive integers expected' in std_err
assert "LayoutError: expected two positive integers, got '0x1'" in std_err
assert not new_pdf.exists()

def test_existing_target_file_throws_error(self, capsys, existing_pdf):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_pictures_to_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ def test_layout(new_pdf):


def test_invalid_layout_raises_error(new_pdf):
with pytest.raises(LayoutError, match='two positive integers expected'):
with pytest.raises(
LayoutError, match=r'expected two positive integers, got \(1, 0\)'
):
pictures_to_pdf(PIC_FILE, output_file=new_pdf, layout=(1, 0))

assert not new_pdf.exists()
Expand Down
14 changes: 10 additions & 4 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ def test_landscape_unchanged(self, page_size, expected):
),
)
def test_invalid_page_size_raises_error(self, page_size):
with pytest.raises(PageSizeError, match='two positive numbers expected'):
with pytest.raises(
PageSizeError, match=r'expected two positive numbers, got \(.+\)'
):
PictureShow()._validate_page_size(page_size, landscape=False)

def test_invalid_page_size_name_raises_error(self):
Expand Down Expand Up @@ -302,8 +304,10 @@ def test_str(self, layout, expected):
pytest.param((1, 1.0), id='invalid type'),
),
)
def test_invalid_layout_raises_error(self, layout):
with pytest.raises(LayoutError, match='two positive integers expected'):
def test_invalid_layout_tuple_raises_error(self, layout):
with pytest.raises(
LayoutError, match=r'expected two positive integers, got \(.+\)'
):
PictureShow()._validate_layout(layout)

@pytest.mark.parametrize(
Expand All @@ -314,7 +318,9 @@ def test_invalid_layout_raises_error(self, layout):
),
)
def test_invalid_layout_str_raises_error(self, layout):
with pytest.raises(LayoutError, match='two positive integers expected'):
with pytest.raises(
LayoutError, match="expected two positive integers, got '.+'"
):
PictureShow()._validate_layout(layout)


Expand Down

0 comments on commit 7b777d2

Please # to comment.