-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconftest.py
122 lines (99 loc) · 3.85 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
#
# Copyright 2009 Benjamin Mellor
#
# This file is part of Fundy.
#
# Fundy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
This is not a module of fundy, just configuration for the tests.
Should ideally live in the test directory, but it doesn't seem to work there
(adding options seems to be ignored unless you run py.test from the same
directory that has the conftest.py module, and running py.test from the test
directory fails due to the base fundy directory not being in path).
"""
import sys
import py
class ConftestPlugin(object):
def pytest_addoption(self, parser):
group = parser.addgroup('Fundy')
group.addoption('-T', '--translate',
action='store_true', default=False,
help='Run tests on translated Fundy interpreter')
def pytest_funcarg__fundy_cpython(self, pyfuncitem):
return CPythonFuncarg(pyfuncitem)
def pytest_funcarg__fundy_rpython(self, pyfuncitem):
return RPythonFuncarg(pyfuncitem)
def pytest_funcarg__fundy_translated(self, pyfuncitem):
return TranslatedFuncarg(pyfuncitem)
class CPythonFuncarg(object):
def __init__(self, pyfuncitem):
name = pyfuncitem.name
self.tempdir = pyfuncitem.config.mktemp(name, numbered=True)
@staticmethod
def raw_run(args):
from interactive import main
return main(['<fundy-test>'] + args)
def run(self, args):
try:
return self.raw_run(args), None, None
except Exception, e:
return None, e, sys.exc_info()[2]
def run_code(self, code):
tmpfile = self.tempdir.join('tmp.fy')
tmpfile.write(code)
do_run = lambda: self.run([tmpfile.strpath])
result, out, err = py.io.StdCaptureFD.call(do_run)
ret, exc, tb = result
return ret, out, err, exc, tb
class RPythonFuncarg(CPythonFuncarg):
@staticmethod
def raw_run(args):
from interactive import main
from utils import preparer
preparer.prepare(for_translation=True)
try:
ret = CPythonFuncarg.raw_run(args)
finally:
preparer.prepare(for_translation=False)
return ret
class TranslatedFuncarg(CPythonFuncarg):
exe_wrapper = None
def __init__(self, pyfuncitem):
if not pyfuncitem.config.option.translate:
py.test.skip('specify -T to run translated tests')
super(TranslatedFuncarg, self).__init__(pyfuncitem)
self.make()
@classmethod
def make(cls):
if cls.exe_wrapper is None:
from subprocess import check_call, CalledProcessError
try:
check_call(['make', 'fundy-c'])
except CalledProcessError:
@staticmethod
def exe_wrapper(args):
py.test.fail('make failed first time; not re-running')
cls.exe_wrapper = exe_wrapper
raise
else:
@staticmethod
def exe_wrapper(args):
exe_path = py.path.local('fundy-c').strpath
exe_and_args = [exe_path] + args
return check_call(exe_and_args)
cls.exe_wrapper = exe_wrapper
@classmethod
def raw_run(cls, args):
return cls.exe_wrapper(args)