File: utils.py

package info (click to toggle)
prospector 1.10.3-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,436 kB
  • sloc: python: 4,200; makefile: 180
file content (65 lines) | stat: -rw-r--r-- 2,243 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
from __future__ import annotations

import contextlib
import sys
from pathlib import Path
from unittest.mock import patch

from prospector.config import ProspectorConfig
from prospector.run import Prospector


@contextlib.contextmanager
def patch_cli(*args: list[str], target: str = "sys.argv"):
    with patch(target, args):
        yield


@contextlib.contextmanager
def patch_cwd(set_cwd: Path):
    # oddness here : Path.cwd() uses os.getcwd() under the hood in python<=3.9 but
    # for python 3.10+, they return different things if only one is patched; therefore,
    # for this test to work in all python versions prospector supports, both need to
    # be patched (or, an "if python version" statement but it's easier to just patch both)
    cwd_str = str(set_cwd.absolute())
    with patch("pathlib.Path.cwd", new=lambda: set_cwd), patch("os.getcwd", new=lambda: cwd_str), patch(
        "os.curdir", new=cwd_str
    ):
        # Turns out that Python 3.10 added the `getcwd` to the _NormalAccessor instead of falling
        # back on os.getcwd, and so this needs to be patched too...
        if sys.version_info[:2] == (3, 10):
            # sigh...
            with patch("pathlib._NormalAccessor.getcwd", new=lambda _: cwd_str):
                yield
        else:
            yield


@contextlib.contextmanager
def patch_execution(*args: list[str], set_cwd: Path = None):
    """
    Utility to patch builtins to simulate running prospector in a particular directory
    with particular commandline args

    :param set_cwd:  Simulate changing directory into the given directory
    :param args:  Any additional command-line arguments to pass to prospector
    """
    args = ("prospector",) + args
    with patch_cli(*args):
        if set_cwd:
            with patch_cwd(set_cwd):
                yield
        else:
            yield


@contextlib.contextmanager
def patch_workdir_argv(target: str = "sys.argv", args: list[str] | None = None, workdir: Path | None = None):
    if args is None:
        args = ["prospector"]
    with patch_cli(*args, target=target):
        config = ProspectorConfig(workdir=workdir)
        config.paths = [workdir]
        pros = Prospector(config)
        pros.execute()
        yield pros