From 4f258f800b326accc96438760c8642eae406c717 Mon Sep 17 00:00:00 2001 From: Martin Boonk Date: Tue, 7 May 2024 19:33:53 +0200 Subject: [PATCH] runapptests - Unify log messages --- bin/runapptests.js | 88 ++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 50 deletions(-) diff --git a/bin/runapptests.js b/bin/runapptests.js index f83958a2b..1ead38d86 100755 --- a/bin/runapptests.js +++ b/bin/runapptests.js @@ -45,67 +45,58 @@ var emu = require(BASE_DIR+"/core/lib/emulator.js"); // Last set of text received var lastTxt; +function getSanitizedLastLine(){ + return emu.getLastLine().replaceAll("\r", ""); +} + function ERROR(s) { console.error(s); process.exit(1); } function getValue(js){ - console.log(`> Getting value for "${js}"`); + if (verbose) + console.log(`> GETTING VALUE FOR \`${js}\``); emu.tx(`\x10print(JSON.stringify(${js}))\n`); - var result = emu.getLastLine(); - console.log(`> GOT "${result}"`); + var result = getSanitizedLastLine(); + + if (verbose) + console.log(` GOT \`${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){ let isOK; - let array = step.value; - if (step.value === undefined) - array = getValue(step.js); switch (step.is.toLowerCase()){ - case "notempty": isOK = assertCondition(array && array.length > 0, step.text); break; - case "undefinedorempty": isOK = assertCondition(array && array.length == 0 || !array, step.text); break; + case "notempty": isOK = getValue(`${step.js} && ${step.js}.length > 0`); break; + case "undefinedorempty": isOK = getValue(`!${step.js} || (${step.js} && ${step.js}.length === 0)`); break; } 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 - console.log("FAIL - ASSERT ARRAY " + step.is.toUpperCase(), step.text); + console.log("> FAIL - ASSERT ARRAY " + step.is.toUpperCase(), step.text ? "- " + step.text : ""); return isOK; } function assertValue(step){ - console.debug("assertValue", step); let isOK; let value = step.js; if (value === undefined) value = step.value; switch (step.is.toLowerCase()){ - case "truthy": isOK = assertCondition(getValue(`!!${value}`), step.text); break; - case "falsy": isOK = assertCondition(getValue(`!${value}`), step.text); break; - case "true": isOK = assertCondition(getValue(`${value} === true`), step.text); break; - case "false": isOK = assertCondition(getValue(`${value} === false`), step.text); break; - case "equal": isOK = assertCondition(getValue(`${value} == ${step.to}`), step.text); break; - case "function": isOK = assertCondition(getValue(`typeof ${value} === "function"`), step.text); break; + case "truthy": isOK = getValue(`!!${value}`); break; + case "falsy": isOK = getValue(`!${value}`); break; + case "true": isOK = getValue(`${value} === true`); break; + case "false": isOK = getValue(`${value} === false`); break; + case "equal": isOK = getValue(`${value} == ${step.to}`); break; + case "function": isOK = getValue(`typeof ${value} === "function"`); break; } if (isOK) - console.log("OK - ASSERT " + step.is.toUpperCase(), step.text); + console.log("> OK - ASSERT " + step.is.toUpperCase(), step.text ? "- " + step.text : ""); else - console.log("FAIL - ASSERT " + step.is.toUpperCase(), step.text); + console.log("> FAIL - ASSERT " + step.is.toUpperCase(), step.text ? "- " + step.text : ""); return isOK; } @@ -157,9 +148,9 @@ function assertCall(step){ } } if (isOK) - console.log("OK", step.text); + console.log("OK - ASSERT CALL", step.text ? "- " + step.text : ""); else - console.log("FAIL", step.text); + console.log("FAIL - ASSERT CALL", step.text ? "- " + step.text : ""); return isOK; } @@ -177,13 +168,13 @@ function runStep(step, subtest, test, state){ break; case "load" : p = p.then(() => { - console.log(`> Loading file "${step.fn}"`); + console.log(`> LOADING FILE "${step.fn}"`); emu.tx(`load(${JSON.stringify(step.fn)})\n`); }); break; case "cmd" : p = p.then(() => { - console.log(`> Sending JS "${step.js}"`); + console.log(`> SENDING JS \`${step.js}\``, step.text ? "- " + step.text : ""); emu.tx(`${step.js}\n`); }); break; @@ -210,7 +201,7 @@ function runStep(step, subtest, test, state){ let parent = step.parent ? step.parent : "Bangle"; if (!step.paramsArray) 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`); }); @@ -219,9 +210,9 @@ function runStep(step, subtest, test, state){ p = p.then(() => { console.log(`> Evaluate "${step.js}"`); emu.tx(`\x10print(JSON.stringify(${step.js}))\n`); - var result = emu.getLastLine(); + var result = getSanitizedLastLine(); var expected = JSON.stringify(step.eq); - console.log("> GOT "+result); + console.log("> GOT `"+result+"`"); if (result!=expected) { console.log("> FAIL: EXPECTED "+expected); state.ok = false; @@ -253,14 +244,14 @@ function runStep(step, subtest, test, state){ case "saveMemoryUsage" : p = p.then(() => { emu.tx(`\x10print(process.memory().usage)\n`); - subtest.memUsage = parseInt( emu.getLastLine()); + subtest.memUsage = parseInt( getSanitizedLastLine()); console.log("> CURRENT MEMORY USAGE", subtest.memUsage); }); break; case "checkMemoryUsage" : p = p.then(() => { emu.tx(`\x10print(process.memory().usage)\n`); - var memUsage = emu.getLastLine(); + var memUsage = getSanitizedLastLine(); console.log("> CURRENT MEMORY USAGE", memUsage); if (subtest.memUsage != memUsage ) { console.log("> FAIL: EXPECTED MEMORY USAGE OF "+subtest.memUsage); @@ -272,7 +263,7 @@ function runStep(step, subtest, test, state){ p = p.then(()=>{ return new Promise(resolve => { setTimeout(()=>{ - console.log("Waited for", step.ms); + console.log("> WAITED FOR", step.ms); resolve(); }, step.ms); }) @@ -280,7 +271,7 @@ function runStep(step, subtest, test, state){ break; case "upload" : 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())); }); break; @@ -299,9 +290,6 @@ function runTest(test, testState) { if (app.custom) ERROR(`App ${JSON.stringify(appId)} requires HTML customisation`); return apploader.getAppFilesString(app).then(command => { - console.log("Handling command", command); - - // What about dependencies?? let p = Promise.resolve(); test.tests.forEach((subtest,subtestIdx) => { let state = { ok: true}; @@ -312,25 +300,25 @@ function runTest(test, testState) { console.log(`"${test.description}`); console.log(`==============================`); emu.factoryReset(); - console.log("> Sending app "+test.app); + console.log("> SENDING APP "+test.app); emu.tx(command); - console.log("> Sent app"); + console.log("> SENT APP"); emu.tx("reset()\n"); - console.log("> Reset"); + console.log("> RESET"); }); subtest.steps.forEach(step => { p = p.then(()=>{ return runStep(step, subtest, test, state).catch((e)=>{ - console.log("STEP FAILED:", e, step); + console.log("> STEP FAILED:", e, step); state.ok = false; }) }); }); 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({ app: test.app, number: subtestIdx,