cubicweb/devtools/qunit.py
changeset 12824 bc623a3e44e9
parent 12823 e830ace445bf
equal deleted inserted replaced
12823:e830ace445bf 12824:bc623a3e44e9
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # details.
    14 # details.
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 import os, os.path as osp
    18 import os
       
    19 import os.path as osp
    19 import time
    20 import time
    20 import errno
    21 import errno
    21 import shutil
    22 import shutil
    22 from queue import Queue, Empty
    23 from queue import Queue, Empty
    23 from tempfile import mkdtemp, TemporaryDirectory
    24 from tempfile import mkdtemp, TemporaryDirectory
    90                             "'%s' with the return code '%s' and the output:\n\n%s" %
    91                             "'%s' with the return code '%s' and the output:\n\n%s" %
    91                             (' '.join(cmd), self._process.poll(), log))
    92                             (' '.join(cmd), self._process.poll(), log))
    92 
    93 
    93     def stop(self):
    94     def stop(self):
    94         if self._process is not None and self._process.poll() is None:
    95         if self._process is not None and self._process.poll() is None:
    95             assert self._process.returncode is None,  self._process.returncode
    96             assert self._process.returncode is None, self._process.returncode
    96             self._process.terminate()
    97             self._process.terminate()
    97             self._process.wait()
    98             self._process.wait()
    98             assert self._process.returncode == 0, "Error: firefox return code is %s, see %s" %\
    99             assert self._process.returncode == 0, "Error: firefox return code is %s, see %s" %\
    99                                                   (self._process.returncode, self.log_file)
   100                                                   (self._process.returncode, self.log_file)
   100             self._process = None
   101             self._process = None
   112     timeout_error = RuntimeError
   113     timeout_error = RuntimeError
   113 
   114 
   114     def setUp(self):
   115     def setUp(self):
   115         super(QUnitTestCase, self).setUp()
   116         super(QUnitTestCase, self).setUp()
   116         self.test_queue = Queue()
   117         self.test_queue = Queue()
       
   118 
   117         class MyQUnitResultController(QUnitResultController):
   119         class MyQUnitResultController(QUnitResultController):
   118             tc = self
   120             tc = self
   119             test_queue = self.test_queue
   121             test_queue = self.test_queue
   120         self._qunit_controller = MyQUnitResultController
   122         self._qunit_controller = MyQUnitResultController
   121         self.webapp.app.appli.vreg.register(MyQUnitResultController)
   123         self.webapp.app.appli.vreg.register(MyQUnitResultController)
   191 
   193 
   192 class QUnitResultController(Controller):
   194 class QUnitResultController(Controller):
   193 
   195 
   194     __regid__ = 'qunit_result'
   196     __regid__ = 'qunit_result'
   195 
   197 
   196 
       
   197     # Class variables to circumvent the instantiation of a new Controller for each request.
   198     # Class variables to circumvent the instantiation of a new Controller for each request.
   198     _log_stack = [] # store QUnit log messages
   199     _log_stack = []  # store QUnit log messages
   199     _current_module_name = '' # store the current QUnit module name
   200     _current_module_name = ''  # store the current QUnit module name
   200 
   201 
   201     def publish(self, rset=None):
   202     def publish(self, rset=None):
   202         event = self._cw.form['event']
   203         event = self._cw.form['event']
   203         getattr(self, 'handle_%s' % event)()
   204         getattr(self, 'handle_%s' % event)()
   204         return b''
   205         return b''
   205 
   206 
   206     def handle_module_start(self):
   207     def handle_module_start(self):
   207         self.__class__._current_module_name = self._cw.form.get('name', '')
   208         self.__class__._current_module_name = self._cw.form.get('name', '')
   208 
   209 
   209     def handle_test_done(self):
   210     def handle_test_done(self):
   210         name = '%s // %s' %  (self._current_module_name, self._cw.form.get('name', ''))
   211         name = '%s // %s' % (self._current_module_name, self._cw.form.get('name', ''))
   211         failures = int(self._cw.form.get('failures', 0))
   212         failures = int(self._cw.form.get('failures', 0))
   212         total = int(self._cw.form.get('total', 0))
   213         total = int(self._cw.form.get('total', 0))
   213 
   214 
   214         self._log_stack.append('%i/%i assertions failed' % (failures, total))
   215         self._log_stack.append('%i/%i assertions failed' % (failures, total))
   215         msg = '\n'.join(self._log_stack)
   216         msg = '\n'.join(self._log_stack)