Ticket #10200: scream-if-error-in-loaddata-command.diff
File scream-if-error-in-loaddata-command.diff, 7.7 KB (added by , 16 years ago) |
---|
-
django/core/management/commands/loaddata.py
1 from django.core.management.base import BaseCommand 1 from django.core.management.base import BaseCommand, CommandError 2 2 from django.core.management.color import no_style 3 3 from optparse import make_option 4 4 import sys … … 73 73 if verbosity > 1: 74 74 print "Loading '%s' fixtures..." % fixture_name 75 75 else: 76 sys.stderr.write(77 self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." %78 (fixture_name, format)))79 76 transaction.rollback() 80 77 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) 82 81 83 82 if os.path.isabs(fixture_name): 84 83 fixture_dirs = [fixture_name] … … 100 99 fixture = open(full_path, 'r') 101 100 if label_found: 102 101 fixture.close() 103 print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %104 (fixture_name, humanize(fixture_dir)))105 102 transaction.rollback() 106 103 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) 108 107 else: 109 108 fixture_count += 1 110 109 objects_in_fixture = 0 … … 128 127 transaction.leave_transaction_management() 129 128 if show_traceback: 130 129 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) 137 134 fixture.close() 138 135 139 136 # If the fixture we loaded contains 0 objects, assume that an 140 137 # error was encountered during fixture loading. 141 138 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)))145 139 transaction.rollback() 146 140 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: 149 145 if verbosity > 1: 150 146 print "No %s fixture '%s' in %s." % \ 151 147 (format, fixture_name, humanize(fixture_dir)) -
tests/modeltests/fixtures/models.py
73 73 74 74 # Try to load fixture 2 using format discovery; this will fail 75 75 # 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() 76 80 >>> management.call_command('loaddata', 'fixture2', verbosity=0) # doctest: +ELLIPSIS 77 Multiple fixtures named 'fixture2' in '...fixtures'. Aborting. 81 Traceback (most recent call last): 82 ... 83 SystemExit: 1 84 >>> print sys.stderr.getvalue() 85 Error: Multiple fixtures named 'fixture2' in '...fixtures'. Aborting. 86 <BLANKLINE> 87 >>> sys.stderr = savestderr 78 88 79 89 >>> Article.objects.all() 80 90 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] -
tests/regressiontests/fixtures_regress/models.py
100 100 101 101 # redirect stderr for the next few tests... 102 102 >>> import sys 103 >>> from StringIO import StringIO 103 104 >>> savestderr = sys.stderr 104 >>> sys.stderr = sys.stdout105 >>> sys.stderr = StringIO() 105 106 106 107 # Loading data of an unknown format should fail 107 108 >>> management.call_command('loaddata', 'bad_fixture1.unkn', verbosity=0) 108 Problem installing fixture 'bad_fixture1': unkn is not a known serialization format. 109 Traceback (most recent call last): 110 ... 111 SystemExit: 1 112 >>> print sys.stderr.getvalue() 113 Error: Problem installing fixture 'bad_fixture1': unkn is not a known serialization format. 114 <BLANKLINE> 109 115 110 116 # Loading a fixture file with invalid data using explicit filename 117 >>> sys.stderr.truncate(0) 111 118 >>> management.call_command('loaddata', 'bad_fixture2.xml', verbosity=0) 112 No fixture data found for 'bad_fixture2'. (File format may be invalid.) 119 Traceback (most recent call last): 120 ... 121 SystemExit: 1 122 >>> print sys.stderr.getvalue() 123 Error: No fixture data found for 'bad_fixture2'. (File format may be invalid.) 124 <BLANKLINE> 113 125 114 126 # Loading a fixture file with invalid data without file extension 127 >>> sys.stderr.truncate(0) 115 128 >>> management.call_command('loaddata', 'bad_fixture2', verbosity=0) 116 No fixture data found for 'bad_fixture2'. (File format may be invalid.) 129 Traceback (most recent call last): 130 ... 131 SystemExit: 1 132 >>> print sys.stderr.getvalue() 133 Error: No fixture data found for 'bad_fixture2'. (File format may be invalid.) 134 <BLANKLINE> 117 135 118 136 # Loading a fixture file with no data returns an error 137 >>> sys.stderr.truncate(0) 119 138 >>> management.call_command('loaddata', 'empty', verbosity=0) 120 No fixture data found for 'empty'. (File format may be invalid.) 139 Traceback (most recent call last): 140 ... 141 SystemExit: 1 142 >>> print sys.stderr.getvalue() 143 Error: No fixture data found for 'empty'. (File format may be invalid.) 144 <BLANKLINE> 121 145 122 146 # If any of the fixtures contain an error, loading is aborted 123 147 # (Regression for #9011 - error message is correct) 148 >>> sys.stderr.truncate(0) 124 149 >>> management.call_command('loaddata', 'bad_fixture2', 'animal', verbosity=0) 125 No fixture data found for 'bad_fixture2'. (File format may be invalid.) 150 Traceback (most recent call last): 151 ... 152 SystemExit: 1 153 >>> print sys.stderr.getvalue() 154 Error: No fixture data found for 'bad_fixture2'. (File format may be invalid.) 155 <BLANKLINE> 126 156 127 157 >>> sys.stderr = savestderr 128 158