Skip to content

Commit 331d820

Browse files
committed
feat(MongoBinary): add ability to disable SYSTEM_BINARY version check
fixes #529
1 parent 2b2a188 commit 331d820

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

docs/api/config-options.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ Format:
9292
- `/path/to/binary/mongod` (POSIX)
9393
- `C:/path/to/binary/mongod.exe` (DOS)
9494

95+
### SYSTEM_BINARY_VERSION_CHECK
96+
97+
Option `SYSTEM_BINARY_VERSION_CHECK` is used to disable the version conflict check if [`SYSTEM_BINARY`](#system_binary) is set and version returned from `mongod_system_binary --version` does not match [`VERSION`](#version)
98+
99+
Default: `true`
100+
95101
### MD5_CHECK
96102

97103
Option `MD5_CHECK` is used to enable an md5 check after download

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,21 @@ export class MongoBinary {
100100
!isNullOrUndefined(spawnOutput),
101101
new Error('Couldnt find an version from system binary output!')
102102
);
103-
const binaryVersion = spawnOutput[1];
104-
105-
if (semver.neq(options.version, binaryVersion)) {
106-
// we will log the version number of the system binary and the version requested so the user can see the difference
107-
console.warn(
108-
'getPath: MongoMemoryServer: Possible version conflict\n' +
109-
` SystemBinary version: "${binaryVersion}"\n` +
110-
` Requested version: "${options.version}"\n\n` +
111-
' Using SystemBinary!'
112-
);
103+
104+
// 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
105+
if (envToBool(resolveConfig(ResolveConfigVariables.SYSTEM_BINARY_VERSION_CHECK))) {
106+
log('getPath: Checking & Warning about version conflicts');
107+
const binaryVersion = spawnOutput[1];
108+
109+
if (semver.neq(options.version, binaryVersion)) {
110+
// we will log the version number of the system binary and the version requested so the user can see the difference
111+
console.warn(
112+
'getPath: MongoMemoryServer: Possible version conflict\n' +
113+
` SystemBinary version: "${binaryVersion}"\n` +
114+
` Requested version: "${options.version}"\n\n` +
115+
' Using SystemBinary!'
116+
);
117+
}
113118
}
114119
} else {
115120
throw new Error(

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ describe('MongoBinary', () => {
109109
afterEach(() => {
110110
delete process.env[envName(ResolveConfigVariables.VERSION)];
111111
delete process.env[envName(ResolveConfigVariables.SYSTEM_BINARY)];
112+
delete process.env[envName(ResolveConfigVariables.SYSTEM_BINARY_VERSION_CHECK)];
112113
});
113114

114115
it('should return and check an SystemBinary', async () => {
@@ -157,6 +158,9 @@ build environment:
157158
);
158159
process.env[envName(ResolveConfigVariables.VERSION)] = '4.4.0'; // set it explicitly to that version to test non-matching versions
159160
process.env[envName(ResolveConfigVariables.SYSTEM_BINARY)] = sysBinaryPath;
161+
expect(resolveConfig(ResolveConfigVariables.SYSTEM_BINARY_VERSION_CHECK)).toStrictEqual(
162+
'true'
163+
);
160164

161165
const output = await MongoBinary.getPath();
162166
expect(childProcess.spawnSync).toHaveBeenCalledTimes(1);
@@ -165,6 +169,38 @@ build environment:
165169
expect(console.warn).toHaveBeenCalledTimes(1);
166170
});
167171

172+
it('should return and check an SYSTEM_BINARY and dont warn version conflict if SYSTEM_BINARY_VERSION_CHECK is false', async () => {
173+
jest.spyOn(console, 'warn');
174+
// Output taken from mongodb x64 for "ubuntu" version "4.0.25"
175+
// DO NOT INDENT THE TEXT
176+
jest.spyOn(childProcess, 'spawnSync').mockReturnValue(
177+
// @ts-expect-error Because "Buffer" is missing values from type, but they are not used in code, so its fine
178+
{
179+
stdout: Buffer.from(`db version v4.0.25
180+
git version: e2416422da84a0b63cde2397d60b521758b56d1b
181+
OpenSSL version: OpenSSL 1.1.1f 31 Mar 2020
182+
allocator: tcmalloc
183+
modules: none
184+
build environment:
185+
distmod: ubuntu1804
186+
distarch: x86_64
187+
target_arch: x86_64`),
188+
}
189+
);
190+
process.env[envName(ResolveConfigVariables.VERSION)] = '4.4.0'; // set it explicitly to that version to test non-matching versions
191+
process.env[envName(ResolveConfigVariables.SYSTEM_BINARY)] = sysBinaryPath;
192+
expect(resolveConfig(ResolveConfigVariables.SYSTEM_BINARY_VERSION_CHECK)).toStrictEqual(
193+
'true'
194+
);
195+
process.env[envName(ResolveConfigVariables.SYSTEM_BINARY_VERSION_CHECK)] = 'false';
196+
197+
const output = await MongoBinary.getPath();
198+
expect(childProcess.spawnSync).toHaveBeenCalledTimes(1);
199+
expect(MongoBinary.download).not.toHaveBeenCalled();
200+
expect(console.warn).not.toHaveBeenCalled();
201+
expect(output).toBe(sysBinaryPath);
202+
});
203+
168204
it('should throw an error if SYSTEM_BINARY is set, but no binary can be found', async () => {
169205
process.env[envName(ResolveConfigVariables.SYSTEM_BINARY)] = sysBinaryPath;
170206
jest.spyOn(DryMongoBinary, 'locateBinary').mockResolvedValue(undefined);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export enum ResolveConfigVariables {
2121
ARCHIVE_NAME = 'ARCHIVE_NAME',
2222
RUNTIME_DOWNLOAD = 'RUNTIME_DOWNLOAD',
2323
USE_HTTP = 'USE_HTTP',
24+
SYSTEM_BINARY_VERSION_CHECK = 'SYSTEM_BINARY_VERSION_CHECK',
2425
}
2526

2627
export const ENV_CONFIG_PREFIX = 'MONGOMS_';
@@ -30,6 +31,7 @@ export const defaultValues = new Map<ResolveConfigVariables, string>([
3031
[ResolveConfigVariables.PREFER_GLOBAL_PATH, 'true'],
3132
[ResolveConfigVariables.RUNTIME_DOWNLOAD, 'true'],
3233
[ResolveConfigVariables.USE_HTTP, 'false'],
34+
[ResolveConfigVariables.SYSTEM_BINARY_VERSION_CHECK, 'true'],
3335
]);
3436

3537
/**

0 commit comments

Comments
 (0)