Coverage for src/pytest_xray/comment_limits.py: 100.0%
21 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-16 14:00 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-16 14:00 +0000
1"""Client-side payload policy, not a claimed Jira/Xray server limit."""
3from pytest_xray.exceptions import XrayError
5DEFAULT_COMMENT_LIMIT = 32_000
6TRUNCATION_NOTICE = '\n[TRUNCATED by pytest-jira-xray-plus; full text is in the local full-report archive.]'
9def bound_comments(results, limit, archive_path):
10 """Return a copy with bounded UTF-8 comments; require lossless retention first."""
11 oversized = [test for test in results['tests'] if len(test.get('comment', '').encode('utf-8')) > limit]
12 if oversized and not archive_path:
13 raise XrayError(
14 'Comment exceeds --xray-comment-limit; export withheld. '
15 'Set --xray-full-report=PATH to retain the complete payload before truncation.'
16 )
17 if archive_path:
18 from pytest_xray.file_publisher import FilePublisher
20 FilePublisher(archive_path).publish(results)
21 bounded = dict(results, tests=[])
22 for test in results['tests']:
23 item = dict(test)
24 comment = item.get('comment', '')
25 if len(comment.encode('utf-8')) > limit:
26 # Keep the inherited wiki noformat block balanced when shortening it.
27 closing = '{noformat}' if comment.startswith('{noformat:') and comment.endswith('{noformat}') else ''
28 suffix = TRUNCATION_NOTICE + closing
29 prefix = comment.encode('utf-8')[: limit - len(suffix.encode('utf-8'))].decode('utf-8', errors='ignore')
30 item['comment'] = prefix + suffix
31 bounded['tests'].append(item)
32 return bounded, len(oversized)