runapptests - Unify log messages

pull/3399/head
Martin Boonk 2024-05-07 19:33:53 +02:00
parent 56f7dd192c
commit 4f258f800b
1 changed files with 38 additions and 50 deletions

View File

@ -45,67 +45,58 @@ var emu = require(BASE_DIR+"/core/lib/emulator.js");
// Last set of text received // Last set of text received
var lastTxt; var lastTxt;
function getSanitizedLastLine(){
return emu.getLastLine().replaceAll("\r", "");
}
function ERROR(s) { function ERROR(s) {
console.error(s); console.error(s);
process.exit(1); process.exit(1);
} }
function getValue(js){ function getValue(js){
console.log(`> Getting value for "${js}"`); if (verbose)
console.log(`> GETTING VALUE FOR \`${js}\``);
emu.tx(`\x10print(JSON.stringify(${js}))\n`); emu.tx(`\x10print(JSON.stringify(${js}))\n`);
var result = emu.getLastLine(); var result = getSanitizedLastLine();
console.log(`> GOT "${result}"`);
if (verbose)
console.log(` GOT \`${result}\``);
return JSON.parse(result); return JSON.parse(result);
} }
function assertFail(text){
console.log("> FAIL: " + text);
return false;
}
function assertCondition(condition) {
if (!condition) {
return false;
}
return true;
}
function assertArray(step){ function assertArray(step){
let isOK; let isOK;
let array = step.value;
if (step.value === undefined)
array = getValue(step.js);
switch (step.is.toLowerCase()){ switch (step.is.toLowerCase()){
case "notempty": isOK = assertCondition(array && array.length > 0, step.text); break; case "notempty": isOK = getValue(`${step.js} && ${step.js}.length > 0`); break;
case "undefinedorempty": isOK = assertCondition(array && array.length == 0 || !array, step.text); break; case "undefinedorempty": isOK = getValue(`!${step.js} || (${step.js} && ${step.js}.length === 0)`); break;
} }
if (isOK) if (isOK)
console.log("OK - ASSERT ARRAY " + step.is.toUpperCase(), step.text); console.log("> OK - ASSERT ARRAY " + step.is.toUpperCase(), step.text ? "- " + step.text : "");
else else
console.log("FAIL - ASSERT ARRAY " + step.is.toUpperCase(), step.text); console.log("> FAIL - ASSERT ARRAY " + step.is.toUpperCase(), step.text ? "- " + step.text : "");
return isOK; return isOK;
} }
function assertValue(step){ function assertValue(step){
console.debug("assertValue", step);
let isOK; let isOK;
let value = step.js; let value = step.js;
if (value === undefined) if (value === undefined)
value = step.value; value = step.value;
switch (step.is.toLowerCase()){ switch (step.is.toLowerCase()){
case "truthy": isOK = assertCondition(getValue(`!!${value}`), step.text); break; case "truthy": isOK = getValue(`!!${value}`); break;
case "falsy": isOK = assertCondition(getValue(`!${value}`), step.text); break; case "falsy": isOK = getValue(`!${value}`); break;
case "true": isOK = assertCondition(getValue(`${value} === true`), step.text); break; case "true": isOK = getValue(`${value} === true`); break;
case "false": isOK = assertCondition(getValue(`${value} === false`), step.text); break; case "false": isOK = getValue(`${value} === false`); break;
case "equal": isOK = assertCondition(getValue(`${value} == ${step.to}`), step.text); break; case "equal": isOK = getValue(`${value} == ${step.to}`); break;
case "function": isOK = assertCondition(getValue(`typeof ${value} === "function"`), step.text); break; case "function": isOK = getValue(`typeof ${value} === "function"`); break;
} }
if (isOK) if (isOK)
console.log("OK - ASSERT " + step.is.toUpperCase(), step.text); console.log("> OK - ASSERT " + step.is.toUpperCase(), step.text ? "- " + step.text : "");
else else
console.log("FAIL - ASSERT " + step.is.toUpperCase(), step.text); console.log("> FAIL - ASSERT " + step.is.toUpperCase(), step.text ? "- " + step.text : "");
return isOK; return isOK;
} }
@ -157,9 +148,9 @@ function assertCall(step){
} }
} }
if (isOK) if (isOK)
console.log("OK", step.text); console.log("OK - ASSERT CALL", step.text ? "- " + step.text : "");
else else
console.log("FAIL", step.text); console.log("FAIL - ASSERT CALL", step.text ? "- " + step.text : "");
return isOK; return isOK;
} }
@ -177,13 +168,13 @@ function runStep(step, subtest, test, state){
break; break;
case "load" : case "load" :
p = p.then(() => { p = p.then(() => {
console.log(`> Loading file "${step.fn}"`); console.log(`> LOADING FILE "${step.fn}"`);
emu.tx(`load(${JSON.stringify(step.fn)})\n`); emu.tx(`load(${JSON.stringify(step.fn)})\n`);
}); });
break; break;
case "cmd" : case "cmd" :
p = p.then(() => { p = p.then(() => {
console.log(`> Sending JS "${step.js}"`); console.log(`> SENDING JS \`${step.js}\``, step.text ? "- " + step.text : "");
emu.tx(`${step.js}\n`); emu.tx(`${step.js}\n`);
}); });
break; break;
@ -210,7 +201,7 @@ function runStep(step, subtest, test, state){
let parent = step.parent ? step.parent : "Bangle"; let parent = step.parent ? step.parent : "Bangle";
if (!step.paramsArray) step.paramsArray = []; if (!step.paramsArray) step.paramsArray = [];
let args = JSON.stringify([step.event].concat(step.paramsArray)); let args = JSON.stringify([step.event].concat(step.paramsArray));
console.log(`> Emit "${step.event}" on ${parent} with parameters ${JSON.stringify(step.paramsArray)}`); console.log(`> EMIT "${step.event}" on ${parent} with parameters ${JSON.stringify(step.paramsArray, null, null)}`);
emu.tx(`${parent}.emit.apply(${parent}, ${args})\n`); emu.tx(`${parent}.emit.apply(${parent}, ${args})\n`);
}); });
@ -219,9 +210,9 @@ function runStep(step, subtest, test, state){
p = p.then(() => { p = p.then(() => {
console.log(`> Evaluate "${step.js}"`); console.log(`> Evaluate "${step.js}"`);
emu.tx(`\x10print(JSON.stringify(${step.js}))\n`); emu.tx(`\x10print(JSON.stringify(${step.js}))\n`);
var result = emu.getLastLine(); var result = getSanitizedLastLine();
var expected = JSON.stringify(step.eq); var expected = JSON.stringify(step.eq);
console.log("> GOT "+result); console.log("> GOT `"+result+"`");
if (result!=expected) { if (result!=expected) {
console.log("> FAIL: EXPECTED "+expected); console.log("> FAIL: EXPECTED "+expected);
state.ok = false; state.ok = false;
@ -253,14 +244,14 @@ function runStep(step, subtest, test, state){
case "saveMemoryUsage" : case "saveMemoryUsage" :
p = p.then(() => { p = p.then(() => {
emu.tx(`\x10print(process.memory().usage)\n`); emu.tx(`\x10print(process.memory().usage)\n`);
subtest.memUsage = parseInt( emu.getLastLine()); subtest.memUsage = parseInt( getSanitizedLastLine());
console.log("> CURRENT MEMORY USAGE", subtest.memUsage); console.log("> CURRENT MEMORY USAGE", subtest.memUsage);
}); });
break; break;
case "checkMemoryUsage" : case "checkMemoryUsage" :
p = p.then(() => { p = p.then(() => {
emu.tx(`\x10print(process.memory().usage)\n`); emu.tx(`\x10print(process.memory().usage)\n`);
var memUsage = emu.getLastLine(); var memUsage = getSanitizedLastLine();
console.log("> CURRENT MEMORY USAGE", memUsage); console.log("> CURRENT MEMORY USAGE", memUsage);
if (subtest.memUsage != memUsage ) { if (subtest.memUsage != memUsage ) {
console.log("> FAIL: EXPECTED MEMORY USAGE OF "+subtest.memUsage); console.log("> FAIL: EXPECTED MEMORY USAGE OF "+subtest.memUsage);
@ -272,7 +263,7 @@ function runStep(step, subtest, test, state){
p = p.then(()=>{ p = p.then(()=>{
return new Promise(resolve => { return new Promise(resolve => {
setTimeout(()=>{ setTimeout(()=>{
console.log("Waited for", step.ms); console.log("> WAITED FOR", step.ms);
resolve(); resolve();
}, step.ms); }, step.ms);
}) })
@ -280,7 +271,7 @@ function runStep(step, subtest, test, state){
break; break;
case "upload" : case "upload" :
p = p.then(()=>{ p = p.then(()=>{
console.log("Uploading", step.file); console.log("> UPLOADING" + (step.load ? " AND LOADING" : ""), step.file);
emu.tx(AppInfo.getFileUploadCommands(step.as, require("fs").readFileSync(BASE_DIR + "/" + step.file).toString())); emu.tx(AppInfo.getFileUploadCommands(step.as, require("fs").readFileSync(BASE_DIR + "/" + step.file).toString()));
}); });
break; break;
@ -299,9 +290,6 @@ function runTest(test, testState) {
if (app.custom) ERROR(`App ${JSON.stringify(appId)} requires HTML customisation`); if (app.custom) ERROR(`App ${JSON.stringify(appId)} requires HTML customisation`);
return apploader.getAppFilesString(app).then(command => { return apploader.getAppFilesString(app).then(command => {
console.log("Handling command", command);
// What about dependencies??
let p = Promise.resolve(); let p = Promise.resolve();
test.tests.forEach((subtest,subtestIdx) => { test.tests.forEach((subtest,subtestIdx) => {
let state = { ok: true}; let state = { ok: true};
@ -312,25 +300,25 @@ function runTest(test, testState) {
console.log(`"${test.description}`); console.log(`"${test.description}`);
console.log(`==============================`); console.log(`==============================`);
emu.factoryReset(); emu.factoryReset();
console.log("> Sending app "+test.app); console.log("> SENDING APP "+test.app);
emu.tx(command); emu.tx(command);
console.log("> Sent app"); console.log("> SENT APP");
emu.tx("reset()\n"); emu.tx("reset()\n");
console.log("> Reset"); console.log("> RESET");
}); });
subtest.steps.forEach(step => { subtest.steps.forEach(step => {
p = p.then(()=>{ p = p.then(()=>{
return runStep(step, subtest, test, state).catch((e)=>{ return runStep(step, subtest, test, state).catch((e)=>{
console.log("STEP FAILED:", e, step); console.log("> STEP FAILED:", e, step);
state.ok = false; state.ok = false;
}) })
}); });
}); });
p = p.finally(()=>{ p = p.finally(()=>{
console.log("RESULT for", test.app + (subtest.description ? (": " + subtest.description) : ""), "test", subtestIdx, (state.ok ? "OK": "FAIL")); console.log("> RESULT -", (state.ok ? "OK": "FAIL") , "- " + test.app + (subtest.description ? (" - " + subtest.description) : ""));
testState.push({ testState.push({
app: test.app, app: test.app,
number: subtestIdx, number: subtestIdx,