[Pkg-javascript-commits] [libjs-fetch] 01/01: Make the tests work

Ximin Luo infinity0 at debian.org
Mon Aug 7 17:30:30 UTC 2017


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch master
in repository libjs-fetch.

commit ac6d07d3182aafa8945dd63cf6f628200f65fe11
Author: Ximin Luo <infinity0 at debian.org>
Date:   Mon Aug 7 19:30:05 2017 +0200

    Make the tests work
---
 debian/clean                                       |   1 +
 debian/control                                     |   4 +
 .../mocha-phantomjs-core/browser-shim.js           | 156 +++++++++++++++++++++
 debian/rules                                       |  12 +-
 4 files changed, 171 insertions(+), 2 deletions(-)

diff --git a/debian/clean b/debian/clean
index cfde634..97007ea 100644
--- a/debian/clean
+++ b/debian/clean
@@ -1 +1,2 @@
 fetch.min.js
+node_modules/*
diff --git a/debian/control b/debian/control
index 0a94f3e..030a137 100644
--- a/debian/control
+++ b/debian/control
@@ -7,6 +7,10 @@ Build-Depends: debhelper (>= 10),
                dpkg-dev (>= 1.17.14),
                lsof <!nocheck>,
                node-uglify,
+               chai <!nocheck>,
+               mocha <!nocheck>,
+               node-browserify-lite <!nocheck>,
+               node-es6-promise <!nocheck>,
                phantomjs <!nocheck>,
                xauth <!nocheck>,
                xvfb <!nocheck>
diff --git a/debian/missing-sources/mocha-phantomjs-core/browser-shim.js b/debian/missing-sources/mocha-phantomjs-core/browser-shim.js
new file mode 100644
index 0000000..9e25c52
--- /dev/null
+++ b/debian/missing-sources/mocha-phantomjs-core/browser-shim.js
@@ -0,0 +1,156 @@
+(function(){
+
+  if (typeof Function.prototype.bind !== 'function') {
+    Function.prototype.bind = function bind(obj) {
+      var args = Array.prototype.slice.call(arguments, 1),
+        self = this,
+        nop = function() {},
+        bound = function() {
+          return self.apply(
+            this instanceof nop ? this : (obj || {}), args.concat(
+              Array.prototype.slice.call(arguments)
+            )
+          )
+        }
+      nop.prototype = this.prototype || {}
+      bound.prototype = new nop()
+      return bound
+    }
+  }
+
+  function isFileReady(readyState) {
+    // Check to see if any of the ways a file can be ready are available as properties on the file's element
+    return (!readyState || readyState == 'loaded' || readyState == 'complete' || readyState == 'uninitialized')
+  }
+
+  function shimMochaProcess(M) {
+    // Mocha needs a process.stdout.write in order to change the cursor position.
+    M.process = M.process || {}
+    M.process.stdout = M.process.stdout || process.stdout
+    M.process.stdout.write = function(s) { window.callPhantom({ stdout: s }) }
+    window.callPhantom({ getColWith: true })
+  }
+
+  function shimMochaInstance(m) {
+    var origRun = m.run, origUi = m.ui
+    m.ui = function() {
+      var retval = origUi.apply(mocha, arguments)
+      window.callPhantom({ configureMocha: true })
+      m.reporter = function() {}
+      return retval
+    }
+    m.run = function() {
+      window.callPhantom({ testRunStarted: m.suite.suites.length })
+      m.runner = origRun.apply(mocha, arguments)
+      if (m.runner.stats && m.runner.stats.end) {
+        window.callPhantom({ testRunEnded: m.runner })
+      } else {
+        m.runner.on('end', function() {
+          window.callPhantom({ testRunEnded: m.runner })
+        })
+      }
+      return m.runner
+    }
+  }
+
+  Object.defineProperty(window, 'checkForMocha', {
+    value: function() {
+      var scriptTags = document.querySelectorAll('script'),
+          mochaScript = Array.prototype.filter.call(scriptTags, function(s) {
+            var src = s.getAttribute('src')
+            return src && src.match(/mocha\.js$/)
+          })[0]
+
+      if (mochaScript) {
+        mochaScript.onreadystatechange = mochaScript.onload = function () {
+          if (isFileReady(mochaScript.readyState)) {
+            initMochaPhantomJS()
+          }
+        }
+      }
+    }
+  })
+
+  if ('mozInnerScreenX' in window) {
+    // in slimerjs, we can stub out a setter to shim Mocha. phantomjs 2 fails
+    // to allow the property to be reconfigured...
+    Object.defineProperty(window, 'mocha', {
+      get: function() { return undefined },
+      set: function(m) {
+        shimMochaInstance(m)
+        delete window.mocha
+        window.mocha = m
+      },
+      configurable: true
+    })
+
+    Object.defineProperty(window, 'Mocha', {
+      get: function() { return undefined },
+      set: function(m) {
+        delete window.Mocha
+        window.Mocha = m
+        shimMochaProcess(m)
+      },
+      configurable: true
+    })
+  } else {
+    Object.defineProperty(window, 'initMochaPhantomJS', {
+      value: function () {
+        shimMochaProcess(Mocha)
+        shimMochaInstance(mocha)
+        delete window.initMochaPhantomJS
+      },
+      configurable: true
+    })
+  }
+
+  // Mocha needs the formating feature of console.log so copy node's format function and
+  // monkey-patch it into place. This code is copied from node's, links copyright applies.
+  // https://github.com/joyent/node/blob/master/lib/util.js
+  if (!console.format) {
+    console.format = function(f) {
+      if (typeof f !== 'string') {
+        return Array.prototype.map.call(arguments, function(arg) {
+          try {
+            return JSON.stringify(arg)
+          }
+          catch (_) {
+            return '[Circular]'
+          }
+        }).join(' ')
+      }
+      var i = 1;
+      var args = arguments;
+      var len = args.length;
+      var str = String(f).replace(/%[sdj%]/g, function(x) {
+        if (x === '%%') return '%';
+        if (i >= len) return x;
+        switch (x) {
+          case '%s': return String(args[i++]);
+          case '%d': return Number(args[i++]);
+          case '%j':
+            try {
+              return JSON.stringify(args[i++]);
+            } catch (_) {
+              return '[Circular]';
+            }
+          default:
+            return x;
+        }
+      });
+      for (var x = args[i]; i < len; x = args[++i]) {
+        if (x === null || typeof x !== 'object') {
+          str += ' ' + x;
+        } else {
+          str += ' ' + JSON.stringify(x);
+        }
+      }
+      return str;
+    };
+    var origError = console.error;
+    console.error = function(){ origError.call(console, console.format.apply(console, arguments)); };
+    var origLog = console.log;
+    console.log = function(){ origLog.call(console, console.format.apply(console, arguments)); };
+  }
+
+})();
diff --git a/debian/rules b/debian/rules
index ef16201..98c7113 100755
--- a/debian/rules
+++ b/debian/rules
@@ -8,8 +8,16 @@
 
 override_dh_auto_configure:
 ifeq (,$(findstring nocheck,$(DEB_BUILD_PROFILES)))
-	mkdir node_modules
-	ln -s $(CURDIR)/debian/missing-sources/mocha-phantomjs-core/ ./node_modules/
+	mkdir -p node_modules/chai node_modules/promise-polyfill
+	ln -sf $(CURDIR)/debian/missing-sources/mocha-phantomjs-core/ ./node_modules/
+	ln -sf /usr/lib/nodejs/mocha ./node_modules/
+	# promise-polyfill not in Debian, use es6-promise
+	ln -sf /usr/lib/nodejs/es6-promise/dist/es6-promise.js ./node_modules/promise-polyfill/promise.js
+	# Build our own browserified chai from chai's node modules
+	# TODO: ideally this should be fixed in the chai package, file them a bug
+	NODE_PATH=/usr/lib/nodejs browserify-lite /usr/lib/nodejs/chai/index.js --outfile node_modules/chai/chai.js --standalone chai
+	# this is needed to work around a node-browserify-lite deficiency, for test-worker.html to work
+	sed -i -e '1,10s/window/this/g' node_modules/chai/chai.js
 endif
 
 override_dh_auto_build:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/libjs-fetch.git



More information about the Pkg-javascript-commits mailing list