coverbadge
Advanced tools
Comparing version 0.3.0 to 0.4.0
#!/usr/bin/env node | ||
const yargs = require('yargs'); | ||
const chalk = require('chalk'); | ||
const { coverbadge } = require('../'); | ||
@@ -7,2 +8,3 @@ | ||
const { circle } = require('../services/circle'); | ||
const { sendSlackWebhook } = require('../services/slack'); | ||
@@ -12,2 +14,3 @@ const argv = yargs | ||
.default('o', 'badge.svg', '(output file path)') | ||
.default('vcs', 'github') | ||
.nargs('o', 1) | ||
@@ -18,26 +21,35 @@ .alias('s', 'service') | ||
.alias('t', 'token') | ||
.default('vcs', 'github') | ||
.argv | ||
.alias('b', 'branch') | ||
.argv; | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
const displayCoverageInfo = (pastCoverage, coverage) => { | ||
const coverageText = chalk.underline.bold(`${coverage}%`); | ||
const distance = chalk.bold(`${coverage > pastCoverage ? '+' : ''}${(coverage - pastCoverage).toFixed(2)}%`); | ||
let lcov = ''; | ||
let emoji = coverage === 100 ? '💯' : '🔖'; | ||
let text = chalk.green(`Coverage remained the same at ${chalk.underline.bold(`${coverage}%`)}.`); | ||
process.stdin.on('data', function(chunk) { | ||
lcov += chunk; | ||
}); | ||
if (pastCoverage > coverage) { | ||
emoji = '⚠️'; | ||
text = chalk.red(`Coverage decreased (${distance}) to ${coverageText}.`); | ||
} else if (pastCoverage < coverage) { | ||
emoji = coverage === 100 ? '💯' : '🎉'; | ||
text = chalk.green(`Coverage increased (${distance}) to ${coverageText}.`); | ||
} | ||
process.stdin.on('end', () => { | ||
if (argv && argv.o) { | ||
return `${emoji} ${text}`; | ||
}; | ||
const cli = (lcov, options = {}) => { | ||
if (options.o) { | ||
let preBuild = Promise.resolve(); | ||
if (argv.s) { | ||
if (argv.s === 'circle') { | ||
if (options.s) { | ||
if (options.s === 'circle') { | ||
preBuild = circle({ | ||
username: argv.u, | ||
project: argv.p, | ||
token: argv.t || null, | ||
vcs: argv.vcs, | ||
outputPath: argv.o, | ||
username: options.u, | ||
project: options.p, | ||
token: options.t || null, | ||
vcs: options.vcs, | ||
outputPath: options.o, | ||
}); | ||
@@ -50,4 +62,33 @@ } | ||
.catch(() => Promise.resolve()) | ||
.then(() => coverbadge(lcov, argv.o)); | ||
.then(() => coverbadge(lcov, options.o)) | ||
.then(([lastCoverage, coverage]) => { | ||
if (typeof lastCoverage === 'number') { | ||
console.log(displayCoverageInfo(lastCoverage, coverage)); | ||
if (options.slack) { | ||
sendSlackWebhook(options.slack, lastCoverage, coverage, argv); | ||
} | ||
} | ||
console.log(chalk.yellow(`Badge has successfully saved to ${chalk.underline(options.o)}!`)); | ||
}); | ||
} | ||
}; | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
let data = ''; | ||
/* istanbul ignore next */ | ||
process.stdin.on('data', function(chunk) { | ||
data += chunk; | ||
}); | ||
/* istanbul ignore next */ | ||
process.stdin.on('end', () => cli(data, argv)); | ||
module.exports = { | ||
displayCoverageInfo, | ||
cli, | ||
}; |
23
index.js
@@ -42,3 +42,3 @@ #!/usr/bin/env node | ||
} | ||
} | ||
}; | ||
@@ -62,16 +62,2 @@ const getPastCoverage = (outputPath) => { | ||
return false; | ||
} | ||
const displayCoverageInfo = (pastCoverage, coverage) => { | ||
const coverageText = chalk.underline.bold(`${coverage}%`); | ||
const distance = chalk.bold(`${coverage > pastCoverage ? '+' : ''}${(coverage - pastCoverage).toFixed(2)}%`); | ||
if (pastCoverage > coverage) { | ||
return chalk.red(`⚠️ Coverage decreased (${distance}) to ${coverageText}.`); | ||
} else if (pastCoverage < coverage) { | ||
const emoji = coverage === 100 ? '💯' : '🎉'; | ||
return chalk.green(`${emoji} Coverage increased (${distance}) to ${coverageText}.`); | ||
} | ||
return chalk.green(`🔖 Coverage remained the same at ${chalk.underline.bold(`${coverage}%`)}.`); | ||
}; | ||
@@ -99,7 +85,3 @@ | ||
if (pastCoverage) { | ||
console.log(displayCoverageInfo(pastCoverage, coverage)); | ||
} | ||
console.log(chalk.yellow(`Badge has successfully saved to ${chalk.underline(outputPath)}!`)); | ||
return [pastCoverage, coverage]; | ||
}) | ||
@@ -112,4 +94,3 @@ ); | ||
getPastCoverage, | ||
displayCoverageInfo, | ||
coverbadge, | ||
}; |
{ | ||
"name": "coverbadge", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Create a coverage badge without any service provider.", | ||
@@ -18,2 +18,9 @@ "main": "index.js", | ||
}, | ||
"jest": { | ||
"collectCoverageFrom": [ | ||
"**/*.{js,jsx}", | ||
"!**/node_modules/**", | ||
"!**/coverage/**" | ||
] | ||
}, | ||
"dependencies": { | ||
@@ -20,0 +27,0 @@ "chalk": "^1.1.3", |
@@ -21,6 +21,8 @@ const path = require('path'); | ||
token = null, | ||
branch, | ||
}) => { | ||
const tokenQuery = token ? `circle-token=${token}&` : ''; | ||
const branchQuery = branch ? `/tree/${encodeURIComponent(branch)}` : ''; | ||
return api(`project/${vcs}/${username}/${project}?${tokenQuery}limit=1&filter=successful`) | ||
return api(`project/${vcs}/${username}/${project}${branchQuery}?${tokenQuery}limit=1&filter=running`) | ||
}; | ||
@@ -40,6 +42,6 @@ | ||
getLastBadgeURL = (artifacts, outputPath) => { | ||
const artifact = artifacts.find(art => art.path.indexOf(outputPath) >= 0); | ||
const getLastBadgeURL = (artifacts, outputPath) => { | ||
const artifact = artifacts.find(art => art.path.endsWith(path.basename(outputPath))); | ||
return artifact.url; | ||
return artifact && artifact.url; | ||
}; | ||
@@ -60,2 +62,3 @@ | ||
vcs = 'github', | ||
branch, | ||
outputPath, | ||
@@ -68,2 +71,3 @@ }) => { | ||
vcs, | ||
branch, | ||
}; | ||
@@ -82,3 +86,9 @@ | ||
module.exports = { | ||
HOST, | ||
api, | ||
getBuildSummary, | ||
getBuildArtifacts, | ||
getLastBadgeURL, | ||
downloadBadge, | ||
circle, | ||
}; |
@@ -131,26 +131,7 @@ import fs from 'fs'; | ||
describe('displayCoverageInfo', () => { | ||
it('should display correct info when decreased', () => { | ||
expect(displayCoverageInfo(66, 55)).toMatchSnapshot(); | ||
}); | ||
it('should display correct info when increased', () => { | ||
expect(displayCoverageInfo(55, 66)).toMatchSnapshot(); | ||
}); | ||
it('should display correct info when increased and hit 100%', () => { | ||
expect(displayCoverageInfo(55, 100)).toMatchSnapshot(); | ||
}); | ||
it('should display correct info when remained', () => { | ||
expect(displayCoverageInfo(55, 55)).toMatchSnapshot(); | ||
}); | ||
}); | ||
describe('coverbadge', () => { | ||
beforeAll(() => { | ||
console.log = jest.fn(); | ||
}); | ||
const outputPath = './some/output/path.svg'; | ||
afterEach(() => { | ||
fs.deleteFileSync(outputPath); | ||
jest.clearAllMocks(); | ||
@@ -160,4 +141,2 @@ parse.resetInfo(); | ||
const outputPath = './some/output/path.svg'; | ||
it('should call svg', async () => { | ||
@@ -199,8 +178,12 @@ parse.setInfo([{ | ||
it('should call console two times if there is past badge', async () => { | ||
it('should return lastCoverage and current coverage', async () => { | ||
parse.setInfo([{ | ||
lines: { found: 15, hit: 5 }, | ||
}]); | ||
fs.writeFileSync(outputPath, '<text>10%</text>'); | ||
await coverbadge(mockedLcovInfo, outputPath); | ||
const [lastCoverage, coverage] = await coverbadge(mockedLcovInfo, outputPath); | ||
expect(console.log).toHaveBeenCalledTimes(2); | ||
expect(lastCoverage).toBe(10); | ||
expect(coverage).toBe(33.33); | ||
@@ -210,6 +193,10 @@ fs.deleteFileSync(outputPath); | ||
it('should call console one time if there is no past badge', async () => { | ||
await coverbadge(mockedLcovInfo, outputPath); | ||
it('should return only current coverage if no last', async () => { | ||
parse.setInfo([{ | ||
lines: { found: 15, hit: 5 }, | ||
}]); | ||
const [lastCoverage, coverage] = await coverbadge(mockedLcovInfo, outputPath); | ||
expect(console.log).toHaveBeenCalledTimes(1); | ||
expect(lastCoverage).toBe(false); | ||
expect(coverage).toBe(33.33); | ||
}); | ||
@@ -216,0 +203,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
129223
22
864
5
28