Skip to content

Commit 3cb858b

Browse files
authored
fix: resolve "Invalid Semver version" problem #345 #335 (#346)
* fix: fix "Invalid Semver version" - add tests for "latest" for macos & windows - fix "Invalid Version: latest" for macos & windows closes #345 * fix: remove dedent - replace all occurences with "dedent" with proper strings closes #335
1 parent 98bd5e1 commit 3cb858b

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

packages/mongodb-memory-server-core/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"camelcase": "^6.0.0",
6464
"cross-spawn": "^7.0.3",
6565
"debug": "^4.1.1",
66-
"dedent": "^0.7.0",
6766
"find-cache-dir": "^3.3.1",
6867
"find-package-json": "^1.2.0",
6968
"get-port": "^5.1.1",

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import LockFile from 'lockfile';
55
import mkdirp from 'mkdirp';
66
import findCacheDir from 'find-cache-dir';
77
import { execSync } from 'child_process';
8-
import dedent from 'dedent';
98
import { promisify } from 'util';
109
import MongoBinaryDownload from './MongoBinaryDownload';
1110
import resolveConfig, { envToBool } from './resolve-config';
@@ -147,13 +146,12 @@ export default class MongoBinary {
147146

148147
if (version !== LATEST_VERSION && version !== binaryVersion) {
149148
// we will log the version number of the system binary and the version requested so the user can see the difference
150-
log(dedent`
151-
MongoMemoryServer: Possible version conflict
152-
SystemBinary version: ${binaryVersion}
153-
Requested version: ${version}
154-
155-
Using SystemBinary!
156-
`);
149+
log(
150+
'MongoMemoryServer: Possible version conflict\n' +
151+
` SystemBinary version: ${binaryVersion}\n` +
152+
` Requested version: ${version}\n\n` +
153+
' Using SystemBinary!'
154+
);
157155
}
158156
}
159157
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { HttpsProxyAgent } from 'https-proxy-agent';
1414
import { promisify } from 'util';
1515
import resolveConfig, { envToBool } from './resolve-config';
1616
import debug from 'debug';
17-
import dedent from 'dedent';
1817

1918
const log = debug('MongoMS:MongoBinaryDownload');
2019

@@ -324,10 +323,10 @@ export default class MongoBinaryDownload {
324323
if (response.statusCode != 200) {
325324
if (response.statusCode === 403) {
326325
reject(
327-
new Error(dedent`
328-
Status Code is 403 (MongoDB's 404)\n
329-
This means that the requested version-platform combination dosnt exist
330-
`)
326+
new Error(
327+
"Status Code is 403 (MongoDB's 404)\n" +
328+
"This means that the requested version-platform combination doesn't exist"
329+
)
331330
);
332331
return;
333332
}

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { execSync } from 'child_process';
33
import resolveConfig from './resolve-config';
44
import debug from 'debug';
55
import * as semver from 'semver';
6+
import { isNullOrUndefined } from 'util';
67

78
const log = debug('MongoMS:MongoBinaryDownloadUrl');
89

@@ -73,10 +74,12 @@ export default class MongoBinaryDownloadUrl {
7374
async getArchiveNameWin(): Promise<string> {
7475
let name = `mongodb-${this.platform}`;
7576
name += `-${this.arch}`;
76-
if (semver.satisfies(this.version, '4.2.x')) {
77-
name += '-2012plus';
78-
} else if (semver.lt(this.version, '4.0.0')) {
79-
name += '-2008plus-ssl';
77+
if (!isNullOrUndefined(semver.coerce(this.version))) {
78+
if (semver.satisfies(this.version, '4.2.x')) {
79+
name += '-2012plus';
80+
} else if (semver.lt(this.version, '4.0.0')) {
81+
name += '-2008plus-ssl';
82+
}
8083
}
8184
name += `-${this.version}.zip`;
8285
return name;
@@ -88,10 +91,11 @@ export default class MongoBinaryDownloadUrl {
8891
*/
8992
async getArchiveNameOsx(): Promise<string> {
9093
let name = `mongodb-osx`;
91-
if (semver.gte(this.version, '3.2.0')) {
94+
const version = semver.coerce(this.version);
95+
if (!isNullOrUndefined(version) && semver.gte(version, '3.2.0')) {
9296
name += '-ssl';
9397
}
94-
if (semver.gte(this.version, '4.2.0')) {
98+
if (isNullOrUndefined(version) || semver.gte(version, '4.2.0')) {
9599
name = `mongodb-macos`; // somehow these files are not listed in https://www.mongodb.org/dl/osx
96100
}
97101
name += `-${this.arch}`;
@@ -332,7 +336,9 @@ export default class MongoBinaryDownloadUrl {
332336
case 'darwin':
333337
return 'osx';
334338
case 'win32':
335-
return semver.gte(this.version, '4.3.0') ? 'windows' : 'win32';
339+
const version = semver.coerce(this.version);
340+
if (isNullOrUndefined(version)) return 'windows';
341+
return semver.gte(version, '4.3.0') ? 'windows' : 'win32';
336342
case 'linux':
337343
case 'elementary OS':
338344
return 'linux';

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ import MongoBinaryDownloadUrl from '../MongoBinaryDownloadUrl';
33
describe('MongoBinaryDownloadUrl', () => {
44
describe('getDownloadUrl()', () => {
55
describe('for mac', () => {
6+
it('latest', async () => {
7+
const du = new MongoBinaryDownloadUrl({
8+
platform: 'darwin',
9+
arch: 'x64',
10+
version: 'latest',
11+
});
12+
expect(await du.getDownloadUrl()).toBe(
13+
'https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-latest.tgz'
14+
);
15+
});
16+
617
it('4.4', async () => {
718
const du = new MongoBinaryDownloadUrl({
819
platform: 'darwin',
@@ -102,6 +113,17 @@ describe('MongoBinaryDownloadUrl', () => {
102113
'https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-4.4.0.zip'
103114
);
104115
});
116+
117+
it('latest (windows)', async () => {
118+
const du = new MongoBinaryDownloadUrl({
119+
platform: 'win32',
120+
arch: 'x64',
121+
version: 'latest',
122+
});
123+
expect(await du.getDownloadUrl()).toBe(
124+
'https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-latest.zip'
125+
);
126+
});
105127
});
106128

107129
it('fallback', async () => {

0 commit comments

Comments
 (0)