File: test_cmdline_main.py

package info (click to toggle)
python-pyinstrument 5.1.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,624 kB
  • sloc: python: 6,713; ansic: 897; makefile: 46; sh: 26; javascript: 18
file content (100 lines) | stat: -rw-r--r-- 2,567 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
from pathlib import Path

import pytest

from pyinstrument.__main__ import main
from pyinstrument.renderers.base import FrameRenderer

from .util import BUSY_WAIT_SCRIPT

fake_renderer_instance = None


class FakeRenderer(FrameRenderer):
    def __init__(self, time=None, **kwargs):
        self.time = time
        super().__init__(**kwargs)
        global fake_renderer_instance
        fake_renderer_instance = self
        print("instance")

    def default_processors(self):
        """
        Return a list of processors that this renderer uses by default.
        """
        return []

    def render(self, session) -> str:
        return ""


def test_renderer_option(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
    (tmp_path / "test_program.py").write_text(BUSY_WAIT_SCRIPT)
    monkeypatch.setattr(
        "sys.argv",
        [
            "pyinstrument",
            "-r",
            "test.test_cmdline_main.FakeRenderer",
            "-p",
            "time=percent_of_total",
            "test_program.py",
        ],
    )
    monkeypatch.chdir(tmp_path)

    global fake_renderer_instance
    fake_renderer_instance = None

    main()

    assert fake_renderer_instance is not None
    assert fake_renderer_instance.time == "percent_of_total"


def test_json_renderer_option(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
    (tmp_path / "test_program.py").write_text(BUSY_WAIT_SCRIPT)
    monkeypatch.setattr(
        "sys.argv",
        [
            "pyinstrument",
            "-r",
            "test.test_cmdline_main.FakeRenderer",
            "-p",
            'processor_options={"some_option": 44}',
            "test_program.py",
        ],
    )
    monkeypatch.chdir(tmp_path)

    global fake_renderer_instance
    fake_renderer_instance = None

    main()

    assert fake_renderer_instance is not None
    assert fake_renderer_instance.processor_options["some_option"] == 44


def test_dotted_renderer_option(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
    (tmp_path / "test_program.py").write_text(BUSY_WAIT_SCRIPT)
    monkeypatch.setattr(
        "sys.argv",
        [
            "pyinstrument",
            "-r",
            "test.test_cmdline_main.FakeRenderer",
            "-p",
            "processor_options.other_option=13",
            "test_program.py",
        ],
    )
    monkeypatch.chdir(tmp_path)

    global fake_renderer_instance
    fake_renderer_instance = None

    main()

    assert fake_renderer_instance is not None
    assert fake_renderer_instance.processor_options["other_option"] == 13