Django

Code

Changeset 8108

Show
Ignore:
Timestamp:
07/27/08 13:25:06 (5 months ago)
Author:
mtredinnick
Message:

Fixed the admin_scripts tests to check for the right output with Python 2.4.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/tests/regressiontests/admin_scripts/tests.py

    r8056 r8108  
    11""" 
    2 A series of tests to establish that the command-line managment tools work as  
     2A series of tests to establish that the command-line managment tools work as 
    33advertised - especially with regards to the handling of the DJANGO_SETTINGS_MODULE 
    44and default settings.py files. 
     
    1414class AdminScriptTestCase(unittest.TestCase): 
    1515    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__)) 
    1717        settings_file = open(os.path.join(test_dir,filename), 'w') 
    1818        settings_file.write('# Settings file automatically generated by regressiontests.admin_scripts test case\n') 
     
    2929            if hasattr(settings,s): 
    3030                settings_file.write("%s = '%s'\n" % (s, str(getattr(settings,s)))) 
    31                  
     31 
    3232        if apps is None: 
    3333            apps = ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts'] 
     
    3535        if apps: 
    3636            settings_file.write("INSTALLED_APPS = %s\n" % apps) 
    37          
     37 
    3838        settings_file.close() 
    39          
     39 
    4040    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__)) 
    4242        os.remove(os.path.join(test_dir, filename)) 
    4343        # Also try to remove the pyc file; if it exists, it could 
     
    4747        except OSError: 
    4848            pass 
    49              
     49 
    5050    def run_test(self, script, args, settings_file=None, apps=None): 
    5151        test_dir = os.path.dirname(os.path.dirname(__file__)) 
    5252        project_dir = os.path.dirname(test_dir) 
    5353        base_dir = os.path.dirname(project_dir) 
    54          
     54 
    5555        # Build the command line 
    5656        cmd = '%s "%s"' % (sys.executable, script) 
    5757        cmd += ''.join([' %s' % arg for arg in args]) 
    58          
     58 
    5959        # Remember the old environment 
    6060        old_django_settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', None) 
    6161        old_python_path = os.environ.get('PYTHONPATH', None) 
    6262        old_cwd = os.getcwd() 
    63          
     63 
    6464        # Set the test environment 
    6565        if settings_file: 
     
    6767        elif 'DJANGO_SETTINGS_MODULE' in os.environ: 
    6868            del os.environ['DJANGO_SETTINGS_MODULE'] 
    69          
     69 
    7070        os.environ['PYTHONPATH'] = os.pathsep.join([test_dir,base_dir]) 
    7171 
     
    7474        stdin, stdout, stderr = os.popen3(cmd) 
    7575        out, err = stdout.read(), stderr.read() 
    76          
     76 
    7777        # Restore the old environment 
    7878        if old_django_settings_module: 
     
    8383        # Move back to the old working directory 
    8484        os.chdir(old_cwd) 
    85          
     85 
    8686        return out, err 
    87          
     87 
    8888    def run_django_admin(self, args, settings_file=None): 
    8989        bin_dir = os.path.dirname(bin.__file__) 
    9090        return self.run_test(os.path.join(bin_dir,'django-admin.py'), args, settings_file) 
    91          
     91 
    9292    def run_manage(self, args, settings_file=None): 
    9393        conf_dir = os.path.dirname(conf.__file__) 
    9494        template_manage_py = os.path.join(conf_dir, 'project_template', 'manage.py') 
    9595 
    96         test_dir = os.path.dirname(os.path.dirname(__file__))  
     96        test_dir = os.path.dirname(os.path.dirname(__file__)) 
    9797        test_manage_py = os.path.join(test_dir, 'manage.py') 
    9898        shutil.copyfile(template_manage_py, test_manage_py) 
     
    102102        # Cleanup - remove the generated manage.py script 
    103103        os.remove(test_manage_py) 
    104          
     104 
    105105        return stdout, stderr 
    106106 
     
    121121class DjangoAdminNoSettings(AdminScriptTestCase): 
    122122    "A series of tests for django-admin.py when there is no settings.py file." 
    123                  
     123 
    124124    def test_builtin_command(self): 
    125125        "no settings: django-admin builtin commands fail with an import error when no settings provided" 
     
    128128        self.assertNoOutput(out) 
    129129        self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') 
    130                  
     130 
    131131    def test_builtin_with_bad_settings(self): 
    132132        "no settings: django-admin builtin commands fail if settings file (from argument) doesn't exist" 
     
    150150    def setUp(self): 
    151151        self.write_settings('settings.py') 
    152          
     152 
    153153    def tearDown(self): 
    154154        self.remove_settings('settings.py') 
    155              
     155 
    156156    def test_builtin_command(self): 
    157157        "default: django-admin builtin commands fail with an import error when no settings provided" 
     
    160160        self.assertNoOutput(out) 
    161161        self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') 
    162          
     162 
    163163    def test_builtin_with_settings(self): 
    164164        "default: django-admin builtin commands succeed if settings are provided as argument" 
     
    195195        self.assertNoOutput(out) 
    196196        self.assertOutput(err, "Unknown command: 'noargs_command'") 
    197              
     197 
    198198    def test_custom_command_with_settings(self): 
    199199        "default: django-admin can't execute user commands, even if settings are provided as argument" 
     
    202202        self.assertNoOutput(out) 
    203203        self.assertOutput(err, "Unknown command: 'noargs_command'") 
    204              
     204 
    205205    def test_custom_command_with_environment(self): 
    206206        "default: django-admin can't execute user commands, even if settings are provided in environment" 
     
    216216    def setUp(self): 
    217217        self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) 
    218          
     218 
    219219    def tearDown(self): 
    220220        self.remove_settings('settings.py') 
    221          
     221 
    222222    def test_builtin_command(self): 
    223223        "minimal: django-admin builtin commands fail with an import error when no settings provided" 
     
    226226        self.assertNoOutput(out) 
    227227        self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') 
    228          
     228 
    229229    def test_builtin_with_settings(self): 
    230230        "minimal: django-admin builtin commands fail if settings are provided as argument" 
     
    254254        self.assertNoOutput(out) 
    255255        self.assertOutput(err, "Could not import settings 'bad_settings'") 
    256              
     256 
    257257    def test_custom_command(self): 
    258258        "minimal: django-admin can't execute user commands" 
     
    261261        self.assertNoOutput(out) 
    262262        self.assertOutput(err, "Unknown command: 'noargs_command'") 
    263              
     263 
    264264    def test_custom_command_with_settings(self): 
    265265        "minimal: django-admin can't execute user commands, even if settings are provided as argument" 
     
    268268        self.assertNoOutput(out) 
    269269        self.assertOutput(err, "Unknown command: 'noargs_command'") 
    270              
     270 
    271271    def test_custom_command_with_environment(self): 
    272272        "minimal: django-admin can't execute user commands, even if settings are provided in environment" 
     
    282282    def setUp(self): 
    283283        self.write_settings('alternate_settings.py') 
    284          
     284 
    285285    def tearDown(self): 
    286286        self.remove_settings('alternate_settings.py') 
    287              
     287 
    288288    def test_builtin_command(self): 
    289289        "alternate: django-admin builtin commands fail with an import error when no settings provided" 
     
    346346    """A series of tests for django-admin.py when multiple settings files 
    347347    (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 
    349349    alternate settings must be used by the running script. 
    350350    """ 
     
    352352        self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) 
    353353        self.write_settings('alternate_settings.py') 
    354          
     354 
    355355    def tearDown(self): 
    356356        self.remove_settings('settings.py') 
    357357        self.remove_settings('alternate_settings.py') 
    358              
     358 
    359359    def test_builtin_command(self): 
    360360        "alternate: django-admin builtin commands fail with an import error when no settings provided" 
     
    411411        self.assertNoOutput(out) 
    412412        self.assertOutput(err, "Unknown command: 'noargs_command'") 
    413          
     413 
    414414########################################################################## 
    415415# MANAGE.PY TESTS 
     
    420420class ManageNoSettings(AdminScriptTestCase): 
    421421    "A series of tests for manage.py when there is no settings.py file." 
    422                  
     422 
    423423    def test_builtin_command(self): 
    424424        "no settings: manage.py builtin commands fail with an import error when no settings provided" 
     
    427427        self.assertNoOutput(out) 
    428428        self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'") 
    429                  
     429 
    430430    def test_builtin_with_bad_settings(self): 
    431431        "no settings: manage.py builtin commands fail if settings file (from argument) doesn't exist" 
     
    449449    def setUp(self): 
    450450        self.write_settings('settings.py') 
    451          
     451 
    452452    def tearDown(self): 
    453453        self.remove_settings('settings.py') 
    454              
     454 
    455455    def test_builtin_command(self): 
    456456        "default: manage.py builtin commands succeed when default settings are appropriate" 
     
    459459        self.assertNoOutput(err) 
    460460        self.assertOutput(out, 'CREATE TABLE') 
    461          
     461 
    462462    def test_builtin_with_settings(self): 
    463463        "default: manage.py builtin commands succeed if settings are provided as argument" 
     
    494494        self.assertNoOutput(err) 
    495495        self.assertOutput(out, "EXECUTE:NoArgsCommand") 
    496              
     496 
    497497    def test_custom_command_with_settings(self): 
    498498        "default: manage.py can execute user commands when settings are provided as argument" 
     
    501501        self.assertNoOutput(err) 
    502502        self.assertOutput(out, "EXECUTE:NoArgsCommand") 
    503              
     503 
    504504    def test_custom_command_with_environment(self): 
    505505        "default: manage.py can execute user commands when settings are provided in environment" 
     
    515515    def setUp(self): 
    516516        self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) 
    517          
     517 
    518518    def tearDown(self): 
    519519        self.remove_settings('settings.py') 
    520          
     520 
    521521    def test_builtin_command(self): 
    522522        "minimal: manage.py builtin commands fail with an import error when no settings provided" 
     
    525525        self.assertNoOutput(out) 
    526526        self.assertOutput(err, 'App with label admin_scripts could not be found') 
    527          
     527 
    528528    def test_builtin_with_settings(self): 
    529529        "minimal: manage.py builtin commands fail if settings are provided as argument" 
     
    553553        self.assertNoOutput(out) 
    554554        self.assertOutput(err, 'App with label admin_scripts could not be found') 
    555              
     555 
    556556    def test_custom_command(self): 
    557557        "minimal: manage.py can't execute user commands without appropriate settings" 
     
    560560        self.assertNoOutput(out) 
    561561        self.assertOutput(err, "Unknown command: 'noargs_command'") 
    562              
     562 
    563563    def test_custom_command_with_settings(self): 
    564564        "minimal: manage.py can't execute user commands, even if settings are provided as argument" 
     
    567567        self.assertNoOutput(out) 
    568568        self.assertOutput(err, "Unknown command: 'noargs_command'") 
    569              
     569 
    570570    def test_custom_command_with_environment(self): 
    571571        "minimal: manage.py can't execute user commands, even if settings are provided in environment" 
     
    581581    def setUp(self): 
    582582        self.write_settings('alternate_settings.py') 
    583          
     583 
    584584    def tearDown(self): 
    585585        self.remove_settings('alternate_settings.py') 
    586              
     586 
    587587    def test_builtin_command(self): 
    588588        "alternate: manage.py builtin commands fail with an import error when no default settings provided" 
     
    645645    """A series of tests for manage.py when multiple settings files 
    646646    (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 
    648648    alternate settings must be used by the running script. 
    649649    """ 
     
    651651        self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) 
    652652        self.write_settings('alternate_settings.py') 
    653          
     653 
    654654    def tearDown(self): 
    655655        self.remove_settings('settings.py') 
    656656        self.remove_settings('alternate_settings.py') 
    657              
     657 
    658658    def test_builtin_command(self): 
    659659        "multiple: manage.py builtin commands fail with an import error when no settings provided" 
     
    723723    def setUp(self): 
    724724        self.write_settings('settings.py') 
    725          
     725 
    726726    def tearDown(self): 
    727727        self.remove_settings('settings.py') 
    728      
     728 
    729729    def test_version(self): 
    730730        "--version is handled as a special case" 
     
    739739        args = ['--help'] 
    740740        out, err = self.run_manage(args) 
    741         if sys.version_info < (2, 4): 
     741        if sys.version_info < (2, 5): 
    742742            self.assertOutput(out, "usage: manage.py [options]") 
    743743        else: 
     
    751751        self.assertNoOutput(err) 
    752752        self.assertOutput(out, "Prints the CREATE TABLE, custom SQL and CREATE INDEX SQL statements for the given model module name(s).") 
    753      
     753 
    754754    def test_base_command(self): 
    755755        "User BaseCommands can execute when a label is provided" 
     
    758758        self.assertNoOutput(err) 
    759759        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 
    761761    def test_base_command_no_label(self): 
    762762        "User BaseCommands can execute when no labels are provided" 
     
    786786        self.assertNoOutput(err) 
    787787        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 
    789789    def test_noargs(self): 
    790790        "NoArg Commands can be executed" 
     
    799799        out, err = self.run_manage(args) 
    800800        self.assertOutput(err, "Error: Command doesn't accept any arguments") 
    801          
     801 
    802802    def test_app_command(self): 
    803803        "User AppCommands can execute when a single app name is provided" 
     
    806806        self.assertNoOutput(err) 
    807807        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'])) 
    809809        self.assertOutput(out, "'>, options=[('pythonpath', None), ('settings', None), ('traceback', None)]") 
    810                  
     810 
    811811    def test_app_command_no_apps(self): 
    812812        "User AppCommands raise an error when no app name is provided" 
     
    831831        out, err = self.run_manage(args) 
    832832        self.assertOutput(err, "App with label NOT_AN_APP could not be found") 
    833              
     833 
    834834    def test_app_command_some_invalid_appnames(self): 
    835835        "User AppCommands can execute when some of the provided app names are invalid" 
     
    844844        self.assertNoOutput(err) 
    845845        self.assertOutput(out, "EXECUTE:LabelCommand label=testlabel, options=[('pythonpath', None), ('settings', None), ('traceback', None)]") 
    846          
     846 
    847847    def test_label_command_no_label(self): 
    848848        "User LabelCommands raise an error if no label is provided" 
     
    864864    django-admin command arguments are parsed in 2 parts; the core arguments 
    865865    (--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 
    867867    passed to the command parser, which extracts commands of interest to the 
    868     individual command.     
     868    individual command. 
    869869    """ 
    870870    def setUp(self): 
    871871        self.write_settings('settings.py', apps=['django.contrib.auth','django.contrib.contenttypes']) 
    872872        self.write_settings('alternate_settings.py') 
    873          
     873 
    874874    def tearDown(self): 
    875875        self.remove_settings('settings.py')