Code

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

File scream-if-error-in-loaddata-command.diff, 7.7 KB (added by lgs, 5 years ago)
Line 
1Index: django/core/management/commands/loaddata.py
2===================================================================
3--- django/core/management/commands/loaddata.py (revisión: 9808)
4+++ django/core/management/commands/loaddata.py (copia de trabajo)
5@@ -1,4 +1,4 @@
6-from django.core.management.base import BaseCommand
7+from django.core.management.base import BaseCommand, CommandError
8 from django.core.management.color import no_style
9 from optparse import make_option
10 import sys
11@@ -73,12 +73,11 @@
12                 if verbosity > 1:
13                     print "Loading '%s' fixtures..." % fixture_name
14             else:
15-                sys.stderr.write(
16-                    self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." %
17-                        (fixture_name, format)))
18                 transaction.rollback()
19                 transaction.leave_transaction_management()
20-                return
21+                error_msg = ("Problem installing fixture '%s': %s is not a known serialization format." %
22+                             (fixture_name, format))
23+                raise CommandError(error_msg)
24 
25             if os.path.isabs(fixture_name):
26                 fixture_dirs = [fixture_name]
27@@ -100,11 +99,11 @@
28                         fixture = open(full_path, 'r')
29                         if label_found:
30                             fixture.close()
31-                            print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
32-                                (fixture_name, humanize(fixture_dir)))
33                             transaction.rollback()
34                             transaction.leave_transaction_management()
35-                            return
36+                            error_msg = ("Multiple fixtures named '%s' in %s. Aborting." %
37+                                         (fixture_name, humanize(fixture_dir)))
38+                            raise CommandError(error_msg)
39                         else:
40                             fixture_count += 1
41                             objects_in_fixture = 0
42@@ -128,24 +127,21 @@
43                                 transaction.leave_transaction_management()
44                                 if show_traceback:
45                                     traceback.print_exc()
46-                                else:
47-                                    sys.stderr.write(
48-                                        self.style.ERROR("Problem installing fixture '%s': %s\n" %
49-                                             (full_path, ''.join(traceback.format_exception(sys.exc_type,
50-                                                 sys.exc_value, sys.exc_traceback)))))
51-                                return
52+                                error_msg = ("Problem installing fixture '%s': %s\n" %
53+                                             (full_path, ''.join(traceback.format_exception(sys.exc_type,
54+                                                                                            sys.exc_value, sys.exc_traceback))))
55+                                raise CommandError(error_msg)
56                             fixture.close()
57 
58                             # If the fixture we loaded contains 0 objects, assume that an
59                             # error was encountered during fixture loading.
60                             if objects_in_fixture == 0:
61-                                sys.stderr.write(
62-                                    self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" %
63-                                        (fixture_name)))
64                                 transaction.rollback()
65                                 transaction.leave_transaction_management()
66-                                return
67-                    except:
68+                                error_msg = ("No fixture data found for '%s'. (File format may be invalid.)" %
69+                                             fixture_name)
70+                                raise CommandError(error_msg)
71+                    except IOError:
72                         if verbosity > 1:
73                             print "No %s fixture '%s' in %s." % \
74                                 (format, fixture_name, humanize(fixture_dir))
75Index: tests/modeltests/fixtures/models.py
76===================================================================
77--- tests/modeltests/fixtures/models.py (revisión: 9808)
78+++ tests/modeltests/fixtures/models.py (copia de trabajo)
79@@ -73,8 +73,18 @@
80 
81 # Try to load fixture 2 using format discovery; this will fail
82 # because there are two fixture2's in the fixtures directory
83+>>> import sys
84+>>> from StringIO import StringIO
85+>>> savestderr = sys.stderr
86+>>> sys.stderr = StringIO()
87 >>> management.call_command('loaddata', 'fixture2', verbosity=0) # doctest: +ELLIPSIS
88-Multiple fixtures named 'fixture2' in '...fixtures'. Aborting.
89+Traceback (most recent call last):
90+...
91+SystemExit: 1
92+>>> print sys.stderr.getvalue()
93+Error: Multiple fixtures named 'fixture2' in '...fixtures'. Aborting.
94+<BLANKLINE>
95+>>> sys.stderr = savestderr
96 
97 >>> Article.objects.all()
98 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
99Index: tests/regressiontests/fixtures_regress/models.py
100===================================================================
101--- tests/regressiontests/fixtures_regress/models.py    (revisión: 9808)
102+++ tests/regressiontests/fixtures_regress/models.py    (copia de trabajo)
103@@ -100,29 +100,59 @@
104 
105 # redirect stderr for the next few tests...
106 >>> import sys
107+>>> from StringIO import StringIO
108 >>> savestderr = sys.stderr
109->>> sys.stderr = sys.stdout
110+>>> sys.stderr = StringIO()
111 
112 # Loading data of an unknown format should fail
113 >>> management.call_command('loaddata', 'bad_fixture1.unkn', verbosity=0)
114-Problem installing fixture 'bad_fixture1': unkn is not a known serialization format.
115+Traceback (most recent call last):
116+...
117+SystemExit: 1
118+>>> print sys.stderr.getvalue()
119+Error: Problem installing fixture 'bad_fixture1': unkn is not a known serialization format.
120+<BLANKLINE>
121 
122 # Loading a fixture file with invalid data using explicit filename
123+>>> sys.stderr.truncate(0)
124 >>> management.call_command('loaddata', 'bad_fixture2.xml', verbosity=0)
125-No fixture data found for 'bad_fixture2'. (File format may be invalid.)
126+Traceback (most recent call last):
127+...
128+SystemExit: 1
129+>>> print sys.stderr.getvalue()
130+Error: No fixture data found for 'bad_fixture2'. (File format may be invalid.)
131+<BLANKLINE>
132 
133 # Loading a fixture file with invalid data without file extension
134+>>> sys.stderr.truncate(0)
135 >>> management.call_command('loaddata', 'bad_fixture2', verbosity=0)
136-No fixture data found for 'bad_fixture2'. (File format may be invalid.)
137+Traceback (most recent call last):
138+...
139+SystemExit: 1
140+>>> print sys.stderr.getvalue()
141+Error: No fixture data found for 'bad_fixture2'. (File format may be invalid.)
142+<BLANKLINE>
143 
144 # Loading a fixture file with no data returns an error
145+>>> sys.stderr.truncate(0)
146 >>> management.call_command('loaddata', 'empty', verbosity=0)
147-No fixture data found for 'empty'. (File format may be invalid.)
148+Traceback (most recent call last):
149+...
150+SystemExit: 1
151+>>> print sys.stderr.getvalue()
152+Error: No fixture data found for 'empty'. (File format may be invalid.)
153+<BLANKLINE>
154 
155 # If any of the fixtures contain an error, loading is aborted
156 # (Regression for #9011 - error message is correct)
157+>>> sys.stderr.truncate(0)
158 >>> management.call_command('loaddata', 'bad_fixture2', 'animal', verbosity=0)
159-No fixture data found for 'bad_fixture2'. (File format may be invalid.)
160+Traceback (most recent call last):
161+...
162+SystemExit: 1
163+>>> print sys.stderr.getvalue()
164+Error: No fixture data found for 'bad_fixture2'. (File format may be invalid.)
165+<BLANKLINE>
166 
167 >>> sys.stderr = savestderr
168