File: coverage

package info (click to toggle)
debusine 0.14.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,056 kB
  • sloc: python: 193,072; sh: 848; javascript: 335; makefile: 116
file content (521 lines) | stat: -rwxr-xr-x 15,622 bytes parent folder | download | duplicates (2)
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env python3
#
# Copyright © The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.

"""Run coverage testing on a subset of Debusine's code."""

import abc
import argparse
import importlib
import importlib.util
import os
import re
import shlex
import subprocess
import sys
import types
from collections import defaultdict
from collections.abc import Generator
from contextlib import contextmanager
from pathlib import Path
from typing import NamedTuple, Self

from rich.console import Console

COVERAGE = ["python3", "-m", "coverage"]

console = Console()
top_srcdir = Path(sys.argv[0]).parent.parent.absolute()


class Fail(Exception):
    """There was an error in the script."""


@contextmanager
def local_sys_path() -> Generator[None]:
    """Temporarily add top_srcdir to sys.path."""
    old_sys_path = list(sys.path)
    try:
        sys.path = [top_srcdir.as_posix()] + sys.path
        yield
    finally:
        sys.path = old_sys_path


class Options(NamedTuple):
    """Test running options."""

    #: True to stop tests at the first failure
    failfast: bool
    #: Patterns used with -k
    patterns: list[str]
    #: Verbose output
    verbose: bool
    #: Use pg_virtualenv
    isolate_db: bool = False
    #: Do not run commands, print them only
    dry_run: bool = False
    #: Run tests in parallel if possible
    parallel: bool = True


class Runner(abc.ABC):
    """A way to run tests."""

    #: True if this runner can run on multiple targets with the same command
    MULTIPLE_TARGETS: bool = False

    def __init__(self, options: Options, targets: list["Target"]) -> None:
        """
        Set up runner.

        :param targets: list of targets to run
        """
        if not self.MULTIPLE_TARGETS and len(targets) > 1:
            raise AssertionError(
                "multiple targets given to runner that doesn't support them"
            )
        self.options = options
        self.targets = targets
        self.returncode: int | None = None

    def get_wrappers_cmd(self) -> list[str]:
        """Get generic wrappers to be prepended to command line."""
        return []

    def get_coverage_cmd(self) -> list[str]:
        """Get coverage testing command line."""
        return COVERAGE + ["run"]

    @abc.abstractmethod
    def get_runner_cmd(self) -> list[str]:
        """Get the test runner command line."""

    def get_cmd(self) -> list[str]:
        """Get the command to run all tests."""
        cmd = []
        cmd += self.get_wrappers_cmd()
        cmd += self.get_coverage_cmd()
        cmd += self.get_runner_cmd()
        return cmd

    def get_environment(self) -> dict[str, str] | None:
        """Get extra environment vars to use for tests."""
        return None

    @property
    def name(self) -> str:
        """Get a string identifying this runner."""
        return ' '.join(t.modname for t in self.targets)

    def run(self) -> None:
        """Run tests for the current targets."""
        kwargs: dict[str, str] = {}
        cmd = self.get_cmd()
        if env := self.get_environment():
            run_env = dict(os.environ)
            run_env.update(env)
            kwargs["env"] = run_env
            for k, v in env.items():
                console.print(
                    "$", f"export {k}={v!r}", markup=False, style="green"
                )
        console.print("$", shlex.join(cmd), markup=False, style="green")
        if self.options.dry_run:
            return

        result = subprocess.run(
            cmd,
            cwd=top_srcdir,
            **kwargs,
        )
        self.stdout = result.stdout
        self.stderr = result.stderr
        self.returncode = result.returncode
        if self.returncode != 0:
            console.print(
                f"Tests failed for {self.name}",
                markup=False,
                style="bright_red",
            )


class Pytest(Runner):
    """Run tests with pytest."""

    MULTIPLE_TARGETS = True

    def get_wrappers_cmd(self) -> list[str]:
        """Get generic wrappers to be prepended to command line."""
        cmd = super().get_wrappers_cmd()
        if self.options.isolate_db:
            cmd += ["pg_virtualenv"]
        return cmd

    def get_runner_cmd(self) -> list[str]:
        """Get the test runner command line."""
        return []

    def get_cmd(self) -> list[str]:
        """Get the test runner command line."""
        cmd: list[str] = []
        cmd += self.get_wrappers_cmd()
        cmd += ["pytest", "--cov=debusine", "--cov-append", "--cov-report="]
        if self.options.verbose:
            cmd.append("-v")
        if self.options.failfast:
            cmd.append("--exitfirst")
        for pattern in self.options.patterns:
            cmd += ["-k", pattern]
        if self.targets:
            cmd.append("--pyargs")
            for target in self.targets:
                cmd.append(target.modname)
        return cmd


class PytestSerial(Pytest):
    """Run pytest without parallelism."""

    def get_cmd(self) -> list[str]:
        """Get the test runner command line."""
        cmd = super().get_cmd()
        cmd += ["--numprocesses", "0"]
        return cmd


class PytestMain(Pytest):
    """Run pytest on the main debusine code."""

    def get_cmd(self) -> list[str]:
        """Get the test runner command line."""
        res = super().get_cmd()
        res.append("--ignore=debusine/signing")
        return res


class PytestSigning(Pytest):
    """Run pytest on the debusine signing code."""

    def get_cmd(self) -> list[str]:
        """Get the test runner command line."""
        res = super().get_cmd()
        res.append("--ds=debusine.signing.settings")
        return res


class PytestMainSerial(PytestSerial, PytestMain):
    """PytestMain without parallelism."""


class PytestSigningSerial(PytestSerial, PytestSigning):
    """PytestSigning without parallelism."""


class Runners:
    """Collection of runners and their targets."""

    def __init__(self, options: Options) -> None:
        """Initialize structures."""
        self.options = options
        self.multiple: dict[type[Runner], list[Target]] = defaultdict(list)
        self.runners: list[Runner] = []
        self.collecting: bool = False

    def __enter__(self) -> Self:
        """Start collecting targets."""
        self.collecting = True
        return self

    def __exit__(
        self,
        exc_type: type[BaseException] | None,  # noqa: U100
        exc_val: BaseException | None,  # noqa: U100
        exc_tb: types.TracebackType | None,  # noqa: U100
    ) -> None:
        """Stop collecting targets."""
        for runner_class, targets in self.multiple.items():
            self.runners.append(runner_class(self.options, targets))
        self.multiple.clear()
        self.collecting = False

    def add(self, target: "Target") -> None:
        """Add a target."""
        assert self.collecting
        if target.runner.MULTIPLE_TARGETS:
            self.multiple[target.runner].append(target)
        else:
            self.runners.append(target.runner(self.options, [target]))

    @property
    def targets(self) -> list["Target"]:
        """Get a list of all targets."""
        assert not self.collecting
        return [target for runner in self.runners for target in runner.targets]

    def run(self) -> None:
        """Run all runners."""
        assert not self.collecting
        for runner in self.runners:
            runner.run()


class Coverage:
    """Collect coverage data and generate reports."""

    def __init__(self, targets: list["Target"], *, dry_run: bool = False):
        """Store coverage options."""
        self.targets = targets
        self.dry_run = dry_run

    def run(self, *args: str) -> None:
        """Run a coverage command."""
        cmd: list[str] = []
        cmd += COVERAGE
        cmd.extend(args)
        console.print("$", shlex.join(cmd), markup=False, style="green")
        if self.dry_run:
            return
        subprocess.run(cmd, cwd=top_srcdir, check=True)

    def __enter__(self) -> Self:
        """Start coverage collection."""
        self.run("erase")
        return self

    def __exit__(
        self,
        exc_type: type[BaseException] | None,  # noqa: U100
        exc_val: BaseException | None,
        exc_tb: types.TracebackType | None,  # noqa: U100
    ) -> None:
        """Do coverage reporting."""
        if exc_val is not None:
            return
        # self.run("combine")

        report_args = [
            "--precision=2",
            "--include",
            ",".join(t.include for t in self.targets),
        ]
        if os.environ.get("HTML_COVERAGE") == "1":
            self.run("html", *report_args)
        self.run("report", "--skip-covered", "--show-missing", *report_args)


class Target(NamedTuple):
    """Test target."""

    #: Path relative to top_srcdir
    relpath: Path
    #: Module name
    modname: str
    #: Include pattern
    include: str
    #: Test runner to use
    runner: type[Runner]

    @classmethod
    def get_runner(cls, options: Options, modname: str) -> type[Runner]:
        """Select the runner to use for the given module."""
        main_runner: type[Runner]
        signing_runner: type[Runner]
        if options.parallel:
            main_runner = PytestMain
            signing_runner = PytestSigning
        else:
            main_runner = PytestMainSerial
            signing_runner = PytestSigningSerial

        if not (m := re.match(r"debusine\.(?P<app>\w+)(\.|$)", modname)):
            return main_runner

        match m.group("app"):
            case "signing":
                return signing_runner
            case _:
                return main_runner

    @classmethod
    def create_dir(cls, options: Options, path: Path) -> Self:
        """Infer a test target from a directory."""
        path = path.absolute()
        relpath = path.relative_to(top_srcdir)
        modname = relpath.as_posix().replace("/", ".")
        include = f"{relpath}/*"

        if relpath.name == "templatetags":
            # Add "include" for coverage (since it's not a subdirectory
            # of the directory where the code under test is)
            templatetags_tests = relpath.parent / "templatetags_tests"
            include += f",{templatetags_tests}/*"

            # Fix "modname": the runner needs it to find the tests
            modname = modname.replace(".templatetags", ".templatetags_tests")

        return Target(
            relpath=relpath,
            modname=modname,
            include=include,
            runner=cls.get_runner(options, modname),
        )

    @classmethod
    def create_file(cls, options: Options, path: Path) -> Self:
        """Infer a test target from a file."""
        # Test a single module, inferring test location from debusine
        # coding practices
        path = path.absolute()
        relpath = path.relative_to(top_srcdir)
        name = relpath.name.removesuffix(".py")
        testfile = path.parent / "tests" / f"test_{name}.py"

        if relpath.parent.name == "templatetags":
            # Tests for templatetags live in a different directory
            # (not a subdirectory of the templatetags/ but in its
            # parent directory in "templatetags_tests/")
            testfile = (
                path.parent.parent / "templatetags_tests" / f"test_{name}.py"
            )

        testfile_relpath = testfile.relative_to(top_srcdir)
        if not testfile.exists():
            raise Fail(
                f"{testfile_relpath} not found for"
                f" {path.relative_to(top_srcdir)}"
            )
        modname = (
            testfile_relpath.as_posix().removesuffix(".py").replace("/", ".")
        )
        return Target(
            relpath=relpath,
            modname=modname,
            include=relpath.as_posix(),
            runner=cls.get_runner(options, modname),
        )

    @classmethod
    def create(cls, options: Options, path: Path) -> Self:
        """Infer a test target from a path."""
        if path.is_dir():
            return cls.create_dir(options, path)
        elif path.is_file():
            return cls.create_file(options, path)
        else:
            with local_sys_path():
                try:
                    spec = importlib.util.find_spec(path.as_posix())
                except ModuleNotFoundError:
                    raise Fail(
                        "running a test by name is not yet implemented:"
                        " use -k instead"
                    )
            path = Path(spec.origin)
            if path.name == "__init__.py":
                path = path.parent
            return cls.create(options, path)


def main():
    """Run coverage testing on a subset of Debusine's code."""
    parser = argparse.ArgumentParser(
        description="Run coverage testing on a subset of Debusine's code."
    )
    parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        help="Verbose output",
    )
    parser.add_argument(
        "-f",
        "--failfast",
        action="store_true",
        help="stop running the test suite after first failed test",
    )
    parser.add_argument(
        "-k",
        dest="patterns",
        action="append",
        type=str,
        help=(
            "Only run test methods and classes"
            " that match the pattern or substring."
            " Can be used multiple times. Same as unittest -k option."
        ),
    )
    parser.add_argument(
        "--dry-run",
        action="store_true",
        help="print computed todo list instead of running tests",
    )
    parser.add_argument(
        "--isolate-db",
        action="store_true",
        help="use pg_virtualenv to run tests in a separate database",
    )
    parser.add_argument(
        "--parallel",
        action=argparse.BooleanOptionalAction,
        default=True,
        help="enable/disable running tests in parallel. Default: enabled",
    )

    parser.add_argument(
        "path",
        type=Path,
        nargs="*",
        action="store",
        help="code path to test for coverage",
    )
    args = parser.parse_args()

    options = Options(
        failfast=args.failfast,
        patterns=args.patterns or [],
        verbose=args.verbose,
        isolate_db=args.isolate_db,
        dry_run=args.dry_run,
        parallel=args.parallel,
    )

    paths: list[Path]
    if not args.path or any(
        p.exists() and p.samefile(Path("debusine")) for p in args.path
    ):
        paths = [
            p
            for p in Path("debusine").iterdir()
            if p.is_dir() and not p.name.startswith("_")
        ]
    else:
        paths = args.path

    # Build the list of test/coverage targets
    with Runners(options) as runners:
        for path in paths:
            runners.add(Target.create(options, path))

    # Run tests and collect coverage
    with Coverage(runners.targets, dry_run=args.dry_run):
        runners.run()


if __name__ == "__main__":
    try:
        main()
    except Fail as e:
        console.print(e, style="bold red")
        sys.exit(1)
    except Exception:
        console.print_exception()
        sys.exit(2)