Skip to content

Commit

Permalink
Merge pull request #48 from troypoulter/add-ci-argument
Browse files Browse the repository at this point in the history
Add ci argument to fail on badge changes
  • Loading branch information
olavoparno authored Oct 8, 2021
2 parents fb2ac04 + d25789f commit af36871
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 8 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [Simple Usage](#simple-usage)
- [Advanced Usage](#advanced-usage)
- [Usage as a part of your githooks](#usage-as-a-part-of-your-githooks)
- [Usage as a part of your CI](#usage-as-a-part-of-your-ci)
- [See more examples](#see-more-examples)
- [Contributors](#contributors)
- [License](#license)
Expand Down Expand Up @@ -137,6 +138,25 @@

---

## Usage as a part of your CI

You may want to have peace of mind that contributors have run `istanbul-badges-readme` locally by performing a simple check in your CI.

The `--ci` argument will throw an error if the badges generated do not match what is already in the `README.md`.

You can add this to your **package.json** as follows:

```json
"scripts": {
"make-badges": "istanbul-badges-readme",
"make-badges:ci": "npm run make-badges -- --ci",
}
```

Where the script `make-badges:ci` will run your existing `make-badges` script by just adding `--ci` as an argument.

This is a useful addition/alternative to the githooks approach for some use cases such as larger codebases, slow computers etc, where it isn't always feasible to run all the tests and produce coverage on each commit.

## See more examples

[Examples folder](./examples/README.md)
Expand Down
9 changes: 8 additions & 1 deletion src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ export const getNewReadme =
const valueToChangeIndex = valueToChangeStart.indexOf(')');
const valueToChangeFinal = valueToChangeStart.substring(1, valueToChangeIndex);

newReadmeFile = newReadmeFile.replace(enpatterned(valueToChangeFinal), enpatterned(coverageBadge as string));
const oldBadge = enpatterned(valueToChangeFinal);
const newBadge = enpatterned(coverageBadge as string);

if (getArgumentValue('ci') && oldBadge !== newBadge) {
reject("The coverage badge has changed, which isn't allowed with the `ci` argument");
}

newReadmeFile = newReadmeFile.replace(oldBadge, newBadge);
});

resolve(newReadmeFile);
Expand Down
42 changes: 35 additions & 7 deletions tests/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ describe('Tests editor', () => {

it('should getNewReadme without coverageBadge', async () => {
const brokenJsonCoveragePath = path.join(__dirname, '../tests/mocks/brokenCoverage.json');

const brokenJsonCoverageFile = fs.readFileSync(brokenJsonCoveragePath, 'utf-8');

getNewReadme(
'fake readme data',
brokenJsonCoverageFile,
)([{ key: 'key', value: 'wronghash' }]).catch((error) => {
expect(error).toEqual('There has been an error getting new coverage badges');
});
return expect(
getNewReadme('fake readme data', brokenJsonCoverageFile)([{ key: 'key', value: 'wronghash' }]),
).rejects.toMatch('There has been an error getting new coverage badges');
});

it('should break writeNewReadme with failure', () => {
Expand All @@ -76,4 +72,36 @@ describe('Tests editor', () => {

expect(customBadgeLabel).toEqual('https://img.shields.io/badge/customBadLabel-95.45%25-brightgreen.svg');
});

it('should have no errors using --ci when readme matches the coverage summary', () => {
const accurateCoveragePath = path.join(__dirname, '../tests/mocks/accurateCoverage.json');
const accurateCoverageFile = fs.readFileSync(accurateCoveragePath, 'utf-8');

const accurateReadmePath = path.join(__dirname, '../tests/mocks/accurateReadme.md');
const accurateReadmeFile = fs.readFileSync(accurateReadmePath, 'utf-8');

const readmeHashes = getReadmeHashes(accurateReadmeFile);

process.argv.push('--ci');

return expect(getNewReadme(accurateReadmeFile, accurateCoverageFile)(readmeHashes)).resolves.toBe(
accurateReadmeFile,
);
});

it('should throw error using --ci, when readme does not match coverage summary', () => {
const inaccurateCoveragePath = path.join(__dirname, '../tests/mocks/inaccurateCoverage.json');
const inaccurateCoverageFile = fs.readFileSync(inaccurateCoveragePath, 'utf-8');

const accurateReadmePath = path.join(__dirname, '../tests/mocks/accurateReadme.md');
const accurateReadmeFile = fs.readFileSync(accurateReadmePath, 'utf-8');

const readmeHashes = getReadmeHashes(accurateReadmeFile);

process.argv.push('--ci');

return expect(getNewReadme(accurateReadmeFile, inaccurateCoverageFile)(readmeHashes)).rejects.toMatch(
"The coverage badge has changed, which isn't allowed with the `ci` argument",
);
});
});
8 changes: 8 additions & 0 deletions tests/mocks/accurateCoverage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"total": {
"lines": { "total": 155, "covered": 143, "skipped": 0, "pct": 92.26 },
"statements": { "total": 177, "covered": 162, "skipped": 0, "pct": 91.53 },
"functions": { "total": 48, "covered": 40, "skipped": 0, "pct": 83.33 },
"branches": { "total": 36, "covered": 30, "skipped": 0, "pct": 83.33 }
}
}
3 changes: 3 additions & 0 deletions tests/mocks/accurateReadme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| Statements | Branches | Functions | Lines |
| ------------------------------------------------------------------------------- | ---------------------------------------------------------------------- | ------------------------------------------------------------------------ | --------------------------------------------------------------------- |
| ![Statements](https://img.shields.io/badge/statements-91.53%25-brightgreen.svg) | ![Branches](https://img.shields.io/badge/branches-83.33%25-yellow.svg) | ![Functions](https://img.shields.io/badge/functions-83.33%25-yellow.svg) | ![Lines](https://img.shields.io/badge/lines-92.26%25-brightgreen.svg) |
8 changes: 8 additions & 0 deletions tests/mocks/inaccurateCoverage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"total": {
"lines": { "total": 155, "covered": 143, "skipped": 0, "pct": 84.26 },
"statements": { "total": 177, "covered": 162, "skipped": 0, "pct": 34.53 },
"functions": { "total": 48, "covered": 40, "skipped": 0, "pct": 10.33 },
"branches": { "total": 36, "covered": 30, "skipped": 0, "pct": 95.33 }
}
}

0 comments on commit af36871

Please sign in to comment.