Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] middleware/testRunner: Update resources from OpenUI5 #349

Merged
merged 2 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 82 additions & 12 deletions lib/middleware/testRunner/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,52 @@
this.aPages.push(sTestPage);
};

function XHRQueue(iMaxParallelRequests, iWaitTime) {
this.iLimit = iMaxParallelRequests === undefined ? Infinity : iMaxParallelRequests;
this.iWaitTime = iWaitTime === undefined ? 0 : iWaitTime;
this.aPendingTasks = [];
this.oRunningTasks = new Set();
this.iLastTaskExecution = -Infinity;
}

XHRQueue.prototype.ajax = function(sUrl, options) {
var oTask = {
url: sUrl,
options: options
};
oTask.promise = new Promise(function(resolve, reject) {
oTask.resolve = resolve;
oTask.reject = reject;
});
this.aPendingTasks.push(oTask);
this._processNext();
return oTask.promise;
};

XHRQueue.prototype._processNext = function() {
var iNow = Date.now();
var iDelay = iNow - this.iLastTaskExecution;
if ( iDelay < this.iWaitTime ) {
setTimeout(function() {
this._processNext();
}.bind(this), iDelay);
return;
}
if ( this.aPendingTasks.length > 0 && this.oRunningTasks.size < this.iLimit ) {
var oTask = this.aPendingTasks.shift();
this.oRunningTasks.add(oTask);
this.iLastTaskExecution = iNow;
Promise.resolve(jQuery.ajax(oTask.url, oTask.options))
.then(oTask.resolve, oTask.reject)
.finally(function() {
this.oRunningTasks.delete(oTask);
this._processNext();
}.bind(this));
}
};

var oXHRQueue = new XHRQueue(50, 2);

/*
* Template for test results
*/
Expand Down Expand Up @@ -62,6 +108,21 @@
window.sap.ui.qunit.TestRunner = {

checkTestPage: function(sTestPage, bSequential) {
var t0 = Date.now();
var oPromise =
this._checkTestPage(sTestPage, bSequential)
.then(function(aTestPages) {
var t1 = Date.now();
window.console.log("[DEBUG] checkTestPage(\"" + sTestPage + "\") found " + aTestPages.length + " pages in " + (t1 - t0) + "msec.");
window.console.log("[DEBUG] checkTestPage(\"" + sTestPage + "\") currently running IFrames: " + window.frames.length);
return aTestPages;
});
oPromise.done = oPromise.then; // compat for Deferred
oPromise.fail = oPromise.catch; // compat for Deferred
return oPromise;
},

_checkTestPage: function(sTestPage, bSequential) {

var oPromise = new Promise(function(resolve, reject) {

Expand All @@ -70,25 +131,28 @@
reject("QUnit: invalid test page specified");
}

/*
if (window.console && typeof window.console.log === "function") {
window.console.log("QUnit: checking test page: " + sTestPage);
}
}*/

// check for an existing test page and check for test suite or page
jQuery.get(sTestPage).done(function(sData) {
oXHRQueue.ajax(sTestPage).then(function(sData) {
if (/(?:window\.suite\s*=|function\s*suite\s*\(\s*\)\s*{)/.test(sData)
|| (/data-sap-ui-testsuite/.test(sData) && !/sap\/ui\/test\/starter\/runTest/.test(sData)) ) {
// window.console.log("[DEBUG] _checkTestPage checking testsuite page: " + sTestPage);
var $frame = jQuery("<iframe>");
var that = this;

var onSuiteReady = function(oIFrame) {
that.findTestPages(oIFrame, bSequential).then(function(aTestPages) {
var aTestPagesFiltered = aTestPages.filter(function(e, i, a) { return a.indexOf(e) === i; });
$frame.remove();
// avoid duplicates in test pages
resolve(aTestPages.filter(function(e, i, a) { return a.indexOf(e) === i; }));
resolve(aTestPagesFiltered);
}, function(oError) {
if (window.console && typeof window.console.error === "function") {
window.console.error("QUnit: failed to load page '" + sTestPage + "'");
window.console.error("QUnit: failed to load page '" + sTestPage + "'" + " Error: " + oError);
}
$frame.remove();
resolve([]);
Expand All @@ -111,7 +175,7 @@
} else {
resolve([sTestPage]);
}
}.bind(this)).fail(function(xhr,status,msg) {
}.bind(this)).catch(function(xhr,status,msg) {
var text = (xhr ? xhr.status + " " : "") + (msg || status || 'unspecified error');
if (window.console && typeof window.console.error === "function") {
window.console.error("QUnit: failed to load page '" + sTestPage + "': " + text);
Expand All @@ -123,17 +187,18 @@

}.bind(this));

oPromise.done = oPromise.then; // compat for Deferred
oPromise.fail = oPromise.catch; // compat for Deferred

return oPromise;

},

findTestPages: function(oIFrame, bSequential) {

return Promise.resolve(oIFrame.contentWindow.suite()).then(function(oSuite) {
window.console.log("[DEBUG] findTestPages oIFrame source: " + oIFrame.src);
var aPages = oSuite && oSuite.getTestPages() || [];
for (var i = 0; i < aPages.length; i++) {
window.console.log("[DEBUG] findTestPages oIFrame source " + oIFrame.src + ": " + aPages[i]);
}
return new Promise(function(resolve, reject) {

try {
Expand All @@ -146,7 +211,7 @@

var aTestPages = [];
aTestPagePromises.push(aPages.reduce(function(oPromise, sTestPage) {
return oPromise.then(this.checkTestPage.bind(this, sTestPage, bSequential)).then(function(aFoundTestPages) {
return oPromise.then(this._checkTestPage.bind(this, sTestPage, bSequential)).then(function(aFoundTestPages) {
aTestPages = aTestPages.concat(aFoundTestPages);
});
}.bind(this), Promise.resolve([])).then(function() {
Expand All @@ -157,7 +222,7 @@

for (var i = 0, l = aPages.length; i < l; i++) {
var sTestPage = aPages[i];
aTestPagePromises.push(this.checkTestPage(sTestPage, bSequential));
aTestPagePromises.push(this._checkTestPage(sTestPage, bSequential));
}

}
Expand All @@ -169,6 +234,11 @@
aTestPages = aTestPages.concat(aFoundTestPages[i]);
}
resolve(aTestPages);
})
.catch(function(oError){
if (window.console && typeof window.console.error === "function") {
window.console.error("[DEBUG] findTestPages Promise.all error: " + oError);
}
});
}

Expand Down Expand Up @@ -267,10 +337,10 @@
var sHTML = fnTemplate(oContext);
var $testResult = jQuery(sHTML);
var $children = $testResult.children();
jQuery($children[0]).click(function() {
jQuery($children[0]).on("click", function() {
jQuery(this.nextSibling).toggle();
});
jQuery($children[1]).find("li.test > p").click(function() {
jQuery($children[1]).find("li.test > p").on("click", function() {
jQuery(this.parentElement.children[2]).toggle();
});
$testResult.appendTo("div.test-reporting");
Expand Down
30 changes: 15 additions & 15 deletions lib/middleware/testRunner/testrunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
});
jQuery(selectbox).data("options", options);

jQuery(textbox).bind("change keyup", function() {
jQuery(textbox).on("change keyup", function() {
var options = jQuery(selectbox).empty().data("options");
var search = jQuery(this).val().trim();
var regex = new RegExp(search, "gi");

jQuery.each(options, function(i) {
var option = options[i];
if(option.text.match(regex) !== null) {
jQuery(selectbox).append(jQuery("<option>").text(option.text).val(option.value).dblclick(function() {
jQuery(selectbox).append(jQuery("<option>").text(option.text).val(option.value).on("dblclick", function() {
jQuery("#selectedTests").append(jQuery(this).clone());
}));
}
Expand Down Expand Up @@ -77,7 +77,7 @@
$this.val("Show all results");
}
}
jQuery("#showResults").click(showResults);
jQuery("#showResults").on("click", showResults);

jQuery("#testPage").val(sap.ui.qunit.TestRunner.getTestPageUrl());

Expand All @@ -92,7 +92,7 @@
var fnTemplate = Handlebars.compile(jQuery("#qunitResults").html());
var sHTML = fnTemplate(oContext);
jQuery("#testPageSelectDiv").html(sHTML);
jQuery("#testPageSelect > option").dblclick(function () {
jQuery("#testPageSelect > option").on("dblclick", function () {
jQuery("#selectedTests").append(jQuery("#find").clone());
});
jQuery("#copyButtons").show();
Expand All @@ -103,34 +103,34 @@
jQuery("#console").val(aTestPages.join("\n"));
});
}
jQuery("#find").click(find);
jQuery("#find").on("click", find);

function copy() {
var aSelectedTests = jQuery("#testPageSelect")[0].selectedOptions;
for (var i = 0; i < aSelectedTests.length; i++) {
jQuery("#selectedTests").append(jQuery("<option>").text(jQuery(aSelectedTests[i]).text()).val(jQuery(aSelectedTests[i]).text()));
}
}jQuery("#copy").click(copy);
}jQuery("#copy").on("click", copy);

function copyAll() {
jQuery("#selectedTests").append(jQuery("#testPageSelect > option").clone());
}
jQuery("#copyall").click(copyAll);
jQuery("#copyall").on("click", copyAll);

function remove() {
var aSelectedTests = jQuery("#selectedTests")[0].selectedOptions;
for (var i = aSelectedTests.length-1; i >= 0; i--) {
jQuery(aSelectedTests[i]).remove();
}
}jQuery("#remove").click(remove);
}jQuery("#remove").on("click", remove);

function removeAll() {
jQuery("#selectedTests > option").remove();
}
jQuery("#removeall").click(removeAll);
jQuery("#removeall").on("click", removeAll);

function run() {
jQuery("#open").click();
jQuery("#open").trigger("click");
window.oStartTime = new Date();
var $this = jQuery("#run");
if ($this.val() === "Run") {
Expand Down Expand Up @@ -160,7 +160,7 @@
}
}

jQuery("#run").click(function () {
jQuery("#run").on("click", function () {
if(jQuery("#selectedTests>option").length === 0) {
alert("Please select at least on test to execute");
} else {
Expand All @@ -184,7 +184,7 @@
jQuery("#open").attr("style", "width:0px;height:0px;margin:5px;border-style:solid;border-width:0 12px 12px 12px;border-color:transparent transparent #007dc0 transparent;float: right;position: relative;left: -10px;cursor: pointer;");
}
}
jQuery("#open").click(open);
jQuery("#open").on("click", open);


function openCoverage() {
Expand All @@ -196,7 +196,7 @@
jQuery("#openCoverage").attr("style", "width:0px;height:0px;margin:5px;border-style:solid;border-width:0 12px 12px 12px;border-color:transparent transparent #007dc0 transparent;float: right;position: relative;left: -10px;cursor: pointer;");
}
}
jQuery("#openCoverage").click(openCoverage);
jQuery("#openCoverage").on("click", openCoverage);

function openReporting() {
jQuery("#showResults").toggle();
Expand All @@ -211,12 +211,12 @@
jQuery("#openReporting").attr("style", "width:0px;height:0px;margin:5px;border-style:solid;border-width:0 12px 12px 12px;border-color:transparent transparent #007dc0 transparent;float: right;position: relative;left: -10px;cursor: pointer;");
}
}
jQuery("#openReporting").click(openReporting);
jQuery("#openReporting").on("click", openReporting);

function stop() {
sap.ui.qunit.TestRunner.stopTests();
}
jQuery("#stop").click(stop);
jQuery("#stop").on("click", stop);

setInterval(function() {
if (window.aTestPages) {
Expand Down
28 changes: 28 additions & 0 deletions scripts/update-testRunner-resources.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

set -e

# Go to project root
cd "$(dirname -- "$0")/.."

# Download latest files
cd lib/middleware/testRunner
curl -L -O https://github.com/SAP/openui5/raw/master/src/sap.ui.core/test/sap/ui/qunit/TestRunner.js
curl -L -O https://github.com/SAP/openui5/raw/master/src/sap.ui.core/test/sap/ui/qunit/testrunner.css
curl -L -O https://github.com/SAP/openui5/raw/master/src/sap.ui.core/test/sap/ui/qunit/testrunner.html

# Check if there are updates
git diff --quiet && echo "No updates. Exiting..." && exit 0

# Get latest commit url for src/sap.ui.core/test/sap/ui/qunit path
COMMIT_URL=$(curl "https://api.github.com/repos/SAP/openui5/commits?sha=master&path=src/sap.ui.core/test/sap/ui/qunit" | \
node -r fs -e "process.stdout.write(JSON.parse(fs.readFileSync(\"/dev/stdin\", \"utf8\"))[0].html_url)")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be jq. Needs to be installed however


echo "Creating new commit..."

# Add changes and create commit
git add .
git commit \
-m "[FIX] middleware/testRunner: Update resources from OpenUI5" \
-m "Based on:
$COMMIT_URL"