Commits

Philip Jenvey committed a0e9d0c

check for usemodules in appdirect tests

Comments (0)

Files changed (1)

pypy/tool/pytest/apptest.py

 from pypy.conftest import PyPyClassCollector
 from inspect import getmro
 
+RENAMED_USEMODULES = dict(
+    _winreg='winreg',
+    exceptions='__exceptions__',
+    rctime='time',
+    struct='_struct',
+    thread='_thread',
+    )
 
 class AppError(Exception):
     def __init__(self, excinfo):
         return repr(value)
 
 
-def run_with_python(python_, target_, **definitions):
+def run_with_python(python_, target_, usemodules, **definitions):
     if python_ is None:
         py.test.skip("Cannot find the default python3 interpreter to run with -A")
     # we assume that the source of target_ is in utf-8. Unfortunately, we don't
     helpers = r"""# -*- encoding: utf-8 -*-
 if 1:
     import sys
+%s
     def skip(message):
         print(message)
         raise SystemExit(0)
                                 dict)) or value is None:
             defs.append("self.%s = %s\n" % (symbol, py3k_repr(value)))
 
+    check_usemodules = ''
+    if usemodules:
+        usemodules = [RENAMED_USEMODULES.get(name, name)
+                      for name in usemodules]
+        check_usemodules = """\
+    if not set(%r).issubset(sys.builtin_module_names):
+        sys.exit(81)""" % usemodules
+
     source = list(py.code.Source(target_))
     while source[0].startswith(('@py.test.mark.', '@pytest.mark.')):
         source.pop(0)
     else:
         target_name = target_.__name__
     with pyfile.open('w') as f:
-        f.write(helpers)
+        f.write(helpers % check_usemodules)
         f.write('\n'.join(defs))
         f.write('def %s():\n' % target_name)
         f.write('\n'.join(source))
     print pyfile.read()
     print >> sys.stdout, stdout
     print >> sys.stderr, stderr
-    if res > 0:
+    if res == 81:
+        py.test.skip('%r was not compiled w/ required usemodules: %r' %
+                     (python_, usemodules))
+    elif res > 0:
         raise AssertionError("Subprocess failed:\n" + stderr)
 
 
         target = self.obj
         src = extract_docstring_if_empty_function(target)
         if self.config.option.runappdirect:
-            return run_with_python(self.config.option.python, src)
+            return run_with_python(self.config.option.python, src, None)
         space = gettestobjspace()
         filename = self._getdynfilename(target)
         func = app2interp_temp(src, filename=filename)
         space = target.im_self.space
         if self.config.option.runappdirect:
             appexec_definitions = self.parent.obj.__dict__
-            return run_with_python(self.config.option.python, src,
+            spaceconfig = getattr(self.parent.obj, 'spaceconfig', None)
+            usemodules = spaceconfig.get('usemodules') if spaceconfig else None
+            return run_with_python(self.config.option.python, src, usemodules,
                                    **appexec_definitions)
         filename = self._getdynfilename(target)
         func = app2interp_temp(src, filename=filename)