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
This commit is contained in:
David Taylor 2017-08-18 19:08:58 +01:00 committed by Sam
parent e976b98efc
commit d65570a8a1
3 changed files with 76 additions and 65 deletions

4
.gitignore vendored
View File

@ -117,3 +117,7 @@ vendor/bundle/*
#ignore jetbrains ide file #ignore jetbrains ide file
*.iml *.iml
# ignore nodejs files
/node_modules
/package-lock.json

View File

@ -1,11 +1,11 @@
desc "Runs the qunit test suite" 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 "rack"
require "socket" 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" abort "PhantomJS is not installed. Download from http://phantomjs.org"
end end
@ -37,7 +37,7 @@ task "qunit:test", [:timeout, :qunit_path, :use_chrome] => :environment do |_, a
test_path = "#{Rails.root}/vendor/assets/javascripts" test_path = "#{Rails.root}/vendor/assets/javascripts"
qunit_path = args[:qunit_path] || "/qunit" 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}" cmd = "node #{test_path}/run-qunit-chrome.js http://localhost:#{port}#{qunit_path}"
else else
cmd = "phantomjs #{test_path}/run-qunit.js http://localhost:#{port}#{qunit_path}" cmd = "phantomjs #{test_path}/run-qunit.js http://localhost:#{port}#{qunit_path}"

View File

@ -3,6 +3,8 @@
// Requires chrome-launcher and chrome-remote-interface from npm // Requires chrome-launcher and chrome-remote-interface from npm
// An up-to-date version of chrome is also required // An up-to-date version of chrome is also required
/* globals Promise */
var args = process.argv.slice(2); var args = process.argv.slice(2);
if (args.length < 1 || args.length > 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 chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface'); const CDP = require('chrome-remote-interface');
(async function() { (function() {
async function launchChrome() { function launchChrome() {
return await chromeLauncher.launch({ return chromeLauncher.launch({
chromeFlags: [ chromeFlags: [
'--disable-gpu', '--disable-gpu',
'--headless' '--headless',
'--no-sandbox'
] ]
}); });
} }
const chrome = await launchChrome();
const protocol = await CDP({
port: chrome.port
});
const { launchChrome().then(chrome => {
Page, CDP({
Runtime port: chrome.port
} = protocol; }).then(protocol => {
await Page.enable(); const {Page, Runtime} = protocol;
await Runtime.enable(); Promise.all([Page.enable(), Runtime.enable()]).then(()=>{
Runtime.consoleAPICalled((response) => { Runtime.consoleAPICalled((response) => {
const message = response['args'][0].value; const message = response['args'][0].value;
// If it's a simple test result, write without newline // If it's a simple test result, write without newline
if(message === "." || message === "F"){ if(message === "." || message === "F"){
process.stdout.write(message); process.stdout.write(message);
}else{ }else{
console.log(message); 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 (numFails.result.type !== 'undefined') { Page.navigate({
clearInterval(interval); url: args[0]
protocol.close(); });
chrome.kill();
if (numFails.value > 0) { Page.loadEventFired(() => {
process.exit(1);
} else {
process.exit();
}
}
}
}, 250);
}); 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 // The following functions are converted to strings
// And then sent to chrome to be evalaluated // And then sent to chrome to be evalaluated
function logQUnit() { function logQUnit() {