Skip to content

Commit 03c8412

Browse files
committed
fix(MongoBinary): dont execute systembinary "--version" cmd if version check is false
maybe this will make it a workaround for third-party binaries that maybe dont conform to the official version output.
1 parent 613e670 commit 03c8412

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

packages/mongodb-memory-server-core/src/util/MongoBinary.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,20 @@ export class MongoBinary {
8787
if (!!options.systemBinary) {
8888
// this case should actually never be false, because if "SYSTEM_BINARY" is set, "locateBinary" will run "getSystemPath" which tests the path for permissions
8989
if (!isNullOrUndefined(binaryPath)) {
90-
log(`getPath: Spawning binaryPath "${binaryPath}" to get version`);
91-
const spawnOutput = spawnSync(binaryPath, ['--version'])
92-
// NOTE: "stdout" seemingly can be "undefined", see https://github.com/typegoose/mongodb-memory-server/issues/742#issuecomment-2528284865
93-
.stdout?.toString()
94-
// this regex is to match the first line of the "mongod --version" output "db version v4.0.25" OR "db version v4.2.19-11-ge2f2736"
95-
.match(/^\s*db\s+version\s+v?(\d+\.\d+\.\d+)(-\d*)?(-[a-zA-Z0-9].*)?\s*$/im);
96-
97-
assertion(
98-
!isNullOrUndefined(spawnOutput),
99-
new Error('Couldnt find an version from system binary output!')
100-
);
101-
10290
// dont warn if the versions dont match if "SYSTEM_BINARY_VERSION_CHECK" is false, but still test the binary if it is available to be executed
10391
if (envToBool(resolveConfig(ResolveConfigVariables.SYSTEM_BINARY_VERSION_CHECK))) {
92+
log(`getPath: Spawning binaryPath "${binaryPath}" to get version`);
93+
const spawnOutput = spawnSync(binaryPath, ['--version'])
94+
// NOTE: "stdout" seemingly can be "undefined", see https://github.com/typegoose/mongodb-memory-server/issues/742#issuecomment-2528284865
95+
.stdout?.toString()
96+
// this regex is to match the first line of the "mongod --version" output "db version v4.0.25" OR "db version v4.2.19-11-ge2f2736"
97+
.match(/^\s*db\s+version\s+v?(\d+\.\d+\.\d+)(-\d*)?(-[a-zA-Z0-9].*)?\s*$/im);
98+
99+
assertion(
100+
!isNullOrUndefined(spawnOutput),
101+
new Error('Couldnt find an version from system binary output!')
102+
);
103+
104104
log('getPath: Checking & Warning about version conflicts');
105105
const binaryVersion = spawnOutput[1];
106106

packages/mongodb-memory-server-core/src/util/__tests__/MongoBinary.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ build environment:
192192
expect(console.warn).toHaveBeenCalledTimes(1);
193193
});
194194

195-
it('should return and check an SYSTEM_BINARY and dont warn version conflict if SYSTEM_BINARY_VERSION_CHECK is false', async () => {
195+
it('should return and check a SYSTEM_BINARY and dont warn version conflict if SYSTEM_BINARY_VERSION_CHECK is false', async () => {
196196
jest.spyOn(console, 'warn');
197197
// Output taken from mongodb x64 for "ubuntu" version "4.0.25"
198198
// DO NOT INDENT THE TEXT
@@ -218,7 +218,7 @@ build environment:
218218
process.env[envName(ResolveConfigVariables.SYSTEM_BINARY_VERSION_CHECK)] = 'false';
219219

220220
const output = await MongoBinary.getPath();
221-
expect(childProcess.spawnSync).toHaveBeenCalledTimes(1);
221+
expect(childProcess.spawnSync).toHaveBeenCalledTimes(0);
222222
expect(MongoBinary.download).not.toHaveBeenCalled();
223223
expect(console.warn).not.toHaveBeenCalled();
224224
expect(output).toBe(sysBinaryPath);

0 commit comments

Comments
 (0)