2121
2222import os
2323import sys
24+ import datetime
2425import glob
25- import shutil
2626import StringIO
27- import webbrowser
27+ import subprocess
28+ import tempfile
2829from PyQt4 .QtCore import *
2930from PyQt4 .QtGui import *
3031
5152
5253QGISAPP , CANVAS , IFACE , PARENT = getQgisTestApp ()
5354
54- PALREPORTDIR = ''
55- if 'PAL_REPORT' in os .environ :
56- PALREPORTDIR = os .path .join (str (QDir .tempPath ()), 'pal_test_report' )
57- # clear out old reports, if temp dir exists
58- if os .path .exists (PALREPORTDIR ):
59- shutil .rmtree (PALREPORTDIR , True )
60- os .mkdir (PALREPORTDIR )
55+ PALREPORT = 'PAL_REPORT' in os .environ
56+ PALREPORTS = {}
6157
6258
6359class TestQgsPalLabeling (TestCase ):
@@ -78,8 +74,6 @@ def setUpClass(cls):
7874 cls ._QgisApp , cls ._Canvas , cls ._Iface , cls ._Parent = \
7975 QGISAPP , CANVAS , IFACE , PARENT
8076
81- cls ._PalReportDir = PALREPORTDIR
82-
8377 # verify that spatialite provider is available
8478 msg = ('\n Spatialite provider not found, '
8579 'SKIPPING TEST SUITE' )
@@ -116,8 +110,7 @@ def setUpClass(cls):
116110 # default for labeling test data sources: WGS 84 / UTM zone 13N
117111 crs .createFromSrid (32613 )
118112 cls ._MapRenderer .setDestinationCrs (crs )
119- # TODO: match and store platform's native logical output dpi
120- cls ._MapRenderer .setOutputSize (QSize (600 , 400 ), 72 )
113+ # use platform's native logical output dpi for QgsMapRenderer on launch
121114
122115 cls ._Pal = QgsPalLabeling ()
123116 cls ._MapRenderer .setLabelingEngine (cls ._Pal )
@@ -219,16 +212,9 @@ def renderCheck(self, mismatch=0):
219212 chk .setControlName (self ._Test )
220213 chk .setMapRenderer (self ._MapRenderer )
221214 res = chk .runTest (self ._Test , mismatch )
222- if self . _PalReportDir and not res : # don't report ok checks
215+ if PALREPORT and not res : # don't report ok checks
223216 testname = self ._TestGroup + ' . ' + self ._Test
224- report = '<html>'
225- report += '<head><title>{0}</title></head>' .format (testname )
226- report += '<body>' + chk .report ().toLocal8Bit () + '</body>'
227- report += '</html>'
228- f = QFile (os .path .join (self ._PalReportDir , testname + '.html' ))
229- if f .open (QIODevice .ReadWrite | QIODevice .Truncate ):
230- f .write (report )
231- f .close ()
217+ PALREPORTS [testname ] = str (chk .report ().toLocal8Bit ())
232218 msg = '\n Render check failed for "{0}"' .format (self ._Test )
233219 return res , msg
234220
@@ -304,10 +290,30 @@ def runSuite(module, tests):
304290 print '\n ' + out .getvalue ()
305291 out .close ()
306292
307- if PALREPORTDIR :
308- for report in os .listdir (PALREPORTDIR ):
309- webbrowser .open_new_tab ('file://' +
310- os .path .join (PALREPORTDIR , report ))
293+ if PALREPORTS :
294+ teststamp = 'PAL Test Report: ' + \
295+ datetime .datetime .now ().strftime ('%Y-%m-%d %X' )
296+ report = '<html><head><title>{0}</title></head><body>' .format (teststamp )
297+ report += '\n <h2>Failed Tests: {0}</h2>' .format (len (PALREPORTS ))
298+ for k , v in PALREPORTS .iteritems ():
299+ report += '\n <h3>{0}</h3>\n {1}' .format (k , v )
300+ report += '</body></html>'
301+
302+ tmp = tempfile .NamedTemporaryFile (suffix = ".html" , delete = False )
303+ palreport = tmp .name
304+ tmp .write (report )
305+ tmp .close ()
306+
307+ if sys .platform [:3 ] in ('win' , 'dar' ):
308+ import webbrowser
309+ webbrowser .open_new_tab ("file://{0}" .format (palreport ))
310+ else :
311+ # some Linux OS pause execution on webbrowser open, so background it
312+ cmd = 'import webbrowser;' \
313+ 'webbrowser.open_new_tab("file://{0}")' .format (palreport )
314+ p = subprocess .Popen ([sys .executable , "-c" , cmd ],
315+ stdout = subprocess .PIPE ,
316+ stderr = subprocess .STDOUT ).pid
311317
312318 return res
313319
@@ -318,4 +324,4 @@ def runSuite(module, tests):
318324 b = 'TestQgsPalLabelingBase.'
319325 tests = [b + 'test_write_read_settings' ]
320326 res = runSuite (sys .modules [__name__ ], tests )
321- sys .exit (int ( not res .wasSuccessful () ))
327+ sys .exit (not res .wasSuccessful ())
0 commit comments