From d65570a8a1febcd64492588b02fecb74415b7efe Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 18 Aug 2017 19:08:58 +0100 Subject: [PATCH] Preparation for using chrome for qunit in docker images (#5062) Move use_chrome option to ENV variable Rewrite script to work with node 6 (current LTS version used in discourse_docker) Add node stuff to gitignore --- .gitignore | 4 + lib/tasks/qunit.rake | 6 +- vendor/assets/javascripts/run-qunit-chrome.js | 131 +++++++++--------- 3 files changed, 76 insertions(+), 65 deletions(-) diff --git a/.gitignore b/.gitignore index f096ebc684e..60094ab222b 100644 --- a/.gitignore +++ b/.gitignore @@ -117,3 +117,7 @@ vendor/bundle/* #ignore jetbrains ide file *.iml + +# ignore nodejs files +/node_modules +/package-lock.json diff --git a/lib/tasks/qunit.rake b/lib/tasks/qunit.rake index 813084da5e2..98d69c8403e 100644 --- a/lib/tasks/qunit.rake +++ b/lib/tasks/qunit.rake @@ -1,11 +1,11 @@ desc "Runs the qunit test suite" -task "qunit:test", [:timeout, :qunit_path, :use_chrome] => :environment do |_, args| +task "qunit:test", [:timeout, :qunit_path] => :environment do |_, args| require "rack" require "socket" - unless %x{which phantomjs > /dev/null 2>&1} || args[:use_chrome] + unless %x{which phantomjs > /dev/null 2>&1} || ENV["USE_CHROME"] abort "PhantomJS is not installed. Download from http://phantomjs.org" end @@ -37,7 +37,7 @@ task "qunit:test", [:timeout, :qunit_path, :use_chrome] => :environment do |_, a test_path = "#{Rails.root}/vendor/assets/javascripts" qunit_path = args[:qunit_path] || "/qunit" - if args[:use_chrome] + if ENV["USE_CHROME"] cmd = "node #{test_path}/run-qunit-chrome.js http://localhost:#{port}#{qunit_path}" else cmd = "phantomjs #{test_path}/run-qunit.js http://localhost:#{port}#{qunit_path}" diff --git a/vendor/assets/javascripts/run-qunit-chrome.js b/vendor/assets/javascripts/run-qunit-chrome.js index bbe194a91e4..b3cf09dbdec 100644 --- a/vendor/assets/javascripts/run-qunit-chrome.js +++ b/vendor/assets/javascripts/run-qunit-chrome.js @@ -3,6 +3,8 @@ // Requires chrome-launcher and chrome-remote-interface from npm // An up-to-date version of chrome is also required +/* globals Promise */ + var args = process.argv.slice(2); if (args.length < 1 || args.length > 2) { @@ -13,83 +15,88 @@ if (args.length < 1 || args.length > 2) { const chromeLauncher = require('chrome-launcher'); const CDP = require('chrome-remote-interface'); -(async function() { +(function() { - async function launchChrome() { - return await chromeLauncher.launch({ + function launchChrome() { + return chromeLauncher.launch({ chromeFlags: [ '--disable-gpu', - '--headless' + '--headless', + '--no-sandbox' ] }); } - const chrome = await launchChrome(); - const protocol = await CDP({ - port: chrome.port - }); - const { - Page, - Runtime - } = protocol; - await Page.enable(); - await Runtime.enable(); + launchChrome().then(chrome => { + CDP({ + port: chrome.port + }).then(protocol => { + const {Page, Runtime} = protocol; + Promise.all([Page.enable(), Runtime.enable()]).then(()=>{ - Runtime.consoleAPICalled((response) => { - const message = response['args'][0].value; + Runtime.consoleAPICalled((response) => { + const message = response['args'][0].value; - // If it's a simple test result, write without newline - if(message === "." || message === "F"){ - process.stdout.write(message); - }else{ - console.log(message); - } - }); - - Page.navigate({ - url: args[0] - }); - - Page.loadEventFired(async() => { - - await Runtime.evaluate({ - expression: `(${qunit_script})()` - }); - - const timeout = parseInt(args[1] || 300000, 10); - var start = Date.now(); - - var interval = setInterval(async() => { - if (Date.now() > start + timeout) { - console.error("Tests timed out"); - - protocol.close(); - chrome.kill(); - process.exit(124); - } else { - - const numFails = await Runtime.evaluate({ - expression: `(${check_script})()` + // If it's a simple test result, write without newline + if(message === "." || message === "F"){ + process.stdout.write(message); + }else{ + console.log(message); + } }); - if (numFails.result.type !== 'undefined') { - clearInterval(interval); - protocol.close(); - chrome.kill(); + Page.navigate({ + url: args[0] + }); - if (numFails.value > 0) { - process.exit(1); - } else { - process.exit(); - } - } - } - }, 250); + Page.loadEventFired(() => { + + Runtime.evaluate({ + expression: `(${qunit_script})()` + }).then(() => { + const timeout = parseInt(args[1] || 300000, 10); + var start = Date.now(); - }); + var interval = setInterval(() => { + if (Date.now() > start + timeout) { + console.error("Tests timed out"); + protocol.close(); + chrome.kill(); + process.exit(124); + } else { + + Runtime.evaluate({ + expression: `(${check_script})()` + }).then((numFails) => { + if (numFails.result.type !== 'undefined') { + clearInterval(interval); + protocol.close(); + chrome.kill(); + + if (numFails.value > 0) { + process.exit(1); + } else { + process.exit(); + } + } + }).catch(error); + } + }, 250); + }).catch(error(1)); + }); + }).catch(error(3)); + }).catch(error(4)); + }).catch(error(5)); })(); +function error(code){ + return function(){ + console.log("A promise failed to resolve code:"+code); + process.exit(1); + }; +} + // The following functions are converted to strings // And then sent to chrome to be evalaluated function logQUnit() { @@ -161,4 +168,4 @@ function check() { return window.qunitDone.failed; } } -const check_script = check.toString(); \ No newline at end of file +const check_script = check.toString();