Skip to content

Commit

Permalink
fix(MongoBinary): dont execute systembinary "--version" cmd if versio…
Browse files Browse the repository at this point in the history
…n check is false

maybe this will make it a workaround for third-party binaries that maybe dont conform
to the official version output.
  • Loading branch information
hasezoey committed Dec 9, 2024
1 parent 613e670 commit 03c8412
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions packages/mongodb-memory-server-core/src/util/MongoBinary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ export class MongoBinary {
if (!!options.systemBinary) {
// this case should actually never be false, because if "SYSTEM_BINARY" is set, "locateBinary" will run "getSystemPath" which tests the path for permissions
if (!isNullOrUndefined(binaryPath)) {
log(`getPath: Spawning binaryPath "${binaryPath}" to get version`);
const spawnOutput = spawnSync(binaryPath, ['--version'])
// NOTE: "stdout" seemingly can be "undefined", see https://github.com/typegoose/mongodb-memory-server/issues/742#issuecomment-2528284865
.stdout?.toString()
// 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"
.match(/^\s*db\s+version\s+v?(\d+\.\d+\.\d+)(-\d*)?(-[a-zA-Z0-9].*)?\s*$/im);

assertion(
!isNullOrUndefined(spawnOutput),
new Error('Couldnt find an version from system binary output!')
);

// 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
if (envToBool(resolveConfig(ResolveConfigVariables.SYSTEM_BINARY_VERSION_CHECK))) {
log(`getPath: Spawning binaryPath "${binaryPath}" to get version`);
const spawnOutput = spawnSync(binaryPath, ['--version'])
// NOTE: "stdout" seemingly can be "undefined", see https://github.com/typegoose/mongodb-memory-server/issues/742#issuecomment-2528284865
.stdout?.toString()
// 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"
.match(/^\s*db\s+version\s+v?(\d+\.\d+\.\d+)(-\d*)?(-[a-zA-Z0-9].*)?\s*$/im);

assertion(
!isNullOrUndefined(spawnOutput),
new Error('Couldnt find an version from system binary output!')
);

log('getPath: Checking & Warning about version conflicts');
const binaryVersion = spawnOutput[1];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ build environment:
expect(console.warn).toHaveBeenCalledTimes(1);
});

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

const output = await MongoBinary.getPath();
expect(childProcess.spawnSync).toHaveBeenCalledTimes(1);
expect(childProcess.spawnSync).toHaveBeenCalledTimes(0);
expect(MongoBinary.download).not.toHaveBeenCalled();
expect(console.warn).not.toHaveBeenCalled();
expect(output).toBe(sysBinaryPath);
Expand Down

0 comments on commit 03c8412

Please sign in to comment.