Ticket #9751: 9751-with-tests.diff
File 9751-with-tests.diff, 7.5 KB (added by , 16 years ago) |
---|
-
django/core/management/__init__.py
diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py index f1192fe..026e426 100644
a b def get_commands(): 105 105 # Find the project directory 106 106 try: 107 107 from django.conf import settings 108 module = import_module(settings.SETTINGS_MODULE.split('.', 1)[0]) 109 project_directory = setup_environ(module, 110 settings.SETTINGS_MODULE) 111 except (AttributeError, EnvironmentError, ImportError): 108 module = import_module(settings.SETTINGS_MODULE) 109 project_directory = setup_environ(module, settings.SETTINGS_MODULE) 110 except (AttributeError, EnvironmentError, ImportError, KeyError): 112 111 project_directory = None 113 112 114 113 # Find and load the management module for each installed app. … … def call_command(name, *args, **options): 156 155 raise CommandError, "Unknown command: %r" % name 157 156 158 157 # Grab out a list of defaults from the options. optparse does this for us 159 # when the script runs from the command line, but since call_command can 158 # when the script runs from the command line, but since call_command can 160 159 # be called programatically, we need to simulate the loading and handling 161 160 # of defaults (see #10080 for details). 162 161 defaults = dict([(o.dest, o.default) 163 for o in klass.option_list 162 for o in klass.option_list 164 163 if o.default is not NO_DEFAULT]) 165 164 defaults.update(options) 166 165 … … def setup_environ(settings_mod, original_settings_path=None): 316 315 # Add this project to sys.path so that it's importable in the conventional 317 316 # way. For example, if this file (manage.py) lives in a directory 318 317 # "myproject", this code would add "/path/to/myproject" to sys.path. 319 project_directory, settings_filename = os.path.split(settings_mod.__file__) 318 if '__init__.py' in settings_mod.__file__: 319 p = os.path.dirname(settings_mod.__file__) 320 else: 321 p = settings_mod.__file__ 322 project_directory, settings_filename = os.path.split(p) 320 323 if project_directory == os.curdir or not project_directory: 321 324 project_directory = os.getcwd() 322 325 project_name = os.path.basename(project_directory) -
tests/regressiontests/admin_scripts/tests.py
diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py index dd2f515..1e15f97 100644
a b from django import conf, bin, get_version 13 13 from django.conf import settings 14 14 15 15 class AdminScriptTestCase(unittest.TestCase): 16 def write_settings(self, filename, apps=None ):16 def write_settings(self, filename, apps=None, is_dir=False): 17 17 test_dir = os.path.dirname(os.path.dirname(__file__)) 18 settings_file = open(os.path.join(test_dir,filename), 'w') 18 if is_dir: 19 settings_dir = os.path.join(test_dir,filename) 20 os.mkdir(settings_dir) 21 settings_file = open(os.path.join(settings_dir,'__init__.py'), 'w') 22 else: 23 settings_file = open(os.path.join(test_dir, filename), 'w') 19 24 settings_file.write('# Settings file automatically generated by regressiontests.admin_scripts test case\n') 20 25 exports = [ 21 26 'DATABASE_ENGINE', … … class AdminScriptTestCase(unittest.TestCase): 38 43 39 44 settings_file.close() 40 45 41 def remove_settings(self, filename ):46 def remove_settings(self, filename, is_dir=False): 42 47 test_dir = os.path.dirname(os.path.dirname(__file__)) 43 48 full_name = os.path.join(test_dir, filename) 44 os.remove(full_name) 49 if is_dir: 50 shutil.rmtree(full_name) 51 else: 52 os.remove(full_name) 45 53 46 54 # Also try to remove the compiled file; if it exists, it could 47 55 # mess up later tests that depend upon the .py file not existing … … class DjangoAdminMultipleSettings(AdminScriptTestCase): 509 517 self.assertNoOutput(err) 510 518 self.assertOutput(out, "EXECUTE:NoArgsCommand") 511 519 520 521 class DjangoAdminSettingsDirectory(AdminScriptTestCase): 522 """A series of tests for django-admin.py when the settings file 523 is in a directory. 524 """ 525 def setUp(self): 526 self.write_settings('settings', is_dir=True) 527 528 def tearDown(self): 529 self.remove_settings('settings', is_dir=True) 530 531 #Regression test for 9751 532 def test_setup_environ(self): 533 "directory: startapp creates the correct directory" 534 test_dir = os.path.dirname(os.path.dirname(__file__)) 535 args = ['startapp','settings_test'] 536 out, err = self.run_django_admin(args,'settings') 537 self.assertNoOutput(err) 538 self.assertTrue(os.path.exists(os.path.join(test_dir, 'settings_test'))) 539 shutil.rmtree(os.path.join(test_dir, 'settings_test')) 540 541 def test_builtin_command(self): 542 "directory: django-admin builtin commands fail with an import error when no settings provided" 543 args = ['sqlall','admin_scripts'] 544 out, err = self.run_django_admin(args) 545 self.assertNoOutput(out) 546 self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') 547 548 def test_builtin_with_bad_settings(self): 549 "directory: django-admin builtin commands fail if settings file (from argument) doesn't exist" 550 args = ['sqlall','--settings=bad_settings', 'admin_scripts'] 551 out, err = self.run_django_admin(args) 552 self.assertOutput(err, "Could not import settings 'bad_settings'") 553 554 def test_builtin_with_bad_environment(self): 555 "directory: django-admin builtin commands fail if settings file (from environment) doesn't exist" 556 args = ['sqlall','admin_scripts'] 557 out, err = self.run_django_admin(args,'bad_settings') 558 self.assertNoOutput(out) 559 self.assertOutput(err, "Could not import settings 'bad_settings'") 560 561 def test_custom_command(self): 562 "directory: django-admin can't execute user commands unless settings are provided" 563 args = ['noargs_command'] 564 out, err = self.run_django_admin(args) 565 self.assertNoOutput(out) 566 self.assertOutput(err, "Unknown command: 'noargs_command'") 567 568 def test_builtin_with_settings(self): 569 "directory: django-admin builtin commands succeed if settings are provided as argument" 570 args = ['sqlall','--settings=settings', 'admin_scripts'] 571 out, err = self.run_django_admin(args) 572 self.assertNoOutput(err) 573 self.assertOutput(out, 'CREATE TABLE') 574 575 def test_builtin_with_environment(self): 576 "directory: django-admin builtin commands succeed if settings are provided in the environment" 577 args = ['sqlall','admin_scripts'] 578 out, err = self.run_django_admin(args,'settings') 579 self.assertNoOutput(err) 580 self.assertOutput(out, 'CREATE TABLE') 581 582 512 583 ########################################################################## 513 584 # MANAGE.PY TESTS 514 585 # This next series of test classes checks the environment processing … … class ArgumentOrder(AdminScriptTestCase): 1075 1146 out, err = self.run_manage(args) 1076 1147 self.assertNoOutput(err) 1077 1148 self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', 'y'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]") 1078