~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to tests/regressiontests/admin_scripts/tests.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2008-11-15 19:15:33 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20081115191533-xbt1ut2xf4fvwtvc
Tags: 1.0.1-0ubuntu1
* New upstream release:
  - Bug fixes.

* The tests/ sub-directory appaers to have been dropped upstream, so pull
  our patch to workaround the tests and modify the rules.

Show diffs side-by-side

added added

removed removed

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