Skip to content

Commit 79911d7

Browse files
authored
fix(MongoBinary::getPath): fix mongo binary version matching regex (#625)
fix regex that extracts the local mongo binary version so that it can handle versions that include patches and/or other tags. fixes #624
1 parent 5badaec commit 79911d7

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

docs/api/config-options.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ Search for what version is used:
5353
- [`windows`](https://www.mongodb.org/dl/win32)
5454
- [`linux`](https://dl.mongodb.org/dl/linux)
5555

56+
:::note
57+
When using [`SYSTEM_BINARY`](#system_binary) and [`SYSTEM_BINARY_VERSION_CHECK`](#system_binary_version_check), ONLY the major, minor, and patch versions of the system binary will be compared against the desired binary.
58+
59+
That is, a system binary version of `4.2.19-11-ge2f2736a` will match a mongodb required version of `4.2.19`. DO NOT set the mongodb required version to the full `4.2.19-11-ge2f2736a` version as the check which examines the binary version will strip the additional tags.
60+
:::
61+
5662
### DEBUG
5763

5864
Option `DEBUG` is used to enable Debug Output

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ export class MongoBinary {
9292
log(`getPath: Spawning binaryPath "${binaryPath}" to get version`);
9393
const spawnOutput = spawnSync(binaryPath, ['--version'])
9494
.stdout.toString()
95-
// this regex is to match the first line of the "mongod --version" output "db version v4.0.25"
96-
.match(/^\s*db\s+version\s+v?(\d+\.\d+\.\d+)\s*$/im);
95+
// 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"
96+
.match(/^\s*db\s+version\s+v?(\d+\.\d+\.\d+)(-\d*)?(-[a-zA-Z0-9].*)?\s*$/im);
9797

9898
assertion(
9999
!isNullOrUndefined(spawnOutput),

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,32 @@ build environment:
138138
expect(output).toBe(sysBinaryPath);
139139
});
140140

141+
it('should return and check an SystemBinary and match even if binary has patch version', async () => {
142+
// Output taken from mongodb x64 for "debian" version "4.2.19-11-ge2f2736"
143+
// DO NOT INDENT THE TEXT
144+
jest.spyOn(childProcess, 'spawnSync').mockReturnValue(
145+
// @ts-expect-error Because "Buffer" is missing values from type, but they are not used in code, so its fine
146+
{
147+
stdout: Buffer.from(`db version v4.2.19-11-ge2f2736
148+
git version: e2f27369cdfb8c417b026afb323c810226417206
149+
OpenSSL version: OpenSSL 1.1.1n 15 Mar 2022
150+
allocator: tcmalloc
151+
modules: none
152+
build environment:
153+
distmod: debian10
154+
distarch: x86_64
155+
target_arch: x86_64`),
156+
}
157+
);
158+
process.env[envName(ResolveConfigVariables.VERSION)] = '4.2.19'; // set it explicitly to that version to test matching versions
159+
process.env[envName(ResolveConfigVariables.SYSTEM_BINARY)] = sysBinaryPath;
160+
161+
const output = await MongoBinary.getPath();
162+
expect(childProcess.spawnSync).toHaveBeenCalledTimes(1);
163+
expect(MongoBinary.download).not.toHaveBeenCalled();
164+
expect(output).toBe(sysBinaryPath);
165+
});
166+
141167
it('should return and check an SYSTEM_BINARY and warn version conflict', async () => {
142168
jest.spyOn(console, 'warn').mockImplementation(() => void 0);
143169
// Output taken from mongodb x64 for "ubuntu" version "4.0.25"

0 commit comments

Comments
 (0)