Skip to content

Commit

Permalink
Improve version checks to avoid mistakes in the versioning (facebook#…
Browse files Browse the repository at this point in the history
…35296)

Summary:
Pull Request resolved: facebook#35296

This change adds some version checks and enforces that every version matches some specific format based on the build type we are trying to run.

## Changelog
[General][Changed] - Improve version checks

Differential Revision: D41161756

fbshipit-source-id: 10f5d22f0f1a9049397414c95f95fb8b6f5332a8
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Nov 10, 2022
1 parent 1453ef1 commit 7833857
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 51 deletions.
247 changes: 227 additions & 20 deletions scripts/__tests__/version-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
* @format
*/

const {parseVersion, isReleaseBranch} = require('../version-utils');
const {
parseVersion,
isReleaseBranch,
validateBuildType,
} = require('../version-utils');

let execResult = null;
jest.mock('shelljs', () => ({
Expand Down Expand Up @@ -38,37 +42,86 @@ describe('version-utils', () => {
});

describe('parseVersion', () => {
it('should throw error if invalid match', () => {
it('should throw error if buildType is undefined', () => {
function testInvalidVersion() {
parseVersion('<invalid version>');
parseVersion('v0.10.5');
}
expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot(
`"Unsupported build type: undefined"`,
);
});

it('should throw error if buildType is not `release`, `dry-run` or `nightly`', () => {
function testInvalidVersion() {
parseVersion('v0.10.5', 'invalid_build_type');
}
expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot(
`"Unsupported build type: invalid_build_type"`,
);
});
it('should throw error if invalid match with release', () => {
function testInvalidVersion() {
parseVersion('<invalid version>', 'release');
}
expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot(
`"You must pass a correctly formatted version; couldn't parse <invalid version>"`,
);
});
it('should throw error if invalid match with dry-run', () => {
function testInvalidVersion() {
parseVersion('<invalid version>', 'dry-run');
}
expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot(
`"You must pass a correctly formatted version; couldn't parse <invalid version>"`,
);
});
it('should throw error if invalid match with nightly', () => {
function testInvalidVersion() {
parseVersion('<invalid version>', 'nightly');
}
expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot(
`"You must pass a correctly formatted version; couldn't parse <invalid version>"`,
);
});

it('should parse pre-release version with .', () => {
const {version, major, minor, patch, prerelease} =
parseVersion('0.66.0-rc.4');
it('should parse pre-release version with release and `.`', () => {
const {version, major, minor, patch, prerelease} = parseVersion(
'0.66.0-rc.4',
'release',
);
expect(version).toBe('0.66.0-rc.4');
expect(major).toBe('0');
expect(minor).toBe('66');
expect(patch).toBe('0');
expect(prerelease).toBe('rc.4');
});

it('should parse pre-release version with -', () => {
const {version, major, minor, patch, prerelease} =
parseVersion('0.66.0-rc-4');
it('should parse pre-release version with release and `-`', () => {
const {version, major, minor, patch, prerelease} = parseVersion(
'0.66.0-rc-4',
'release',
);
expect(version).toBe('0.66.0-rc-4');
expect(major).toBe('0');
expect(minor).toBe('66');
expect(patch).toBe('0');
expect(prerelease).toBe('rc-4');
});

it('should reject pre-release version with random prerelease pattern', () => {
function testInvalidVersion() {
parseVersion('0.66.0-something_invalid', 'release');
}
expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot(
`"Version 0.66.0-something_invalid is not valid for Release"`,
);
});

it('should parse stable version', () => {
const {version, major, minor, patch, prerelease} = parseVersion('0.66.0');
const {version, major, minor, patch, prerelease} = parseVersion(
'0.66.0',
'release',
);
expect(version).toBe('0.66.0');
expect(major).toBe('0');
expect(minor).toBe('66');
Expand All @@ -77,42 +130,196 @@ describe('version-utils', () => {
});

it('should parse pre-release version from tag', () => {
const {version, major, minor, patch, prerelease} =
parseVersion('v0.66.1-rc.4');
const {version, major, minor, patch, prerelease} = parseVersion(
'v0.66.1-rc.4',
'release',
);
expect(version).toBe('0.66.1-rc.4');
expect(major).toBe('0');
expect(minor).toBe('66');
expect(patch).toBe('1');
expect(prerelease).toBe('rc.4');
});

it('should reject pre-release version from tag with random prerelease pattern', () => {
function testInvalidVersion() {
parseVersion('v0.66.0-something_invalid', 'release');
}
expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot(
`"Version 0.66.0-something_invalid is not valid for Release"`,
);
});

it('should parse stable version from tag', () => {
const {version, major, minor, patch, prerelease} =
parseVersion('v0.66.0');
const {version, major, minor, patch, prerelease} = parseVersion(
'v0.66.0',
'release',
);
expect(version).toBe('0.66.0');
expect(major).toBe('0');
expect(minor).toBe('66');
expect(patch).toBe('0');
expect(prerelease).toBeUndefined();
});

it('should parse nightly fake version', () => {
const {version, major, minor, patch, prerelease} = parseVersion('0.0.0');
expect(version).toBe('0.0.0');
it('should reject nightly with no prerelease', () => {
// this should fail
function testInvalidFunction() {
parseVersion('0.0.0', 'nightly');
}
expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot(
`"Version 0.0.0 is not valid for nightlies"`,
);
});

it('should reject nightly with prerelease but wrong version numbers', () => {
// this should fail
function testInvalidFunction() {
parseVersion('1.2.3-pre-release', 'nightly');
}
expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot(
`"Version 1.2.3-pre-release is not valid for nightlies"`,
);
});

it('should parse nightly with 0.0.0 and a prerelease part', () => {
// this should fail
const {version, major, minor, patch, prerelease} = parseVersion(
'0.0.0-pre-release',
'nightly',
);

expect(version).toBe('0.0.0-pre-release');
expect(major).toBe('0');
expect(minor).toBe('0');
expect(patch).toBe('0');
expect(prerelease).toBe('pre-release');
});
it('should parse dryrun with release version', () => {
const {version, major, minor, patch, prerelease} = parseVersion(
'0.7.3',
'dry-run',
);
expect(version).toBe('0.7.3');
expect(major).toBe('0');
expect(minor).toBe('7');
expect(patch).toBe('3');
expect(prerelease).toBeUndefined();
});

it('should parse dryrun fake version', () => {
const {version, major, minor, patch, prerelease} =
parseVersion('1000.0.0');
it('should parse dryrun with prerelease . version', () => {
const {version, major, minor, patch, prerelease} = parseVersion(
'0.20.3-rc.0',
'dry-run',
);
expect(version).toBe('0.20.3-rc.0');
expect(major).toBe('0');
expect(minor).toBe('20');
expect(patch).toBe('3');
expect(prerelease).toBe('rc.0');
});

it('should parse dryrun with prerelease - version', () => {
const {version, major, minor, patch, prerelease} = parseVersion(
'0.20.3-rc-0',
'dry-run',
);
expect(version).toBe('0.20.3-rc-0');
expect(major).toBe('0');
expect(minor).toBe('20');
expect(patch).toBe('3');
expect(prerelease).toBe('rc-0');
});

it('should parse dryrun with main version', () => {
const {version, major, minor, patch, prerelease} = parseVersion(
'1000.0.0',
'dry-run',
);
expect(version).toBe('1000.0.0');
expect(major).toBe('1000');
expect(minor).toBe('0');
expect(patch).toBe('0');
expect(prerelease).toBeUndefined();
});

it('should parse dryrun with v1000.0.1 version', () => {
const {version, major, minor, patch, prerelease} = parseVersion(
'v1000.0.1',
'dry-run',
);
expect(version).toBe('1000.0.1');
expect(major).toBe('1000');
expect(minor).toBe('0');
expect(patch).toBe('1');
expect(prerelease).toBeUndefined();
});
it('should parse dryrun with nightly version', () => {
const {version, major, minor, patch, prerelease} = parseVersion(
'0.0.0-something-else',
'dry-run',
);
expect(version).toBe('0.0.0-something-else');
expect(major).toBe('0');
expect(minor).toBe('0');
expect(patch).toBe('0');
expect(prerelease).toBe('something-else');
});

it('should reject dryrun invalid values', () => {
function testInvalidFunction() {
parseVersion('1000.0.4', 'dry-run');
}
expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot(
`"Version 1000.0.4 is not valid for dry-runs"`,
);
});

it('should reject dryrun for invalid prerelease', () => {
function testInvalidFunction() {
parseVersion('0.6.4-something-else', 'dry-run');
}
expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot(
`"Version 0.6.4-something-else is not valid for dry-runs"`,
);
});

it('should reject dryrun for nightlies with invalid prerelease', () => {
function testInvalidFunction() {
parseVersion('0.0.0', 'dry-run');
}
expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot(
`"Version 0.0.0 is not valid for dry-runs"`,
);
});
});

describe('Validate version', () => {
it('Throw error if the buildType is unknown', () => {
function testInvalidFunction() {
validateBuildType('wrong_build');
}
expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot(
`"Unsupported build type: wrong_build"`,
);
});
it('Does not throw if the buildType is release', () => {
function testValidCall() {
validateBuildType('release');
}
expect(testValidCall).not.toThrowError();
});
it('Does not throw if the buildType is nightly', () => {
function testValidCall() {
validateBuildType('nightly');
}
expect(testValidCall).not.toThrowError();
});
it('Does not throw if the buildType is dry-run', () => {
function testValidCall() {
validateBuildType('dry-run');
}
expect(testValidCall).not.toThrowError();
});
});
});
2 changes: 1 addition & 1 deletion scripts/bump-oss-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async function main() {
}

let latest = false;
const {version, prerelease} = parseVersion(releaseVersion);
const {version, prerelease} = parseVersion(releaseVersion, 'release');
if (!prerelease) {
const {setLatest} = await inquirer.prompt({
type: 'confirm',
Expand Down
17 changes: 15 additions & 2 deletions scripts/prepare-package-for-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,26 @@ if (branch && !isReleaseBranch(branch) && !isDryRun) {
exit(1);
}

const {version} = parseVersion(releaseVersion);
const buildType = isDryRun
? 'dry-run'
: isReleaseBranch(branch)
? 'release'
: 'nightly';

const {version} = parseVersion(
releaseVersion,
isDryRun ? 'dry-run' : buildType,
);
if (version == null) {
console.error(`Invalid version provided: ${releaseVersion}`);
exit(1);
}

if (exec(`node scripts/set-rn-version.js --to-version ${version}`).code) {
if (
exec(
`node scripts/set-rn-version.js --to-version ${version} --build-type ${buildType}`,
).code
) {
echo(`Failed to set React Native version to ${version}`);
exit(1);
}
Expand Down
13 changes: 11 additions & 2 deletions scripts/publish-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@ const argv = yargs
.strict().argv;
const nightlyBuild = argv.nightly;
const dryRunBuild = argv.dryRun;
const releaseBuild = argv.release;
const isCommitly = nightlyBuild || dryRunBuild;

const buildType = releaseBuild
? 'release'
: nightlyBuild
? 'nightly'
: 'dry-run';

if (!argv.help) {
echo(`The temp publishing folder is ${tmpPublishingFolder}`);
}
Expand All @@ -97,7 +104,7 @@ let version,
minor,
prerelease = null;
try {
({version, major, minor, prerelease} = parseVersion(rawVersion));
({version, major, minor, prerelease} = parseVersion(rawVersion, buildType));
} catch (e) {
echo(e.message);
exit(1);
Expand All @@ -122,7 +129,9 @@ if (dryRunBuild) {
// For stable, pre-release releases, we rely on CircleCI job `prepare_package_for_release` to handle this
if (isCommitly) {
if (
exec(`node scripts/set-rn-version.js --to-version ${releaseVersion}`).code
exec(
`node scripts/set-rn-version.js --to-version ${releaseVersion} --build-type ${buildType}`,
).code
) {
echo(`Failed to set version number to ${releaseVersion}`);
exit(1);
Expand Down
Loading

0 comments on commit 7833857

Please sign in to comment.