aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/werror_logs_test.py
blob: c6489389399936e193667965a6ea7529a9bd2eae (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python3
# Copyright 2024 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for werror_logs.py."""

import io
import logging
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import textwrap
from typing import Dict
import unittest
from unittest import mock

import werror_logs


class SilenceLogs:
    """Used by Test.silence_logs to ignore all logging output."""

    def filter(self, _record):
        return False


def create_warning_info(packages: Dict[str, int]) -> werror_logs.WarningInfo:
    """Constructs a WarningInfo conveniently in one line.

    Mostly useful because `WarningInfo` has a defaultdict field, and those
    don't `assertEqual` to regular dict fields.
    """
    x = werror_logs.WarningInfo()
    x.packages.update(packages)
    return x


class Test(unittest.TestCase):
    """Tests for werror_logs."""

    def silence_logs(self):
        f = SilenceLogs()
        log = logging.getLogger()
        log.addFilter(f)
        self.addCleanup(log.removeFilter, f)

    def make_tempdir(self) -> Path:
        tempdir = tempfile.mkdtemp("werror_logs_test_")
        self.addCleanup(shutil.rmtree, tempdir)
        return Path(tempdir)

    def test_clang_warning_parsing_parses_flag_errors(self):
        self.assertEqual(
            werror_logs.ClangWarning.try_parse_line(
                "clang-17: error: optimization flag -foo is not supported "
                "[-Werror,-Wfoo]"
            ),
            werror_logs.ClangWarning(
                name="-Wfoo",
                message="optimization flag -foo is not supported",
                location=None,
            ),
        )

    def test_clang_warning_parsing_doesnt_care_about_werror_order(self):
        self.assertEqual(
            werror_logs.ClangWarning.try_parse_line(
                "clang-17: error: optimization flag -foo is not supported "
                "[-Wfoo,-Werror]"
            ),
            werror_logs.ClangWarning(
                name="-Wfoo",
                message="optimization flag -foo is not supported",
                location=None,
            ),
        )

    def test_clang_warning_parsing_parses_code_errors(self):
        self.assertEqual(
            werror_logs.ClangWarning.try_parse_line(
                "/path/to/foo/bar/baz.cc:12:34: error: don't do this "
                "[-Werror,-Wbar]"
            ),
            werror_logs.ClangWarning(
                name="-Wbar",
                message="don't do this",
                location=werror_logs.ClangWarningLocation(
                    file="/path/to/foo/bar/baz.cc",
                    line=12,
                    column=34,
                ),
            ),
        )

    def test_clang_warning_parsing_parses_implicit_errors(self):
        self.assertEqual(
            werror_logs.ClangWarning.try_parse_line(
                # N.B., "-Werror" is missing in this message
                "/path/to/foo/bar/baz.cc:12:34: error: don't do this "
                "[-Wbar]"
            ),
            werror_logs.ClangWarning(
                name="-Wbar",
                message="don't do this",
                location=werror_logs.ClangWarningLocation(
                    file="/path/to/foo/bar/baz.cc",
                    line=12,
                    column=34,
                ),
            ),
        )

    def test_clang_warning_parsing_canonicalizes_correctly(self):
        canonical_forms = (
            ("/build/foo/bar/baz.cc", "/build/{board}/bar/baz.cc"),
            ("///build//foo///bar//baz.cc", "/build/{board}/bar/baz.cc"),
            ("/build/baz.cc", "/build/baz.cc"),
            ("/build.cc", "/build.cc"),
            (".", "."),
        )

        for before, after in canonical_forms:
            self.assertEqual(
                werror_logs.ClangWarning.try_parse_line(
                    f"{before}:12:34: error: don't do this [-Werror,-Wbar]",
                    canonicalize_board_root=True,
                ),
                werror_logs.ClangWarning(
                    name="-Wbar",
                    message="don't do this",
                    location=werror_logs.ClangWarningLocation(
                        file=after,
                        line=12,
                        column=34,
                    ),
                ),
            )

    def test_clang_warning_parsing_doesnt_canonicalize_if_not_asked(self):
        self.assertEqual(
            werror_logs.ClangWarning.try_parse_line(
                "/build/foo/bar/baz.cc:12:34: error: don't do this "
                "[-Werror,-Wbar]",
                canonicalize_board_root=False,
            ),
            werror_logs.ClangWarning(
                name="-Wbar",
                message="don't do this",
                location=werror_logs.ClangWarningLocation(
                    file="/build/foo/bar/baz.cc",
                    line=12,
                    column=34,
                ),
            ),
        )

    def test_clang_warning_parsing_skips_uninteresting_lines(self):
        self.silence_logs()

        pointless = (
            "",
            "foo",
            "error: something's wrong",
            "clang-14: warning: something's wrong [-Wsomething]",
            "clang-14: error: something's wrong",
        )
        for line in pointless:
            self.assertIsNone(
                werror_logs.ClangWarning.try_parse_line(line), line
            )

    def test_aggregation_correctly_scrapes_warnings(self):
        aggregated = werror_logs.AggregatedWarnings()
        aggregated.add_report_json(
            {
                "cwd": "/var/tmp/portage/sys-devel/llvm/foo/bar",
                "stdout": textwrap.dedent(
                    """\
                    Foo
                    clang-17: error: failed to blah [-Werror,-Wblah]
                    /path/to/file.cc:1:2: error: other error [-Werror,-Wother]
                    """
                ),
            }
        )
        aggregated.add_report_json(
            {
                "cwd": "/var/tmp/portage/sys-devel/llvm/foo/bar",
                "stdout": textwrap.dedent(
                    """\
                    Foo
                    clang-17: error: failed to blah [-Werror,-Wblah]
                    /path/to/file.cc:1:3: error: other error [-Werror,-Wother]
                    Bar
                    """
                ),
            }
        )

        self.assertEqual(aggregated.num_reports, 2)
        self.assertEqual(
            dict(aggregated.warnings),
            {
                werror_logs.ClangWarning(
                    name="-Wblah",
                    message="failed to blah",
                    location=None,
                ): create_warning_info(
                    packages={"sys-devel/llvm": 2},
                ),
                werror_logs.ClangWarning(
                    name="-Wother",
                    message="other error",
                    location=werror_logs.ClangWarningLocation(
                        file="/path/to/file.cc",
                        line=1,
                        column=2,
                    ),
                ): create_warning_info(
                    packages={"sys-devel/llvm": 1},
                ),
                werror_logs.ClangWarning(
                    name="-Wother",
                    message="other error",
                    location=werror_logs.ClangWarningLocation(
                        file="/path/to/file.cc",
                        line=1,
                        column=3,
                    ),
                ): create_warning_info(
                    packages={"sys-devel/llvm": 1},
                ),
            },
        )

    def test_aggregation_guesses_packages_correctly(self):
        aggregated = werror_logs.AggregatedWarnings()
        cwds = (
            "/var/tmp/portage/sys-devel/llvm/foo/bar",
            "/var/cache/portage/sys-devel/llvm/foo/bar",
            "/build/amd64-host/var/tmp/portage/sys-devel/llvm/foo/bar",
            "/build/amd64-host/var/cache/portage/sys-devel/llvm/foo/bar",
        )
        for d in cwds:
            # If the directory isn't recognized, this will raise.
            aggregated.add_report_json(
                {
                    "cwd": d,
                    "stdout": "clang-17: error: foo [-Werror,-Wfoo]",
                }
            )

        self.assertEqual(len(aggregated.warnings), 1)
        warning, warning_info = next(iter(aggregated.warnings.items()))
        self.assertEqual(warning.name, "-Wfoo")
        self.assertEqual(
            warning_info, create_warning_info({"sys-devel/llvm": len(cwds)})
        )

    def test_aggregation_raises_if_package_name_cant_be_guessed(self):
        aggregated = werror_logs.AggregatedWarnings()
        with self.assertRaises(werror_logs.UnknownPackageNameError):
            aggregated.add_report_json({})

    def test_warning_by_flag_summarization_works_in_simple_case(self):
        string_io = io.StringIO()
        werror_logs.summarize_warnings_by_flag(
            {
                werror_logs.ClangWarning(
                    name="-Wother",
                    message="other error",
                    location=werror_logs.ClangWarningLocation(
                        file="/path/to/some/file.cc",
                        line=1,
                        column=2,
                    ),
                ): create_warning_info(
                    {
                        "sys-devel/llvm": 3000,
                        "sys-devel/gcc": 1,
                    }
                ),
                werror_logs.ClangWarning(
                    name="-Wother",
                    message="other error",
                    location=werror_logs.ClangWarningLocation(
                        file="/path/to/some/file.cc",
                        line=1,
                        column=3,
                    ),
                ): create_warning_info(
                    {
                        "sys-devel/llvm": 1,
                    }
                ),
            },
            file=string_io,
        )
        result = string_io.getvalue()
        self.assertEqual(
            result,
            textwrap.dedent(
                """\
                ## Instances of each fatal warning:
                \t-Wother: 3,002
                """
            ),
        )

    def test_warning_by_package_summarization_works_in_simple_case(self):
        string_io = io.StringIO()
        werror_logs.summarize_per_package_warnings(
            (
                create_warning_info(
                    {
                        "sys-devel/llvm": 3000,
                        "sys-devel/gcc": 1,
                    }
                ),
                create_warning_info(
                    {
                        "sys-devel/llvm": 1,
                    }
                ),
            ),
            file=string_io,
        )
        result = string_io.getvalue()
        self.assertEqual(
            result,
            textwrap.dedent(
                """\
                ## Per-package warning counts:
                \tsys-devel/llvm: 3,001
                \t sys-devel/gcc:     1
                """
            ),
        )

    def test_cq_builder_determination_works(self):
        self.assertEqual(
            werror_logs.cq_builder_name_from_werror_logs_path(
                "gs://chromeos-image-archive/staryu-cq/"
                "R123-15771.0.0-94466-8756713501925941617/"
                "staryu.20240207.fatal_clang_warnings.tar.xz"
            ),
            "staryu-cq",
        )

    @mock.patch.object(subprocess, "run")
    def test_tarball_downloading_works(self, run_mock):
        tempdir = self.make_tempdir()
        unpack_dir = tempdir / "unpack"
        download_dir = tempdir / "download"

        gs_urls = [
            "gs://foo/bar-cq/build-number/123.fatal_clang_warnings.tar.xz",
            "gs://foo/baz-cq/build-number/124.fatal_clang_warnings.tar.xz",
            "gs://foo/qux-cq/build-number/125.fatal_clang_warnings.tar.xz",
        ]
        named_gs_urls = [
            (werror_logs.cq_builder_name_from_werror_logs_path(x), x)
            for x in gs_urls
        ]
        werror_logs.download_and_unpack_werror_tarballs(
            unpack_dir, download_dir, gs_urls
        )

        # Just verify that this executed the correct commands. Normally this is
        # a bit fragile, but given that this function internally is pretty
        # complex (starting up a threadpool, etc), extra checking is nice.
        want_gsutil_commands = [
            [
                "gsutil",
                "cp",
                gs_url,
                download_dir / name / os.path.basename(gs_url),
            ]
            for name, gs_url in named_gs_urls
        ]
        want_untar_commands = [
            ["tar", "xaf", gsutil_command[-1]]
            for gsutil_command in want_gsutil_commands
        ]

        cmds = []
        for call_args in run_mock.call_args_list:
            call_positional_args = call_args[0]
            cmd = call_positional_args[0]
            cmds.append(cmd)
        cmds.sort()
        self.assertEqual(
            cmds, sorted(want_gsutil_commands + want_untar_commands)
        )

    @mock.patch.object(subprocess, "run")
    def test_tarball_downloading_fails_if_exceptions_are_raised(self, run_mock):
        self.silence_logs()

        def raise_exception(*_args, check=False, **_kwargs):
            self.assertTrue(check)
            raise subprocess.CalledProcessError(returncode=1, cmd=[])

        run_mock.side_effect = raise_exception
        tempdir = self.make_tempdir()
        unpack_dir = tempdir / "unpack"
        download_dir = tempdir / "download"

        gs_urls = [
            "gs://foo/bar-cq/build-number/123.fatal_clang_warnings.tar.xz",
            "gs://foo/baz-cq/build-number/124.fatal_clang_warnings.tar.xz",
            "gs://foo/qux-cq/build-number/125.fatal_clang_warnings.tar.xz",
        ]
        with self.assertRaisesRegex(ValueError, r"3 download\(s\) failed"):
            werror_logs.download_and_unpack_werror_tarballs(
                unpack_dir, download_dir, gs_urls
            )
        self.assertEqual(run_mock.call_count, 3)


if __name__ == "__main__":
    unittest.main()