Skip to content

Commit 93b17a7

Browse files
author
Tim Layton
committed
add tests for readme version updating
1 parent 53920ba commit 93b17a7

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

scripts/readme-update-version.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
import {execSync} from 'child_process';
2-
import {writeFileSync, readFileSync} from 'fs-extra';
3-
import {resolve} from 'path';
4-
1+
const {execSync} = require('child_process');
2+
const {writeFileSync, readFileSync} = require('fs-extra');
3+
const {resolve} = require('path');
54
const {version: newVersion} = require('../package.json');
5+
const {semverRegExp, readmes} = require('./utilities');
66

77
const root = resolve(__dirname, '..');
8-
const regExSemVer = new RegExp(
9-
/(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/,
10-
'g',
11-
);
128
const readmes = Object.freeze(['README.md', 'src/components/README.md']);
139

1410
// eslint-disable-next-line no-console
1511
console.log(`🆕 Updating version in ${readmes.join(', ')}...`);
1612
readmes.map((readme) => resolve(root, readme)).forEach((file) => {
1713
writeFileSync(
1814
file,
19-
readFileSync(file, 'utf8').replace(regExSemVer, newVersion),
15+
readFileSync(file, 'utf8').replace(semverRegExp, newVersion),
2016
);
2117
});
2218

scripts/utilities/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const {readmes} = require('./readmes');
2+
const {semverRegExp} = require('./semver-reg-exp');
3+
4+
module.exports.readmes = readmes;
5+
module.exports.semverRegExp = semverRegExp;

scripts/utilities/readmes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const readmes = Object.freeze(['README.md', 'src/components/README.md']);
2+
module.exports.readmes = readmes;

scripts/utilities/semver-reg-exp.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const semverRegExp = new RegExp(
2+
/(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/,
3+
'g',
4+
);
5+
module.exports.semverRegExp = semverRegExp;

tests/readme-update-version.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const fs = require('fs-extra');
2+
const {semverRegExp, readmes} = require('../scripts/utilities');
3+
4+
describe('readme-update-version', () => {
5+
it('matches 5 semver numbers in READMEs', () => {
6+
let occurances = 0;
7+
readmes.forEach((readmePath) => {
8+
const readme = fs.readFileSync(readmePath, 'utf8');
9+
occurances += (readme.match(semverRegExp) || []).length;
10+
});
11+
12+
expect(occurances).toBe(5);
13+
});
14+
15+
it('semverRegExp matches a variety of valid semver numbers', () => {
16+
const testCase =
17+
'Both version 2.5.0 and 99.99.99-beta.1 are valid, foo.bar.qux is not';
18+
const occurances = (testCase.match(semverRegExp) || []).length;
19+
20+
expect(occurances).toBe(2);
21+
});
22+
});

0 commit comments

Comments
 (0)