Skip to content

Commit

Permalink
feat: coverage badge.
Browse files Browse the repository at this point in the history
  • Loading branch information
h13i32maru committed Jul 26, 2015
1 parent d4febe5 commit a140b9c
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 4 deletions.
18 changes: 17 additions & 1 deletion src/Publisher/Builder/CoverageBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export default class CoverageBuilder extends DocBuilder {
/**
* execute building output.
* @param {function(coverage: CoverageObject, filePath: string)} callback - is called with coverage.
* @param {function(badge: string, filePath: string)} badgeCallback - is called with coverage badge.
*/
exec(callback) {
exec(callback, badgeCallback) {
let docs = this._find({kind: ['class', 'method', 'member', 'get', 'set', 'constructor', 'function', 'variable']});
let expectCount = docs.length;
let actualCount = 0;
Expand All @@ -34,5 +35,20 @@ export default class CoverageBuilder extends DocBuilder {
};

callback(coverage, 'coverage.json');

// create badge
let ratio = Math.floor(100 * actualCount / expectCount);
let color;
if (ratio < 50) {
color = '#db654f';
} else if (coverage < 90) {
color = '#dab226';
} else {
color = '#4fc921';
}
let badge = this._readTemplate('image/badge.svg');
badge = badge.replace(/@ratio@/g, ratio + '%');
badge = badge.replace(/@color@/g, color);
badgeCallback(badge, 'badge.svg');
}
}
1 change: 1 addition & 0 deletions src/Publisher/Builder/DocBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ export default class DocBuilder {
* @param {CoverageObject} coverageObj - target coverage object.
* @returns {string} html of coverage badge.
* @private
* @deprecated
*/
_buildCoverageHTML(coverageObj) {
let coverage = Math.floor(100 * coverageObj.actualCount / coverageObj.expectCount);
Expand Down
1 change: 1 addition & 0 deletions src/Publisher/Builder/IndexDocBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class IndexDocBuilder extends DocBuilder {
}

let result = ice.html;
// fixme: purge this code when esdoc hosting is shipped.
if (this._config.coverage) {
let $ = cheerio.load(result);
$('.esdoc-coverage').html(this._buildCoverageHTML(this._coverage));
Expand Down
2 changes: 1 addition & 1 deletion src/Publisher/Builder/SourceDocBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class SourceDocBuilder extends DocBuilder {
let coverage;
if (useCoverage) coverage = this._coverage.files;

if (useCoverage) ice.load('coverageBadge', this._buildCoverageHTML(this._coverage));
ice.drop('coverageBadge', !useCoverage);
ice.attr('files', 'data-use-coverage', !!useCoverage);

if (useCoverage) {
Expand Down
2 changes: 2 additions & 0 deletions src/Publisher/Builder/template/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ table.files-summary .coverage-count {
}

.total-coverage-count {
position: relative;
bottom: 2px;
font-size: 12px;
color: #666;
font-weight: 500;
Expand Down
17 changes: 17 additions & 0 deletions src/Publisher/Builder/template/image/badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/Publisher/Builder/template/source.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1>Source <span class="esdoc-coverage" data-ice="coverageBadge"></span><span data-ice="totalCoverageCount" class="total-coverage-count"></span></h1>
<h1>Source <img data-ice="coverageBadge" src="./badge.svg"><span data-ice="totalCoverageCount" class="total-coverage-count"></span></h1>

<table class="files-summary" data-ice="files">
<thead>
Expand Down
8 changes: 7 additions & 1 deletion src/Publisher/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export default function publish(values, asts, config) {
fs.outputFileSync(filePath, json, {encoding: 'utf8'});
}

function writeBadge(badge, fileName) {
log(fileName);
let filePath = path.resolve(config.destination, fileName);
fs.outputFileSync(filePath, badge, {encoding: 'utf8'});
}

function writeAST(astJSON, fileName) {
let filePath = path.resolve(config.destination, fileName);
fs.outputFileSync(filePath, astJSON, {encoding: 'utf8'});
Expand All @@ -58,7 +64,7 @@ export default function publish(values, asts, config) {
}

if (config.coverage) {
new CoverageBuilder(data, config).exec(writeCoverage);
new CoverageBuilder(data, config).exec(writeCoverage, writeBadge);
}

new IdentifiersDocBuilder(data, config).exec(writeHTML);
Expand Down
9 changes: 9 additions & 0 deletions test/src/BuilderTest/CoverageDocTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ describe('Coverage:', ()=> {
}
});
});

/** @test {CoverageBuilder#exec} */
it('creates coverage badge', ()=>{
let json = fs.readFileSync('./test/fixture/esdoc/coverage.json', {encoding: 'utf8'}).toString();
let coverage = JSON.parse(json);
let badge = fs.readFileSync('./test/fixture/esdoc/badge.svg', {encoding: 'utf8'}).toString();
let ratio = Math.floor(100 * coverage.actualCount / coverage.expectCount) + '%';
assert(badge.includes(ratio));
});
});

0 comments on commit a140b9c

Please sign in to comment.