|
| 1 | +import pymupdf |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +g_root = os.path.normpath(f'{__file__}/../../') |
| 9 | +g_root = os.path.relpath(g_root) |
| 10 | + |
| 11 | +sys.path.insert(0, g_root) |
| 12 | +try: |
| 13 | + import setup |
| 14 | +finally: |
| 15 | + del sys.path[0] |
| 16 | + |
| 17 | + |
| 18 | +def _file_line(path, text, re_match, offset=+2): |
| 19 | + ''' |
| 20 | + Returns <file>:<line> for location of regex match. |
| 21 | + |
| 22 | + path: |
| 23 | + filename. |
| 24 | + text: |
| 25 | + Contents of <filename>. |
| 26 | + re_match: |
| 27 | + A re.Match. |
| 28 | + offset: |
| 29 | + Added to line number of start of <re_match>. Default offset=2 is |
| 30 | + because callers usually grep for leading newline, and line numbers are |
| 31 | + generally 1-based. |
| 32 | + ''' |
| 33 | + text_before = text[:re_match.start()] |
| 34 | + line = text_before.count('\n') + offset |
| 35 | + return f'{path}:{line}' |
| 36 | + |
| 37 | + |
| 38 | +def test_release_versions(): |
| 39 | + ''' |
| 40 | + PyMuPDF and default MuPDF must have same major.minor version. |
| 41 | + ''' |
| 42 | + version_p_tuple = [int(i) for i in setup.version_p.split('.')] |
| 43 | + version_mupdf_tuple = [int(i) for i in setup.version_mupdf.split('.')] |
| 44 | + assert version_p_tuple[:2] == version_mupdf_tuple[:2], \ |
| 45 | + f'PyMuPDF and MuPDF major.minor versions do not match. {setup.version_p=} {setup.version_mupdf=}.' |
| 46 | + |
| 47 | + |
| 48 | +def test_release_bug_template(): |
| 49 | + ''' |
| 50 | + Bug report template must list current PyMuPDF version. |
| 51 | + ''' |
| 52 | + p = f'{g_root}/.github/ISSUE_TEMPLATE/bug_report.yml' |
| 53 | + expected = f'\n - {setup.version_p}\n' |
| 54 | + with open(p) as f: |
| 55 | + text = f.read() |
| 56 | + assert expected in text, f'{p}:1: Failed to find line for {setup.version_p=}, {expected!r}.' |
| 57 | + |
| 58 | + |
| 59 | +def test_release_changelog_version(): |
| 60 | + ''' |
| 61 | + In changes.txt, first mentioned of MuPDF must match setup.version_mupdf. |
| 62 | + ''' |
| 63 | + # In changes.txt, first item must match setup.version_p. |
| 64 | + p = f'{g_root}/changes.txt' |
| 65 | + with open(p) as f: |
| 66 | + text = f.read() |
| 67 | + m = re.search(f'\n[*][*]Changes in version ([0-9.]+)[*][*]\n', text) |
| 68 | + assert m, f'Cannot parse {p}.' |
| 69 | + assert m[1] == setup.version_p, \ |
| 70 | + f'{_file_line(p, text, m)}: Cannot find {setup.version_p=} in first changelog item: {m[0].strip()!r}.' |
| 71 | + |
| 72 | + |
| 73 | +def test_release_changelog_mupdf_version(): |
| 74 | + ''' |
| 75 | + In changes.txt, first mentioned of MuPDF must match setup.version_mupdf. |
| 76 | + ''' |
| 77 | + p = f'{g_root}/changes.txt' |
| 78 | + with open(p) as f: |
| 79 | + text = f.read() |
| 80 | + m = re.search(f'\n[*] Use MuPDF-([0-9.]+)[.]\n', text) |
| 81 | + assert m, f'Cannot parse {p}.' |
| 82 | + assert m[1] == setup.version_mupdf, \ |
| 83 | + f'{_file_line(p, text, m)}: First mentioned MuPDF version does not match {setup.version_mupdf=}: {m[0].strip()!r}.' |
0 commit comments