| 1 | import os |
| 2 | import sys |
| 3 | import threading |
| 4 | import unittest |
| 5 | |
| 6 | from django import conf, bin |
| 7 | from django.conf import settings |
| 8 | from django.core import exceptions |
| 9 | from django.db.models import loading |
| 10 | from django.utils.datastructures import SortedDict |
| 11 | |
| 12 | import shutil |
| 13 | |
| 14 | class AdminScriptTestCase(unittest.TestCase): |
| 15 | def write_settings(self, filename, apps=None): |
| 16 | test_dir = os.path.dirname(os.path.dirname(__file__)) |
| 17 | settings_file = open(os.path.join(test_dir,filename), 'w') |
| 18 | settings_file.write('# Settings file automatically generated by regressiontests.admin_scripts test case\n') |
| 19 | exports = [ |
| 20 | 'DATABASE_ENGINE', |
| 21 | 'DATABASE_NAME', |
| 22 | 'DATABASE_USER', |
| 23 | 'DATABASE_PASSWORD', |
| 24 | 'DATABASE_HOST', |
| 25 | 'DATABASE_PORT', |
| 26 | 'ROOT_URLCONF' |
| 27 | ] |
| 28 | for s in exports: |
| 29 | if hasattr(settings,s): |
| 30 | settings_file.write("%s = '%s'\n" % (s, str(getattr(settings,s)))) |
| 31 | |
| 32 | if apps is None: |
| 33 | apps = ['django.contrib.auth', 'django.contrib.contenttypes', 'regressiontests.admin_scripts'] |
| 34 | |
| 35 | if apps: |
| 36 | settings_file.write("INSTALLED_APPS = %s\n" % apps) |
| 37 | |
| 38 | settings_file.close() |
| 39 | |
| 40 | def remove_settings(self, filename): |
| 41 | test_dir = os.path.dirname(os.path.dirname(__file__)) |
| 42 | os.remove(os.path.join(test_dir, filename)) |
| 43 | # Also try to remove the pyc file; if it exists, it could |
| 44 | # mess up later tests that depend upon the .py file not existing |
| 45 | try: |
| 46 | os.remove(os.path.join(test_dir, filename + 'c')) |
| 47 | except OSError: |
| 48 | pass |
| 49 | |
| 50 | def run_test(self, script, args, settings_file=None, apps=None): |
| 51 | test_dir = os.path.dirname(os.path.dirname(__file__)) |
| 52 | project_dir = os.path.dirname(test_dir) |
| 53 | base_dir = os.path.dirname(project_dir) |
| 54 | |
| 55 | # Build the command line |
| 56 | cmd = 'python "%s"' % script |
| 57 | cmd += ''.join(' %s' % arg for arg in args) |
| 58 | |
| 59 | # Remember the old environment |
| 60 | old_django_settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', None) |
| 61 | old_python_path = os.environ.get('PYTHONPATH', None) |
| 62 | |
| 63 | # Set the test environment |
| 64 | if settings_file: |
| 65 | os.environ['DJANGO_SETTINGS_MODULE'] = settings_file |
| 66 | elif 'DJANGO_SETTINGS_MODULE' in os.environ: |
| 67 | del os.environ['DJANGO_SETTINGS_MODULE'] |
| 68 | |
| 69 | os.environ['PYTHONPATH'] = os.pathsep.join([project_dir,base_dir]) |
| 70 | |
| 71 | # Move to the test directory and run |
| 72 | os.chdir(test_dir) |
| 73 | stdin, stdout, stderr = os.popen3(cmd) |
| 74 | out, err = stdout.read(), stderr.read() |
| 75 | |
| 76 | # Restore the old environment |
| 77 | if old_django_settings_module: |
| 78 | os.environ['DJANGO_SETTINGS_MODULE'] = old_django_settings_module |
| 79 | if old_python_path: |
| 80 | os.environ['PYTHONPATH'] = old_python_path |
| 81 | |
| 82 | return out, err |
| 83 | |
| 84 | def run_django_admin(self, args, settings_file=None): |
| 85 | bin_dir = os.path.dirname(bin.__file__) |
| 86 | return self.run_test(os.path.join(bin_dir,'django-admin.py'), args, settings_file) |
| 87 | |
| 88 | def run_manage(self, args, settings_file=None): |
| 89 | conf_dir = os.path.dirname(conf.__file__) |
| 90 | template_manage_py = os.path.join(conf_dir, 'project_template', 'manage.py') |
| 91 | |
| 92 | test_dir = os.path.dirname(os.path.dirname(__file__)) |
| 93 | test_manage_py = os.path.join(test_dir, 'manage.py') |
| 94 | shutil.copyfile(template_manage_py, test_manage_py) |
| 95 | |
| 96 | stdout, stderr = self.run_test('./manage.py', args, settings_file) |
| 97 | |
| 98 | # Cleanup - remove the generated manage.py script |
| 99 | os.remove(test_manage_py) |
| 100 | |
| 101 | return stdout, stderr |
| 102 | |
| 103 | def assertNoOutput(self, stream): |
| 104 | "Utility assertion: assert that the given stream is empty" |
| 105 | self.assertEquals(len(stream), 0, "Stream should be empty: actually contains '%s'" % stream) |
| 106 | def assertOutput(self, stream, msg): |
| 107 | "Utility assertion: assert that the given message exists in the output" |
| 108 | self.assertNotEquals(stream.find(msg), -1, |
| 109 | "'%s' does not match actual output text '%s'" % (msg, stream)) |
| 110 | |
| 111 | ########################################################################## |
| 112 | # DJANGO ADMIN TESTS |
| 113 | # This first series of test classes checks the environment processing |
| 114 | # of the django-admin.py script |
| 115 | ########################################################################## |
| 116 | |
| 117 | class DjangoAdminDefaultSettings(AdminScriptTestCase): |
| 118 | """A series of tests for django-admin.py when using a settings.py file that |
| 119 | contains the test application. |
| 120 | """ |
| 121 | def setUp(self): |
| 122 | self.write_settings('settings.py') |
| 123 | |
| 124 | def tearDown(self): |
| 125 | self.remove_settings('settings.py') |
| 126 | |
| 127 | def test_builtin_command(self): |
| 128 | "default: django-admin builtin commands fail with an import error when no settings provided" |
| 129 | args = ['sqlall','admin_scripts'] |
| 130 | out, err = self.run_django_admin(args) |
| 131 | self.assertNoOutput(out) |
| 132 | self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') |
| 133 | |
| 134 | def test_builtin_with_settings(self): |
| 135 | "default: django-admin builtin commands succeed if settings are provided as argument" |
| 136 | args = ['sqlall','--settings=regressiontests.settings', 'admin_scripts'] |
| 137 | out, err = self.run_django_admin(args) |
| 138 | self.assertNoOutput(err) |
| 139 | self.assertOutput(out, 'CREATE TABLE') |
| 140 | |
| 141 | def test_builtin_with_environment(self): |
| 142 | "default: django-admin builtin commands succeed if settings are provided in the environment" |
| 143 | args = ['sqlall','admin_scripts'] |
| 144 | out, err = self.run_django_admin(args,'regressiontests.settings') |
| 145 | self.assertNoOutput(err) |
| 146 | self.assertOutput(out, 'CREATE TABLE') |
| 147 | |
| 148 | def test_builtin_with_bad_settings(self): |
| 149 | "default: django-admin builtin commands succeed if settings file (from argument) doesn't exist" |
| 150 | args = ['sqlall','--settings=regressiontests.bad_settings', 'admin_scripts'] |
| 151 | out, err = self.run_django_admin(args) |
| 152 | self.assertNoOutput(out) |
| 153 | self.assertOutput(err, "Could not import settings 'regressiontests.bad_settings'") |
| 154 | |
| 155 | def test_builtin_with_bad_environment(self): |
| 156 | "default: django-admin builtin commands fail if settings file (from environment) doesn't exist" |
| 157 | args = ['sqlall','admin_scripts'] |
| 158 | out, err = self.run_django_admin(args,'regressiontests.bad_settings') |
| 159 | self.assertNoOutput(out) |
| 160 | self.assertOutput(err, "Could not import settings 'regressiontests.bad_settings'") |
| 161 | |
| 162 | def test_custom_command(self): |
| 163 | "default: django-admin can't execute user commands" |
| 164 | args = ['noargs_command'] |
| 165 | out, err = self.run_django_admin(args) |
| 166 | self.assertNoOutput(out) |
| 167 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 168 | |
| 169 | def test_custom_command_with_settings(self): |
| 170 | "default: django-admin can't execute user commands, even if settings are provided as argument" |
| 171 | args = ['noargs_command', '--settings=regressiontests.settings'] |
| 172 | out, err = self.run_django_admin(args) |
| 173 | self.assertNoOutput(out) |
| 174 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 175 | |
| 176 | def test_custom_command_with_environment(self): |
| 177 | "default: django-admin can't execute user commands, even if settings are provided in environment" |
| 178 | args = ['noargs_command'] |
| 179 | out, err = self.run_django_admin(args,'regressiontests.settings') |
| 180 | self.assertNoOutput(out) |
| 181 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 182 | |
| 183 | class DjangoAdminMinimalSettings(AdminScriptTestCase): |
| 184 | """A series of tests for django-admin.py when using a settings.py file that |
| 185 | doesn't contain the test application. |
| 186 | """ |
| 187 | def setUp(self): |
| 188 | self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) |
| 189 | |
| 190 | def tearDown(self): |
| 191 | self.remove_settings('settings.py') |
| 192 | |
| 193 | def test_builtin_command(self): |
| 194 | "minimal: django-admin builtin commands fail with an import error when no settings provided" |
| 195 | args = ['sqlall','admin_scripts'] |
| 196 | out, err = self.run_django_admin(args) |
| 197 | self.assertNoOutput(out) |
| 198 | self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') |
| 199 | |
| 200 | def test_builtin_with_settings(self): |
| 201 | "minimal: django-admin builtin commands fail if settings are provided as argument" |
| 202 | args = ['sqlall','--settings=regressiontests.settings', 'admin_scripts'] |
| 203 | out, err = self.run_django_admin(args) |
| 204 | self.assertNoOutput(out) |
| 205 | self.assertOutput(err, 'App with label admin_scripts could not be found') |
| 206 | |
| 207 | def test_builtin_with_environment(self): |
| 208 | "minimal: django-admin builtin commands fail if settings are provided in the environment" |
| 209 | args = ['sqlall','admin_scripts'] |
| 210 | out, err = self.run_django_admin(args,'regressiontests.settings') |
| 211 | self.assertNoOutput(out) |
| 212 | self.assertOutput(err, 'App with label admin_scripts could not be found') |
| 213 | |
| 214 | def test_builtin_with_bad_settings(self): |
| 215 | "minimal: django-admin builtin commands succeed if settings file (from argument) doesn't exist" |
| 216 | args = ['sqlall','--settings=regressiontests.bad_settings', 'admin_scripts'] |
| 217 | out, err = self.run_django_admin(args) |
| 218 | self.assertNoOutput(out) |
| 219 | self.assertOutput(err, "Could not import settings 'regressiontests.bad_settings'") |
| 220 | |
| 221 | def test_builtin_with_bad_environment(self): |
| 222 | "minimal: django-admin builtin commands succeed if settings file (from environment) doesn't exist" |
| 223 | args = ['sqlall','admin_scripts'] |
| 224 | out, err = self.run_django_admin(args,'regressiontests.bad_settings') |
| 225 | self.assertNoOutput(out) |
| 226 | self.assertOutput(err, "Could not import settings 'regressiontests.bad_settings'") |
| 227 | |
| 228 | def test_custom_command(self): |
| 229 | "minimal: django-admin can't execute user commands" |
| 230 | args = ['noargs_command'] |
| 231 | out, err = self.run_django_admin(args) |
| 232 | self.assertNoOutput(out) |
| 233 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 234 | |
| 235 | def test_custom_command_with_settings(self): |
| 236 | "minimal: django-admin can't execute user commands, even if settings are provided as argument" |
| 237 | args = ['noargs_command', '--settings=regressiontests.settings'] |
| 238 | out, err = self.run_django_admin(args) |
| 239 | self.assertNoOutput(out) |
| 240 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 241 | |
| 242 | def test_custom_command_with_environment(self): |
| 243 | "minimal: django-admin can't execute user commands, even if settings are provided in environment" |
| 244 | args = ['noargs_command'] |
| 245 | out, err = self.run_django_admin(args,'regressiontests.settings') |
| 246 | self.assertNoOutput(out) |
| 247 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 248 | |
| 249 | class DjangoAdminAlternateSettings(AdminScriptTestCase): |
| 250 | """A series of tests for django-admin.py when using a settings file |
| 251 | with a name other than 'settings.py'. |
| 252 | """ |
| 253 | def setUp(self): |
| 254 | self.write_settings('alternate_settings.py') |
| 255 | |
| 256 | def tearDown(self): |
| 257 | self.remove_settings('alternate_settings.py') |
| 258 | |
| 259 | def test_builtin_command(self): |
| 260 | "alternate: django-admin builtin commands fail with an import error when no settings provided" |
| 261 | args = ['sqlall','admin_scripts'] |
| 262 | out, err = self.run_django_admin(args) |
| 263 | self.assertNoOutput(out) |
| 264 | self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') |
| 265 | |
| 266 | def test_builtin_with_settings(self): |
| 267 | "alternate: django-admin builtin commands succeed if settings are provided as argument" |
| 268 | args = ['sqlall','--settings=regressiontests.alternate_settings', 'admin_scripts'] |
| 269 | out, err = self.run_django_admin(args) |
| 270 | self.assertNoOutput(err) |
| 271 | self.assertOutput(out, 'CREATE TABLE') |
| 272 | |
| 273 | def test_builtin_with_environment(self): |
| 274 | "alternate: django-admin builtin commands succeed if settings are provided in the environment" |
| 275 | args = ['sqlall','admin_scripts'] |
| 276 | out, err = self.run_django_admin(args,'regressiontests.alternate_settings') |
| 277 | self.assertNoOutput(err) |
| 278 | self.assertOutput(out, 'CREATE TABLE') |
| 279 | |
| 280 | def test_builtin_with_bad_settings(self): |
| 281 | "alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist" |
| 282 | args = ['sqlall','--settings=regressiontests.bad_settings', 'admin_scripts'] |
| 283 | out, err = self.run_django_admin(args) |
| 284 | self.assertNoOutput(out) |
| 285 | self.assertOutput(err, "Could not import settings 'regressiontests.bad_settings'") |
| 286 | |
| 287 | def test_builtin_with_bad_environment(self): |
| 288 | "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist" |
| 289 | args = ['sqlall','admin_scripts'] |
| 290 | out, err = self.run_django_admin(args,'regressiontests.bad_settings') |
| 291 | self.assertNoOutput(out) |
| 292 | self.assertOutput(err, "Could not import settings 'regressiontests.bad_settings'") |
| 293 | |
| 294 | def test_custom_command(self): |
| 295 | "alternate: django-admin can't execute user commands" |
| 296 | args = ['noargs_command'] |
| 297 | out, err = self.run_django_admin(args) |
| 298 | self.assertNoOutput(out) |
| 299 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 300 | |
| 301 | def test_custom_command_with_settings(self): |
| 302 | "alternate: django-admin can't execute user commands, even if explicit settings are provided" |
| 303 | args = ['noargs_command', '--settings=regressiontests.alternate_settings'] |
| 304 | out, err = self.run_django_admin(args) |
| 305 | self.assertNoOutput(out) |
| 306 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 307 | |
| 308 | def test_custom_command_with_environment(self): |
| 309 | "alternate: django-admin can't execute user commands, even if an explicit environment is provided" |
| 310 | args = ['noargs_command'] |
| 311 | out, err = self.run_django_admin(args,'regressiontests.alternate_settings') |
| 312 | self.assertNoOutput(out) |
| 313 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 314 | |
| 315 | |
| 316 | class DjangoAdminMultipleSettings(AdminScriptTestCase): |
| 317 | """A series of tests for django-admin.py when multiple settings files |
| 318 | (including the default 'settings.py') are available. The default settings |
| 319 | file is insufficient for performing the operations described, so the |
| 320 | alternate settings must be used by the running script. |
| 321 | """ |
| 322 | def setUp(self): |
| 323 | self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) |
| 324 | self.write_settings('alternate_settings.py') |
| 325 | |
| 326 | def tearDown(self): |
| 327 | self.remove_settings('settings.py') |
| 328 | self.remove_settings('alternate_settings.py') |
| 329 | |
| 330 | def test_builtin_command(self): |
| 331 | "alternate: django-admin builtin commands fail with an import error when no settings provided" |
| 332 | args = ['sqlall','admin_scripts'] |
| 333 | out, err = self.run_django_admin(args) |
| 334 | self.assertNoOutput(out) |
| 335 | self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') |
| 336 | |
| 337 | def test_builtin_with_settings(self): |
| 338 | "alternate: django-admin builtin commands succeed if settings are provided as argument" |
| 339 | args = ['sqlall','--settings=regressiontests.alternate_settings', 'admin_scripts'] |
| 340 | out, err = self.run_django_admin(args) |
| 341 | self.assertNoOutput(err) |
| 342 | self.assertOutput(out, 'CREATE TABLE') |
| 343 | |
| 344 | def test_builtin_with_environment(self): |
| 345 | "alternate: django-admin builtin commands succeed if settings are provided in the environment" |
| 346 | args = ['sqlall','admin_scripts'] |
| 347 | out, err = self.run_django_admin(args,'regressiontests.alternate_settings') |
| 348 | self.assertNoOutput(err) |
| 349 | self.assertOutput(out, 'CREATE TABLE') |
| 350 | |
| 351 | def test_builtin_with_bad_settings(self): |
| 352 | "alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist" |
| 353 | args = ['sqlall','--settings=regressiontests.bad_settings', 'admin_scripts'] |
| 354 | out, err = self.run_django_admin(args) |
| 355 | self.assertOutput(err, "Could not import settings 'regressiontests.bad_settings'") |
| 356 | |
| 357 | def test_builtin_with_bad_environment(self): |
| 358 | "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist" |
| 359 | args = ['sqlall','admin_scripts'] |
| 360 | out, err = self.run_django_admin(args,'regressiontests.bad_settings') |
| 361 | self.assertNoOutput(out) |
| 362 | self.assertOutput(err, "Could not import settings 'regressiontests.bad_settings'") |
| 363 | |
| 364 | def test_custom_command(self): |
| 365 | "alternate: django-admin can't execute user commands" |
| 366 | args = ['noargs_command'] |
| 367 | out, err = self.run_django_admin(args) |
| 368 | self.assertNoOutput(out) |
| 369 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 370 | |
| 371 | def test_custom_command_with_settings(self): |
| 372 | "alternate: django-admin can't execute user commands, even if explicit settings are provided" |
| 373 | args = ['noargs_command', '--settings=regressiontests.alternate_settings'] |
| 374 | out, err = self.run_django_admin(args) |
| 375 | self.assertNoOutput(out) |
| 376 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 377 | |
| 378 | def test_custom_command_with_environment(self): |
| 379 | "alternate: django-admin can't execute user commands, even if an explicit environment is provided" |
| 380 | args = ['noargs_command'] |
| 381 | out, err = self.run_django_admin(args,'regressiontests.alternate_settings') |
| 382 | self.assertNoOutput(out) |
| 383 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 384 | |
| 385 | ########################################################################## |
| 386 | # MANAGE.PY TESTS |
| 387 | # This next series of test classes checks the environment processing |
| 388 | # of the generated manage.py script |
| 389 | ########################################################################## |
| 390 | |
| 391 | class ManageDefaultSettings(AdminScriptTestCase): |
| 392 | """A series of tests for manage.py when using a settings.py file that |
| 393 | contains the test application. |
| 394 | """ |
| 395 | def setUp(self): |
| 396 | self.write_settings('settings.py') |
| 397 | |
| 398 | def tearDown(self): |
| 399 | self.remove_settings('settings.py') |
| 400 | |
| 401 | def test_builtin_command(self): |
| 402 | "default: manage.py builtin commands fail with an import error when no settings provided" |
| 403 | args = ['sqlall','admin_scripts'] |
| 404 | out, err = self.run_manage(args) |
| 405 | self.assertNoOutput(err) |
| 406 | self.assertOutput(out, 'CREATE TABLE') |
| 407 | |
| 408 | def test_builtin_with_settings(self): |
| 409 | "default: manage.py builtin commands succeed if settings are provided as argument" |
| 410 | args = ['sqlall','--settings=settings', 'admin_scripts'] |
| 411 | out, err = self.run_manage(args) |
| 412 | self.assertNoOutput(err) |
| 413 | self.assertOutput(out, 'CREATE TABLE') |
| 414 | |
| 415 | def test_builtin_with_environment(self): |
| 416 | "default: manage.py builtin commands succeed if settings are provided in the environment" |
| 417 | args = ['sqlall','admin_scripts'] |
| 418 | out, err = self.run_manage(args,'settings') |
| 419 | self.assertNoOutput(err) |
| 420 | self.assertOutput(out, 'CREATE TABLE') |
| 421 | |
| 422 | def test_builtin_with_bad_settings(self): |
| 423 | "default: manage.py builtin commands succeed if settings file (from argument) doesn't exist" |
| 424 | args = ['sqlall','--settings=bad_settings', 'admin_scripts'] |
| 425 | out, err = self.run_manage(args) |
| 426 | self.assertNoOutput(out) |
| 427 | self.assertOutput(err, "Could not import settings 'bad_settings'") |
| 428 | |
| 429 | def test_builtin_with_bad_environment(self): |
| 430 | "default: manage.py builtin commands fail if settings file (from environment) doesn't exist" |
| 431 | args = ['sqlall','admin_scripts'] |
| 432 | out, err = self.run_manage(args,'bad_settings') |
| 433 | self.assertNoOutput(err) |
| 434 | self.assertOutput(out, 'CREATE TABLE') |
| 435 | |
| 436 | def test_custom_command(self): |
| 437 | "default: manage.py can execute user commands" |
| 438 | args = ['noargs_command'] |
| 439 | out, err = self.run_manage(args) |
| 440 | self.assertNoOutput(err) |
| 441 | self.assertOutput(out, "EXECUTE:NoArgsCommand") |
| 442 | |
| 443 | def test_custom_command_with_settings(self): |
| 444 | "default: manage.py can execute user commands when settings are provided as argument" |
| 445 | args = ['noargs_command', '--settings=settings'] |
| 446 | out, err = self.run_manage(args) |
| 447 | self.assertNoOutput(err) |
| 448 | self.assertOutput(out, "EXECUTE:NoArgsCommand") |
| 449 | |
| 450 | def test_custom_command_with_environment(self): |
| 451 | "default: manage.py can execute user commands when settings are provided in environment" |
| 452 | args = ['noargs_command'] |
| 453 | out, err = self.run_manage(args,'settings') |
| 454 | self.assertNoOutput(err) |
| 455 | self.assertOutput(out, "EXECUTE:NoArgsCommand") |
| 456 | |
| 457 | class ManageMinimalSettings(AdminScriptTestCase): |
| 458 | """A series of tests for manage.py when using a settings.py file that |
| 459 | doesn't contain the test application. |
| 460 | """ |
| 461 | def setUp(self): |
| 462 | self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) |
| 463 | |
| 464 | def tearDown(self): |
| 465 | self.remove_settings('settings.py') |
| 466 | |
| 467 | def test_builtin_command(self): |
| 468 | "minimal: manage.py builtin commands fail with an import error when no settings provided" |
| 469 | args = ['sqlall','admin_scripts'] |
| 470 | out, err = self.run_manage(args) |
| 471 | self.assertNoOutput(out) |
| 472 | self.assertOutput(err, 'App with label admin_scripts could not be found') |
| 473 | |
| 474 | def test_builtin_with_settings(self): |
| 475 | "minimal: manage.py builtin commands fail if settings are provided as argument" |
| 476 | args = ['sqlall','--settings=settings', 'admin_scripts'] |
| 477 | out, err = self.run_manage(args) |
| 478 | self.assertNoOutput(out) |
| 479 | self.assertOutput(err, 'App with label admin_scripts could not be found') |
| 480 | |
| 481 | def test_builtin_with_environment(self): |
| 482 | "minimal: manage.py builtin commands fail if settings are provided in the environment" |
| 483 | args = ['sqlall','admin_scripts'] |
| 484 | out, err = self.run_manage(args,'settings') |
| 485 | self.assertNoOutput(out) |
| 486 | self.assertOutput(err, 'App with label admin_scripts could not be found') |
| 487 | |
| 488 | def test_builtin_with_bad_settings(self): |
| 489 | "minimal: manage.py builtin commands succeed if settings file (from argument) doesn't exist" |
| 490 | args = ['sqlall','--settings=bad_settings', 'admin_scripts'] |
| 491 | out, err = self.run_manage(args) |
| 492 | self.assertNoOutput(out) |
| 493 | self.assertOutput(err, "Could not import settings 'bad_settings'") |
| 494 | |
| 495 | def test_builtin_with_bad_environment(self): |
| 496 | "minimal: manage.py builtin commands succeed if settings file (from environment) doesn't exist" |
| 497 | args = ['sqlall','admin_scripts'] |
| 498 | out, err = self.run_manage(args,'bad_settings') |
| 499 | self.assertNoOutput(out) |
| 500 | self.assertOutput(err, 'App with label admin_scripts could not be found') |
| 501 | |
| 502 | def test_custom_command(self): |
| 503 | "minimal: manage.py can't execute user commands" |
| 504 | args = ['noargs_command'] |
| 505 | out, err = self.run_manage(args) |
| 506 | self.assertNoOutput(out) |
| 507 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 508 | |
| 509 | def test_custom_command_with_settings(self): |
| 510 | "minimal: manage.py can't execute user commands, even if settings are provided as argument" |
| 511 | args = ['noargs_command', '--settings=settings'] |
| 512 | out, err = self.run_manage(args) |
| 513 | self.assertNoOutput(out) |
| 514 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 515 | |
| 516 | def test_custom_command_with_environment(self): |
| 517 | "minimal: manage.py can't execute user commands, even if settings are provided in environment" |
| 518 | args = ['noargs_command'] |
| 519 | out, err = self.run_manage(args,'settings') |
| 520 | self.assertNoOutput(out) |
| 521 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 522 | |
| 523 | class ManageAlternateSettings(AdminScriptTestCase): |
| 524 | """A series of tests for manage.py when using a settings file |
| 525 | with a name other than 'settings.py'. |
| 526 | """ |
| 527 | def setUp(self): |
| 528 | self.write_settings('alternate_settings.py') |
| 529 | |
| 530 | def tearDown(self): |
| 531 | self.remove_settings('alternate_settings.py') |
| 532 | |
| 533 | def test_builtin_command(self): |
| 534 | "alternate: manage.py builtin commands fail with an import error when no settings provided" |
| 535 | args = ['sqlall','admin_scripts'] |
| 536 | out, err = self.run_manage(args) |
| 537 | self.assertNoOutput(out) |
| 538 | self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'") |
| 539 | |
| 540 | def test_builtin_with_settings(self): |
| 541 | "alternate: manage.py builtin commands succeed if settings are provided as argument" |
| 542 | args = ['sqlall','--settings=alternate_settings', 'admin_scripts'] |
| 543 | out, err = self.run_manage(args) |
| 544 | self.assertNoOutput(out) |
| 545 | self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'") |
| 546 | |
| 547 | def test_builtin_with_environment(self): |
| 548 | "alternate: manage.py builtin commands succeed if settings are provided in the environment" |
| 549 | args = ['sqlall','admin_scripts'] |
| 550 | out, err = self.run_manage(args,'alternate_settings') |
| 551 | self.assertNoOutput(out) |
| 552 | self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'") |
| 553 | |
| 554 | def test_builtin_with_bad_settings(self): |
| 555 | "alternate: manage.py builtin commands fail if settings file (from argument) doesn't exist" |
| 556 | args = ['sqlall','--settings=bad_settings', 'admin_scripts'] |
| 557 | out, err = self.run_manage(args) |
| 558 | self.assertNoOutput(out) |
| 559 | self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'") |
| 560 | |
| 561 | def test_builtin_with_bad_environment(self): |
| 562 | "alternate: manage.py builtin commands fail if settings file (from environment) doesn't exist" |
| 563 | args = ['sqlall','admin_scripts'] |
| 564 | out, err = self.run_manage(args,'bad_settings') |
| 565 | self.assertNoOutput(out) |
| 566 | self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'") |
| 567 | |
| 568 | def test_custom_command(self): |
| 569 | "alternate: manage.py can't execute user commands" |
| 570 | args = ['noargs_command'] |
| 571 | out, err = self.run_manage(args) |
| 572 | self.assertNoOutput(out) |
| 573 | self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'") |
| 574 | |
| 575 | def test_custom_command_with_settings(self): |
| 576 | "alternate: manage.py can't execute user commands, even if settings are provided as argument" |
| 577 | args = ['noargs_command', '--settings=alternate_settings'] |
| 578 | out, err = self.run_manage(args) |
| 579 | self.assertNoOutput(out) |
| 580 | self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'") |
| 581 | |
| 582 | def test_custom_command_with_environment(self): |
| 583 | "alternate: manage.py can't execute user commands, even if settings are provided in environment" |
| 584 | args = ['noargs_command'] |
| 585 | out, err = self.run_manage(args,'alternate_settings') |
| 586 | self.assertNoOutput(out) |
| 587 | self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'") |
| 588 | |
| 589 | |
| 590 | class ManageMultipleSettings(AdminScriptTestCase): |
| 591 | """A series of tests for manage.py when multiple settings files |
| 592 | (including the default 'settings.py') are available. The default settings |
| 593 | file is insufficient for performing the operations described, so the |
| 594 | alternate settings must be used by the running script. |
| 595 | """ |
| 596 | def setUp(self): |
| 597 | self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) |
| 598 | self.write_settings('alternate_settings.py') |
| 599 | |
| 600 | def tearDown(self): |
| 601 | self.remove_settings('settings.py') |
| 602 | self.remove_settings('alternate_settings.py') |
| 603 | |
| 604 | def test_builtin_command(self): |
| 605 | "multiple: manage.py builtin commands fail with an import error when no settings provided" |
| 606 | args = ['sqlall','admin_scripts'] |
| 607 | out, err = self.run_manage(args) |
| 608 | self.assertNoOutput(out) |
| 609 | self.assertOutput(err, 'App with label admin_scripts could not be found.') |
| 610 | |
| 611 | def test_builtin_with_settings(self): |
| 612 | "multiple: manage.py builtin commands succeed if settings are provided as argument" |
| 613 | args = ['sqlall','--settings=alternate_settings', 'admin_scripts'] |
| 614 | out, err = self.run_manage(args) |
| 615 | self.assertNoOutput(err) |
| 616 | self.assertOutput(out, 'CREATE TABLE') |
| 617 | |
| 618 | def test_builtin_with_environment(self): |
| 619 | "multiple: manage.py builtin commands succeed if settings are provided in the environment" |
| 620 | args = ['sqlall','admin_scripts'] |
| 621 | out, err = self.run_manage(args,'alternate_settings') |
| 622 | self.assertNoOutput(out) |
| 623 | self.assertOutput(err, 'App with label admin_scripts could not be found.') |
| 624 | |
| 625 | def test_builtin_with_bad_settings(self): |
| 626 | "multiple: manage.py builtin commands fail if settings file (from argument) doesn't exist" |
| 627 | args = ['sqlall','--settings=bad_settings', 'admin_scripts'] |
| 628 | out, err = self.run_manage(args) |
| 629 | self.assertNoOutput(out) |
| 630 | self.assertOutput(err, "Could not import settings 'bad_settings'") |
| 631 | |
| 632 | def test_builtin_with_bad_environment(self): |
| 633 | "multiple: manage.py builtin commands fail if settings file (from environment) doesn't exist" |
| 634 | args = ['sqlall','admin_scripts'] |
| 635 | out, err = self.run_manage(args,'bad_settings') |
| 636 | self.assertNoOutput(out) |
| 637 | self.assertOutput(err, "App with label admin_scripts could not be found") |
| 638 | |
| 639 | def test_custom_command(self): |
| 640 | "multiple: manage.py can't execute user commands using default settings" |
| 641 | args = ['noargs_command'] |
| 642 | out, err = self.run_manage(args) |
| 643 | self.assertNoOutput(out) |
| 644 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 645 | |
| 646 | def test_custom_command_with_settings(self): |
| 647 | "multiple: manage.py can execute user commands if settings are provided as argument" |
| 648 | args = ['noargs_command', '--settings=alternate_settings'] |
| 649 | out, err = self.run_manage(args) |
| 650 | self.assertNoOutput(err) |
| 651 | self.assertOutput(out, "EXECUTE:NoArgsCommand") |
| 652 | |
| 653 | def test_custom_command_with_environment(self): |
| 654 | "multiple: manage.py can execute user commands if settings are provided in environment" |
| 655 | args = ['noargs_command'] |
| 656 | out, err = self.run_manage(args,'alternate_settings') |
| 657 | self.assertNoOutput(out) |
| 658 | self.assertOutput(err, "Unknown command: 'noargs_command'") |
| 659 | |
| 660 | ########################################################################## |
| 661 | # COMMAND PROCESSING TESTS |
| 662 | # Finally, we check that commands are correctly handled - in particular, |
| 663 | # that arguments to the commands are correctly parsed and processed. |
| 664 | ########################################################################## |
| 665 | |
| 666 | class CommandTypes(AdminScriptTestCase): |
| 667 | "Tests for the various types of base command types that can be defined." |
| 668 | def setUp(self): |
| 669 | self.write_settings('settings.py') |
| 670 | |
| 671 | def tearDown(self): |
| 672 | self.remove_settings('settings.py') |
| 673 | |
| 674 | def test_base_command(self): |
| 675 | "User BaseCommands can execute when a label is provided" |
| 676 | args = ['base_command','testlabel'] |
| 677 | out, err = self.run_manage(args) |
| 678 | self.assertNoOutput(err) |
| 679 | self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options={'pythonpath': None, 'traceback': None, 'settings': None}") |
| 680 | |
| 681 | def test_base_command_no_label(self): |
| 682 | "User BaseCommands can execute when no labels are provided" |
| 683 | args = ['base_command'] |
| 684 | out, err = self.run_manage(args) |
| 685 | self.assertNoOutput(err) |
| 686 | self.assertOutput(out, "EXECUTE:BaseCommand labels=(), options={'pythonpath': None, 'traceback': None, 'settings': None}") |
| 687 | |
| 688 | def test_base_command_multiple_label(self): |
| 689 | "User BaseCommands can execute when no labels are provided" |
| 690 | args = ['base_command','testlabel','anotherlabel'] |
| 691 | out, err = self.run_manage(args) |
| 692 | self.assertNoOutput(err) |
| 693 | self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel', 'anotherlabel'), options={'pythonpath': None, 'traceback': None, 'settings': None}") |
| 694 | |
| 695 | def test_noargs(self): |
| 696 | "NoArg Commands can be executed" |
| 697 | args = ['noargs_command'] |
| 698 | out, err = self.run_manage(args) |
| 699 | self.assertNoOutput(err) |
| 700 | self.assertOutput(out, "EXECUTE:NoArgsCommand options={'pythonpath': None, 'traceback': None, 'settings': None}") |
| 701 | |
| 702 | def test_noargs_with_args(self): |
| 703 | "NoArg Commands raise an error if an argument is provided" |
| 704 | args = ['noargs_command','argument'] |
| 705 | out, err = self.run_manage(args) |
| 706 | self.assertOutput(err, "Error: Command doesn't accept any arguments") |
| 707 | |
| 708 | def test_app_command(self): |
| 709 | "User AppCommands can execute when a single app name is provided" |
| 710 | args = ['app_command', 'auth'] |
| 711 | out, err = self.run_manage(args) |
| 712 | self.assertNoOutput(err) |
| 713 | self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.auth.models'") |
| 714 | self.assertOutput(out, "django/contrib/auth/models.pyc'>, options={'pythonpath': None, 'traceback': None, 'settings': None}") |
| 715 | |
| 716 | def test_app_command_no_apps(self): |
| 717 | "User AppCommands raise an error when no app name is provided" |
| 718 | args = ['app_command'] |
| 719 | out, err = self.run_manage(args) |
| 720 | self.assertOutput(err, 'Error: Enter at least one appname.') |
| 721 | |
| 722 | def test_app_command_multiple_apps(self): |
| 723 | "User AppCommands raise an error when multiple app names are provided" |
| 724 | args = ['app_command','auth','contenttypes'] |
| 725 | out, err = self.run_manage(args) |
| 726 | self.assertNoOutput(err) |
| 727 | self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.auth.models'") |
| 728 | self.assertOutput(out, "django/contrib/auth/models.pyc'>, options={'pythonpath': None, 'traceback': None, 'settings': None}") |
| 729 | self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.contenttypes.models'") |
| 730 | self.assertOutput(out, "django/contrib/contenttypes/models.pyc'>, options={'pythonpath': None, 'traceback': None, 'settings': None}") |
| 731 | |
| 732 | def test_app_command_invalid_appname(self): |
| 733 | "User AppCommands can execute when a single app name is provided" |
| 734 | args = ['app_command', 'NOT_AN_APP'] |
| 735 | out, err = self.run_manage(args) |
| 736 | self.assertOutput(err, "App with label NOT_AN_APP could not be found") |
| 737 | |
| 738 | def test_app_command_some_invalid_appnames(self): |
| 739 | "User AppCommands can execute when some of the provided app names are invalid" |
| 740 | args = ['app_command', 'auth', 'NOT_AN_APP'] |
| 741 | out, err = self.run_manage(args) |
| 742 | self.assertOutput(err, "App with label NOT_AN_APP could not be found") |
| 743 | |
| 744 | def test_label_command(self): |
| 745 | "User LabelCommands can execute when a label is provided" |
| 746 | args = ['label_command','testlabel'] |
| 747 | out, err = self.run_manage(args) |
| 748 | self.assertNoOutput(err) |
| 749 | self.assertOutput(out, "EXECUTE:LabelCommand label=testlabel, options={'pythonpath': None, 'traceback': None, 'settings': None}") |
| 750 | |
| 751 | def test_label_command_no_label(self): |
| 752 | "User LabelCommands raise an error if no label is provided" |
| 753 | args = ['label_command'] |
| 754 | out, err = self.run_manage(args) |
| 755 | self.assertOutput(err, 'Enter at least one label') |
| 756 | |
| 757 | def test_label_command_multiple_label(self): |
| 758 | "User LabelCommands are executed multiple times if multiple labels are provided" |
| 759 | args = ['label_command','testlabel','anotherlabel'] |
| 760 | out, err = self.run_manage(args) |
| 761 | self.assertNoOutput(err) |
| 762 | self.assertOutput(out, "EXECUTE:LabelCommand label=testlabel, options={'pythonpath': None, 'traceback': None, 'settings': None}") |
| 763 | self.assertOutput(out, "EXECUTE:LabelCommand label=anotherlabel, options={'pythonpath': None, 'traceback': None, 'settings': None}") |