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