Ticket #10200: scream-if-error-in-loaddata-command.diff

File scream-if-error-in-loaddata-command.diff, 7.7 KB (added by Lorenzo Gil Sanchez, 16 years ago)
  • django/core/management/commands/loaddata.py

     
    1 from django.core.management.base import BaseCommand
     1from django.core.management.base import BaseCommand, CommandError
    22from django.core.management.color import no_style
    33from optparse import make_option
    44import sys
     
    7373                if verbosity > 1:
    7474                    print "Loading '%s' fixtures..." % fixture_name
    7575            else:
    76                 sys.stderr.write(
    77                     self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." %
    78                         (fixture_name, format)))
    7976                transaction.rollback()
    8077                transaction.leave_transaction_management()
    81                 return
     78                error_msg = ("Problem installing fixture '%s': %s is not a known serialization format." %
     79                             (fixture_name, format))
     80                raise CommandError(error_msg)
    8281
    8382            if os.path.isabs(fixture_name):
    8483                fixture_dirs = [fixture_name]
     
    10099                        fixture = open(full_path, 'r')
    101100                        if label_found:
    102101                            fixture.close()
    103                             print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
    104                                 (fixture_name, humanize(fixture_dir)))
    105102                            transaction.rollback()
    106103                            transaction.leave_transaction_management()
    107                             return
     104                            error_msg = ("Multiple fixtures named '%s' in %s. Aborting." %
     105                                         (fixture_name, humanize(fixture_dir)))
     106                            raise CommandError(error_msg)
    108107                        else:
    109108                            fixture_count += 1
    110109                            objects_in_fixture = 0
     
    128127                                transaction.leave_transaction_management()
    129128                                if show_traceback:
    130129                                    traceback.print_exc()
    131                                 else:
    132                                     sys.stderr.write(
    133                                         self.style.ERROR("Problem installing fixture '%s': %s\n" %
    134                                              (full_path, ''.join(traceback.format_exception(sys.exc_type,
    135                                                  sys.exc_value, sys.exc_traceback)))))
    136                                 return
     130                                error_msg = ("Problem installing fixture '%s': %s\n" %
     131                                             (full_path, ''.join(traceback.format_exception(sys.exc_type,
     132                                                                                            sys.exc_value, sys.exc_traceback))))
     133                                raise CommandError(error_msg)
    137134                            fixture.close()
    138135
    139136                            # If the fixture we loaded contains 0 objects, assume that an
    140137                            # error was encountered during fixture loading.
    141138                            if objects_in_fixture == 0:
    142                                 sys.stderr.write(
    143                                     self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" %
    144                                         (fixture_name)))
    145139                                transaction.rollback()
    146140                                transaction.leave_transaction_management()
    147                                 return
    148                     except:
     141                                error_msg = ("No fixture data found for '%s'. (File format may be invalid.)" %
     142                                             fixture_name)
     143                                raise CommandError(error_msg)
     144                    except IOError:
    149145                        if verbosity > 1:
    150146                            print "No %s fixture '%s' in %s." % \
    151147                                (format, fixture_name, humanize(fixture_dir))
  • tests/modeltests/fixtures/models.py

     
    7373
    7474# Try to load fixture 2 using format discovery; this will fail
    7575# because there are two fixture2's in the fixtures directory
     76>>> import sys
     77>>> from StringIO import StringIO
     78>>> savestderr = sys.stderr
     79>>> sys.stderr = StringIO()
    7680>>> management.call_command('loaddata', 'fixture2', verbosity=0) # doctest: +ELLIPSIS
    77 Multiple fixtures named 'fixture2' in '...fixtures'. Aborting.
     81Traceback (most recent call last):
     82...
     83SystemExit: 1
     84>>> print sys.stderr.getvalue()
     85Error: Multiple fixtures named 'fixture2' in '...fixtures'. Aborting.
     86<BLANKLINE>
     87>>> sys.stderr = savestderr
    7888
    7989>>> Article.objects.all()
    8090[<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
  • tests/regressiontests/fixtures_regress/models.py

     
    100100
    101101# redirect stderr for the next few tests...
    102102>>> import sys
     103>>> from StringIO import StringIO
    103104>>> savestderr = sys.stderr
    104 >>> sys.stderr = sys.stdout
     105>>> sys.stderr = StringIO()
    105106
    106107# Loading data of an unknown format should fail
    107108>>> management.call_command('loaddata', 'bad_fixture1.unkn', verbosity=0)
    108 Problem installing fixture 'bad_fixture1': unkn is not a known serialization format.
     109Traceback (most recent call last):
     110...
     111SystemExit: 1
     112>>> print sys.stderr.getvalue()
     113Error: Problem installing fixture 'bad_fixture1': unkn is not a known serialization format.
     114<BLANKLINE>
    109115
    110116# Loading a fixture file with invalid data using explicit filename
     117>>> sys.stderr.truncate(0)
    111118>>> management.call_command('loaddata', 'bad_fixture2.xml', verbosity=0)
    112 No fixture data found for 'bad_fixture2'. (File format may be invalid.)
     119Traceback (most recent call last):
     120...
     121SystemExit: 1
     122>>> print sys.stderr.getvalue()
     123Error: No fixture data found for 'bad_fixture2'. (File format may be invalid.)
     124<BLANKLINE>
    113125
    114126# Loading a fixture file with invalid data without file extension
     127>>> sys.stderr.truncate(0)
    115128>>> management.call_command('loaddata', 'bad_fixture2', verbosity=0)
    116 No fixture data found for 'bad_fixture2'. (File format may be invalid.)
     129Traceback (most recent call last):
     130...
     131SystemExit: 1
     132>>> print sys.stderr.getvalue()
     133Error: No fixture data found for 'bad_fixture2'. (File format may be invalid.)
     134<BLANKLINE>
    117135
    118136# Loading a fixture file with no data returns an error
     137>>> sys.stderr.truncate(0)
    119138>>> management.call_command('loaddata', 'empty', verbosity=0)
    120 No fixture data found for 'empty'. (File format may be invalid.)
     139Traceback (most recent call last):
     140...
     141SystemExit: 1
     142>>> print sys.stderr.getvalue()
     143Error: No fixture data found for 'empty'. (File format may be invalid.)
     144<BLANKLINE>
    121145
    122146# If any of the fixtures contain an error, loading is aborted
    123147# (Regression for #9011 - error message is correct)
     148>>> sys.stderr.truncate(0)
    124149>>> management.call_command('loaddata', 'bad_fixture2', 'animal', verbosity=0)
    125 No fixture data found for 'bad_fixture2'. (File format may be invalid.)
     150Traceback (most recent call last):
     151...
     152SystemExit: 1
     153>>> print sys.stderr.getvalue()
     154Error: No fixture data found for 'bad_fixture2'. (File format may be invalid.)
     155<BLANKLINE>
    126156
    127157>>> sys.stderr = savestderr
    128158
Back to Top