File: test_pytest_plugin.py

package info (click to toggle)
python-typeguard 4.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: python: 6,271; makefile: 5
file content (77 lines) | stat: -rw-r--r-- 2,528 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
from textwrap import dedent

import pytest
from pytest import MonkeyPatch, Pytester

from typeguard import CollectionCheckStrategy, ForwardRefPolicy, TypeCheckConfiguration


@pytest.fixture
def config(monkeypatch: MonkeyPatch) -> TypeCheckConfiguration:
    config = TypeCheckConfiguration()
    monkeypatch.setattr("typeguard._pytest_plugin.global_config", config)
    return config


def test_config_options(pytester: Pytester, config: TypeCheckConfiguration) -> None:
    pytester.makepyprojecttoml(
        '''
        [tool.pytest.ini_options]
        typeguard-packages = """
        mypackage
        otherpackage"""
        typeguard-debug-instrumentation = true
        typeguard-typecheck-fail-callback = "mypackage:failcallback"
        typeguard-forward-ref-policy = "ERROR"
        typeguard-collection-check-strategy = "ALL_ITEMS"
        '''
    )
    pytester.makepyfile(
        mypackage=(
            dedent(
                """
            def failcallback():
                pass
            """
            )
        )
    )

    pytester.plugins = ["typeguard"]
    pytester.syspathinsert()
    pytestconfig = pytester.parseconfigure()
    assert pytestconfig.getini("typeguard-packages") == ["mypackage", "otherpackage"]
    assert config.typecheck_fail_callback.__name__ == "failcallback"
    assert config.debug_instrumentation is True
    assert config.forward_ref_policy is ForwardRefPolicy.ERROR
    assert config.collection_check_strategy is CollectionCheckStrategy.ALL_ITEMS


def test_commandline_options(
    pytester: Pytester, config: TypeCheckConfiguration
) -> None:
    pytester.makepyfile(
        mypackage=(
            dedent(
                """
            def failcallback():
                pass
            """
            )
        )
    )

    pytester.plugins = ["typeguard"]
    pytester.syspathinsert()
    pytestconfig = pytester.parseconfigure(
        "--typeguard-packages=mypackage,otherpackage",
        "--typeguard-typecheck-fail-callback=mypackage:failcallback",
        "--typeguard-debug-instrumentation",
        "--typeguard-forward-ref-policy=ERROR",
        "--typeguard-collection-check-strategy=ALL_ITEMS",
    )
    assert pytestconfig.getoption("typeguard_packages") == "mypackage,otherpackage"
    assert config.typecheck_fail_callback.__name__ == "failcallback"
    assert config.debug_instrumentation is True
    assert config.forward_ref_policy is ForwardRefPolicy.ERROR
    assert config.collection_check_strategy is CollectionCheckStrategy.ALL_ITEMS