Skip to content

Commit

Permalink
feat: 支持直接获取增量覆盖率和增量摘要
Browse files Browse the repository at this point in the history
  • Loading branch information
Jodeee committed Apr 24, 2023
1 parent 2681923 commit ad65bb9
Show file tree
Hide file tree
Showing 20 changed files with 3,325 additions and 230 deletions.
163 changes: 0 additions & 163 deletions .eslintrc

This file was deleted.

33 changes: 33 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

module.exports = {
root: true,
extends: 'eslint-config-egg',
rules: {
'valid-jsdoc': 0,
'no-script-url': 0,
'no-multi-spaces': 0,
'default-case': 0,
'no-case-declarations': 0,
'one-var-declaration-per-line': 0,
'no-restricted-syntax': 0,
'jsdoc/require-param': 0,
'jsdoc/check-param-names': 0,
'jsdoc/require-param-description': 0,
'jsdoc/require-returns-description': 0,
'arrow-parens': 0,
'prefer-promise-reject-errors': 0,
'no-control-regex': 0,
'no-use-before-define': 0,
'array-callback-return': 0,
'no-bitwise': 0,
'no-self-compare': 0,
'@typescript-eslint/no-var-requires': 0,
'@typescript-eslint/ban-ts-ignore': 0,
'@typescript-eslint/no-use-before-define': 0,
'@typescript-eslint/no-this-alias': 0,
'one-var': 0,
'no-sparse-arrays': 0,
'no-useless-concat': 0,
},
};
120 changes: 90 additions & 30 deletions bin/macaca-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,104 @@ const path = require('path');
const EOL = require('os').EOL;
const program = require('commander');
const Coverage = require('..');
const getDiff = require('../lib/core/diff');
const summaryTemplate = require('../lib/template/summary-template');
const { incrementalReporter } = require('../lib/core/incremental-coverage');

const pkg = require('../package.json');
const cwd = process.cwd();

program
.option('-f, --file <s>', 'coverage file')
.option('-v, --versions', 'output version infomation')
.usage('');
.usage('')
.action(options => {
if (options.versions) {
console.info(`${EOL} ${pkg.version} ${EOL}`);
process.exit(0);
}

program.parse(process.argv);
if (options.file) {
const {
collector,
Reporter,
} = Coverage({
runtime: 'web',
});
const coverageFile = path.resolve(program.file);
const __coverage__ = JSON.parse(fs.readFileSync(coverageFile, 'utf8'));
collector.add(__coverage__);
const distDir = path.join(cwd, `coverage_${Date.now()}`);
const reporter = new Reporter(null, distDir);
reporter.addAll([
'html',
'lcov',
'json',
]);
reporter.write(collector, true, () => {
const coverageHtml = path.join(distDir, 'index.html');
console.log(coverageHtml);
});
} else {
program.help();
process.exit(0);
}
});

program
.command('diff')
.description('Get diff')
.option('-c, --currentBranch [String]', 'current branch.', null)
.option('-t, --targetBranch [String]', 'target branch.', 'master')
.option('-o, --output [String]', 'diff save path.', 'coverage-diff.json')
.action(async (option) => {

if (program.versions) {
console.info(`${EOL} ${pkg.version} ${EOL}`);
process.exit(0);
}

if (program.file) {
const {
collector,
Reporter
} = Coverage({
runtime: 'web'
const output = option.output;
const currentBranch = option.currentBranch;
const targetBranch = option.targetBranch;

const diffMap = await getDiff(cwd, currentBranch, targetBranch);
// 将 diff 写入本地
const newDiffMap = {};
for (const file in diffMap) {
const filePath = path.join(cwd, file);
newDiffMap[filePath] = diffMap[file];
}

console.info(`diff path: ${output}`);

fs.writeFileSync(output, JSON.stringify(newDiffMap, null, 2));
});
const coverageFile = path.resolve(program.file);
const __coverage__ = JSON.parse(fs.readFileSync(coverageFile, 'utf8'));
collector.add(__coverage__);
const distDir = path.join(cwd, `coverage_${Date.now()}`);
const reporter = new Reporter(null, distDir);
reporter.addAll([
'html',
'lcov',
'json'
]);
reporter.write(collector, true, () => {
const coverageHtml = path.join(distDir, 'index.html');
console.log(coverageHtml);

program
.command('incremental-reporter')
.description('Get incremental reporter')
.option('-c, --coveragePath [String]', 'coverage path.', 'coverage/coverage-final.json')
.option('-d, --diffPath [String]', 'diff path.', 'coverage-diff.json')
.option('-o, --output [String]', 'reporter path.', 'coverage')
.action(async (option) => {

const output = path.join(cwd, option.output);
const summaryHtmlpath = path.join(output, 'summary.html');
const diffPath = path.join(cwd, option.diffPath);
const coveragePath = path.join(cwd, option.coveragePath);
console.info(`diff path: ${diffPath}`);
console.info(`coverage path: ${coveragePath}`);

const diffMap = require(diffPath);
const coverageMap = require(coveragePath);

const incrementalMap = incrementalReporter(coverageMap, diffMap, {
projectPath: cwd,
needCollectedIncludes: process.env.INCREMENTAL_NEED_COLLECTED_INCLUDES || [],
reporterPath: output,
});

console.info(incrementalMap);
console.info(`reporter: ${output}`);
console.info(`summary: ${summaryHtmlpath}`);
fs.writeFileSync(summaryHtmlpath, summaryTemplate(incrementalMap.reporterHtml));
});
} else {
program.help();
process.exit(0);
}

program.parse(process.argv);

4 changes: 2 additions & 2 deletions lib/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ _.exec = cmd => {
stdio: [
'pipe',
'pipe',
'ignore'
'ignore',
],
cwd: process.cwd()
cwd: process.cwd(),
}).toString().trim();
logger.debug(res);
return res;
Expand Down
2 changes: 1 addition & 1 deletion lib/common/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
const logger = require('xlogger');

module.exports = logger.Logger({
closeFile: true
closeFile: true,
});
22 changes: 22 additions & 0 deletions lib/core/diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const LCL = require('last-commit-log');

module.exports = async (dir, currentBranch, targetBranch) => {
try {
const lcl = new LCL(dir);
const { diff } = LCL;
return await lcl
.getLastCommit()
.then((commit) => {
const diffMap = diff({
targetBranch,
currentBranch: currentBranch || commit.gitBranch,
});
return diffMap || {};
});
} catch (error) {
throw error;
}
};

Loading

0 comments on commit ad65bb9

Please sign in to comment.