Changeset 8108
- Timestamp:
- 07/27/08 13:25:06 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/tests/regressiontests/admin_scripts/tests.py
r8056 r8108 1 1 """ 2 A series of tests to establish that the command-line managment tools work as 2 A series of tests to establish that the command-line managment tools work as 3 3 advertised - especially with regards to the handling of the DJANGO_SETTINGS_MODULE 4 4 and default settings.py files. … … 14 14 class AdminScriptTestCase(unittest.TestCase): 15 15 def write_settings(self, filename, apps=None): 16 test_dir = os.path.dirname(os.path.dirname(__file__)) 16 test_dir = os.path.dirname(os.path.dirname(__file__)) 17 17 settings_file = open(os.path.join(test_dir,filename), 'w') 18 18 settings_file.write('# Settings file automatically generated by regressiontests.admin_scripts test case\n') … … 29 29 if hasattr(settings,s): 30 30 settings_file.write("%s = '%s'\n" % (s, str(getattr(settings,s)))) 31 31 32 32 if apps is None: 33 33 apps = ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts'] … … 35 35 if apps: 36 36 settings_file.write("INSTALLED_APPS = %s\n" % apps) 37 37 38 38 settings_file.close() 39 39 40 40 def remove_settings(self, filename): 41 test_dir = os.path.dirname(os.path.dirname(__file__)) 41 test_dir = os.path.dirname(os.path.dirname(__file__)) 42 42 os.remove(os.path.join(test_dir, filename)) 43 43 # Also try to remove the pyc file; if it exists, it could … … 47 47 except OSError: 48 48 pass 49 49 50 50 def run_test(self, script, args, settings_file=None, apps=None): 51 51 test_dir = os.path.dirname(os.path.dirname(__file__)) 52 52 project_dir = os.path.dirname(test_dir) 53 53 base_dir = os.path.dirname(project_dir) 54 54 55 55 # Build the command line 56 56 cmd = '%s "%s"' % (sys.executable, script) 57 57 cmd += ''.join([' %s' % arg for arg in args]) 58 58 59 59 # Remember the old environment 60 60 old_django_settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', None) 61 61 old_python_path = os.environ.get('PYTHONPATH', None) 62 62 old_cwd = os.getcwd() 63 63 64 64 # Set the test environment 65 65 if settings_file: … … 67 67 elif 'DJANGO_SETTINGS_MODULE' in os.environ: 68 68 del os.environ['DJANGO_SETTINGS_MODULE'] 69 69 70 70 os.environ['PYTHONPATH'] = os.pathsep.join([test_dir,base_dir]) 71 71 … … 74 74 stdin, stdout, stderr = os.popen3(cmd) 75 75 out, err = stdout.read(), stderr.read() 76 76 77 77 # Restore the old environment 78 78 if old_django_settings_module: … … 83 83 # Move back to the old working directory 84 84 os.chdir(old_cwd) 85 85 86 86 return out, err 87 87 88 88 def run_django_admin(self, args, settings_file=None): 89 89 bin_dir = os.path.dirname(bin.__file__) 90 90 return self.run_test(os.path.join(bin_dir,'django-admin.py'), args, settings_file) 91 91 92 92 def run_manage(self, args, settings_file=None): 93 93 conf_dir = os.path.dirname(conf.__file__) 94 94 template_manage_py = os.path.join(conf_dir, 'project_template', 'manage.py') 95 95 96 test_dir = os.path.dirname(os.path.dirname(__file__)) 96 test_dir = os.path.dirname(os.path.dirname(__file__)) 97 97 test_manage_py = os.path.join(test_dir, 'manage.py') 98 98 shutil.copyfile(template_manage_py, test_manage_py) … … 102 102 # Cleanup - remove the generated manage.py script 103 103 os.remove(test_manage_py) 104 104 105 105 return stdout, stderr 106 106 … … 121 121 class DjangoAdminNoSettings(AdminScriptTestCase): 122 122 "A series of tests for django-admin.py when there is no settings.py file." 123 123 124 124 def test_builtin_command(self): 125 125 "no settings: django-admin builtin commands fail with an import error when no settings provided" … … 128 128 self.assertNoOutput(out) 129 129 self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') 130 130 131 131 def test_builtin_with_bad_settings(self): 132 132 "no settings: django-admin builtin commands fail if settings file (from argument) doesn't exist" … … 150 150 def setUp(self): 151 151 self.write_settings('settings.py') 152 152 153 153 def tearDown(self): 154 154 self.remove_settings('settings.py') 155 155 156 156 def test_builtin_command(self): 157 157 "default: django-admin builtin commands fail with an import error when no settings provided" … … 160 160 self.assertNoOutput(out) 161 161 self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') 162 162 163 163 def test_builtin_with_settings(self): 164 164 "default: django-admin builtin commands succeed if settings are provided as argument" … … 195 195 self.assertNoOutput(out) 196 196 self.assertOutput(err, "Unknown command: 'noargs_command'") 197 197 198 198 def test_custom_command_with_settings(self): 199 199 "default: django-admin can't execute user commands, even if settings are provided as argument" … … 202 202 self.assertNoOutput(out) 203 203 self.assertOutput(err, "Unknown command: 'noargs_command'") 204 204 205 205 def test_custom_command_with_environment(self): 206 206 "default: django-admin can't execute user commands, even if settings are provided in environment" … … 216 216 def setUp(self): 217 217 self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) 218 218 219 219 def tearDown(self): 220 220 self.remove_settings('settings.py') 221 221 222 222 def test_builtin_command(self): 223 223 "minimal: django-admin builtin commands fail with an import error when no settings provided" … … 226 226 self.assertNoOutput(out) 227 227 self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') 228 228 229 229 def test_builtin_with_settings(self): 230 230 "minimal: django-admin builtin commands fail if settings are provided as argument" … … 254 254 self.assertNoOutput(out) 255 255 self.assertOutput(err, "Could not import settings 'bad_settings'") 256 256 257 257 def test_custom_command(self): 258 258 "minimal: django-admin can't execute user commands" … … 261 261 self.assertNoOutput(out) 262 262 self.assertOutput(err, "Unknown command: 'noargs_command'") 263 263 264 264 def test_custom_command_with_settings(self): 265 265 "minimal: django-admin can't execute user commands, even if settings are provided as argument" … … 268 268 self.assertNoOutput(out) 269 269 self.assertOutput(err, "Unknown command: 'noargs_command'") 270 270 271 271 def test_custom_command_with_environment(self): 272 272 "minimal: django-admin can't execute user commands, even if settings are provided in environment" … … 282 282 def setUp(self): 283 283 self.write_settings('alternate_settings.py') 284 284 285 285 def tearDown(self): 286 286 self.remove_settings('alternate_settings.py') 287 287 288 288 def test_builtin_command(self): 289 289 "alternate: django-admin builtin commands fail with an import error when no settings provided" … … 346 346 """A series of tests for django-admin.py when multiple settings files 347 347 (including the default 'settings.py') are available. The default settings 348 file is insufficient for performing the operations described, so the 348 file is insufficient for performing the operations described, so the 349 349 alternate settings must be used by the running script. 350 350 """ … … 352 352 self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) 353 353 self.write_settings('alternate_settings.py') 354 354 355 355 def tearDown(self): 356 356 self.remove_settings('settings.py') 357 357 self.remove_settings('alternate_settings.py') 358 358 359 359 def test_builtin_command(self): 360 360 "alternate: django-admin builtin commands fail with an import error when no settings provided" … … 411 411 self.assertNoOutput(out) 412 412 self.assertOutput(err, "Unknown command: 'noargs_command'") 413 413 414 414 ########################################################################## 415 415 # MANAGE.PY TESTS … … 420 420 class ManageNoSettings(AdminScriptTestCase): 421 421 "A series of tests for manage.py when there is no settings.py file." 422 422 423 423 def test_builtin_command(self): 424 424 "no settings: manage.py builtin commands fail with an import error when no settings provided" … … 427 427 self.assertNoOutput(out) 428 428 self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'") 429 429 430 430 def test_builtin_with_bad_settings(self): 431 431 "no settings: manage.py builtin commands fail if settings file (from argument) doesn't exist" … … 449 449 def setUp(self): 450 450 self.write_settings('settings.py') 451 451 452 452 def tearDown(self): 453 453 self.remove_settings('settings.py') 454 454 455 455 def test_builtin_command(self): 456 456 "default: manage.py builtin commands succeed when default settings are appropriate" … … 459 459 self.assertNoOutput(err) 460 460 self.assertOutput(out, 'CREATE TABLE') 461 461 462 462 def test_builtin_with_settings(self): 463 463 "default: manage.py builtin commands succeed if settings are provided as argument" … … 494 494 self.assertNoOutput(err) 495 495 self.assertOutput(out, "EXECUTE:NoArgsCommand") 496 496 497 497 def test_custom_command_with_settings(self): 498 498 "default: manage.py can execute user commands when settings are provided as argument" … … 501 501 self.assertNoOutput(err) 502 502 self.assertOutput(out, "EXECUTE:NoArgsCommand") 503 503 504 504 def test_custom_command_with_environment(self): 505 505 "default: manage.py can execute user commands when settings are provided in environment" … … 515 515 def setUp(self): 516 516 self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) 517 517 518 518 def tearDown(self): 519 519 self.remove_settings('settings.py') 520 520 521 521 def test_builtin_command(self): 522 522 "minimal: manage.py builtin commands fail with an import error when no settings provided" … … 525 525 self.assertNoOutput(out) 526 526 self.assertOutput(err, 'App with label admin_scripts could not be found') 527 527 528 528 def test_builtin_with_settings(self): 529 529 "minimal: manage.py builtin commands fail if settings are provided as argument" … … 553 553 self.assertNoOutput(out) 554 554 self.assertOutput(err, 'App with label admin_scripts could not be found') 555 555 556 556 def test_custom_command(self): 557 557 "minimal: manage.py can't execute user commands without appropriate settings" … … 560 560 self.assertNoOutput(out) 561 561 self.assertOutput(err, "Unknown command: 'noargs_command'") 562 562 563 563 def test_custom_command_with_settings(self): 564 564 "minimal: manage.py can't execute user commands, even if settings are provided as argument" … … 567 567 self.assertNoOutput(out) 568 568 self.assertOutput(err, "Unknown command: 'noargs_command'") 569 569 570 570 def test_custom_command_with_environment(self): 571 571 "minimal: manage.py can't execute user commands, even if settings are provided in environment" … … 581 581 def setUp(self): 582 582 self.write_settings('alternate_settings.py') 583 583 584 584 def tearDown(self): 585 585 self.remove_settings('alternate_settings.py') 586 586 587 587 def test_builtin_command(self): 588 588 "alternate: manage.py builtin commands fail with an import error when no default settings provided" … … 645 645 """A series of tests for manage.py when multiple settings files 646 646 (including the default 'settings.py') are available. The default settings 647 file is insufficient for performing the operations described, so the 647 file is insufficient for performing the operations described, so the 648 648 alternate settings must be used by the running script. 649 649 """ … … 651 651 self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) 652 652 self.write_settings('alternate_settings.py') 653 653 654 654 def tearDown(self): 655 655 self.remove_settings('settings.py') 656 656 self.remove_settings('alternate_settings.py') 657 657 658 658 def test_builtin_command(self): 659 659 "multiple: manage.py builtin commands fail with an import error when no settings provided" … … 723 723 def setUp(self): 724 724 self.write_settings('settings.py') 725 725 726 726 def tearDown(self): 727 727 self.remove_settings('settings.py') 728 728 729 729 def test_version(self): 730 730 "--version is handled as a special case" … … 739 739 args = ['--help'] 740 740 out, err = self.run_manage(args) 741 if sys.version_info < (2, 4):741 if sys.version_info < (2, 5): 742 742 self.assertOutput(out, "usage: manage.py [options]") 743 743 else: … … 751 751 self.assertNoOutput(err) 752 752 self.assertOutput(out, "Prints the CREATE TABLE, custom SQL and CREATE INDEX SQL statements for the given model module name(s).") 753 753 754 754 def test_base_command(self): 755 755 "User BaseCommands can execute when a label is provided" … … 758 758 self.assertNoOutput(err) 759 759 self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', '1'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None)]") 760 760 761 761 def test_base_command_no_label(self): 762 762 "User BaseCommands can execute when no labels are provided" … … 786 786 self.assertNoOutput(err) 787 787 self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', 'y'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None)]") 788 788 789 789 def test_noargs(self): 790 790 "NoArg Commands can be executed" … … 799 799 out, err = self.run_manage(args) 800 800 self.assertOutput(err, "Error: Command doesn't accept any arguments") 801 801 802 802 def test_app_command(self): 803 803 "User AppCommands can execute when a single app name is provided" … … 806 806 self.assertNoOutput(err) 807 807 self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.auth.models'") 808 self.assertOutput(out, os.sep.join(['django','contrib','auth','models.py'])) 808 self.assertOutput(out, os.sep.join(['django','contrib','auth','models.py'])) 809 809 self.assertOutput(out, "'>, options=[('pythonpath', None), ('settings', None), ('traceback', None)]") 810 810 811 811 def test_app_command_no_apps(self): 812 812 "User AppCommands raise an error when no app name is provided" … … 831 831 out, err = self.run_manage(args) 832 832 self.assertOutput(err, "App with label NOT_AN_APP could not be found") 833 833 834 834 def test_app_command_some_invalid_appnames(self): 835 835 "User AppCommands can execute when some of the provided app names are invalid" … … 844 844 self.assertNoOutput(err) 845 845 self.assertOutput(out, "EXECUTE:LabelCommand label=testlabel, options=[('pythonpath', None), ('settings', None), ('traceback', None)]") 846 846 847 847 def test_label_command_no_label(self): 848 848 "User LabelCommands raise an error if no label is provided" … … 864 864 django-admin command arguments are parsed in 2 parts; the core arguments 865 865 (--settings, --traceback and --pythonpath) are parsed using a Lax parser. 866 This Lax parser ignores any unknown options. Then the full settings are 866 This Lax parser ignores any unknown options. Then the full settings are 867 867 passed to the command parser, which extracts commands of interest to the 868 individual command. 868 individual command. 869 869 """ 870 870 def setUp(self): 871 871 self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) 872 872 self.write_settings('alternate_settings.py') 873 873 874 874 def tearDown(self): 875 875 self.remove_settings('settings.py')
