Ticket #8047: 8047_admin_scripts_tests_on_jython.3.diff

File 8047_admin_scripts_tests_on_jython.3.diff, 4.2 KB (added by Leo Soto M., 16 years ago)

Avoid inheriting the whole sys.path, to the child processes. Now it just tries to detect the location of the external backend.

  • tests/regressiontests/admin_scripts/tests.py

    diff -r dfdbb69419da tests/regressiontests/admin_scripts/tests.py
    a b  
    77import unittest
    88import shutil
    99import sys
     10import re
    1011
    1112from django import conf, bin, get_version
    1213from django.conf import settings
     
    4041    def remove_settings(self, filename):
    4142        test_dir = os.path.dirname(os.path.dirname(__file__))
    4243        os.remove(os.path.join(test_dir, filename))
    43         # Also try to remove the pyc file; if it exists, it could
     44        # Also try to remove the compiled file; if it exists, it could
    4445        # mess up later tests that depend upon the .py file not existing
    45         try:
    46             os.remove(os.path.join(test_dir, filename + 'c'))
    47         except OSError:
    48             pass
     46        def try_remove(name):
     47            try:
     48                os.remove(os.path.join(test_dir, name))
     49            except OSError:
     50                pass
     51        # Remove *.pyc
     52        try_remove(filename + 'c')
     53        # Remove *$py.class (for Jython)
     54        try_remove(re.sub(r'\.py$', '$py.class', filename))
     55
     56    def _sys_executable(self):
     57        """
     58        Returns the command line needed to run a python interpreter, including
     59        the options for setting sys.path on Jython, which doesn't recognizes
     60        PYTHONPATH.
     61        """
     62        if sys.platform.startswith('java'):
     63            return "%s -J-Dpython.path=%s" % \
     64                   (sys.executable, os.environ['PYTHONPATH'])
     65        else:
     66            return sys.executable
     67
     68    def _ext_backend_path(self):
     69        """
     70        Returns the path for the external backend package, or None if no
     71        external backend is detected.
     72        """
     73        first_package_re = re.compile(r'(^[^\.]+)\.')
     74        result = first_package_re.findall(settings.DATABASE_ENGINE)
     75        if result:
     76            backend_pkg = __import__(result[0])
     77            backend_dir = os.path.dirname(backend_pkg.__file__)
     78            return os.path.dirname(backend_dir)
    4979
    5080    def run_test(self, script, args, settings_file=None, apps=None):
    5181        test_dir = os.path.dirname(os.path.dirname(__file__))
    5282        project_dir = os.path.dirname(test_dir)
    5383        base_dir = os.path.dirname(project_dir)
    54 
    55         # Build the command line
    56         cmd = '%s "%s"' % (sys.executable, script)
    57         cmd += ''.join([' %s' % arg for arg in args])
     84        ext_backend_base_dir = self._ext_backend_path()
    5885
    5986        # Remember the old environment
    6087        old_django_settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', None)
     
    6693            os.environ['DJANGO_SETTINGS_MODULE'] = settings_file
    6794        elif 'DJANGO_SETTINGS_MODULE' in os.environ:
    6895            del os.environ['DJANGO_SETTINGS_MODULE']
     96        python_path = [test_dir, base_dir]
     97        if ext_backend_base_dir:
     98            python_path.append(ext_backend_base_dir)
     99        os.environ['PYTHONPATH'] = os.pathsep.join(python_path)
    69100
    70         if old_python_path:
    71             os.environ['PYTHONPATH'] = os.pathsep.join([test_dir, base_dir, old_python_path])
    72         else:
    73             os.environ['PYTHONPATH'] = os.pathsep.join([test_dir, base_dir])
     101        # Build the command line
     102        cmd = '%s "%s"' % (self._sys_executable(), script)
     103        cmd += ''.join([' %s' % arg for arg in args])
    74104
    75105        # Move to the test directory and run
    76106        os.chdir(test_dir)
     
    823853        out, err = self.run_manage(args)
    824854        self.assertNoOutput(err)
    825855        self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.auth.models'")
    826         self.assertOutput(out, os.sep.join(['django','contrib','auth','models.pyc']) + "'>, options=[('pythonpath', None), ('settings', None), ('traceback', None)]")
     856        self.assertOutput(out, os.sep.join(['django','contrib','auth','models.py']))
    827857        self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.contenttypes.models'")
    828858        self.assertOutput(out, os.sep.join(['django','contrib','contenttypes','models.py']))
    829859        self.assertOutput(out, "'>, options=[('pythonpath', None), ('settings', None), ('traceback', None)]")
Back to Top