parts/django/tests/regressiontests/admin_scripts/tests.py
changeset 307 c6bca38c1cbf
equal deleted inserted replaced
306:5ff1fc726848 307:c6bca38c1cbf
       
     1 """
       
     2 A series of tests to establish that the command-line managment tools work as
       
     3 advertised - especially with regards to the handling of the DJANGO_SETTINGS_MODULE
       
     4 and default settings.py files.
       
     5 """
       
     6 import os
       
     7 import unittest
       
     8 import shutil
       
     9 import sys
       
    10 import re
       
    11 
       
    12 from django import conf, bin, get_version
       
    13 from django.conf import settings
       
    14 
       
    15 
       
    16 class AdminScriptTestCase(unittest.TestCase):
       
    17     def write_settings(self, filename, apps=None, is_dir=False, sdict=None):
       
    18         test_dir = os.path.dirname(os.path.dirname(__file__))
       
    19         if is_dir:
       
    20             settings_dir = os.path.join(test_dir,filename)
       
    21             os.mkdir(settings_dir)
       
    22             settings_file = open(os.path.join(settings_dir,'__init__.py'), 'w')
       
    23         else:
       
    24             settings_file = open(os.path.join(test_dir, filename), 'w')
       
    25         settings_file.write('# Settings file automatically generated by regressiontests.admin_scripts test case\n')
       
    26         exports = [
       
    27             'DATABASES',
       
    28             'ROOT_URLCONF'
       
    29         ]
       
    30         for s in exports:
       
    31             if hasattr(settings, s):
       
    32                 o = getattr(settings, s)
       
    33                 if not isinstance(o, dict):
       
    34                     o = "'%s'" % o
       
    35                 settings_file.write("%s = %s\n" % (s, o))
       
    36 
       
    37         if apps is None:
       
    38             apps = ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts']
       
    39 
       
    40         if apps:
       
    41             settings_file.write("INSTALLED_APPS = %s\n" % apps)
       
    42 
       
    43         if sdict:
       
    44             for k, v in sdict.items():
       
    45                 settings_file.write("%s = %s\n" % (k, v))
       
    46 
       
    47         settings_file.close()
       
    48 
       
    49     def remove_settings(self, filename, is_dir=False):
       
    50         test_dir = os.path.dirname(os.path.dirname(__file__))
       
    51         full_name = os.path.join(test_dir, filename)
       
    52         if is_dir:
       
    53             shutil.rmtree(full_name)
       
    54         else:
       
    55             os.remove(full_name)
       
    56 
       
    57         # Also try to remove the compiled file; if it exists, it could
       
    58         # mess up later tests that depend upon the .py file not existing
       
    59         try:
       
    60             if sys.platform.startswith('java'):
       
    61                 # Jython produces module$py.class files
       
    62                 os.remove(re.sub(r'\.py$', '$py.class', full_name))
       
    63             else:
       
    64                 # CPython produces module.pyc files
       
    65                 os.remove(full_name + 'c')
       
    66         except OSError:
       
    67             pass
       
    68 
       
    69     def _ext_backend_paths(self):
       
    70         """
       
    71         Returns the paths for any external backend packages.
       
    72         """
       
    73         paths = []
       
    74         first_package_re = re.compile(r'(^[^\.]+)\.')
       
    75         for backend in settings.DATABASES.values():
       
    76             result = first_package_re.findall(backend['ENGINE'])
       
    77             if result and result != 'django':
       
    78                 backend_pkg = __import__(result[0])
       
    79                 backend_dir = os.path.dirname(backend_pkg.__file__)
       
    80                 paths.append(os.path.dirname(backend_dir))
       
    81         return paths
       
    82 
       
    83     def run_test(self, script, args, settings_file=None, apps=None):
       
    84         test_dir = os.path.dirname(os.path.dirname(__file__))
       
    85         project_dir = os.path.dirname(test_dir)
       
    86         base_dir = os.path.dirname(project_dir)
       
    87         ext_backend_base_dirs = self._ext_backend_paths()
       
    88 
       
    89         # Remember the old environment
       
    90         old_django_settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', None)
       
    91         if sys.platform.startswith('java'):
       
    92             python_path_var_name = 'JYTHONPATH'
       
    93         else:
       
    94             python_path_var_name = 'PYTHONPATH'
       
    95 
       
    96         old_python_path = os.environ.get(python_path_var_name, None)
       
    97         old_cwd = os.getcwd()
       
    98 
       
    99         # Set the test environment
       
   100         if settings_file:
       
   101             os.environ['DJANGO_SETTINGS_MODULE'] = settings_file
       
   102         elif 'DJANGO_SETTINGS_MODULE' in os.environ:
       
   103             del os.environ['DJANGO_SETTINGS_MODULE']
       
   104         python_path = [test_dir, base_dir]
       
   105         python_path.extend(ext_backend_base_dirs)
       
   106         os.environ[python_path_var_name] = os.pathsep.join(python_path)
       
   107 
       
   108         # Build the command line
       
   109         executable = sys.executable
       
   110         arg_string = ' '.join(['%s' % arg for arg in args])
       
   111         if ' ' in executable:
       
   112             cmd = '""%s" "%s" %s"' % (executable, script, arg_string)
       
   113         else:
       
   114             cmd = '%s "%s" %s' % (executable, script, arg_string)
       
   115 
       
   116         # Move to the test directory and run
       
   117         os.chdir(test_dir)
       
   118         try:
       
   119             from subprocess import Popen, PIPE
       
   120             p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
       
   121             stdin, stdout, stderr = (p.stdin, p.stdout, p.stderr)
       
   122             p.wait()
       
   123         except ImportError:
       
   124             stdin, stdout, stderr = os.popen3(cmd)
       
   125         out, err = stdout.read(), stderr.read()
       
   126 
       
   127         # Restore the old environment
       
   128         if old_django_settings_module:
       
   129             os.environ['DJANGO_SETTINGS_MODULE'] = old_django_settings_module
       
   130         if old_python_path:
       
   131             os.environ[python_path_var_name] = old_python_path
       
   132         # Move back to the old working directory
       
   133         os.chdir(old_cwd)
       
   134 
       
   135         return out, err
       
   136 
       
   137     def run_django_admin(self, args, settings_file=None):
       
   138         bin_dir = os.path.abspath(os.path.dirname(bin.__file__))
       
   139         return self.run_test(os.path.join(bin_dir,'django-admin.py'), args, settings_file)
       
   140 
       
   141     def run_manage(self, args, settings_file=None):
       
   142         conf_dir = os.path.dirname(conf.__file__)
       
   143         template_manage_py = os.path.join(conf_dir, 'project_template', 'manage.py')
       
   144 
       
   145         test_dir = os.path.dirname(os.path.dirname(__file__))
       
   146         test_manage_py = os.path.join(test_dir, 'manage.py')
       
   147         shutil.copyfile(template_manage_py, test_manage_py)
       
   148 
       
   149         stdout, stderr = self.run_test('./manage.py', args, settings_file)
       
   150 
       
   151         # Cleanup - remove the generated manage.py script
       
   152         os.remove(test_manage_py)
       
   153 
       
   154         return stdout, stderr
       
   155 
       
   156     def assertNoOutput(self, stream):
       
   157         "Utility assertion: assert that the given stream is empty"
       
   158         self.assertEquals(len(stream), 0, "Stream should be empty: actually contains '%s'" % stream)
       
   159     def assertOutput(self, stream, msg):
       
   160         "Utility assertion: assert that the given message exists in the output"
       
   161         self.assertTrue(msg in stream, "'%s' does not match actual output text '%s'" % (msg, stream))
       
   162 
       
   163 ##########################################################################
       
   164 # DJANGO ADMIN TESTS
       
   165 # This first series of test classes checks the environment processing
       
   166 # of the django-admin.py script
       
   167 ##########################################################################
       
   168 
       
   169 
       
   170 class DjangoAdminNoSettings(AdminScriptTestCase):
       
   171     "A series of tests for django-admin.py when there is no settings.py file."
       
   172 
       
   173     def test_builtin_command(self):
       
   174         "no settings: django-admin builtin commands fail with an import error when no settings provided"
       
   175         args = ['sqlall','admin_scripts']
       
   176         out, err = self.run_django_admin(args)
       
   177         self.assertNoOutput(out)
       
   178         self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
       
   179 
       
   180     def test_builtin_with_bad_settings(self):
       
   181         "no settings: django-admin builtin commands fail if settings file (from argument) doesn't exist"
       
   182         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   183         out, err = self.run_django_admin(args)
       
   184         self.assertNoOutput(out)
       
   185         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   186 
       
   187     def test_builtin_with_bad_environment(self):
       
   188         "no settings: django-admin builtin commands fail if settings file (from environment) doesn't exist"
       
   189         args = ['sqlall','admin_scripts']
       
   190         out, err = self.run_django_admin(args,'bad_settings')
       
   191         self.assertNoOutput(out)
       
   192         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   193 
       
   194 
       
   195 class DjangoAdminDefaultSettings(AdminScriptTestCase):
       
   196     """A series of tests for django-admin.py when using a settings.py file that
       
   197     contains the test application.
       
   198     """
       
   199     def setUp(self):
       
   200         self.write_settings('settings.py')
       
   201 
       
   202     def tearDown(self):
       
   203         self.remove_settings('settings.py')
       
   204 
       
   205     def test_builtin_command(self):
       
   206         "default: django-admin builtin commands fail with an import error when no settings provided"
       
   207         args = ['sqlall','admin_scripts']
       
   208         out, err = self.run_django_admin(args)
       
   209         self.assertNoOutput(out)
       
   210         self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
       
   211 
       
   212     def test_builtin_with_settings(self):
       
   213         "default: django-admin builtin commands succeed if settings are provided as argument"
       
   214         args = ['sqlall','--settings=settings', 'admin_scripts']
       
   215         out, err = self.run_django_admin(args)
       
   216         self.assertNoOutput(err)
       
   217         self.assertOutput(out, 'CREATE TABLE')
       
   218 
       
   219     def test_builtin_with_environment(self):
       
   220         "default: django-admin builtin commands succeed if settings are provided in the environment"
       
   221         args = ['sqlall','admin_scripts']
       
   222         out, err = self.run_django_admin(args,'settings')
       
   223         self.assertNoOutput(err)
       
   224         self.assertOutput(out, 'CREATE TABLE')
       
   225 
       
   226     def test_builtin_with_bad_settings(self):
       
   227         "default: django-admin builtin commands fail if settings file (from argument) doesn't exist"
       
   228         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   229         out, err = self.run_django_admin(args)
       
   230         self.assertNoOutput(out)
       
   231         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   232 
       
   233     def test_builtin_with_bad_environment(self):
       
   234         "default: django-admin builtin commands fail if settings file (from environment) doesn't exist"
       
   235         args = ['sqlall','admin_scripts']
       
   236         out, err = self.run_django_admin(args,'bad_settings')
       
   237         self.assertNoOutput(out)
       
   238         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   239 
       
   240     def test_custom_command(self):
       
   241         "default: django-admin can't execute user commands if it isn't provided settings"
       
   242         args = ['noargs_command']
       
   243         out, err = self.run_django_admin(args)
       
   244         self.assertNoOutput(out)
       
   245         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   246 
       
   247     def test_custom_command_with_settings(self):
       
   248         "default: django-admin can execute user commands if settings are provided as argument"
       
   249         args = ['noargs_command', '--settings=settings']
       
   250         out, err = self.run_django_admin(args)
       
   251         self.assertNoOutput(err)
       
   252         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   253 
       
   254     def test_custom_command_with_environment(self):
       
   255         "default: django-admin can execute user commands if settings are provided in environment"
       
   256         args = ['noargs_command']
       
   257         out, err = self.run_django_admin(args,'settings')
       
   258         self.assertNoOutput(err)
       
   259         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   260 
       
   261 class DjangoAdminFullPathDefaultSettings(AdminScriptTestCase):
       
   262     """A series of tests for django-admin.py when using a settings.py file that
       
   263     contains the test application specified using a full path.
       
   264     """
       
   265     def setUp(self):
       
   266         self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes', 'regressiontests.admin_scripts'])
       
   267 
       
   268     def tearDown(self):
       
   269         self.remove_settings('settings.py')
       
   270 
       
   271     def test_builtin_command(self):
       
   272         "fulldefault: django-admin builtin commands fail with an import error when no settings provided"
       
   273         args = ['sqlall','admin_scripts']
       
   274         out, err = self.run_django_admin(args)
       
   275         self.assertNoOutput(out)
       
   276         self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
       
   277 
       
   278     def test_builtin_with_settings(self):
       
   279         "fulldefault: django-admin builtin commands succeed if a settings file is provided"
       
   280         args = ['sqlall','--settings=settings', 'admin_scripts']
       
   281         out, err = self.run_django_admin(args)
       
   282         self.assertNoOutput(err)
       
   283         self.assertOutput(out, 'CREATE TABLE')
       
   284 
       
   285     def test_builtin_with_environment(self):
       
   286         "fulldefault: django-admin builtin commands succeed if the environment contains settings"
       
   287         args = ['sqlall','admin_scripts']
       
   288         out, err = self.run_django_admin(args,'settings')
       
   289         self.assertNoOutput(err)
       
   290         self.assertOutput(out, 'CREATE TABLE')
       
   291 
       
   292     def test_builtin_with_bad_settings(self):
       
   293         "fulldefault: django-admin builtin commands fail if settings file (from argument) doesn't exist"
       
   294         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   295         out, err = self.run_django_admin(args)
       
   296         self.assertNoOutput(out)
       
   297         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   298 
       
   299     def test_builtin_with_bad_environment(self):
       
   300         "fulldefault: django-admin builtin commands fail if settings file (from environment) doesn't exist"
       
   301         args = ['sqlall','admin_scripts']
       
   302         out, err = self.run_django_admin(args,'bad_settings')
       
   303         self.assertNoOutput(out)
       
   304         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   305 
       
   306     def test_custom_command(self):
       
   307         "fulldefault: django-admin can't execute user commands unless settings are provided"
       
   308         args = ['noargs_command']
       
   309         out, err = self.run_django_admin(args)
       
   310         self.assertNoOutput(out)
       
   311         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   312 
       
   313     def test_custom_command_with_settings(self):
       
   314         "fulldefault: django-admin can execute user commands if settings are provided as argument"
       
   315         args = ['noargs_command', '--settings=settings']
       
   316         out, err = self.run_django_admin(args)
       
   317         self.assertNoOutput(err)
       
   318         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   319 
       
   320     def test_custom_command_with_environment(self):
       
   321         "fulldefault: django-admin can execute user commands if settings are provided in environment"
       
   322         args = ['noargs_command']
       
   323         out, err = self.run_django_admin(args,'settings')
       
   324         self.assertNoOutput(err)
       
   325         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   326 
       
   327 class DjangoAdminMinimalSettings(AdminScriptTestCase):
       
   328     """A series of tests for django-admin.py when using a settings.py file that
       
   329     doesn't contain the test application.
       
   330     """
       
   331     def setUp(self):
       
   332         self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes'])
       
   333 
       
   334     def tearDown(self):
       
   335         self.remove_settings('settings.py')
       
   336 
       
   337     def test_builtin_command(self):
       
   338         "minimal: django-admin builtin commands fail with an import error when no settings provided"
       
   339         args = ['sqlall','admin_scripts']
       
   340         out, err = self.run_django_admin(args)
       
   341         self.assertNoOutput(out)
       
   342         self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
       
   343 
       
   344     def test_builtin_with_settings(self):
       
   345         "minimal: django-admin builtin commands fail if settings are provided as argument"
       
   346         args = ['sqlall','--settings=settings', 'admin_scripts']
       
   347         out, err = self.run_django_admin(args)
       
   348         self.assertNoOutput(out)
       
   349         self.assertOutput(err, 'App with label admin_scripts could not be found')
       
   350 
       
   351     def test_builtin_with_environment(self):
       
   352         "minimal: django-admin builtin commands fail if settings are provided in the environment"
       
   353         args = ['sqlall','admin_scripts']
       
   354         out, err = self.run_django_admin(args,'settings')
       
   355         self.assertNoOutput(out)
       
   356         self.assertOutput(err, 'App with label admin_scripts could not be found')
       
   357 
       
   358     def test_builtin_with_bad_settings(self):
       
   359         "minimal: django-admin builtin commands fail if settings file (from argument) doesn't exist"
       
   360         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   361         out, err = self.run_django_admin(args)
       
   362         self.assertNoOutput(out)
       
   363         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   364 
       
   365     def test_builtin_with_bad_environment(self):
       
   366         "minimal: django-admin builtin commands fail if settings file (from environment) doesn't exist"
       
   367         args = ['sqlall','admin_scripts']
       
   368         out, err = self.run_django_admin(args,'bad_settings')
       
   369         self.assertNoOutput(out)
       
   370         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   371 
       
   372     def test_custom_command(self):
       
   373         "minimal: django-admin can't execute user commands unless settings are provided"
       
   374         args = ['noargs_command']
       
   375         out, err = self.run_django_admin(args)
       
   376         self.assertNoOutput(out)
       
   377         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   378 
       
   379     def test_custom_command_with_settings(self):
       
   380         "minimal: django-admin can't execute user commands, even if settings are provided as argument"
       
   381         args = ['noargs_command', '--settings=settings']
       
   382         out, err = self.run_django_admin(args)
       
   383         self.assertNoOutput(out)
       
   384         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   385 
       
   386     def test_custom_command_with_environment(self):
       
   387         "minimal: django-admin can't execute user commands, even if settings are provided in environment"
       
   388         args = ['noargs_command']
       
   389         out, err = self.run_django_admin(args,'settings')
       
   390         self.assertNoOutput(out)
       
   391         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   392 
       
   393 class DjangoAdminAlternateSettings(AdminScriptTestCase):
       
   394     """A series of tests for django-admin.py when using a settings file
       
   395     with a name other than 'settings.py'.
       
   396     """
       
   397     def setUp(self):
       
   398         self.write_settings('alternate_settings.py')
       
   399 
       
   400     def tearDown(self):
       
   401         self.remove_settings('alternate_settings.py')
       
   402 
       
   403     def test_builtin_command(self):
       
   404         "alternate: django-admin builtin commands fail with an import error when no settings provided"
       
   405         args = ['sqlall','admin_scripts']
       
   406         out, err = self.run_django_admin(args)
       
   407         self.assertNoOutput(out)
       
   408         self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
       
   409 
       
   410     def test_builtin_with_settings(self):
       
   411         "alternate: django-admin builtin commands succeed if settings are provided as argument"
       
   412         args = ['sqlall','--settings=alternate_settings', 'admin_scripts']
       
   413         out, err = self.run_django_admin(args)
       
   414         self.assertNoOutput(err)
       
   415         self.assertOutput(out, 'CREATE TABLE')
       
   416 
       
   417     def test_builtin_with_environment(self):
       
   418         "alternate: django-admin builtin commands succeed if settings are provided in the environment"
       
   419         args = ['sqlall','admin_scripts']
       
   420         out, err = self.run_django_admin(args,'alternate_settings')
       
   421         self.assertNoOutput(err)
       
   422         self.assertOutput(out, 'CREATE TABLE')
       
   423 
       
   424     def test_builtin_with_bad_settings(self):
       
   425         "alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist"
       
   426         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   427         out, err = self.run_django_admin(args)
       
   428         self.assertNoOutput(out)
       
   429         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   430 
       
   431     def test_builtin_with_bad_environment(self):
       
   432         "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
       
   433         args = ['sqlall','admin_scripts']
       
   434         out, err = self.run_django_admin(args,'bad_settings')
       
   435         self.assertNoOutput(out)
       
   436         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   437 
       
   438     def test_custom_command(self):
       
   439         "alternate: django-admin can't execute user commands unless settings are provided"
       
   440         args = ['noargs_command']
       
   441         out, err = self.run_django_admin(args)
       
   442         self.assertNoOutput(out)
       
   443         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   444 
       
   445     def test_custom_command_with_settings(self):
       
   446         "alternate: django-admin can execute user commands if settings are provided as argument"
       
   447         args = ['noargs_command', '--settings=alternate_settings']
       
   448         out, err = self.run_django_admin(args)
       
   449         self.assertNoOutput(err)
       
   450         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   451 
       
   452     def test_custom_command_with_environment(self):
       
   453         "alternate: django-admin can execute user commands if settings are provided in environment"
       
   454         args = ['noargs_command']
       
   455         out, err = self.run_django_admin(args,'alternate_settings')
       
   456         self.assertNoOutput(err)
       
   457         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   458 
       
   459 
       
   460 class DjangoAdminMultipleSettings(AdminScriptTestCase):
       
   461     """A series of tests for django-admin.py when multiple settings files
       
   462     (including the default 'settings.py') are available. The default settings
       
   463     file is insufficient for performing the operations described, so the
       
   464     alternate settings must be used by the running script.
       
   465     """
       
   466     def setUp(self):
       
   467         self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes'])
       
   468         self.write_settings('alternate_settings.py')
       
   469 
       
   470     def tearDown(self):
       
   471         self.remove_settings('settings.py')
       
   472         self.remove_settings('alternate_settings.py')
       
   473 
       
   474     def test_builtin_command(self):
       
   475         "alternate: django-admin builtin commands fail with an import error when no settings provided"
       
   476         args = ['sqlall','admin_scripts']
       
   477         out, err = self.run_django_admin(args)
       
   478         self.assertNoOutput(out)
       
   479         self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
       
   480 
       
   481     def test_builtin_with_settings(self):
       
   482         "alternate: django-admin builtin commands succeed if settings are provided as argument"
       
   483         args = ['sqlall','--settings=alternate_settings', 'admin_scripts']
       
   484         out, err = self.run_django_admin(args)
       
   485         self.assertNoOutput(err)
       
   486         self.assertOutput(out, 'CREATE TABLE')
       
   487 
       
   488     def test_builtin_with_environment(self):
       
   489         "alternate: django-admin builtin commands succeed if settings are provided in the environment"
       
   490         args = ['sqlall','admin_scripts']
       
   491         out, err = self.run_django_admin(args,'alternate_settings')
       
   492         self.assertNoOutput(err)
       
   493         self.assertOutput(out, 'CREATE TABLE')
       
   494 
       
   495     def test_builtin_with_bad_settings(self):
       
   496         "alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist"
       
   497         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   498         out, err = self.run_django_admin(args)
       
   499         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   500 
       
   501     def test_builtin_with_bad_environment(self):
       
   502         "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
       
   503         args = ['sqlall','admin_scripts']
       
   504         out, err = self.run_django_admin(args,'bad_settings')
       
   505         self.assertNoOutput(out)
       
   506         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   507 
       
   508     def test_custom_command(self):
       
   509         "alternate: django-admin can't execute user commands unless settings are provided"
       
   510         args = ['noargs_command']
       
   511         out, err = self.run_django_admin(args)
       
   512         self.assertNoOutput(out)
       
   513         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   514 
       
   515     def test_custom_command_with_settings(self):
       
   516         "alternate: django-admin can't execute user commands, even if settings are provided as argument"
       
   517         args = ['noargs_command', '--settings=alternate_settings']
       
   518         out, err = self.run_django_admin(args)
       
   519         self.assertNoOutput(err)
       
   520         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   521 
       
   522     def test_custom_command_with_environment(self):
       
   523         "alternate: django-admin can't execute user commands, even if settings are provided in environment"
       
   524         args = ['noargs_command']
       
   525         out, err = self.run_django_admin(args,'alternate_settings')
       
   526         self.assertNoOutput(err)
       
   527         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   528 
       
   529 
       
   530 class DjangoAdminSettingsDirectory(AdminScriptTestCase):
       
   531     """
       
   532     A series of tests for django-admin.py when the settings file is in a
       
   533     directory. (see #9751).
       
   534     """
       
   535 
       
   536     def setUp(self):
       
   537         self.write_settings('settings', is_dir=True)
       
   538 
       
   539     def tearDown(self):
       
   540         self.remove_settings('settings', is_dir=True)
       
   541 
       
   542     def test_setup_environ(self):
       
   543         "directory: startapp creates the correct directory"
       
   544         test_dir = os.path.dirname(os.path.dirname(__file__))
       
   545         args = ['startapp','settings_test']
       
   546         out, err = self.run_django_admin(args,'settings')
       
   547         self.assertNoOutput(err)
       
   548         self.assert_(os.path.exists(os.path.join(test_dir, 'settings_test')))
       
   549         shutil.rmtree(os.path.join(test_dir, 'settings_test'))
       
   550 
       
   551     def test_builtin_command(self):
       
   552         "directory: django-admin builtin commands fail with an import error when no settings provided"
       
   553         args = ['sqlall','admin_scripts']
       
   554         out, err = self.run_django_admin(args)
       
   555         self.assertNoOutput(out)
       
   556         self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
       
   557 
       
   558     def test_builtin_with_bad_settings(self):
       
   559         "directory: django-admin builtin commands fail if settings file (from argument) doesn't exist"
       
   560         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   561         out, err = self.run_django_admin(args)
       
   562         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   563 
       
   564     def test_builtin_with_bad_environment(self):
       
   565         "directory: django-admin builtin commands fail if settings file (from environment) doesn't exist"
       
   566         args = ['sqlall','admin_scripts']
       
   567         out, err = self.run_django_admin(args,'bad_settings')
       
   568         self.assertNoOutput(out)
       
   569         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   570 
       
   571     def test_custom_command(self):
       
   572         "directory: django-admin can't execute user commands unless settings are provided"
       
   573         args = ['noargs_command']
       
   574         out, err = self.run_django_admin(args)
       
   575         self.assertNoOutput(out)
       
   576         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   577 
       
   578     def test_builtin_with_settings(self):
       
   579         "directory: django-admin builtin commands succeed if settings are provided as argument"
       
   580         args = ['sqlall','--settings=settings', 'admin_scripts']
       
   581         out, err = self.run_django_admin(args)
       
   582         self.assertNoOutput(err)
       
   583         self.assertOutput(out, 'CREATE TABLE')
       
   584 
       
   585     def test_builtin_with_environment(self):
       
   586         "directory: django-admin builtin commands succeed if settings are provided in the environment"
       
   587         args = ['sqlall','admin_scripts']
       
   588         out, err = self.run_django_admin(args,'settings')
       
   589         self.assertNoOutput(err)
       
   590         self.assertOutput(out, 'CREATE TABLE')
       
   591 
       
   592 
       
   593 ##########################################################################
       
   594 # MANAGE.PY TESTS
       
   595 # This next series of test classes checks the environment processing
       
   596 # of the generated manage.py script
       
   597 ##########################################################################
       
   598 
       
   599 class ManageNoSettings(AdminScriptTestCase):
       
   600     "A series of tests for manage.py when there is no settings.py file."
       
   601 
       
   602     def test_builtin_command(self):
       
   603         "no settings: manage.py builtin commands fail with an import error when no settings provided"
       
   604         args = ['sqlall','admin_scripts']
       
   605         out, err = self.run_manage(args)
       
   606         self.assertNoOutput(out)
       
   607         self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
       
   608 
       
   609     def test_builtin_with_bad_settings(self):
       
   610         "no settings: manage.py builtin commands fail if settings file (from argument) doesn't exist"
       
   611         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   612         out, err = self.run_manage(args)
       
   613         self.assertNoOutput(out)
       
   614         self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
       
   615 
       
   616     def test_builtin_with_bad_environment(self):
       
   617         "no settings: manage.py builtin commands fail if settings file (from environment) doesn't exist"
       
   618         args = ['sqlall','admin_scripts']
       
   619         out, err = self.run_manage(args,'bad_settings')
       
   620         self.assertNoOutput(out)
       
   621         self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
       
   622 
       
   623 
       
   624 class ManageDefaultSettings(AdminScriptTestCase):
       
   625     """A series of tests for manage.py when using a settings.py file that
       
   626     contains the test application.
       
   627     """
       
   628     def setUp(self):
       
   629         self.write_settings('settings.py')
       
   630 
       
   631     def tearDown(self):
       
   632         self.remove_settings('settings.py')
       
   633 
       
   634     def test_builtin_command(self):
       
   635         "default: manage.py builtin commands succeed when default settings are appropriate"
       
   636         args = ['sqlall','admin_scripts']
       
   637         out, err = self.run_manage(args)
       
   638         self.assertNoOutput(err)
       
   639         self.assertOutput(out, 'CREATE TABLE')
       
   640 
       
   641     def test_builtin_with_settings(self):
       
   642         "default: manage.py builtin commands succeed if settings are provided as argument"
       
   643         args = ['sqlall','--settings=settings', 'admin_scripts']
       
   644         out, err = self.run_manage(args)
       
   645         self.assertNoOutput(err)
       
   646         self.assertOutput(out, 'CREATE TABLE')
       
   647 
       
   648     def test_builtin_with_environment(self):
       
   649         "default: manage.py builtin commands succeed if settings are provided in the environment"
       
   650         args = ['sqlall','admin_scripts']
       
   651         out, err = self.run_manage(args,'settings')
       
   652         self.assertNoOutput(err)
       
   653         self.assertOutput(out, 'CREATE TABLE')
       
   654 
       
   655     def test_builtin_with_bad_settings(self):
       
   656         "default: manage.py builtin commands succeed if settings file (from argument) doesn't exist"
       
   657         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   658         out, err = self.run_manage(args)
       
   659         self.assertNoOutput(out)
       
   660         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   661 
       
   662     def test_builtin_with_bad_environment(self):
       
   663         "default: manage.py builtin commands fail if settings file (from environment) doesn't exist"
       
   664         args = ['sqlall','admin_scripts']
       
   665         out, err = self.run_manage(args,'bad_settings')
       
   666         self.assertNoOutput(err)
       
   667         self.assertOutput(out, 'CREATE TABLE')
       
   668 
       
   669     def test_custom_command(self):
       
   670         "default: manage.py can execute user commands when default settings are appropriate"
       
   671         args = ['noargs_command']
       
   672         out, err = self.run_manage(args)
       
   673         self.assertNoOutput(err)
       
   674         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   675 
       
   676     def test_custom_command_with_settings(self):
       
   677         "default: manage.py can execute user commands when settings are provided as argument"
       
   678         args = ['noargs_command', '--settings=settings']
       
   679         out, err = self.run_manage(args)
       
   680         self.assertNoOutput(err)
       
   681         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   682 
       
   683     def test_custom_command_with_environment(self):
       
   684         "default: manage.py can execute user commands when settings are provided in environment"
       
   685         args = ['noargs_command']
       
   686         out, err = self.run_manage(args,'settings')
       
   687         self.assertNoOutput(err)
       
   688         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   689 
       
   690 
       
   691 class ManageFullPathDefaultSettings(AdminScriptTestCase):
       
   692     """A series of tests for manage.py when using a settings.py file that
       
   693     contains the test application specified using a full path.
       
   694     """
       
   695     def setUp(self):
       
   696         self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes', 'regressiontests.admin_scripts'])
       
   697 
       
   698     def tearDown(self):
       
   699         self.remove_settings('settings.py')
       
   700 
       
   701     def test_builtin_command(self):
       
   702         "fulldefault: manage.py builtin commands succeed when default settings are appropriate"
       
   703         args = ['sqlall','admin_scripts']
       
   704         out, err = self.run_manage(args)
       
   705         self.assertNoOutput(err)
       
   706         self.assertOutput(out, 'CREATE TABLE')
       
   707 
       
   708     def test_builtin_with_settings(self):
       
   709         "fulldefault: manage.py builtin commands succeed if settings are provided as argument"
       
   710         args = ['sqlall','--settings=settings', 'admin_scripts']
       
   711         out, err = self.run_manage(args)
       
   712         self.assertNoOutput(err)
       
   713         self.assertOutput(out, 'CREATE TABLE')
       
   714 
       
   715     def test_builtin_with_environment(self):
       
   716         "fulldefault: manage.py builtin commands succeed if settings are provided in the environment"
       
   717         args = ['sqlall','admin_scripts']
       
   718         out, err = self.run_manage(args,'settings')
       
   719         self.assertNoOutput(err)
       
   720         self.assertOutput(out, 'CREATE TABLE')
       
   721 
       
   722     def test_builtin_with_bad_settings(self):
       
   723         "fulldefault: manage.py builtin commands succeed if settings file (from argument) doesn't exist"
       
   724         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   725         out, err = self.run_manage(args)
       
   726         self.assertNoOutput(out)
       
   727         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   728 
       
   729     def test_builtin_with_bad_environment(self):
       
   730         "fulldefault: manage.py builtin commands fail if settings file (from environment) doesn't exist"
       
   731         args = ['sqlall','admin_scripts']
       
   732         out, err = self.run_manage(args,'bad_settings')
       
   733         self.assertNoOutput(err)
       
   734         self.assertOutput(out, 'CREATE TABLE')
       
   735 
       
   736     def test_custom_command(self):
       
   737         "fulldefault: manage.py can execute user commands when default settings are appropriate"
       
   738         args = ['noargs_command']
       
   739         out, err = self.run_manage(args)
       
   740         self.assertNoOutput(err)
       
   741         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   742 
       
   743     def test_custom_command_with_settings(self):
       
   744         "fulldefault: manage.py can execute user commands when settings are provided as argument"
       
   745         args = ['noargs_command', '--settings=settings']
       
   746         out, err = self.run_manage(args)
       
   747         self.assertNoOutput(err)
       
   748         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   749 
       
   750     def test_custom_command_with_environment(self):
       
   751         "fulldefault: manage.py can execute user commands when settings are provided in environment"
       
   752         args = ['noargs_command']
       
   753         out, err = self.run_manage(args,'settings')
       
   754         self.assertNoOutput(err)
       
   755         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   756 
       
   757 class ManageMinimalSettings(AdminScriptTestCase):
       
   758     """A series of tests for manage.py when using a settings.py file that
       
   759     doesn't contain the test application.
       
   760     """
       
   761     def setUp(self):
       
   762         self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes'])
       
   763 
       
   764     def tearDown(self):
       
   765         self.remove_settings('settings.py')
       
   766 
       
   767     def test_builtin_command(self):
       
   768         "minimal: manage.py builtin commands fail with an import error when no settings provided"
       
   769         args = ['sqlall','admin_scripts']
       
   770         out, err = self.run_manage(args)
       
   771         self.assertNoOutput(out)
       
   772         self.assertOutput(err, 'App with label admin_scripts could not be found')
       
   773 
       
   774     def test_builtin_with_settings(self):
       
   775         "minimal: manage.py builtin commands fail if settings are provided as argument"
       
   776         args = ['sqlall','--settings=settings', 'admin_scripts']
       
   777         out, err = self.run_manage(args)
       
   778         self.assertNoOutput(out)
       
   779         self.assertOutput(err, 'App with label admin_scripts could not be found')
       
   780 
       
   781     def test_builtin_with_environment(self):
       
   782         "minimal: manage.py builtin commands fail if settings are provided in the environment"
       
   783         args = ['sqlall','admin_scripts']
       
   784         out, err = self.run_manage(args,'settings')
       
   785         self.assertNoOutput(out)
       
   786         self.assertOutput(err, 'App with label admin_scripts could not be found')
       
   787 
       
   788     def test_builtin_with_bad_settings(self):
       
   789         "minimal: manage.py builtin commands fail if settings file (from argument) doesn't exist"
       
   790         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   791         out, err = self.run_manage(args)
       
   792         self.assertNoOutput(out)
       
   793         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   794 
       
   795     def test_builtin_with_bad_environment(self):
       
   796         "minimal: manage.py builtin commands fail if settings file (from environment) doesn't exist"
       
   797         args = ['sqlall','admin_scripts']
       
   798         out, err = self.run_manage(args,'bad_settings')
       
   799         self.assertNoOutput(out)
       
   800         self.assertOutput(err, 'App with label admin_scripts could not be found')
       
   801 
       
   802     def test_custom_command(self):
       
   803         "minimal: manage.py can't execute user commands without appropriate settings"
       
   804         args = ['noargs_command']
       
   805         out, err = self.run_manage(args)
       
   806         self.assertNoOutput(out)
       
   807         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   808 
       
   809     def test_custom_command_with_settings(self):
       
   810         "minimal: manage.py can't execute user commands, even if settings are provided as argument"
       
   811         args = ['noargs_command', '--settings=settings']
       
   812         out, err = self.run_manage(args)
       
   813         self.assertNoOutput(out)
       
   814         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   815 
       
   816     def test_custom_command_with_environment(self):
       
   817         "minimal: manage.py can't execute user commands, even if settings are provided in environment"
       
   818         args = ['noargs_command']
       
   819         out, err = self.run_manage(args,'settings')
       
   820         self.assertNoOutput(out)
       
   821         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   822 
       
   823 class ManageAlternateSettings(AdminScriptTestCase):
       
   824     """A series of tests for manage.py when using a settings file
       
   825     with a name other than 'settings.py'.
       
   826     """
       
   827     def setUp(self):
       
   828         self.write_settings('alternate_settings.py')
       
   829 
       
   830     def tearDown(self):
       
   831         self.remove_settings('alternate_settings.py')
       
   832 
       
   833     def test_builtin_command(self):
       
   834         "alternate: manage.py builtin commands fail with an import error when no default settings provided"
       
   835         args = ['sqlall','admin_scripts']
       
   836         out, err = self.run_manage(args)
       
   837         self.assertNoOutput(out)
       
   838         self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
       
   839 
       
   840     def test_builtin_with_settings(self):
       
   841         "alternate: manage.py builtin commands fail if settings are provided as argument but no defaults"
       
   842         args = ['sqlall','--settings=alternate_settings', 'admin_scripts']
       
   843         out, err = self.run_manage(args)
       
   844         self.assertNoOutput(out)
       
   845         self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
       
   846 
       
   847     def test_builtin_with_environment(self):
       
   848         "alternate: manage.py builtin commands fail if settings are provided in the environment but no defaults"
       
   849         args = ['sqlall','admin_scripts']
       
   850         out, err = self.run_manage(args,'alternate_settings')
       
   851         self.assertNoOutput(out)
       
   852         self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
       
   853 
       
   854     def test_builtin_with_bad_settings(self):
       
   855         "alternate: manage.py builtin commands fail if settings file (from argument) doesn't exist"
       
   856         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   857         out, err = self.run_manage(args)
       
   858         self.assertNoOutput(out)
       
   859         self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
       
   860 
       
   861     def test_builtin_with_bad_environment(self):
       
   862         "alternate: manage.py builtin commands fail if settings file (from environment) doesn't exist"
       
   863         args = ['sqlall','admin_scripts']
       
   864         out, err = self.run_manage(args,'bad_settings')
       
   865         self.assertNoOutput(out)
       
   866         self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
       
   867 
       
   868     def test_custom_command(self):
       
   869         "alternate: manage.py can't execute user commands"
       
   870         args = ['noargs_command']
       
   871         out, err = self.run_manage(args)
       
   872         self.assertNoOutput(out)
       
   873         self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
       
   874 
       
   875     def test_custom_command_with_settings(self):
       
   876         "alternate: manage.py can't execute user commands, even if settings are provided as argument"
       
   877         args = ['noargs_command', '--settings=alternate_settings']
       
   878         out, err = self.run_manage(args)
       
   879         self.assertNoOutput(out)
       
   880         self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
       
   881 
       
   882     def test_custom_command_with_environment(self):
       
   883         "alternate: manage.py can't execute user commands, even if settings are provided in environment"
       
   884         args = ['noargs_command']
       
   885         out, err = self.run_manage(args,'alternate_settings')
       
   886         self.assertNoOutput(out)
       
   887         self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
       
   888 
       
   889 
       
   890 class ManageMultipleSettings(AdminScriptTestCase):
       
   891     """A series of tests for manage.py when multiple settings files
       
   892     (including the default 'settings.py') are available. The default settings
       
   893     file is insufficient for performing the operations described, so the
       
   894     alternate settings must be used by the running script.
       
   895     """
       
   896     def setUp(self):
       
   897         self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes'])
       
   898         self.write_settings('alternate_settings.py')
       
   899 
       
   900     def tearDown(self):
       
   901         self.remove_settings('settings.py')
       
   902         self.remove_settings('alternate_settings.py')
       
   903 
       
   904     def test_builtin_command(self):
       
   905         "multiple: manage.py builtin commands fail with an import error when no settings provided"
       
   906         args = ['sqlall','admin_scripts']
       
   907         out, err = self.run_manage(args)
       
   908         self.assertNoOutput(out)
       
   909         self.assertOutput(err, 'App with label admin_scripts could not be found.')
       
   910 
       
   911     def test_builtin_with_settings(self):
       
   912         "multiple: manage.py builtin commands succeed if settings are provided as argument"
       
   913         args = ['sqlall','--settings=alternate_settings', 'admin_scripts']
       
   914         out, err = self.run_manage(args)
       
   915         self.assertNoOutput(err)
       
   916         self.assertOutput(out, 'CREATE TABLE')
       
   917 
       
   918     def test_builtin_with_environment(self):
       
   919         "multiple: manage.py builtin commands fail if settings are provided in the environment"
       
   920         # FIXME: This doesn't seem to be the correct output.
       
   921         args = ['sqlall','admin_scripts']
       
   922         out, err = self.run_manage(args,'alternate_settings')
       
   923         self.assertNoOutput(out)
       
   924         self.assertOutput(err, 'App with label admin_scripts could not be found.')
       
   925 
       
   926     def test_builtin_with_bad_settings(self):
       
   927         "multiple: manage.py builtin commands fail if settings file (from argument) doesn't exist"
       
   928         args = ['sqlall','--settings=bad_settings', 'admin_scripts']
       
   929         out, err = self.run_manage(args)
       
   930         self.assertNoOutput(out)
       
   931         self.assertOutput(err, "Could not import settings 'bad_settings'")
       
   932 
       
   933     def test_builtin_with_bad_environment(self):
       
   934         "multiple: manage.py builtin commands fail if settings file (from environment) doesn't exist"
       
   935         args = ['sqlall','admin_scripts']
       
   936         out, err = self.run_manage(args,'bad_settings')
       
   937         self.assertNoOutput(out)
       
   938         self.assertOutput(err, "App with label admin_scripts could not be found")
       
   939 
       
   940     def test_custom_command(self):
       
   941         "multiple: manage.py can't execute user commands using default settings"
       
   942         args = ['noargs_command']
       
   943         out, err = self.run_manage(args)
       
   944         self.assertNoOutput(out)
       
   945         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   946 
       
   947     def test_custom_command_with_settings(self):
       
   948         "multiple: manage.py can execute user commands if settings are provided as argument"
       
   949         args = ['noargs_command', '--settings=alternate_settings']
       
   950         out, err = self.run_manage(args)
       
   951         self.assertNoOutput(err)
       
   952         self.assertOutput(out, "EXECUTE:NoArgsCommand")
       
   953 
       
   954     def test_custom_command_with_environment(self):
       
   955         "multiple: manage.py can execute user commands if settings are provided in environment"
       
   956         args = ['noargs_command']
       
   957         out, err = self.run_manage(args,'alternate_settings')
       
   958         self.assertNoOutput(out)
       
   959         self.assertOutput(err, "Unknown command: 'noargs_command'")
       
   960 
       
   961 
       
   962 class ManageValidate(AdminScriptTestCase):
       
   963     def tearDown(self):
       
   964         self.remove_settings('settings.py')
       
   965 
       
   966     def test_nonexistent_app(self):
       
   967         "manage.py validate reports an error on a non-existent app in INSTALLED_APPS"
       
   968         self.write_settings('settings.py', apps=['admin_scriptz.broken_app'], sdict={'USE_I18N': False})
       
   969         args = ['validate']
       
   970         out, err = self.run_manage(args)
       
   971         self.assertNoOutput(out)
       
   972         self.assertOutput(err, 'No module named admin_scriptz')
       
   973 
       
   974     def test_broken_app(self):
       
   975         "manage.py validate reports an ImportError if an app's models.py raises one on import"
       
   976         self.write_settings('settings.py', apps=['admin_scripts.broken_app'])
       
   977         args = ['validate']
       
   978         out, err = self.run_manage(args)
       
   979         self.assertNoOutput(out)
       
   980         self.assertOutput(err, 'ImportError')
       
   981 
       
   982     def test_complex_app(self):
       
   983         "manage.py validate does not raise an ImportError validating a complex app with nested calls to load_app"
       
   984         self.write_settings('settings.py',
       
   985             apps=['admin_scripts.complex_app', 'admin_scripts.simple_app'],
       
   986             sdict={'DEBUG': True})
       
   987         args = ['validate']
       
   988         out, err = self.run_manage(args)
       
   989         self.assertNoOutput(err)
       
   990         self.assertOutput(out, '0 errors found')
       
   991 
       
   992     def test_app_with_import(self):
       
   993         "manage.py validate does not raise errors when an app imports a base class that itself has an abstract base"
       
   994         self.write_settings('settings.py',
       
   995             apps=['admin_scripts.app_with_import', 'django.contrib.comments'],
       
   996             sdict={'DEBUG': True})
       
   997         args = ['validate']
       
   998         out, err = self.run_manage(args)
       
   999         self.assertNoOutput(err)
       
  1000         self.assertOutput(out, '0 errors found')
       
  1001 
       
  1002 ##########################################################################
       
  1003 # COMMAND PROCESSING TESTS
       
  1004 # Check that user-space commands are correctly handled - in particular,
       
  1005 # that arguments to the commands are correctly parsed and processed.
       
  1006 ##########################################################################
       
  1007 
       
  1008 class CommandTypes(AdminScriptTestCase):
       
  1009     "Tests for the various types of base command types that can be defined."
       
  1010     def setUp(self):
       
  1011         self.write_settings('settings.py')
       
  1012 
       
  1013     def tearDown(self):
       
  1014         self.remove_settings('settings.py')
       
  1015 
       
  1016     def test_version(self):
       
  1017         "--version is handled as a special case"
       
  1018         args = ['--version']
       
  1019         out, err = self.run_manage(args)
       
  1020         self.assertNoOutput(err)
       
  1021         # Only check the first part of the version number
       
  1022         self.assertOutput(out, get_version().split('-')[0])
       
  1023 
       
  1024     def test_help(self):
       
  1025         "--help is handled as a special case"
       
  1026         args = ['--help']
       
  1027         out, err = self.run_manage(args)
       
  1028         if sys.version_info < (2, 5):
       
  1029             self.assertOutput(out, "usage: manage.py subcommand [options] [args]")
       
  1030         else:
       
  1031             self.assertOutput(out, "Usage: manage.py subcommand [options] [args]")
       
  1032         self.assertOutput(err, "Type 'manage.py help <subcommand>' for help on a specific subcommand.")
       
  1033 
       
  1034     def test_specific_help(self):
       
  1035         "--help can be used on a specific command"
       
  1036         args = ['sqlall','--help']
       
  1037         out, err = self.run_manage(args)
       
  1038         self.assertNoOutput(err)
       
  1039         self.assertOutput(out, "Prints the CREATE TABLE, custom SQL and CREATE INDEX SQL statements for the given model module name(s).")
       
  1040 
       
  1041     def test_base_command(self):
       
  1042         "User BaseCommands can execute when a label is provided"
       
  1043         args = ['base_command','testlabel']
       
  1044         out, err = self.run_manage(args)
       
  1045         self.assertNoOutput(err)
       
  1046         self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', '1'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1047 
       
  1048     def test_base_command_no_label(self):
       
  1049         "User BaseCommands can execute when no labels are provided"
       
  1050         args = ['base_command']
       
  1051         out, err = self.run_manage(args)
       
  1052         self.assertNoOutput(err)
       
  1053         self.assertOutput(out, "EXECUTE:BaseCommand labels=(), options=[('option_a', '1'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1054 
       
  1055     def test_base_command_multiple_label(self):
       
  1056         "User BaseCommands can execute when no labels are provided"
       
  1057         args = ['base_command','testlabel','anotherlabel']
       
  1058         out, err = self.run_manage(args)
       
  1059         self.assertNoOutput(err)
       
  1060         self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel', 'anotherlabel'), options=[('option_a', '1'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1061 
       
  1062     def test_base_command_with_option(self):
       
  1063         "User BaseCommands can execute with options when a label is provided"
       
  1064         args = ['base_command','testlabel','--option_a=x']
       
  1065         out, err = self.run_manage(args)
       
  1066         self.assertNoOutput(err)
       
  1067         self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1068 
       
  1069     def test_base_command_with_options(self):
       
  1070         "User BaseCommands can execute with multiple options when a label is provided"
       
  1071         args = ['base_command','testlabel','-a','x','--option_b=y']
       
  1072         out, err = self.run_manage(args)
       
  1073         self.assertNoOutput(err)
       
  1074         self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', 'y'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1075 
       
  1076     def test_noargs(self):
       
  1077         "NoArg Commands can be executed"
       
  1078         args = ['noargs_command']
       
  1079         out, err = self.run_manage(args)
       
  1080         self.assertNoOutput(err)
       
  1081         self.assertOutput(out, "EXECUTE:NoArgsCommand options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1082 
       
  1083     def test_noargs_with_args(self):
       
  1084         "NoArg Commands raise an error if an argument is provided"
       
  1085         args = ['noargs_command','argument']
       
  1086         out, err = self.run_manage(args)
       
  1087         self.assertOutput(err, "Error: Command doesn't accept any arguments")
       
  1088 
       
  1089     def test_app_command(self):
       
  1090         "User AppCommands can execute when a single app name is provided"
       
  1091         args = ['app_command', 'auth']
       
  1092         out, err = self.run_manage(args)
       
  1093         self.assertNoOutput(err)
       
  1094         self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.auth.models'")
       
  1095         self.assertOutput(out, os.sep.join(['django','contrib','auth','models.py']))
       
  1096         self.assertOutput(out, "'>, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1097 
       
  1098     def test_app_command_no_apps(self):
       
  1099         "User AppCommands raise an error when no app name is provided"
       
  1100         args = ['app_command']
       
  1101         out, err = self.run_manage(args)
       
  1102         self.assertOutput(err, 'Error: Enter at least one appname.')
       
  1103 
       
  1104     def test_app_command_multiple_apps(self):
       
  1105         "User AppCommands raise an error when multiple app names are provided"
       
  1106         args = ['app_command','auth','contenttypes']
       
  1107         out, err = self.run_manage(args)
       
  1108         self.assertNoOutput(err)
       
  1109         self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.auth.models'")
       
  1110         self.assertOutput(out, os.sep.join(['django','contrib','auth','models.py']))
       
  1111         self.assertOutput(out, "'>, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1112         self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.contenttypes.models'")
       
  1113         self.assertOutput(out, os.sep.join(['django','contrib','contenttypes','models.py']))
       
  1114         self.assertOutput(out, "'>, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1115 
       
  1116     def test_app_command_invalid_appname(self):
       
  1117         "User AppCommands can execute when a single app name is provided"
       
  1118         args = ['app_command', 'NOT_AN_APP']
       
  1119         out, err = self.run_manage(args)
       
  1120         self.assertOutput(err, "App with label NOT_AN_APP could not be found")
       
  1121 
       
  1122     def test_app_command_some_invalid_appnames(self):
       
  1123         "User AppCommands can execute when some of the provided app names are invalid"
       
  1124         args = ['app_command', 'auth', 'NOT_AN_APP']
       
  1125         out, err = self.run_manage(args)
       
  1126         self.assertOutput(err, "App with label NOT_AN_APP could not be found")
       
  1127 
       
  1128     def test_label_command(self):
       
  1129         "User LabelCommands can execute when a label is provided"
       
  1130         args = ['label_command','testlabel']
       
  1131         out, err = self.run_manage(args)
       
  1132         self.assertNoOutput(err)
       
  1133         self.assertOutput(out, "EXECUTE:LabelCommand label=testlabel, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1134 
       
  1135     def test_label_command_no_label(self):
       
  1136         "User LabelCommands raise an error if no label is provided"
       
  1137         args = ['label_command']
       
  1138         out, err = self.run_manage(args)
       
  1139         self.assertOutput(err, 'Enter at least one label')
       
  1140 
       
  1141     def test_label_command_multiple_label(self):
       
  1142         "User LabelCommands are executed multiple times if multiple labels are provided"
       
  1143         args = ['label_command','testlabel','anotherlabel']
       
  1144         out, err = self.run_manage(args)
       
  1145         self.assertNoOutput(err)
       
  1146         self.assertOutput(out, "EXECUTE:LabelCommand label=testlabel, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1147         self.assertOutput(out, "EXECUTE:LabelCommand label=anotherlabel, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
       
  1148 
       
  1149 class ArgumentOrder(AdminScriptTestCase):
       
  1150     """Tests for 2-stage argument parsing scheme.
       
  1151 
       
  1152     django-admin command arguments are parsed in 2 parts; the core arguments
       
  1153     (--settings, --traceback and --pythonpath) are parsed using a Lax parser.
       
  1154     This Lax parser ignores any unknown options. Then the full settings are
       
  1155     passed to the command parser, which extracts commands of interest to the
       
  1156     individual command.
       
  1157     """
       
  1158     def setUp(self):
       
  1159         self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes'])
       
  1160         self.write_settings('alternate_settings.py')
       
  1161 
       
  1162     def tearDown(self):
       
  1163         self.remove_settings('settings.py')
       
  1164         self.remove_settings('alternate_settings.py')
       
  1165 
       
  1166     def test_setting_then_option(self):
       
  1167         "Options passed after settings are correctly handled"
       
  1168         args = ['base_command','testlabel','--settings=alternate_settings','--option_a=x']
       
  1169         out, err = self.run_manage(args)
       
  1170         self.assertNoOutput(err)
       
  1171         self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")
       
  1172 
       
  1173     def test_setting_then_short_option(self):
       
  1174         "Short options passed after settings are correctly handled"
       
  1175         args = ['base_command','testlabel','--settings=alternate_settings','--option_a=x']
       
  1176         out, err = self.run_manage(args)
       
  1177         self.assertNoOutput(err)
       
  1178         self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")
       
  1179 
       
  1180     def test_option_then_setting(self):
       
  1181         "Options passed before settings are correctly handled"
       
  1182         args = ['base_command','testlabel','--option_a=x','--settings=alternate_settings']
       
  1183         out, err = self.run_manage(args)
       
  1184         self.assertNoOutput(err)
       
  1185         self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")
       
  1186 
       
  1187     def test_short_option_then_setting(self):
       
  1188         "Short options passed before settings are correctly handled"
       
  1189         args = ['base_command','testlabel','-a','x','--settings=alternate_settings']
       
  1190         out, err = self.run_manage(args)
       
  1191         self.assertNoOutput(err)
       
  1192         self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")
       
  1193 
       
  1194     def test_option_then_setting_then_option(self):
       
  1195         "Options are correctly handled when they are passed before and after a setting"
       
  1196         args = ['base_command','testlabel','--option_a=x','--settings=alternate_settings','--option_b=y']
       
  1197         out, err = self.run_manage(args)
       
  1198         self.assertNoOutput(err)
       
  1199         self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', 'y'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")