@@ -86,13 +86,11 @@ def lower(value):
86
86
"""
87
87
88
88
89
-
90
89
class IntegrationTest (DjangoPluginTestCase ):
91
90
"""Tests greenfield settings initializations and other weirdnesses"""
92
91
93
92
def setUp (self ):
94
93
self .python = os .path .abspath (os .environ ['_' ])
95
- #print("PYTHON:", self.python)
96
94
if ".tox" in self .python :
97
95
self .env_bin = os .path .dirname (self .python )
98
96
else :
@@ -103,14 +101,15 @@ def set_cwd(self, path):
103
101
self .cwd = path
104
102
105
103
def _cmd (self , * args , ** kwargs ):
106
- #print args, kwargs
107
104
try :
108
105
output = subprocess .check_output (args , cwd = self .cwd , stderr = subprocess .STDOUT , ** kwargs )
109
106
return output .decode ("utf-8" ).strip ()
110
107
except subprocess .CalledProcessError as e :
111
- raise Exception ("Called Processed Error:\n cmd: %s\n exc: %s\n cwd: %s\n output:\n %s" % (args , e , self .cwd , e .output ))
108
+ fmt = "Called Processed Error:\n cmd: %s\n exc: %s\n cwd: %s\n output:\n %s"
109
+ raise Exception (fmt % (args , e , self .cwd , e .output ))
112
110
except OSError as e :
113
- raise Exception ("OS Error:\n cmd: %s\n exc: %s\n cwd: %s\n " % (args , e , self .cwd ))
111
+ fmt = "OS Error:\n cmd: %s\n exc: %s\n cwd: %s\n "
112
+ raise Exception (fmt % (args , e , self .cwd ))
114
113
115
114
def _pycmd (self , * args , ** kwargs ):
116
115
args = [self .python ] + list (args )
@@ -133,15 +132,14 @@ def _start_project(self, project_name):
133
132
data = f .read ()
134
133
data = data .replace ("'django.db.backends.'" , "'django.db.backends.sqlite3'" )
135
134
with open (self .settings_file , "w" ) as f :
136
- f .write (data )
135
+ f .write (data )
137
136
self ._add_installed_app ("django_coverage_plugin" )
138
- # self.addCleanup(shutil.rmtree, self.project_dir)
137
+ self .addCleanup (shutil .rmtree , self .project_dir )
139
138
return output
140
139
141
140
def _add_installed_app (self , app_name ):
142
141
with open (self .settings_file ) as f :
143
142
settings_data = f .read ()
144
- #self._print_file(settings_data, self.settings_file)
145
143
146
144
sep_open , sep_close = ("(" , ")" ) if django .VERSION < (1 , 9 ) else ("[" , "]" )
147
145
try :
@@ -185,18 +183,21 @@ def _run_coverage(self, *args):
185
183
self .assertNotIn ("Disabling plugin" , output )
186
184
env = os .environ .copy ()
187
185
env ['DJANGO_SETTINGS_MODULE' ] = self .settings_file
188
- coverage_report = self ._cmd_global ("coverage" , "report" , "-m" , "--rcfile" , self .config_file , env = env )
186
+ coverage_report = self ._cmd_global (
187
+ "coverage" , "report" , "-m" , "--rcfile" , self .config_file ,
188
+ env = env
189
+ )
189
190
coverage_report = self .parse_coverage_report (coverage_report )
190
191
return output , coverage_report
191
192
192
193
def _create_django_project (self , project_name , app_name ):
193
194
self .cwd = os .getcwd ()
194
195
self ._start_project (project_name )
195
196
self ._start_app (app_name )
196
- #with open(self.settings_file) as f:
197
- # print f.read()
198
197
self .config_file = self ._add_project_file (COVERAGE_CFG_FILE_TEXT , "coverage.cfg" )
199
- self .template_file = self ._add_project_file (TEMPLATE_FILE_TEXT , app_name , "templates" , "target_template.html" )
198
+ self .template_file = self ._add_project_file (
199
+ TEMPLATE_FILE_TEXT , app_name , "templates" , "target_template.html"
200
+ )
200
201
201
202
if django .VERSION < (1 , 8 ):
202
203
define_target_view = '"app_template_render.views.target_view"'
@@ -213,7 +214,9 @@ def _create_django_project(self, project_name, app_name):
213
214
self .views_file = os .path .join (self .project_dir , app_name , "views.py" )
214
215
self .urls_file = os .path .join (self .project_dir , project_name , "urls.py" )
215
216
self .test_views_file = self ._add_project_file ("" , app_name , "templatetags" , "__init__.py" )
216
- self .test_views_file = self ._add_project_file (TEMPLATE_TAG_TEXT , app_name , "templatetags" , "test_tags.py" )
217
+ self .test_views_file = self ._add_project_file (
218
+ TEMPLATE_TAG_TEXT , app_name , "templatetags" , "test_tags.py"
219
+ )
217
220
218
221
self ._add_view_function (app_name , VIEW_FUNC_TEXT )
219
222
self ._add_url (project_name , app_name , "target_view" , "target_view" )
@@ -235,20 +238,24 @@ def _add_view_function(self, app_name, view_text):
235
238
views_data = "%s\n %s\n " % (views_data , view_text )
236
239
self ._save_py_file (self .views_file , views_data )
237
240
238
-
239
241
def _add_url (self , project_name , app_name , view_func , view_name ):
240
242
with open (self .urls_file ) as f :
241
243
urls_data = f .read ()
242
244
243
245
if django .VERSION < (1 , 8 ):
246
+ before = "\n )\n "
247
+ fmt = " url(r'^$', '%(app_name)s.views.target_view', name='target_view')\n )\n "
248
+ after = fmt % locals ()
249
+ urls_data = urls_data .replace (before , after )
250
+
251
+ else :
244
252
urls_data = urls_data .replace (
245
- "\n ) \n " ,
246
- " url(r'^$', '%(app_name) s.views.target_view', name='target_view') \n ) \n " % locals (),
253
+ "urlpatterns = [ " ,
254
+ "import % s.views\n \n urlpatterns = [ " % app_name
247
255
)
248
- else :
249
- urls_data = urls_data .replace ("urlpatterns = [" , "import %s.views\n \n urlpatterns = [" % app_name )
250
256
fmt = ''' url(r'^%(app_name)s/', %(app_name)s.views.%(view_func)s),\n ]'''
251
257
urls_data = urls_data .replace ("]" , fmt % locals ())
258
+
252
259
self ._save_py_file (self .urls_file , urls_data )
253
260
254
261
@django_start_at (1 , 8 )
@@ -261,7 +268,7 @@ def test_template_render(self):
261
268
self .assertNotIn ("ERROR" , output )
262
269
self .assertNotIn ("FAIL" , output )
263
270
264
- import pprint ;pprint .pprint (coverage_report )
271
+ # import pprint;pprint.pprint(coverage_report)
265
272
self .assertIsCovered (coverage_report , "integration_template_render/settings.py" )
266
273
self .assertIsCovered (coverage_report , "integration_template_render/__init__.py" )
267
274
self .assertIsCovered (coverage_report , "integration_template_render/urls.py" )
@@ -306,10 +313,12 @@ def __init__(self, num_lines, num_missing, pct, missing):
306
313
self .missing = missing
307
314
308
315
def __unicode__ (self ):
309
- return u"%s/%s/%s%%%%/%s" % (self .num_lines , self .num_missing , self .pct , self .missing )
316
+ fmt = u"%s/%s/%s%%%%/%s"
317
+ return fmt % (self .num_lines , self .num_missing , self .pct , self .missing )
310
318
311
319
def __repr__ (self ):
312
- return u"ReportInfo(num_lines=%r, num_missing=%r, pct=%r, missing=%r)" % (self .num_lines , self .num_missing , self .pct , self .missing )
320
+ fmt = u"ReportInfo(num_lines=%r, num_missing=%r, pct=%r, missing=%r)"
321
+ return fmt % (self .num_lines , self .num_missing , self .pct , self .missing )
313
322
314
323
def missing_line_numbers (self ):
315
324
for chunk in self .missing .split ("," ):
0 commit comments