Skip to content

Commit 2c76f0d

Browse files
authored
fix(ci): make bumping mongosh apply only to mongosh packages (#2356)
mongosh bump would actually end up overriding most auxiliary packages. This was likely not noticeable before as the versions were already aligned and the ordering of the auxiliary bump was after the mongosh bump in tests.
1 parent c6d8afe commit 2c76f0d

File tree

2 files changed

+149
-19
lines changed

2 files changed

+149
-19
lines changed

packages/build/src/npm-packages/bump.spec.ts

Lines changed: 128 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import path from 'path';
1010
import { PROJECT_ROOT } from './constants';
1111

1212
describe('npm-packages bump', function () {
13-
let fsReadFile: SinonStub;
1413
let fsWriteFile: SinonStub;
14+
const shellApiSrc = path.join(
15+
PROJECT_ROOT,
16+
'packages',
17+
'shell-api',
18+
'src',
19+
'mongosh-version.ts'
20+
);
1521

1622
beforeEach(function () {
17-
fsReadFile = sinon.stub(fs, 'readFile');
18-
1923
fsWriteFile = sinon.stub(fs, 'writeFile');
2024
fsWriteFile.resolves();
2125
});
@@ -25,6 +29,13 @@ describe('npm-packages bump', function () {
2529
});
2630

2731
describe('bumpMongoshReleasePackages', function () {
32+
let fsReadFile: SinonStub;
33+
let getPackagesInTopologicalOrder: sinon.SinonStub;
34+
beforeEach(function () {
35+
fsReadFile = sinon.stub(fs, 'readFile');
36+
getPackagesInTopologicalOrder = sinon.stub();
37+
});
38+
2839
it('warns and does not run if version is not set', async function () {
2940
const consoleWarnSpy = sinon.spy(console, 'warn');
3041
await bumpMongoshReleasePackages('');
@@ -35,9 +46,122 @@ describe('npm-packages bump', function () {
3546
expect(fsWriteFile).not.called;
3647
consoleWarnSpy.restore();
3748
});
49+
50+
it('bumps only mongosh packages', async function () {
51+
const mongoshPath = path.join(PROJECT_ROOT, 'packages', 'mongosh');
52+
const autocompletePath = path.join(
53+
PROJECT_ROOT,
54+
'packages',
55+
'autocomplete'
56+
);
57+
getPackagesInTopologicalOrder.resolves([
58+
{ name: 'mongosh', location: mongoshPath },
59+
{ name: '@mongosh/autocomplete', location: autocompletePath },
60+
]);
61+
62+
const rootProjectJson = path.join(PROJECT_ROOT, 'package.json');
63+
const mongoshProjectJson = path.join(mongoshPath, 'package.json');
64+
const autocompleteProjectJson = path.join(
65+
autocompletePath,
66+
'package.json'
67+
);
68+
const mockPackageJson = [
69+
[
70+
rootProjectJson,
71+
{
72+
name: 'mongosh',
73+
devDependencies: {
74+
mongosh: '0.1.2',
75+
'@mongosh/cli-repl': '0.1.2',
76+
'@mongosh/autocomplete': '1.2.3',
77+
},
78+
},
79+
],
80+
[
81+
mongoshProjectJson,
82+
{
83+
name: 'mongosh',
84+
version: '0.1.2',
85+
devDependencies: {
86+
'@mongosh/cli-repl': '9.9.9',
87+
'@mongosh/autocomplete': '1.2.3',
88+
},
89+
},
90+
],
91+
[
92+
autocompleteProjectJson,
93+
{
94+
name: '@mongosh/autocomplete',
95+
version: '1.2.3',
96+
devDependencies: {
97+
'@mongosh/cli-repl': '0.1.2',
98+
'@mongosh/config': '3.3.3',
99+
},
100+
},
101+
],
102+
];
103+
104+
fsReadFile.throws('Unknown file');
105+
for (const [file, json] of mockPackageJson) {
106+
fsReadFile.withArgs(file, 'utf8').resolves(JSON.stringify(json));
107+
}
108+
109+
const updateShellApiMongoshVersion = sinon.stub();
110+
await bumpMongoshReleasePackages(
111+
'9.9.9',
112+
getPackagesInTopologicalOrder,
113+
updateShellApiMongoshVersion
114+
);
115+
expect(fsWriteFile).callCount(3);
116+
117+
expect(
118+
JSON.parse(
119+
fsWriteFile.withArgs(rootProjectJson).firstCall.args[1] as string
120+
)
121+
).deep.equals({
122+
name: 'mongosh',
123+
devDependencies: {
124+
mongosh: '9.9.9',
125+
'@mongosh/cli-repl': '9.9.9',
126+
'@mongosh/autocomplete': '1.2.3',
127+
},
128+
});
129+
130+
expect(
131+
JSON.parse(
132+
fsWriteFile.withArgs(mongoshProjectJson).firstCall.args[1] as string
133+
)
134+
).deep.equals({
135+
name: 'mongosh',
136+
version: '9.9.9',
137+
devDependencies: {
138+
'@mongosh/cli-repl': '9.9.9',
139+
'@mongosh/autocomplete': '1.2.3',
140+
},
141+
});
142+
143+
expect(
144+
JSON.parse(
145+
fsWriteFile.withArgs(autocompleteProjectJson).firstCall
146+
.args[1] as string
147+
)
148+
).deep.equals({
149+
name: '@mongosh/autocomplete',
150+
version: '1.2.3',
151+
devDependencies: {
152+
'@mongosh/cli-repl': '9.9.9',
153+
'@mongosh/config': '3.3.3',
154+
},
155+
});
156+
});
38157
});
39158

40159
describe('updateShellApiMongoshVersion', function () {
160+
let fsReadFile: SinonStub;
161+
beforeEach(function () {
162+
fsReadFile = sinon.stub(fs, 'readFile');
163+
});
164+
41165
it('updates the file to the set version', async function () {
42166
fsReadFile.resolves(`
43167
/** Current mongosh cli-repl version. */
@@ -47,13 +171,7 @@ describe('npm-packages bump', function () {
47171
await updateShellApiMongoshVersion(newVersion);
48172

49173
expect(fsWriteFile).calledWith(
50-
path.join(
51-
PROJECT_ROOT,
52-
'packages',
53-
'shell-api',
54-
'src',
55-
'mongosh-version.ts'
56-
),
174+
shellApiSrc,
57175
`
58176
/** Current mongosh cli-repl version. */
59177
export const MONGOSH_VERSION = '${newVersion}';`,

packages/build/src/npm-packages/bump.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import {
88
import { promises as fs } from 'fs';
99
import path from 'path';
1010
import { spawnSync as spawnSyncFn } from '../helpers';
11-
import { getPackagesInTopologicalOrder } from '@mongodb-js/monorepo-tools';
11+
import { getPackagesInTopologicalOrder as getPackagesInTopologicalOrderFn } from '@mongodb-js/monorepo-tools';
1212

1313
/** Bumps only the main mongosh release packages to the set version. */
1414
export async function bumpMongoshReleasePackages(
15-
version: string
15+
version: string,
16+
getPackagesInTopologicalOrder: typeof getPackagesInTopologicalOrderFn = getPackagesInTopologicalOrderFn,
17+
updateShellApiMongoshVersionFn: typeof updateShellApiMongoshVersion = updateShellApiMongoshVersion
1618
): Promise<void> {
1719
if (!version) {
1820
console.warn(
@@ -22,20 +24,23 @@ export async function bumpMongoshReleasePackages(
2224
}
2325

2426
console.info(`mongosh: Bumping mongosh release packages to ${version}`);
25-
const monorepoRootPath = path.resolve(__dirname, '..', '..', '..', '..');
27+
const monorepoRootPath = PROJECT_ROOT;
2628
const packages = await getPackagesInTopologicalOrder(monorepoRootPath);
2729

28-
const workspaceNames = packages
29-
.map((p) => p.name)
30-
.filter((name) => MONGOSH_RELEASE_PACKAGES.includes(name));
30+
const bumpedPackages = MONGOSH_RELEASE_PACKAGES;
3131

3232
const locations = [monorepoRootPath, ...packages.map((p) => p.location)];
3333

3434
for (const location of locations) {
3535
const packageJsonPath = path.join(location, 'package.json');
3636
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
3737

38-
packageJson.version = version;
38+
if (
39+
bumpedPackages.includes(packageJson.name as string) &&
40+
location !== monorepoRootPath
41+
) {
42+
packageJson.version = version;
43+
}
3944
for (const grouping of [
4045
'dependencies',
4146
'devDependencies',
@@ -47,7 +52,7 @@ export async function bumpMongoshReleasePackages(
4752
}
4853

4954
for (const name of Object.keys(packageJson[grouping])) {
50-
if (!workspaceNames.includes(name)) {
55+
if (!bumpedPackages.includes(name)) {
5156
continue;
5257
}
5358
packageJson[grouping][name] = version;
@@ -60,7 +65,14 @@ export async function bumpMongoshReleasePackages(
6065
);
6166
}
6267

63-
await updateShellApiMongoshVersion(version);
68+
await updateShellApiMongoshVersionFn(version);
69+
70+
// Update package-lock.json
71+
spawnSync('npm', ['install', '--package-lock-only'], {
72+
stdio: 'inherit',
73+
cwd: monorepoRootPath,
74+
encoding: 'utf8',
75+
});
6476
}
6577

6678
/** Updates the shell-api constant to match the mongosh version. */

0 commit comments

Comments
 (0)