File: check_warnings.py

package info (click to toggle)
pydata-sphinx-theme 0.16.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,088 kB
  • sloc: python: 2,796; javascript: 701; makefile: 42; sh: 12
file content (72 lines) | stat: -rw-r--r-- 2,539 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Check the list of warnings produced by a doc build."""

import sys

from pathlib import Path

from colorama import Fore, init

from pydata_sphinx_theme.utils import escape_ansi


# init colors for all plateforms
init()


def check_warnings(file: Path) -> bool:
    """Check the list of warnings produced by a doc build.

    Raise errors if there are unexpected ones and/or if some are missing.

    Parameters:
        file: the path to the generated warning.txt file from
            the CI build

    Returns:
        0 if the warnings are all there
        1 if some warning are not registered or unexpected
    """
    # print some log
    print("\n=== Sphinx Warnings test ===\n")

    # find the file where all the known warnings are stored
    warning_file = Path(__file__).parent.parent / "warning_list.txt"
    extra_warning_file = Path(__file__).parent.parent / "intermittent_warning_list.txt"

    received_warnings = escape_ansi(file.read_text()).strip().split("\n")
    expected_warnings = warning_file.read_text().strip().split("\n")
    intermittent_warnings = extra_warning_file.read_text().strip().split("\n")
    # filter out empty warnings (happens on notebooks for some reason)
    received_warnings = list(filter(len, received_warnings))

    print(
        f'Checking build warnings in file: "{file}" and comparing to expected '
        f'warnings defined in "{warning_file}" and "{extra_warning_file}"\n\n'
    )

    for exp_w in expected_warnings[::-1] + intermittent_warnings[::-1]:
        found = False
        for rec_w in received_warnings:
            if exp_w in rec_w:
                received_warnings.remove(rec_w)
                if exp_w in expected_warnings:
                    expected_warnings.remove(exp_w)
                elif exp_w in intermittent_warnings:
                    intermittent_warnings.remove(exp_w)
                found = True
                break
        # alert only if an *always expected* warning wasn't raised (not intermittent)
        if not found and exp_w not in intermittent_warnings:
            print(f"{Fore.YELLOW}Warning was not raised: {Fore.RESET}{exp_w}\n")
    # warn about unexpected warnings
    for rec_w in received_warnings[::-1]:
        print(f"{Fore.YELLOW}Unexpected warning: {Fore.RESET}{rec_w}\n")
    return len(received_warnings) or len(expected_warnings)


if __name__ == "__main__":
    # cast the file to path and resolve to an absolute one
    file = Path.cwd() / "warnings.txt"

    # execute the test
    sys.exit(check_warnings(file))