Skip to content

Commit

Permalink
fix(ci): make bumping mongosh apply only to mongosh packages (#2356)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gagik authored Feb 4, 2025
1 parent c6d8afe commit 2c76f0d
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 19 deletions.
138 changes: 128 additions & 10 deletions packages/build/src/npm-packages/bump.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import path from 'path';
import { PROJECT_ROOT } from './constants';

describe('npm-packages bump', function () {
let fsReadFile: SinonStub;
let fsWriteFile: SinonStub;
const shellApiSrc = path.join(
PROJECT_ROOT,
'packages',
'shell-api',
'src',
'mongosh-version.ts'
);

beforeEach(function () {
fsReadFile = sinon.stub(fs, 'readFile');

fsWriteFile = sinon.stub(fs, 'writeFile');
fsWriteFile.resolves();
});
Expand All @@ -25,6 +29,13 @@ describe('npm-packages bump', function () {
});

describe('bumpMongoshReleasePackages', function () {
let fsReadFile: SinonStub;
let getPackagesInTopologicalOrder: sinon.SinonStub;
beforeEach(function () {
fsReadFile = sinon.stub(fs, 'readFile');
getPackagesInTopologicalOrder = sinon.stub();
});

it('warns and does not run if version is not set', async function () {
const consoleWarnSpy = sinon.spy(console, 'warn');
await bumpMongoshReleasePackages('');
Expand All @@ -35,9 +46,122 @@ describe('npm-packages bump', function () {
expect(fsWriteFile).not.called;
consoleWarnSpy.restore();
});

it('bumps only mongosh packages', async function () {
const mongoshPath = path.join(PROJECT_ROOT, 'packages', 'mongosh');
const autocompletePath = path.join(
PROJECT_ROOT,
'packages',
'autocomplete'
);
getPackagesInTopologicalOrder.resolves([
{ name: 'mongosh', location: mongoshPath },
{ name: '@mongosh/autocomplete', location: autocompletePath },
]);

const rootProjectJson = path.join(PROJECT_ROOT, 'package.json');
const mongoshProjectJson = path.join(mongoshPath, 'package.json');
const autocompleteProjectJson = path.join(
autocompletePath,
'package.json'
);
const mockPackageJson = [
[
rootProjectJson,
{
name: 'mongosh',
devDependencies: {
mongosh: '0.1.2',
'@mongosh/cli-repl': '0.1.2',
'@mongosh/autocomplete': '1.2.3',
},
},
],
[
mongoshProjectJson,
{
name: 'mongosh',
version: '0.1.2',
devDependencies: {
'@mongosh/cli-repl': '9.9.9',
'@mongosh/autocomplete': '1.2.3',
},
},
],
[
autocompleteProjectJson,
{
name: '@mongosh/autocomplete',
version: '1.2.3',
devDependencies: {
'@mongosh/cli-repl': '0.1.2',
'@mongosh/config': '3.3.3',
},
},
],
];

fsReadFile.throws('Unknown file');
for (const [file, json] of mockPackageJson) {
fsReadFile.withArgs(file, 'utf8').resolves(JSON.stringify(json));
}

const updateShellApiMongoshVersion = sinon.stub();
await bumpMongoshReleasePackages(
'9.9.9',
getPackagesInTopologicalOrder,
updateShellApiMongoshVersion
);
expect(fsWriteFile).callCount(3);

expect(
JSON.parse(
fsWriteFile.withArgs(rootProjectJson).firstCall.args[1] as string
)
).deep.equals({
name: 'mongosh',
devDependencies: {
mongosh: '9.9.9',
'@mongosh/cli-repl': '9.9.9',
'@mongosh/autocomplete': '1.2.3',
},
});

expect(
JSON.parse(
fsWriteFile.withArgs(mongoshProjectJson).firstCall.args[1] as string
)
).deep.equals({
name: 'mongosh',
version: '9.9.9',
devDependencies: {
'@mongosh/cli-repl': '9.9.9',
'@mongosh/autocomplete': '1.2.3',
},
});

expect(
JSON.parse(
fsWriteFile.withArgs(autocompleteProjectJson).firstCall
.args[1] as string
)
).deep.equals({
name: '@mongosh/autocomplete',
version: '1.2.3',
devDependencies: {
'@mongosh/cli-repl': '9.9.9',
'@mongosh/config': '3.3.3',
},
});
});
});

describe('updateShellApiMongoshVersion', function () {
let fsReadFile: SinonStub;
beforeEach(function () {
fsReadFile = sinon.stub(fs, 'readFile');
});

it('updates the file to the set version', async function () {
fsReadFile.resolves(`
/** Current mongosh cli-repl version. */
Expand All @@ -47,13 +171,7 @@ describe('npm-packages bump', function () {
await updateShellApiMongoshVersion(newVersion);

expect(fsWriteFile).calledWith(
path.join(
PROJECT_ROOT,
'packages',
'shell-api',
'src',
'mongosh-version.ts'
),
shellApiSrc,
`
/** Current mongosh cli-repl version. */
export const MONGOSH_VERSION = '${newVersion}';`,
Expand Down
30 changes: 21 additions & 9 deletions packages/build/src/npm-packages/bump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import {
import { promises as fs } from 'fs';
import path from 'path';
import { spawnSync as spawnSyncFn } from '../helpers';
import { getPackagesInTopologicalOrder } from '@mongodb-js/monorepo-tools';
import { getPackagesInTopologicalOrder as getPackagesInTopologicalOrderFn } from '@mongodb-js/monorepo-tools';

/** Bumps only the main mongosh release packages to the set version. */
export async function bumpMongoshReleasePackages(
version: string
version: string,
getPackagesInTopologicalOrder: typeof getPackagesInTopologicalOrderFn = getPackagesInTopologicalOrderFn,
updateShellApiMongoshVersionFn: typeof updateShellApiMongoshVersion = updateShellApiMongoshVersion
): Promise<void> {
if (!version) {
console.warn(
Expand All @@ -22,20 +24,23 @@ export async function bumpMongoshReleasePackages(
}

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

const workspaceNames = packages
.map((p) => p.name)
.filter((name) => MONGOSH_RELEASE_PACKAGES.includes(name));
const bumpedPackages = MONGOSH_RELEASE_PACKAGES;

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

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

packageJson.version = version;
if (
bumpedPackages.includes(packageJson.name as string) &&
location !== monorepoRootPath
) {
packageJson.version = version;
}
for (const grouping of [
'dependencies',
'devDependencies',
Expand All @@ -47,7 +52,7 @@ export async function bumpMongoshReleasePackages(
}

for (const name of Object.keys(packageJson[grouping])) {
if (!workspaceNames.includes(name)) {
if (!bumpedPackages.includes(name)) {
continue;
}
packageJson[grouping][name] = version;
Expand All @@ -60,7 +65,14 @@ export async function bumpMongoshReleasePackages(
);
}

await updateShellApiMongoshVersion(version);
await updateShellApiMongoshVersionFn(version);

// Update package-lock.json
spawnSync('npm', ['install', '--package-lock-only'], {
stdio: 'inherit',
cwd: monorepoRootPath,
encoding: 'utf8',
});
}

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

0 comments on commit 2c76f0d

Please sign in to comment.