[devtools] Serve JS tests over HTTP (closes #5533285)
authorRémi Cardona <remi.cardona@logilab.fr>
Wed, 13 May 2015 17:49:40 +0200
changeset 10413 22a89d0f4143
parent 10412 3540131a8405
child 10414 eaf42d452410
[devtools] Serve JS tests over HTTP (closes #5533285) Instead of mixing file:// URLs with XHR.
devtools/devctl.py
devtools/qunit.py
web/test/jstests/test_ajax.js
--- a/devtools/devctl.py	Wed May 13 15:58:21 2015 +0200
+++ b/devtools/devctl.py	Wed May 13 17:49:40 2015 +0200
@@ -837,21 +837,11 @@
             p.wait()
 
 
-class GenerateQUnitHTML(Command):
-    """Generate a QUnit html file to see test in your browser"""
-    name = "qunit-html"
-    arguments = '<test file> [<dependancy js file>...]'
-
-    def run(self, args):
-        from cubicweb.devtools.qunit import make_qunit_html
-        print make_qunit_html(args[0], args[1:])
-
 for cmdcls in (UpdateCubicWebCatalogCommand,
                UpdateCubeCatalogCommand,
                #LiveServerCommand,
                NewCubeCommand,
                ExamineLogCommand,
                GenerateSchema,
-               GenerateQUnitHTML,
                ):
     CWCTL.register(cmdcls)
--- a/devtools/qunit.py	Wed May 13 15:58:21 2015 +0200
+++ b/devtools/qunit.py	Wed May 13 17:49:40 2015 +0200
@@ -29,7 +29,9 @@
 from logilab.common.shellutils import getlogin
 
 import cubicweb
+from cubicweb.view import View
 from cubicweb.web.controller import Controller
+from cubicweb.web.views.staticcontrollers import StaticFileController, STATIC_CONTROLLERS
 from cubicweb.devtools.httptest import CubicWebServerTC
 
 
@@ -102,11 +104,14 @@
             test_queue = self.test_queue
         self._qunit_controller = MyQUnitResultController
         self.vreg.register(MyQUnitResultController)
+        self.vreg.register(QUnitView)
+        self.vreg.register(CWSoftwareRootStaticController)
 
     def tearDown(self):
         super(QUnitTestCase, self).tearDown()
         self.vreg.unregister(self._qunit_controller)
-
+        self.vreg.unregister(QUnitView)
+        self.vreg.unregister(CWSoftwareRootStaticController)
 
     def abspath(self, path):
         """use self.__module__ to build absolute path if necessary"""
@@ -137,16 +142,8 @@
         for data in data_files:
             assert osp.exists(data), data
 
-        # generate html test file
-        jquery_dir = 'file://' + self.config.locate_resource('jquery.js')[0]
-        html_test_file = NamedTemporaryFile(suffix='.html', delete=False)
-        html_test_file.write(make_qunit_html(test_file, depends,
-                             base_url=self.config['base-url'],
-                             web_data_path=jquery_dir))
-        html_test_file.flush()
-        # copying data file
-        for data in data_files:
-            copyfile(data, tempfile.tempdir)
+        QUnitView.test_file = test_file
+        QUnitView.depends = depends
 
         while not self.test_queue.empty():
             self.test_queue.get(False)
@@ -158,7 +155,7 @@
         import time; time.sleep(5)
         browser.stop()
         # ... then actually run the test file
-        browser.start(html_test_file.name)
+        browser.start(self.config['base-url'] + "?vid=qunit")
         test_count = 0
         error = False
         def raise_exception(cls, *data):
@@ -224,15 +221,41 @@
         self._log_stack.append('%s: %s' % (result, message))
 
 
+class QUnitView(View):
+    __regid__ = 'qunit'
+
+    templatable = False
+
+    def call(self, **kwargs):
+        self.w(make_qunit_html(self.test_file, self.depends,
+                               base_url=self._cw.base_url()))
+
+
+class CWSoftwareRootStaticController(StaticFileController):
+    __regid__ = 'cwsoftwareroot'
+
+    def publish(self, rset=None):
+        staticdir = cubicweb.CW_SOFTWARE_ROOT
+        relpath = self.relpath[len(self.__regid__) + 1:]
+        return self.static_file(osp.join(staticdir, relpath))
+
+
+STATIC_CONTROLLERS.append(CWSoftwareRootStaticController)
+
+
 def cw_path(*paths):
-  return file_path(osp.join(cubicweb.CW_SOFTWARE_ROOT, *paths))
+    return '/cwsoftwareroot/' + '/'.join(paths)
+
 
 def file_path(path):
-    return 'file://' + osp.abspath(path)
+    l = len(cubicweb.CW_SOFTWARE_ROOT) + 1
+    return '/cwsoftwareroot/' + path[l:]
+
 
 def build_js_script(host):
     return """
     var host = '%s';
+    var BASE_URL = host;
 
     QUnit.moduleStart = function (name) {
       jQuery.ajax({
@@ -272,11 +295,11 @@
     }
     """ % host
 
-def make_qunit_html(test_file, depends=(), base_url=None,
-                    web_data_path=cw_path('web', 'data')):
+
+def make_qunit_html(test_file, depends=(), base_url=None):
     """"""
     data = {
-            'web_data': web_data_path,
+            'web_data': '/data',
             'web_test': cw_path('devtools', 'data'),
         }
 
--- a/web/test/jstests/test_ajax.js	Wed May 13 15:58:21 2015 +0200
+++ b/web/test/jstests/test_ajax.js	Wed May 13 17:49:40 2015 +0200
@@ -26,7 +26,7 @@
         expect(3);
         equals(jQuery('#main').children().length, 0);
         stop();
-        jQuery('#main').loadxhtml('/../ajax_url0.html', {
+        jQuery('#main').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html', {
             callback: function() {
                 try {
                     equals(jQuery('#main').children().length, 1);
@@ -43,7 +43,7 @@
         var scriptsIncluded = jsSources();
         equals(jQuery.inArray('http://foo.js', scriptsIncluded), - 1);
         stop();
-        jQuery('#main').loadxhtml('/../ajax_url1.html', {
+        jQuery('#main').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url1.html', {
             callback: function() {
                 try {
                     var origLength = scriptsIncluded.length;
@@ -66,7 +66,7 @@
         expect(3);
         equals(jQuery('#main').children().length, 0);
         stop();
-        var d = jQuery('#main').loadxhtml('/../ajax_url0.html');
+        var d = jQuery('#main').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html');
         d.addCallback(function() {
             try {
                 equals(jQuery('#main').children().length, 1);
@@ -81,7 +81,7 @@
         expect(1);
         var deferred = new Deferred();
         var result = jQuery.ajax({
-            url: './ajax_url0.html',
+            url: BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html',
             async: false,
             beforeSend: function(xhr) {
                 deferred._req = xhr;
@@ -105,7 +105,7 @@
         expect(3);
         equals(jQuery('#main').children().length, 0);
         stop();
-        var d = jQuery('#main').loadxhtml('/../ajax_url0.html');
+        var d = jQuery('#main').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html');
         d.addCallback(function(data, req, arg1, arg2) {
             try {
                 equals(arg1, 'Hello');
@@ -141,7 +141,7 @@
         });
         stop();
         var result = jQuery.ajax({
-            url: '/../ajax_url0.html',
+            url: BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html',
             async: false,
             beforeSend: function(xhr) {
                 deferred._req = xhr;
@@ -155,7 +155,7 @@
   test('test addErrback', function() {
         expect(1);
         stop();
-        var d = jQuery('#main').loadxhtml('/../ajax_url0.html');
+        var d = jQuery('#main').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html');
         d.addCallback(function() {
             // throw an exception to start errback chain
             try {
@@ -177,7 +177,7 @@
         expect(4);
         var counter = 0;
         stop();
-        var d = jQuery('#main').loadxhtml('/../ajax_url0.html', {
+        var d = jQuery('#main').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html', {
             callback: function() {
                 try {
                     equals(++counter, 1); // should be executed first
@@ -213,7 +213,7 @@
         /* use endswith because in pytest context we have an absolute path */
         ok(jQuery('head link').attr('href').endswith('/qunit.css'));
         stop();
-        jQuery('#main').loadxhtml('/../ajax_url1.html', {
+        jQuery('#main').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url1.html', {
             callback: function() {
                 var origLength = scriptsIncluded.length;
                 scriptsIncluded = jsSources();
@@ -237,7 +237,7 @@
     });
 
     test('test synchronous request loadRemote', function() {
-        var res = loadRemote('/../ajaxresult.json', {},
+        var res = loadRemote(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajaxresult.json', {},
         'GET', true);
         same(res, ['foo', 'bar']);
     });
@@ -250,7 +250,7 @@
             // check that server-response event on CubicWeb is triggered
             events = 'CubicWeb';
         });
-        jQuery('#main').loadxhtml('/../ajax_url0.html', {
+        jQuery('#main').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html', {
             callback: function() {
                 try {
                     equals(events, 'CubicWeb');
@@ -271,7 +271,7 @@
         jQuery(CubicWeb).bind('server-response', function() {
             nodes.push('CubicWeb');
         });
-        jQuery('#main').loadxhtml('/../ajax_url0.html', {
+        jQuery('#main').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html', {
             callback: function() {
                 try {
                     equals(nodes.length, 2);