Skip to content

Prevent Markdown reporter crash when max_length is None#858

Open
PeterDaveHello wants to merge 1 commit intothp:masterfrom
PeterDaveHello:fix-markdown-max-length
Open

Prevent Markdown reporter crash when max_length is None#858
PeterDaveHello wants to merge 1 commit intothp:masterfrom
PeterDaveHello:fix-markdown-max-length

Conversation

@PeterDaveHello
Copy link
Contributor

Guard the None max_length case so markdown reporter no longer raises TypeError on enablement. Add reporter tests for unlimited and trimmed paths to prevent regression.

GitHub Copilot summary:

This pull request introduces a minor fix to the MarkdownReporter.submit method and adds new unit tests to ensure correct handling of the max_length parameter. The main focus is to prevent errors when max_length is None and to verify that report trimming works as expected.

Testing improvements:

  • Added a new test file lib/urlwatch/tests/test_reporters.py with unit tests for MarkdownReporter.submit, including scenarios where max_length is None and where output trimming occurs.

Bug fix:

  • Updated MarkdownReporter.submit in lib/urlwatch/reporters.py to only subtract the trimmed message length from max_length if max_length is not None, preventing possible errors when max_length is unspecified.

job_state = _DummyJobState(diff="line1\nline2\nline3")
reporter = MarkdownReporter(report, report.config["report"]["markdown"], [job_state], datetime.timedelta())

output = list(reporter.submit(max_length=10))
Copy link
Owner

Choose a reason for hiding this comment

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

Wouldn't max_length in the function become negative?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It only goes negative if a very small max_length is passed. This is because submit() first does max_length -= len(trimmed_msg) and the trimmed_msg string is about 58 chars, so max_length=10 becomes -48. Even then it just trims and does not crash. If you want to avoid that impression, I can change the test to use a value >= 60.

Copy link
Owner

@thp thp left a comment

Choose a reason for hiding this comment

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

In which situation would max_length actually become None?

@PeterDaveHello
Copy link
Contributor Author

Agreed that a plain CLI run won't hit this. The crash only happens when the Markdown reporter output is actually consumed (Python/hook/extension usage): with default max_length=None, submit() subtracts from None. This is a minimal guard and does not change behavior when a length is provided.

Copy link
Owner

@thp thp left a comment

Choose a reason for hiding this comment

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

The more I think about it, the more I think that max_length -= len(trimmed_msg) should be done later (only when trimmed is truthy). If it isn't trimmed, we still want to have the full max_length, and if it is trimmed, something like [...] or [trimmed] might be better to allow for more "payload" and less "boilerplate". In particular, if max_length < len(trimmed_msg), then technically we wouldn't be able to show anything, not even the summary or the details. And if the "trimmed message" is quite small (e.g. "[...]" - 5 characters).

Then again, it's probably the _render() method that should do all deciding and value adding (possibly with an additional return value before the footer), so that it knows what the "real" max length is, and either fit the message in there, or trim it.

In other words:

The message "Hello world" (11 characters) and trimmed_msg = "[...]" (5 characters)

with max_length == 11 should still be:

"Hello world"

but with max_length == 10, should be:

"Hello[...]"

Right now, with max_length == 11, it would be:

"Hello [...]"

Reserve trim notice space only when output is trimmed, keeping
exact-length output intact and avoiding None subtraction. Use a short
marker when the notice exceeds max_length. Expand tests for trimming
notice, fallback marker, tiny/zero limits, total length checks, and
exact-length output.
@PeterDaveHello PeterDaveHello force-pushed the fix-markdown-max-length branch from 4f5f740 to ce8e240 Compare December 28, 2025 19:08
@PeterDaveHello PeterDaveHello requested a review from thp December 28, 2025 19:08
trimmed_marker = "[...]\n"
if len(trimmed_marker) > max_length:
trimmed_marker = ""
reserved_length = max_length - len(trimmed_marker) if trimmed_marker else max_length
Copy link
Owner

Choose a reason for hiding this comment

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

If trimmed_marker is the empty string (the only way if trimmed_marker should evaluate to falsy), then len(trimmed_marker) == 0, so the result is the same.

Suggested change
reserved_length = max_length - len(trimmed_marker) if trimmed_marker else max_length
reserved_length = max_length - len(trimmed_marker)

if len(trimmed_marker) > max_length:
trimmed_marker = ""
reserved_length = max_length - len(trimmed_marker) if trimmed_marker else max_length
trimmed, summary, details, footer = MarkdownReporter._render(
Copy link
Owner

Choose a reason for hiding this comment

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

It's unfortunate that we have to call MarkdownReporter._render() twice here. Can't MarkdownReporter._render() contain all that trimming logic?

Copy link
Owner

@thp thp left a comment

Choose a reason for hiding this comment

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

See comments. Ideally call MarkdownReporter._render() only once and let it figure out trimming (since MarkdownReporter._render() already returns the trimmed status, might as well return trimmed_msg instead (being the empty string if not trimmed or not enough space).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants