Debian Codegraph @

tests/utils/check_warnings.py

raw
1 """Check the list of warnings produced by a doc build."""
2
3 import sys
4
5 from pathlib import Path
6
7 from colorama import Fore, init
8
9 from pydata_sphinx_theme.utils import escape_ansi
10
11
12 # init colors for all plateforms
13 init()
14
15
16 def check_warnings(file: Path) -> bool:
17 """Check the list of warnings produced by a doc build.
18
19 Raise errors if there are unexpected ones and/or if some are missing.
20
21 Parameters:
22 file: the path to the generated warning.txt file from
23 the CI build
24
25 Returns:
26 0 if the warnings are all there
27 1 if some warning are not registered or unexpected
28 """
29 # print some log
30 print("\n=== Sphinx Warnings test ===\n")
31
32 # find the file where all the known warnings are stored
33 warning_file = Path(__file__).parents[1] / "warning_list.txt"
34 extra_warning_file = Path(__file__).parents[1] / "intermittent_warning_list.txt"
35
36 received_warnings = escape_ansi(file.read_text()).strip().split("\n")
37 expected_warnings = warning_file.read_text().strip().split("\n")
38 intermittent_warnings = extra_warning_file.read_text().strip().split("\n")
39 # filter out empty warnings (happens on notebooks for some reason)
40 received_warnings = list(filter(len, received_warnings))
41
42 print(
43 f'Checking build warnings in file: "{file}" and comparing to expected '
44 f'warnings defined in "{warning_file}" and "{extra_warning_file}"\n\n'
45 )
46
47 for exp_w in expected_warnings[::-1] + intermittent_warnings[::-1]:
48 found = False
49 for rec_w in received_warnings:
50 if exp_w in rec_w:
51 received_warnings.remove(rec_w)
52 if exp_w in expected_warnings:
53 expected_warnings.remove(exp_w)
54 elif exp_w in intermittent_warnings:
55 intermittent_warnings.remove(exp_w)
56 found = True
57 break
58 # alert only if an *always expected* warning wasn't raised (not intermittent)
59 if not found and exp_w not in intermittent_warnings:
60 print(f"{Fore.YELLOW}Warning was not raised: {Fore.RESET}{exp_w}\n")
61 # warn about unexpected warnings
62 for rec_w in received_warnings[::-1]:
63 print(f"{Fore.YELLOW}Unexpected warning: {Fore.RESET}{rec_w}\n")
64 return len(received_warnings) or len(expected_warnings)
65
66
67 if __name__ == "__main__":
68 # cast the file to path and resolve to an absolute one
69 file = Path.cwd() / "warnings.txt"
70
71 # execute the test
72 sys.exit(check_warnings(file))