Skip to content

Commit a3c2b77

Browse files
julian-smith-artifex-comJorjMcKie
authored andcommitted
tests/conftest.py: also check that tests do not modify pymupdf._globals.
This is causing obscure test failure of new test_md_styles().
1 parent 85f40c1 commit a3c2b77

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import os
23
import platform
34
import sys
@@ -45,6 +46,17 @@ def get_fds():
4546

4647
JM_annot_id_stem = pymupdf.JM_annot_id_stem
4748

49+
def get_members(a):
50+
ret = dict()
51+
for n in dir(a):
52+
if not n.startswith('_'):
53+
v = getattr(a, n)
54+
ret[n] = v
55+
return ret
56+
57+
# Allow post-test checking that pymupdf._globals has not changed.
58+
_globals_pre = get_members(pymupdf._globals)
59+
4860
# Run the test.
4961
rep = yield
5062

@@ -59,6 +71,11 @@ def get_fds():
5971

6072
assert not pymupdf.TOOLS.set_small_glyph_heights()
6173

74+
_globals_post = get_members(pymupdf._globals)
75+
if _globals_post != _globals_pre:
76+
print(f'Test has changed pymupdf._globals from {_globals_pre=} to {_globals_post=}')
77+
assert 0
78+
6279
log_items = pymupdf._log_items()
6380
assert not log_items, f'log() was called; {len(log_items)=}.'
6481

@@ -84,3 +101,22 @@ def get_fds():
84101
if next_fd_after != next_fd_before:
85102
print(f'Test has leaked fds, {next_fd_before=} {next_fd_after=}. {args=} {kwargs=}.')
86103
#assert 0, f'Test has leaked fds, {next_fd_before=} {next_fd_after=}. {args=} {kwargs=}.'
104+
105+
if 0:
106+
# This code can be useful to track down test failures caused by other
107+
# tests modifying global state.
108+
#
109+
# We run a particular test menually after each test returns.
110+
sys.path.insert(0, os.path.dirname(__file__))
111+
try:
112+
import test_tables
113+
finally:
114+
del sys.path[0]
115+
print(f'### Calling test_tables.test_md_styles().')
116+
try:
117+
test_tables.test_md_styles()
118+
except Exception as e:
119+
print(f'### test_tables.test_md_styles() failed: {e}')
120+
raise
121+
else:
122+
print(f'### test_tables.test_md_styles() passed.')

0 commit comments

Comments
 (0)