Skip to content

Commit

Permalink
feat: introduce --log argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeanjones committed May 17, 2024
1 parent 0e48b0f commit 4be3ca6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
16 changes: 16 additions & 0 deletions bin/lcov-result-merger.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,36 @@ const args = yargs(hideBin(process.argv)).command(
default: [],
description: 'Pass globs to ignore file paths',
},
log: {
type: 'boolean',
default: false,
description:
'Show some additional information to the console about what the process is getting up' +
'to (fed to stderr).',
},
});
}
).argv;

(async function () {
if (args.log) {
args.logger = function logger(...messages) {
process.stdout.write(`[lcov-result-merger] ${messages.join('\n')}\n`);
};
}

const files = await fastGlob(args.pattern, {
absolute: true,
ignore: args.ignore,
});

args.logger?.('Matched Files', JSON.stringify(files, null, 2));

const mergeResults = await mergeCoverageReportFiles(files, args);

if (args.outFile) {
await writeFile(args.outFile, mergeResults, 'utf-8');
args.logger?.(`Results written to "${args.outFile}"`);
} else {
process.stdout.write(mergeResults + '\n');
}
Expand Down
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function processFile(sourceDir, data, lcov, config) {
let currentCoverageFile = null;

const lines = data.toString('utf-8').split(/\r?\n/);
config.logger?.(`Read ${lines.length} lines of content`);

for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
Expand All @@ -53,7 +54,15 @@ function processFile(sourceDir, data, lcov, config) {
fullFilePathName
);

config.logger?.(
`Re-writing source file path, Before: "${sourceFilePath}"`
);

sourceFilePath = './' + rootRelPathName;

config.logger?.(
`Re-writing source file path, After: "${sourceFilePath}"`
);
}

currentCoverageFile = lcov.addCoverageFile(sourceFilePath);
Expand All @@ -69,7 +78,10 @@ function processFile(sourceDir, data, lcov, config) {
break;

default:
// do nothing with not implemented prefixes
// do nothing with not implemented prefixes
config.logger?.(
`Ignoring unrecognized/unsupported entry (line #${i}): "${prefix}:${suffix}"`
);
}
}

Expand All @@ -90,6 +102,7 @@ async function mergeCoverageReportFiles(filePaths, options) {

for (const filePath of filePaths) {
const fileContent = await readFile(filePath, 'utf-8');
config.logger?.(`Processing "${filePath}"`);
processFile(path.dirname(filePath), fileContent, report, config);
}

Expand Down
13 changes: 13 additions & 0 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* @property {string} [prependPathFix]
* @property {string} [prepend-path-fix]
* @property {string[]} [ignore]
* @property {boolean} [log]
*
* @property {(...msg: string[]) => void} [logger]
*/

module.exports = class Configuration {
Expand All @@ -17,22 +20,32 @@ module.exports = class Configuration {
constructor(partial = {}) {
/**
* A glob pattern matching one or more lcov files to be merged.
*
* @type {string}
*/
this.pattern = partial.pattern;

/**
* A file to write the merged lcov to.
*
* @type {string|undefined}
*/
this.outFile = partial.outFile;

/**
* File path globs that will be ignored.
*
* @type {string[]}
*/
this.ignore = Array.isArray(partial.ignore) ? partial.ignore : [];

/**
* An optional logger function.
*
* @type {((...msg: string[]) => void) | undefined}
*/
this.logger = partial.logger;

/**
* Modify source file paths to be relative to the working directory that
* the merge operation was run in.
Expand Down

0 comments on commit 4be3ca6

Please sign in to comment.