devtools/qunit.py
changeset 10935 049209b9e9d6
parent 10934 3f18ec9d96dd
child 10945 4861d387a519
equal deleted inserted replaced
10934:3f18ec9d96dd 10935:049209b9e9d6
    87             tc = self
    87             tc = self
    88             test_queue = self.test_queue
    88             test_queue = self.test_queue
    89         self._qunit_controller = MyQUnitResultController
    89         self._qunit_controller = MyQUnitResultController
    90         self.vreg.register(MyQUnitResultController)
    90         self.vreg.register(MyQUnitResultController)
    91         self.vreg.register(QUnitView)
    91         self.vreg.register(QUnitView)
    92         self.vreg.register(CWSoftwareRootStaticController)
    92         self.vreg.register(CWDevtoolsStaticController)
    93 
    93 
    94     def tearDown(self):
    94     def tearDown(self):
    95         super(QUnitTestCase, self).tearDown()
    95         super(QUnitTestCase, self).tearDown()
    96         self.vreg.unregister(self._qunit_controller)
    96         self.vreg.unregister(self._qunit_controller)
    97         self.vreg.unregister(QUnitView)
    97         self.vreg.unregister(QUnitView)
    98         self.vreg.unregister(CWSoftwareRootStaticController)
    98         self.vreg.unregister(CWDevtoolsStaticController)
    99 
       
   100     def abspath(self, path):
       
   101         """use self.__module__ to build absolute path if necessary"""
       
   102         if not osp.isabs(path):
       
   103            dirname = osp.dirname(__import__(self.__module__).__file__)
       
   104            return osp.abspath(osp.join(dirname,path))
       
   105         return path
       
   106 
    99 
   107     def test_javascripts(self):
   100     def test_javascripts(self):
   108         for args in self.all_js_tests:
   101         for args in self.all_js_tests:
   109             self.assertIn(len(args), (1, 2))
   102             self.assertIn(len(args), (1, 2))
   110             test_file = self.abspath(args[0])
   103             test_file = args[0]
   111             if len(args) > 1:
   104             if len(args) > 1:
   112                 depends   = [self.abspath(dep) for dep in args[1]]
   105                 depends = args[1]
   113             else:
   106             else:
   114                 depends = ()
   107                 depends = ()
   115             for js_test in self._test_qunit(test_file, depends):
   108             for js_test in self._test_qunit(test_file, depends):
   116                 yield js_test
   109                 yield js_test
   117 
   110 
   118     @with_tempdir
   111     @with_tempdir
   119     def _test_qunit(self, test_file, depends=(), timeout=10):
   112     def _test_qunit(self, test_file, depends=(), timeout=10):
   120         assert osp.exists(test_file), test_file
       
   121         for dep in depends:
       
   122             assert osp.exists(dep), dep
       
   123 
       
   124         QUnitView.test_file = test_file
   113         QUnitView.test_file = test_file
   125         QUnitView.depends = depends
   114         QUnitView.depends = depends
   126 
   115 
   127         while not self.test_queue.empty():
   116         while not self.test_queue.empty():
   128             self.test_queue.get(False)
   117             self.test_queue.get(False)
   217     def call(self, **kwargs):
   206     def call(self, **kwargs):
   218         w = self.w
   207         w = self.w
   219         req = self._cw
   208         req = self._cw
   220         data = {
   209         data = {
   221             'jquery': req.data_url('jquery.js'),
   210             'jquery': req.data_url('jquery.js'),
   222             'web_test': req.build_url('cwsoftwareroot/devtools/data'),
   211             'devtools': req.build_url('devtools'),
   223         }
   212         }
   224         w(u'''<!DOCTYPE html>
   213         w(u'''<!DOCTYPE html>
   225         <html>
   214         <html>
   226         <head>
   215         <head>
   227         <meta http-equiv="content-type" content="application/html; charset=UTF-8"/>
   216         <meta http-equiv="content-type" content="application/html; charset=UTF-8"/>
   228         <!-- JS lib used as testing framework -->
   217         <!-- JS lib used as testing framework -->
   229         <link rel="stylesheet" type="text/css" media="all" href="%(web_test)s/qunit.css" />
   218         <link rel="stylesheet" type="text/css" media="all" href="%(devtools)s/qunit.css" />
   230         <script src="%(jquery)s" type="text/javascript"></script>
   219         <script src="%(jquery)s" type="text/javascript"></script>
   231         <script src="%(web_test)s/cwmock.js" type="text/javascript"></script>
   220         <script src="%(devtools)s/cwmock.js" type="text/javascript"></script>
   232         <script src="%(web_test)s/qunit.js" type="text/javascript"></script>'''
   221         <script src="%(devtools)s/qunit.js" type="text/javascript"></script>'''
   233         % data)
   222         % data)
   234         w(u'<!-- result report tools -->')
   223         w(u'<!-- result report tools -->')
   235         w(u'<script type="text/javascript">')
   224         w(u'<script type="text/javascript">')
   236         w(u"var BASE_URL = '%s';" % req.base_url())
   225         w(u"var BASE_URL = '%s';" % req.base_url())
   237         w(u'''
   226         w(u'''
   274                            async: false});
   263                            async: false});
   275             });''')
   264             });''')
   276         w(u'</script>')
   265         w(u'</script>')
   277         w(u'<!-- Test script dependencies (tested code for example) -->')
   266         w(u'<!-- Test script dependencies (tested code for example) -->')
   278 
   267 
   279         prefix = len(cubicweb.CW_SOFTWARE_ROOT) + 1
       
   280         for dep in self.depends:
   268         for dep in self.depends:
   281             dep = req.build_url('cwsoftwareroot/') + dep[prefix:]
   269             w(u'    <script src="%s" type="text/javascript"></script>\n' % dep)
   282             w(u'    <script src="%s" type="text/javascript"></script>' % dep)
       
   283 
   270 
   284         w(u'    <!-- Test script itself -->')
   271         w(u'    <!-- Test script itself -->')
   285         test_url = req.build_url('cwsoftwareroot/') + self.test_file[prefix:]
   272         w(u'    <script src="%s" type="text/javascript"></script>' % self.test_file)
   286         w(u'    <script src="%s" type="text/javascript"></script>' % test_url)
       
   287         w(u'''  </head>
   273         w(u'''  </head>
   288         <body>
   274         <body>
   289         <div id="qunit-fixture"></div>
   275         <div id="qunit-fixture"></div>
   290         <div id="qunit"></div>
   276         <div id="qunit"></div>
   291         </body>
   277         </body>
   292         </html>''')
   278         </html>''')
   293 
   279 
   294 
   280 
   295 class CWSoftwareRootStaticController(StaticFileController):
   281 class CWDevtoolsStaticController(StaticFileController):
   296     __regid__ = 'cwsoftwareroot'
   282     __regid__ = 'devtools'
   297 
   283 
   298     def publish(self, rset=None):
   284     def publish(self, rset=None):
   299         staticdir = cubicweb.CW_SOFTWARE_ROOT
   285         staticdir = osp.join(osp.dirname(__file__), 'data')
   300         relpath = self.relpath[len(self.__regid__) + 1:]
   286         relpath = self.relpath[len(self.__regid__) + 1:]
   301         return self.static_file(osp.join(staticdir, relpath))
   287         return self.static_file(osp.join(staticdir, relpath))
   302 
   288 
   303 
   289 
   304 STATIC_CONTROLLERS.append(CWSoftwareRootStaticController)
   290 STATIC_CONTROLLERS.append(CWDevtoolsStaticController)
   305 
   291 
   306 
   292 
   307 if __name__ == '__main__':
   293 if __name__ == '__main__':
   308     unittest_main()
   294     unittest_main()