[Pkg-javascript-commits] [libjs-fetch] 01/01: Run the tests with the embedded mocha-phantomjs

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Aug 7 16:25:03 UTC 2017


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

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

commit 17de9e3dd0f14e33026137591b3ea37e399359f1
Author: Ghislain Antony Vaillant <ghisvail at gmail.com>
Date:   Mon Aug 7 15:57:27 2017 +0100

    Run the tests with the embedded mocha-phantomjs
---
 debian/control                                     |   7 +-
 debian/copyright                                   |   5 +
 .../mocha-phantomjs-core/mocha-phantomjs-core.js   | 240 +++++++++++++++++++++
 debian/rules                                       |  11 +-
 4 files changed, 260 insertions(+), 3 deletions(-)

diff --git a/debian/control b/debian/control
index a80300b..0a94f3e 100644
--- a/debian/control
+++ b/debian/control
@@ -4,7 +4,12 @@ Uploaders: Ghislain Antony Vaillant <ghisvail at gmail.com>
 Section: javascript
 Priority: optional
 Build-Depends: debhelper (>= 10),
-               node-uglify
+               dpkg-dev (>= 1.17.14),
+               lsof <!nocheck>,
+               node-uglify,
+               phantomjs <!nocheck>,
+               xauth <!nocheck>,
+               xvfb <!nocheck>
 Standards-Version: 4.0.1
 Vcs-Browser: https://anonscm.debian.org/cgit/pkg-javascript/libjs-fetch.git
 Vcs-Git: https://anonscm.debian.org/git/pkg-javascript/libjs-fetch.git
diff --git a/debian/copyright b/debian/copyright
index aba2d5b..1c777cb 100644
--- a/debian/copyright
+++ b/debian/copyright
@@ -10,6 +10,11 @@ Files: debian/*
 Copyright: 2017 Ghislain Antony Vaillant <ghisvail at gmail.com>
 License: Expat
 
+Files: debian/missing-sources/mocha-phantomjs-core/*
+Copyright: 2012 Ken Collins
+License: Expat
+Comment: from https://github.com/nathanboktae/mocha-phantomjs-core/releases/tag/2.1.1
+
 License: Expat
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
diff --git a/debian/missing-sources/mocha-phantomjs-core/mocha-phantomjs-core.js b/debian/missing-sources/mocha-phantomjs-core/mocha-phantomjs-core.js
new file mode 100644
index 0000000..4718cd3
--- /dev/null
+++ b/debian/missing-sources/mocha-phantomjs-core/mocha-phantomjs-core.js
@@ -0,0 +1,240 @@
+var
+  system = require('system'),
+  webpage = require('webpage'),
+  fs = require('fs'),
+  stderr = system.stderr || system.stdout,
+  url = system.args[1],
+  reporter = system.args[2] || 'spec',
+  configured = false,
+  runStarted = false,
+  isSlimer = 'MozApplicationEvent' in window,
+  config = {},
+  hookData
+
+try {
+  config = JSON.parse(system.args[3] || '{}')
+} catch (e) {
+  console.log(e)
+  console.log('Bad JSON options')
+  phantom.exit(255)
+}
+
+
+if (!url) {
+  system.stdout.writeLine("Usage: " + (isSlimer ? 'slimerjs' : 'phantomjs') + " mocha-phantomjs-core.js URL REPORTER [CONFIG-AS-JSON]")
+  phantom.exit(255)
+}
+
+if (phantom.version.major < 1 || (phantom.version.major === 1 && phantom.version.minor < 9)) {
+  stderr.writeLine('mocha-phantomjs requires PhantomJS > 1.9.1')
+  phantom.exit(-2)
+}
+
+// Create and configure the client page
+var
+  output = config.file ? fs.open(config.file, 'w') : system.stdout,
+  page = webpage.create({
+    settings: config.settings
+  }),
+  fail = function(msg, errno) {
+    if (output && config.file) {
+      output.close()
+    }
+    if (msg) {
+      stderr.writeLine(msg)
+    }
+    return phantom.exit(errno || 1)
+  }
+
+if (config.hooks) {
+  hookData = {
+    page: page,
+    config: config,
+    reporter: reporter
+  }
+  try {
+    config.hooks = require(config.hooks)
+  }
+  catch (e) {
+    stderr.writeLine('Error loading hooks: ' + e.message)
+    phantom.exit(253)
+  }
+} else {
+  config.hooks = {}
+}
+
+if (config.headers) {
+  page.customHeaders = config.headers
+}
+(config.cookies || []).forEach(function(cookie) {
+  page.addCookie(cookie)
+})
+if (config.viewportSize) {
+  page.viewportSize = config.viewportSize
+}
+
+page.onConsoleMessage = function(msg) {
+  return system.stdout.writeLine(msg)
+}
+page.onResourceError = function(resErr) {
+  if (!config.ignoreResourceErrors) {
+    return stderr.writeLine("Error loading resource " + resErr.url + " (" + resErr.errorCode + "). Details: " + resErr.errorString)
+  }
+}
+page.onError = function(msg, traces) {
+  if (page.evaluate(function() { return !!window.onerror })) return
+
+  fail(msg + '\n' + traces.reduce(function(stack, trace) {
+    return stack + '\n  ' + (trace.function ? ' in ' + trace.function + '' : '')
+            + ' at ' + trace.file + ':' + trace.line
+  }, ''))
+}
+
+// Load the test page
+page.open(url)
+page.onInitialized = function() {
+  page.injectJs('browser-shim.js')
+
+  if (isSlimer && config.settings && config.settings.userAgent) {
+    page.evaluate(function(ua) {
+      navigator.__defineGetter__('userAgent', function() { return ua })
+    }, config.settings.userAgent)
+  }
+}
+page.onResourceReceived = function(resource) {
+  if (resource.url.match(/mocha\.js$/)) {
+    page.evaluate(function() {
+      checkForMocha()
+    })
+  }
+}
+page.onCallback = function(data) {
+  if (data) {
+    if (data.stdout) {
+      output.write(data.stdout)
+    } else if (typeof data.screenshot === 'string') {
+      page.render(data.screenshot + '.png')
+    } else if (data.viewportSize && (data.viewportSize.width || data.viewportSize.height)) {
+      page.viewportSize = {
+        width: data.viewportSize.width || page.viewportSize.width,
+        height: data.viewportSize.height || page.viewportSize.height,
+      }
+    } else if (data.configureColWidth) {
+      page.evaluate(function(columns) {
+        Mocha.reporters.Base.window.width = columns
+      }, parseInt(system.env.COLUMNS || 75) * .75 | 0)
+    } else if (data.configureMocha) {
+      configureMocha()
+    } else if ('testRunStarted' in data) {
+      if (data.testRunStarted == 0) {
+        fail('mocha.run() was called with no tests')
+      }
+      runStarted = true
+    } else if (data.testRunEnded) {
+      if (typeof config.hooks.afterEnd === 'function') {
+        hookData.runner = data.testRunEnded
+        config.hooks.afterEnd(hookData)
+      }
+      if (config.file) {
+        output.close()
+      }
+      setTimeout(function() {
+        phantom.exit(data.testRunEnded.failures)
+      }, 100)
+    } else if (data.sendEvent) {
+      page.sendEvent.apply(page, data.sendEvent)
+    }
+  }
+  return true
+}
+page.onLoadFinished = function(status) {
+  page.onLoadFinished = null
+  if (status !== 'success') {
+    fail('Failed to load the page. Check the url: ' + url)
+    return
+  }
+
+  var loadTimeout = config.loadTimeout || 10000
+  setTimeout(function() {
+    if (!configured) {
+      if (page.evaluate(function() { return !window.mocha })) {
+        fail('mocha was not found in the page within ' + loadTimeout + 'ms of the page loading.')
+      } else if (page.evaluate(function() { return window.initMochaPhantomJS })) {
+        fail('Likely due to external resource loading and timing, your tests require calling `window.initMochaPhantomJS()` before calling any mocha setup functions. See https://github.com/nathanboktae/mocha-phantomjs-core/issues/12')
+      } else {
+        fail('mocha was not initialized within ' + loadTimeout + 'ms of the page loading. Make sure to call `mocha.ui` or `mocha.setup`.')
+      }
+    } else if (!runStarted) {
+      fail('mocha.run() was not called within ' + loadTimeout + 'ms of the page loading.')
+    }
+  }, loadTimeout)
+}
+
+function configureMocha() {
+  page.evaluate(function(config, env) {
+    mocha.env = env
+
+    mocha.useColors(config.useColors)
+    mocha.bail(config.bail)
+    if (config.timeout) {
+      mocha.timeout(config.timeout)
+    }
+    if (config.grep) {
+      mocha.grep(config.grep)
+    }
+    if (config.invert) {
+      mocha.invert()
+    }
+  }, config, system.env)
+
+  // setup a the reporter
+  if (page.evaluate(setupReporter, reporter) !== true) {
+    // we failed to set the reporter - likely a 3rd party reporter than needs to be wrapped
+    try {
+      var customReporter = fs.read(reporter)
+    } catch(e) {
+      fail('Unable to open file \'' + reporter + '\'')
+    }
+
+    var wrapper = function() {
+      var exports, module, process, require;
+      require = function(what) {
+        what = what.replace(/[^a-zA-Z0-9]/g, '')
+        for (var r in Mocha.reporters) {
+          if (r.toLowerCase() === what) {
+            return Mocha.reporters[r]
+          }
+        }
+        throw new Error("Your custom reporter tried to require '" + what + "', but Mocha is not running in Node.js in mocha-phantomjs, so Node modules cannot be required - only other reporters");
+      };
+      module = {};
+      exports = undefined;
+      process = Mocha.process;
+      'customreporter';
+      return Mocha.reporters.Custom = exports || module.exports;
+    },
+    wrappedReporter = wrapper.toString().replace("'customreporter'", "(function() {" + (customReporter.toString()) + "})()")
+
+    page.evaluate(wrappedReporter)
+    if (page.evaluate(function() { return !Mocha.reporters.Custom }) ||
+        page.evaluate(setupReporter) !== true) {
+      fail('Failed to use load and use the custom reporter ' + reporter)
+    }
+  }
+
+  if (typeof config.hooks.beforeStart === 'function') {
+    config.hooks.beforeStart(hookData)
+  }
+  configured = true
+}
+
+function setupReporter(reporter) {
+  try {
+    mocha.setup({
+      reporter: reporter || Mocha.reporters.Custom
+    })
+    return true
+  } catch (error) {
+    return error
+  }
+}
\ No newline at end of file
diff --git a/debian/rules b/debian/rules
index 3592496..ef16201 100755
--- a/debian/rules
+++ b/debian/rules
@@ -6,12 +6,19 @@
 %:
 	dh $@
 
+override_dh_auto_configure:
+ifeq (,$(findstring nocheck,$(DEB_BUILD_PROFILES)))
+	mkdir node_modules
+	ln -s $(CURDIR)/debian/missing-sources/mocha-phantomjs-core/ ./node_modules/
+endif
+
 override_dh_auto_build:
 	uglifyjs -o fetch.min.js fetch.js
 
-# Testing is disabled since it depends on mocha-phantomjs, which has yet to be
-# packaged (#774571).
 override_dh_auto_test:
+ifeq (,$(findstring nocheck,$(DEB_BUILD_PROFILES)))
+	xvfb-run -a -s "-screen 0 640x480x16" ./script/test
+endif
 
 get-orig-source:
 	uscan --download-current-version --force-download --no-symlink

-- 
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