forked from deshaw/pyflyby
-
Notifications
You must be signed in to change notification settings - Fork 2
/
conftest.py
129 lines (105 loc) · 4.29 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
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
from __future__ import absolute_import, division, with_statement
import os
import re
import sys
import pytest
from six import PY3
_already_ran_setup = False
def pytest_runtest_setup(item):
"""
Run the logger setup once.
"""
# Although we only bother doing this once (and never tearDown), we still
# do this in a pytest_runtest_setup, rather than e.g. pytest_configure()
# because otherwise tox+py.test can get confused by having already loaded
# pyflyby from a different location.
global _already_ran_setup
if _already_ran_setup:
return
_already_ran_setup = True
_setup_logger()
def pytest_ignore_collect(path, config):
""" return True to prevent considering this path for collection.
This hook is consulted for all files and directories prior to calling
more specific hooks.
"""
if str(path).endswith('_docxref.py') and PY3:
return True
def pytest_report_header(config):
import IPython
print("IPython %s" % (IPython.__version__))
import pyflyby
dir = os.path.dirname(pyflyby.__file__)
print("pyflyby %s from %s" % (pyflyby.__version__, dir))
if getattr(pytest, 'version_tuple', (0,0))[:2] >= (7, 0):
def pytest_load_initial_conftests(early_config, parser, args):
args[:] = ["--no-success-flaky-report", "--no-flaky-report"] + args
else:
def pytest_cmdline_preparse(config, args):
args[:] = ["--no-success-flaky-report", "--no-flaky-report"] + args
def _setup_logger():
"""
Set up the pyflyby logger to be doctest-friendly.
"""
import logging
import pyflyby
import sys
class TestStream(object):
def write(self, x):
sys.stdout.write(x)
sys.stdout.flush()
def flush(self):
pass
test_handler = logging.StreamHandler(TestStream())
test_handler.formatter = logging.Formatter("[PYFLYBY] %(message)s")
handler = pyflyby.logger.handlers[0]
handler.emit = test_handler.emit
# Set $PYFLYBY_PATH to a predictable value. For other env vars, set to defaults.
PYFLYBY_HOME = os.path.dirname(os.path.realpath(__file__))
os.environ["PYFLYBY_PATH"] = os.path.join(PYFLYBY_HOME, "etc/pyflyby")
os.environ["PYFLYBY_KNOWN_IMPORTS_PATH"] = ""
os.environ["PYFLYBY_MANDATORY_IMPORTS_PATH"] = ""
os.environ["PYFLYBY_LOG_LEVEL"] = ""
os.environ["PYTHONSTARTUP"] = ""
# Make sure that the virtualenv path is first.
os.environ["PATH"] = "%s:%s" % (os.path.dirname(sys.executable),
os.environ["PATH"])
# Detect whether we're inside tox. (Any better way to do this?)
in_tox = '/.tox/' in sys.prefix
if in_tox:
# When in tox, we shouldn't have any usercustomize messing this up.
for k in list(sys.modules.keys()):
assert not k == "pyflyby" or k.startswith("pyflyby.")
import pyflyby
fn = pyflyby.__file__
assert fn.startswith(sys.prefix)
else:
# Unload any already-imported pyflyby. This could happen if the user's
# usercustomize imported pyflyby. That would probably be the "production"
# pyflyby rather than the one being developed & tested.
for k in list(sys.modules.keys()):
if k == "pyflyby" or k.startswith("pyflyby."):
del sys.modules[k]
# Make sure we import pyflyby from this repository, as opposed to any other
# copy of pyflyby. This does prevent us from testing using the test cases of
# one repository against the modules from another repository. But that's not
# a common case; better to avoid confusion in the more common case between
# production vs development pyflyby.
pylib = os.path.join(PYFLYBY_HOME, "lib/python")
sys.path.insert(0, pylib)
os.environ["PYTHONPATH"] = ":".join(
[pylib] + list(filter(None, os.environ.get("PYTHONPATH", "").split(":"))))
import pyflyby
fn = re.sub("[.]py[co]$", ".py", pyflyby.__file__)
expected_fn = os.path.join(PYFLYBY_HOME, "lib/python/pyflyby/__init__.py")
assert fn == expected_fn, "pyflyby got loaded from %s; expected %s" % (fn, expected_fn)
# The following block is a workaround for IPython 0.11 and earlier versions.
# These versions of IPython get confused by sys.stdin not being a regular
# file at import time.
saved_sys_stdin = sys.stdin
try:
sys.stdin = sys.__stdin__
import IPython
IPython # pyflakes
finally:
sys.stdin = saved_sys_stdin