File: testutils.py

package info (click to toggle)
psutils 3.3.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,280 kB
  • sloc: python: 2,984; makefile: 28
file content (253 lines) | stat: -rw-r--r-- 7,793 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""psutils tests utility routines.

Copyright (c) Reuben Thomas 2023-2025.
Released under the GPL version 3, or (at your option) any later version.
"""

import difflib
import os
import re
import shutil
import subprocess
import sys
from collections.abc import Callable
from contextlib import ExitStack, chdir
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from unittest.mock import patch
from warnings import warn

import pytest
from pytest import CaptureFixture, mark, param
from wand.image import Image  # type: ignore


@dataclass
class GeneratedInput:
    paper: str
    pages: int
    border: int = 1


@dataclass
class Case:
    name: str
    args: list[str]
    input: GeneratedInput | str
    error: int | None = None


def remove_creation_date(lines: list[str]) -> list[str]:
    return [l for l in lines if not re.match(r"(% )?%%CreationDate", l)]


def compare_text_files(
    capsys: CaptureFixture[str],
    output_file: os.PathLike[str],
    expected_file: os.PathLike[str],
) -> bool:
    with ExitStack() as stack:
        out_fd = stack.enter_context(open(output_file, encoding="ascii"))
        exp_fd = stack.enter_context(open(expected_file, encoding="ascii"))
        output_lines = remove_creation_date(out_fd.readlines())
        expected_lines = remove_creation_date(exp_fd.readlines())
        diff = list(
            difflib.unified_diff(
                output_lines, expected_lines, str(output_file), str(expected_file)
            )
        )
        if len(diff) > 0:
            with capsys.disabled():
                sys.stdout.writelines(diff)
        return len(diff) == 0


def image_to_bytes(image_file: os.PathLike[str]) -> bytes:
    with Image(filename=image_file) as image:
        bytestr = b""
        # FIXME: If comparison fails, save images that differ for debugging
        for i, frame in enumerate(image.sequence):
            frame_img = Image(image=frame)
            blob = frame_img.make_blob("pnm")
            assert blob is not None or type(blob) is bytes
            bytestr += blob
            # frame_img.save(filename=f"{image_file}-{i}.pnm")
        return bytestr


def compare_image_files(
    capsys: CaptureFixture[str],
    output_file: os.PathLike[str],
    expected_file: os.PathLike[str],
) -> bool:
    with ExitStack() as stack:
        out_fd = stack.enter_context(open(output_file, "rb"))
        exp_fd = stack.enter_context(open(expected_file, "rb"))
        output = out_fd.read()
        expected = exp_fd.read()
        if output == expected:
            return True
        output_bytes = image_to_bytes(output_file)
        expected_bytes = image_to_bytes(expected_file)
        if output_bytes == expected_bytes:
            with capsys.disabled():
                warn(
                    f"{output_file} not identical to {expected_file} but looks the same"
                )
            return True
    return False


def compare_strings(
    capsys: CaptureFixture[str],
    output: str,
    output_file: os.PathLike[str],
    expected_file: os.PathLike[str],
) -> bool:
    with open(output_file, "w", encoding="ascii") as f:
        f.write(output)
    return compare_text_files(capsys, output_file, expected_file)


def file_test(
    function: Callable[[list[str]], None],
    case: Case,
    fixture_dir: Path,
    capsys: CaptureFixture[str],
    datafiles: Path,
    file_type: str,
    regenerate_input: bool,
    regenerate_expected: bool,
) -> None:
    module_name = function.__name__
    expected_file = fixture_dir / module_name / case.name / "expected"
    expected_stderr = (
        fixture_dir / module_name / case.name / f"expected-stderr-{file_type[1:]}.txt"
    )
    if not os.path.exists(expected_stderr):
        expected_stderr = fixture_dir / module_name / case.name / "expected-stderr.txt"
    if isinstance(case.input, str):
        test_file = fixture_dir / case.input
    else:
        basename = f"{case.input.paper}-{case.input.pages}"
        if case.input.border != 1:
            basename += f"-{case.input.border}"
        test_file = fixture_dir / basename
    if regenerate_input and isinstance(case.input, GeneratedInput):
        make_test_input(
            case.input.paper, case.input.pages, test_file, case.input.border
        )
    output_file = datafiles / "output"
    full_args = [*case.args, str(test_file.with_suffix(file_type)), str(output_file)]
    patched_argv = [module_name, *(sys.argv[1:])]
    with chdir(datafiles):
        correct_output = True
        if case.error is None:
            with patch("sys.argv", patched_argv):
                function(full_args)
            if regenerate_expected:
                shutil.copyfile(output_file, expected_file.with_suffix(file_type))
                correct_output = True
            else:
                comparer = (
                    compare_text_files
                    if file_type in (".ps", ".eps")
                    else compare_image_files
                )
                correct_output = comparer(
                    capsys, output_file, expected_file.with_suffix(file_type)
                )
        else:
            with pytest.raises(SystemExit) as e:
                with patch("sys.argv", patched_argv):
                    function(full_args)
            assert e.value.code == case.error
        if regenerate_expected:
            with open(expected_stderr, "w", encoding="utf-8") as f:
                f.write(capsys.readouterr().err)
            correct_stderr = True
        else:
            correct_stderr = compare_strings(
                capsys,
                capsys.readouterr().err,
                datafiles / "stderr.txt",
                expected_stderr,
            )
        if not (correct_output and correct_stderr):
            bad_results: list[str] = []
            if not correct_output:
                bad_results.append("output")
            if not correct_stderr:
                bad_results.append("stderr")
            raise ValueError(
                f"test {','.join(bad_results)} does not match expected output"
            )


def make_tests(
    function: Callable[..., Any],
    fixture_dir: Path,
    *tests: Case,
) -> Any:
    ids = []
    test_cases = []
    for t in tests:
        ids.append(t.name)
        test_cases.append(t)
    return mark.parametrize(
        "function,case,fixture_dir",
        [
            param(
                function,
                case,
                fixture_dir,
                marks=mark.datafiles,
            )
            for case in test_cases
        ],
        ids=ids,
    )


# Make a test PostScript or PDF file of a given number of pages
# Requires a2ps and ps2pdf
# Simply writes a large page number on each page
def make_test_input(
    paper: str, pages: int, file: Path, border: int | None = 1
) -> None:
    # Configuration
    lines_per_page = 4

    # Produce PostScript
    title = file.stem
    text = ("\n" * lines_per_page).join([str(i + 1) for i in range(pages)])
    subprocess.run(
        [
            "a2ps",
            f"--medium={paper}",
            f"--title={title}",
            f"--lines-per-page={lines_per_page}",
            "--portrait",
            "--columns=1",
            "--rows=1",
            f"--border={border}",
            "--no-header",
            f"--output={file.with_suffix('.ps')}",
        ],
        text=True,
        input=text,
        check=True,
    )

    # Convert to PDF if required
    if file.suffix == ".pdf":
        subprocess.check_call(
            [
                "ps2pdf",
                f"-sPAPERSIZE={paper}",
                f"{file.with_suffix('.ps')}",
                f"{file.with_suffix('.pdf')}",
            ]
        )
        os.remove(f"{file.with_suffix('.ps')}")