Skip to content

Commit

Permalink
feat: tweak options
Browse files Browse the repository at this point in the history
  • Loading branch information
Jodeee committed Apr 25, 2023
1 parent 5bf3443 commit 59a4a25
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ node_modules
npm-debug.log
coverage
.nyc_output
test/temp
test/fixture/**/index.html
*.gz
*.sw*
*.un~
15 changes: 8 additions & 7 deletions bin/macaca-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ program
currentBranch: getCurrentBranch({ cwd }),
targetBranch: 'master',
coverageJsonFile: path.resolve(cwd, 'coverage/coverage-final.json'),
output: cwd,
output: path.resolve(cwd, 'coverage'),
cwd,
};

Expand All @@ -79,23 +79,24 @@ program
options.coverageJsonFile = path.resolve(cwd, options.coverageJsonFile);
options.output = path.resolve(cwd, options.output);
mkdirp(options.output);
options.diffFileName = 'diff-data.json';
options.diffReporterFileName = 'diff-repoter.html';
logger.info('diff command options:\n%j', options);

// step2, generate git diff json
const diffData = getDiffData(options);
const diffDataFilePath = path.resolve(options.output, options.diffFileName);
const diffDataFilePath = path.resolve(options.output, 'diff-data.json');
logger.info('gen diff data: %s', diffDataFilePath);
fs.writeFileSync(diffDataFilePath, JSON.stringify(diffData, null, 2));

// step3, generate diff reporter
const coverageMap = require(options.coverageJsonFile);

const diffReporterFilePath = path.resolve(options.output, options.diffReporterFileName);
const incrementalMap = incrementalReporter(coverageMap, diffData, options);
const coverageReporterFilePath = path.resolve(options.output, 'index.html');
const reporterHtmlFilePath = path.resolve(options.output, 'diff-repoter.txt');
const diffReporterFilePath = path.resolve(options.output, 'diff-repoter.html');
logger.info('gen diff reporter: %s', coverageReporterFilePath);
logger.info('gen coverage reporter: %s', coverageReporterFilePath);

console.info(incrementalMap);
fs.writeFileSync(reporterHtmlFilePath, incrementalMap.reporterHtml);
fs.writeFileSync(diffReporterFilePath, summaryTemplate(incrementalMap.reporterHtml));
});

Expand Down
17 changes: 4 additions & 13 deletions lib/web/incremental-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ const incrementalCoverage = (coverageMap = {}, diffMap = [], options = {}) => {

const summary = {};
const coverage = {};
const { projectPath, needCollectedIncludes = [] } = options;
const { cwd, needCollectedIncludes = [] } = options;

for (const file in diffMap) {

if (!diffMap[file]?.length) continue;
if (!coverageMap[file]) {
try {
const relativePath = path.relative(projectPath, file);
const relativePath = path.relative(cwd, file);
const match = needCollectedIncludes.filter(reg => minimatch(relativePath, reg));
if (!match.length) continue;
} catch (error) {
Expand Down Expand Up @@ -149,11 +149,6 @@ const incrementalCoverage = (coverageMap = {}, diffMap = [], options = {}) => {
};

const incrementalReporter = (coverageMap = {}, diffMap = [], options = {}) => {
const {
projectPath,
reporterPath,
ossUrl,
} = options;

const incrementalMap = incrementalCoverage(coverageMap, diffMap, options);
const { coverage, summary } = incrementalMap;
Expand All @@ -162,7 +157,7 @@ const incrementalReporter = (coverageMap = {}, diffMap = [], options = {}) => {
try {
const collector = new Collector();
collector.add(coverage);
const reporter = new Reporter(null, reporterPath);
const reporter = new Reporter(null, options.output);
reporter.addAll([
'html',
'json',
Expand All @@ -175,11 +170,7 @@ const incrementalReporter = (coverageMap = {}, diffMap = [], options = {}) => {
}

// incremental coverage summary
const reporterHtml = reporterTemplate(summary, {
projectPath,
reporterPath,
ossUrl,
});
const reporterHtml = reporterTemplate(summary, options);
return {
coverage,
summary,
Expand Down
8 changes: 4 additions & 4 deletions lib/web/template/reporter-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const getFilePath = (dir, fileName) => {
};

const reporterTemplate = (summary, options) => {
const { projectPath, reporterPath, ossUrl } = options;
const { cwd, output, reporterUrl } = options;

const rowCoverList = [];
const rowNotCoverList = [];
Expand All @@ -26,8 +26,8 @@ const reporterTemplate = (summary, options) => {
if (!statements.total) continue;
const statementsPercent = `${(statements.cover / statements.total * 100).toFixed()}`;

const relativePath = path.relative(projectPath, name);
const fileCoveragePath = getFilePath(reporterPath, `${relativePath}.html`) || '';
const relativePath = path.relative(cwd, name);
const fileCoveragePath = getFilePath(output, `${relativePath}.html`) || '';

if (!fileCoveragePath) {
const rowStr = `
Expand All @@ -39,7 +39,7 @@ const reporterTemplate = (summary, options) => {
</tr>`;
rowNotCoverList.push(rowStr);
} else {
const reportUrl = `${ossUrl || '.'}/${fileCoveragePath.substr(reporterPath.length + 1)}`;
const reportUrl = `${reporterUrl || '.'}/${fileCoveragePath.substr(output.length + 1)}`;
const notCovNum = statements.total - statements.cover;
let backgroundColor = '#FCDDE1';
const percent = Number(statementsPercent) || 0;
Expand Down
18 changes: 9 additions & 9 deletions test/incremental-coverage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const path = require('path');
const assert = require('assert');
const { incrementalReporter } = require('../lib/core/incremental-coverage');
const { incrementalReporter } = require('../lib/web/incremental-coverage');
const cwd = process.cwd();

describe('./test/incremental-coverage.test.js', () => {
Expand All @@ -29,8 +29,8 @@ describe('./test/incremental-coverage.test.js', () => {
})
it('incremental coverage should work', () => {
const incrementalMap = incrementalReporter(newCoverageMap, newDiffMap, {
projectPath: cwd,
reporterPath: path.join(cwd, 'test/fixture/report'),
cwd,
output: path.join(cwd, 'test/fixture/report'),
});
assert.equal(incrementalMap.coverage[test1].s[1], 1);
assert.equal(incrementalMap.coverage[test2], undefined);
Expand All @@ -40,8 +40,8 @@ describe('./test/incremental-coverage.test.js', () => {
it('incremental summary should work', () => {

let incrementalMap = incrementalReporter(newCoverageMap, newDiffMap, {
projectPath: cwd,
reporterPath: path.join(cwd, 'test/fixture/report'),
cwd,
output: path.join(cwd, 'test/fixture/report'),
});
const summary1 = incrementalMap.summary[test1];
assert.equal(summary1.statements.cover, 11);
Expand All @@ -56,9 +56,9 @@ describe('./test/incremental-coverage.test.js', () => {

// needCollectedIncludes config
incrementalMap = incrementalReporter(newCoverageMap, newDiffMap, {
projectPath: process.cwd(),
cwd,
needCollectedIncludes: ['**/test2.js'],
reporterPath: path.join(process.cwd(), 'test/fixture/report'),
output: path.join(cwd, 'test/fixture/report'),
});

const summary3 = incrementalMap.summary[test2];
Expand All @@ -73,9 +73,9 @@ describe('./test/incremental-coverage.test.js', () => {

it('incremental reporter should work', () => {
const incrementalMap = incrementalReporter(newCoverageMap, newDiffMap, {
projectPath: process.cwd(),
cwd,
needCollectedIncludes: ['**/test2.js', '**/test4.js'],
reporterPath: path.join(process.cwd(), 'test/fixture/report'),
output: path.join(cwd, 'test/fixture/report'),
});

const reg = /<td class="coverage-percent".*>\d+%<\/td>/g;
Expand Down

0 comments on commit 59a4a25

Please sign in to comment.