-
Notifications
You must be signed in to change notification settings - Fork 4
/
conftest.py
90 lines (66 loc) · 2.13 KB
/
conftest.py
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
r"""*pytest configuration for the* ``stdio_mgr`` *test suite*.
``stdio_mgr`` provides a context manager for convenient
mocking and/or wrapping of ``stdin``/``stdout``/``stderr``
interactions.
**Author**
Brian Skinn ([email protected])
**File Created**
6 Feb 2019
**Copyright**
\(c) Brian Skinn 2018-2019
**Source Repository**
http://github.com/bskinn/stdio-mgr
**Documentation**
See README.rst at the GitHub repository
**License**
The MIT License; see |license_txt|_ for full license terms
**Members**
"""
import os
import sys
import warnings
import _pytest.warnings
import pytest
from stdio_mgr import stdio_mgr
@pytest.fixture(scope="session")
def warnings_are_errors(pytestconfig):
"""Provide concise access to '-W error::Warning' CLI option."""
try:
cmdline_filters = pytestconfig.getoption("pythonwarnings")
except ValueError:
return False
# Explicit bool conversion to quiet pytest 'don't assert None' warning
return bool(cmdline_filters and "error::Warning" in cmdline_filters)
@pytest.fixture
def check_warnings_plugin_enabled():
"""Check the warnings module is enabled."""
# Check pytest was not invoked with -p no:warnings.
try:
impl = warnings._showwarnmsg_impl
except AttributeError:
# Python < 3.6
impl = warnings.showwarning
return impl.__name__ == "append"
@pytest.fixture
def enable_warnings_plugin(request):
"""Enable warnings for a single test."""
assert check_warnings_plugin_enabled
with _pytest.warnings.catch_warnings_for_item(
config=request.config,
ihook=request.node.ihook,
when="runtest",
item=request.node,
):
yield
@pytest.fixture(autouse=True, scope="session")
def add_stdio_mgr(doctest_namespace):
"""Add stdio_mgr to doctest namespace."""
doctest_namespace["stdio_mgr"] = stdio_mgr
doctest_namespace["os"] = os
@pytest.fixture(scope="session")
def convert_newlines():
"""Supply platform-dependent newline transform function."""
if sys.platform == "win32":
return lambda s: s.replace("\n", "\r\n")
else:
return lambda s: s