Skip to content

Instantly share code, notes, and snippets.

@ipoddubny
Created June 1, 2016 14:12
Show Gist options
  • Save ipoddubny/9d8e84e51c4f4e4ac15fb9930cbc4bfe to your computer and use it in GitHub Desktop.
Save ipoddubny/9d8e84e51c4f4e4ac15fb9930cbc4bfe to your computer and use it in GitHub Desktop.
Asterisk code coverage (for patched Asterisk)
'use strict';
const fs = require('fs');
const _ = require('lodash');
if (process.argv.length !== 3) {
console.log(`Usage: node ${process.argv[1]} FILENAME`);
process.exit(1);
}
const file = fs.readFileSync(process.argv[2], 'utf-8');
// {
// 'context': {
// '_X.': {
// '1': ['App', 'args', 4], ...
// }, ...
// }, ...
// }
//
let dp = {};
function addApp (c, e, p, app, args, usage) {
if (!dp[c]) {
dp[c] = {};
}
if (!dp[c][e]) {
dp[c][e] = {};
}
dp[c][e][p] = [app, args, usage];
}
let context = '---';
let exten;
for (let line of file.split('\n')) {
// new extension starts
let matches = line.match(/'([^']+)'(?: \(CID match '[^']*'\))? =>\s+(\d+)\.\s(\w+)\((.*)\) (\d+)/);
if (matches) {
// extension matches[1] is not used
// priority matches[2] is not used
let prio, app, args, usage;
[, exten, prio, app, args, usage] = matches;
addApp(context, exten, prio, app, args, usage);
continue;
}
// next line describing extension
matches = line.match(/^\s+(?:\[\w+\])?\s+(\d+)\.\s(\w+)\((.*)\) (\d+)/);
if (matches) {
let [, prio, app, args, usage] = matches;
addApp(context, exten, prio, app, args, usage);
continue;
}
// start of a new context
matches = line.match(/^\[ Context '([^']+)' created by/);
if (matches) {
context = matches[1];
continue;
}
}
const colors = require('ansi-256-colors');
function getColor(usage) {
let fg = [0, 0, 0];
let bg;
if (usage == 0) {
bg = [3, 2, 2];
} else if (usage > 0 && usage < 10 ) {
bg = [1, 3, 1];
} else if (usage > 10) {
bg = [1, 5, 1];
}
return function (string) {
return colors.fg.getRgb.apply(colors.fg, fg) + colors.bg.getRgb.apply(colors.bg, bg) + string + colors.reset;
}
}
let covered = 0, uncovered = 0;
_.each(dp, (extens, context) => {
/* // skip [..._<c>] generated contexts
if (context.match(/\d+$/)) {
return;
}
*/
console.log(`[${context}]`);
_.each(extens, (prios, exten) => {
_.each(prios, ([app, data, usage], prio) => {
usage = +usage;
if (usage) {
covered++;
} else {
uncovered++;
}
console.log(getColor(usage)(`exten => ${exten},${prio},${app}(${data})`));
});
});
console.log('');
});
console.log(`Covered: ${covered}/${covered + uncovered}, ${Math.floor(100*covered / (covered + uncovered))}%`);
{
"name": "coverage",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"ansi-256-colors": "^1.1.0",
"lodash": "^4.13.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment