From 57de3d6165fbdde4650e698089123d72376fd93d Mon Sep 17 00:00:00 2001 From: Trozza Date: Fri, 8 Oct 2021 19:02:59 +1100 Subject: [PATCH 1/4] feat: add ci argument to fail if a badge changes --- src/editor.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/editor.ts b/src/editor.ts index d0a9180..f11014c 100644 --- a/src/editor.ts +++ b/src/editor.ts @@ -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); From cd5ab968d67502c1d250ab1ca145e55257ede7c1 Mon Sep 17 00:00:00 2001 From: Trozza Date: Fri, 8 Oct 2021 19:28:46 +1100 Subject: [PATCH 2/4] docs: update readme with ci usage --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index b0da0c6..de52d19 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 and just adds `--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) From 4bdb2b5955a8163618572fc61cf8d9af3b93c820 Mon Sep 17 00:00:00 2001 From: Troy Poulter Date: Fri, 8 Oct 2021 23:27:39 +1100 Subject: [PATCH 3/4] Update README.md Co-authored-by: Olavo Parno --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index de52d19..8380389 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,7 @@ You can add this to your **package.json** as follows: } ``` -Where the script `make-badges:ci` will run your existing `make-badges` script and just adds `--ci` as an argument. +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. From d6bcf4bc74ad1e54e02829fa433d0ae36e507cfd Mon Sep 17 00:00:00 2001 From: Trozza Date: Sat, 9 Oct 2021 08:24:48 +1100 Subject: [PATCH 4/4] test: add tests for --ci argument --- tests/editor.spec.ts | 42 ++++++++++++++++++++++++----- tests/mocks/accurateCoverage.json | 8 ++++++ tests/mocks/accurateReadme.md | 3 +++ tests/mocks/inaccurateCoverage.json | 8 ++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 tests/mocks/accurateCoverage.json create mode 100644 tests/mocks/accurateReadme.md create mode 100644 tests/mocks/inaccurateCoverage.json diff --git a/tests/editor.spec.ts b/tests/editor.spec.ts index e980f93..1f7d581 100644 --- a/tests/editor.spec.ts +++ b/tests/editor.spec.ts @@ -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', () => { @@ -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", + ); + }); }); diff --git a/tests/mocks/accurateCoverage.json b/tests/mocks/accurateCoverage.json new file mode 100644 index 0000000..0f1fbd0 --- /dev/null +++ b/tests/mocks/accurateCoverage.json @@ -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 } + } +} diff --git a/tests/mocks/accurateReadme.md b/tests/mocks/accurateReadme.md new file mode 100644 index 0000000..d244c7d --- /dev/null +++ b/tests/mocks/accurateReadme.md @@ -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) | diff --git a/tests/mocks/inaccurateCoverage.json b/tests/mocks/inaccurateCoverage.json new file mode 100644 index 0000000..8a99e4a --- /dev/null +++ b/tests/mocks/inaccurateCoverage.json @@ -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 } + } +}