Skip to content

Commit 75990dd

Browse files
huntiefacebook-github-bot
authored andcommitted
[skip ci] Add Flow, add positive test case for monorepo publish step (facebook#42936)
Summary: Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D53607809
1 parent f6f4615 commit 75990dd

File tree

2 files changed

+79
-18
lines changed

2 files changed

+79
-18
lines changed

scripts/monorepo/__tests__/find-and-publish-all-bumped-packages-test.js

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,31 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
9+
* @oncall react_native
810
*/
911

1012
const {PUBLISH_PACKAGES_TAG} = require('../constants');
1113
const {
1214
findAndPublishAllBumpedPackages,
1315
getTagsFromCommitMessage,
1416
} = require('../find-and-publish-all-bumped-packages');
15-
const forEachPackage = require('../for-each-package');
16-
const {spawnSync} = require('child_process');
1717

18-
jest.mock('child_process', () => ({spawnSync: jest.fn()}));
19-
jest.mock('../for-each-package', () => jest.fn());
18+
const spawnSync = jest.fn();
19+
const forEachPackage = jest.fn();
20+
const execMock = jest.fn();
21+
22+
jest.mock('child_process', () => ({spawnSync}));
23+
jest.mock('shelljs', () => ({exec: execMock}));
24+
jest.mock('../for-each-package', () => forEachPackage);
2025

2126
describe('findAndPublishAllBumpedPackages', () => {
2227
beforeEach(() => {
23-
// Silence logs.
2428
jest.spyOn(console, 'log').mockImplementation(() => {});
2529
});
26-
it('throws an error if updated version is not 0.x.y', () => {
30+
31+
test('should throw an error if updated version is not 0.x.y', async () => {
2732
const mockedPackageNewVersion = '1.0.0';
2833

2934
forEachPackage.mockImplementationOnce(callback => {
@@ -40,10 +45,64 @@ describe('findAndPublishAllBumpedPackages', () => {
4045
stdout: `This is my commit message\n\n${PUBLISH_PACKAGES_TAG}`,
4146
}));
4247

43-
expect(() => findAndPublishAllBumpedPackages()).toThrow(
48+
await expect(findAndPublishAllBumpedPackages()).rejects.toThrow(
4449
`Package version expected to be 0.x.y, but received ${mockedPackageNewVersion}`,
4550
);
4651
});
52+
53+
test('should publish all changed packages', async () => {
54+
forEachPackage.mockImplementationOnce(callback => {
55+
callback('absolute/path/to/package-a', 'to/package-a', {
56+
version: '0.72.1',
57+
});
58+
callback('absolute/path/to/package-b', 'to/package-b', {
59+
version: '0.72.1',
60+
});
61+
callback('absolute/path/to/package-c', 'to/package-b', {
62+
version: '0.72.0',
63+
});
64+
});
65+
66+
spawnSync.mockImplementationOnce(() => ({
67+
stdout: `- "version": "0.72.0"\n+ "version": "0.72.1"\n`,
68+
}));
69+
spawnSync.mockImplementationOnce(() => ({
70+
stdout: `This is my commit message\n\n${PUBLISH_PACKAGES_TAG}`,
71+
}));
72+
spawnSync.mockImplementationOnce(() => ({
73+
stdout: `- "version": "0.72.0"\n+ "version": "0.72.1"\n`,
74+
}));
75+
spawnSync.mockImplementationOnce(() => ({
76+
stdout: `This is my commit message\n\n${PUBLISH_PACKAGES_TAG}`,
77+
}));
78+
spawnSync.mockImplementationOnce(() => ({
79+
stdout: '\n',
80+
}));
81+
spawnSync.mockImplementationOnce(() => ({
82+
stdout: `This is my commit message\n\n${PUBLISH_PACKAGES_TAG}`,
83+
}));
84+
85+
execMock.mockImplementation(() => ({code: 0}));
86+
87+
await findAndPublishAllBumpedPackages();
88+
89+
expect(execMock.mock.calls).toMatchInlineSnapshot(`
90+
Array [
91+
Array [
92+
"npm publish",
93+
Object {
94+
"cwd": "absolute/path/to/package-a",
95+
},
96+
],
97+
Array [
98+
"npm publish",
99+
Object {
100+
"cwd": "absolute/path/to/package-b",
101+
},
102+
],
103+
]
104+
`);
105+
});
47106
});
48107

49108
describe('getTagsFromCommitMessage', () => {

scripts/monorepo/find-and-publish-all-bumped-packages.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
9+
* @oncall react_native
810
*/
911

1012
const {publishPackage} = require('../npm-utils');
@@ -16,7 +18,7 @@ const path = require('path');
1618
const ROOT_LOCATION = path.join(__dirname, '..', '..');
1719
const NPM_CONFIG_OTP = process.env.NPM_CONFIG_OTP;
1820

19-
function getTagsFromCommitMessage(msg) {
21+
function getTagsFromCommitMessage(msg /*: string */) /*: Array<string> */ {
2022
// ex message we're trying to parse tags out of
2123
// `_some_message_here_${PUBLISH_PACKAGES_TAG}&tagA&tagB\n`;
2224
return msg
@@ -26,12 +28,12 @@ function getTagsFromCommitMessage(msg) {
2628
.slice(1);
2729
}
2830

29-
const findAndPublishAllBumpedPackages = () => {
31+
async function findAndPublishAllBumpedPackages() {
3032
console.log('Traversing all packages inside /packages...');
3133

3234
forEachPackage(
3335
(packageAbsolutePath, packageRelativePathFromRoot, packageManifest) => {
34-
if (packageManifest.private) {
36+
if (packageManifest.private === true) {
3537
console.log(`\u23ED Skipping private package ${packageManifest.name}`);
3638

3739
return;
@@ -58,17 +60,17 @@ const findAndPublishAllBumpedPackages = () => {
5860
process.exit(1);
5961
}
6062

61-
const previousVersionPatternMatches = diff.match(
62-
/- {2}"version": "([0-9]+.[0-9]+.[0-9]+)"/,
63-
);
63+
const previousVersionPatternMatches = diff
64+
.toString()
65+
.match(/- {2}"version": "([0-9]+.[0-9]+.[0-9]+)"/);
6466

6567
if (!previousVersionPatternMatches) {
6668
console.log(`\uD83D\uDD0E No version bump for ${packageManifest.name}`);
6769

6870
return;
6971
}
7072

71-
const {stdout: commitMessage, stderr: commitMessageStderr} = spawnSync(
73+
const {stdout, stderr: commitMessageStderr} = spawnSync(
7274
'git',
7375
[
7476
'log',
@@ -79,6 +81,7 @@ const findAndPublishAllBumpedPackages = () => {
7981
],
8082
{cwd: ROOT_LOCATION, shell: true, stdio: 'pipe', encoding: 'utf-8'},
8183
);
84+
const commitMessage = stdout.toString();
8285

8386
if (commitMessageStderr) {
8487
console.log(
@@ -131,12 +134,11 @@ const findAndPublishAllBumpedPackages = () => {
131134
}
132135
},
133136
);
134-
135-
process.exit(0);
136-
};
137+
}
137138

138139
if (require.main === module) {
139-
findAndPublishAllBumpedPackages();
140+
// eslint-disable-next-line no-void
141+
void findAndPublishAllBumpedPackages();
140142
}
141143

142144
module.exports = {

0 commit comments

Comments
 (0)