Skip to content

Commit 5ce583e

Browse files
committed
fix(MongoBinaryDownload): handle all options from "MongoBinaryOpts" in constructor
fixes #514
1 parent 2637ed4 commit 5ce583e

File tree

3 files changed

+88
-24
lines changed

3 files changed

+88
-24
lines changed

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

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,73 @@ export class MongoBinaryDownload {
3333
dlProgress: MongoBinaryDownloadProgress;
3434
_downloadingUrl?: string;
3535

36-
checkMD5: boolean;
37-
downloadDir: string;
38-
arch: string;
39-
version: string;
40-
platform: string;
41-
42-
constructor({ platform, arch, downloadDir, version, checkMD5 }: MongoBinaryOpts) {
43-
this.platform = platform ?? os.platform();
44-
this.arch = arch ?? os.arch();
45-
version = version ?? resolveConfig(ResolveConfigVariables.VERSION);
36+
/**These options are kind of raw, they are not run through DryMongoBinary.generateOptions */
37+
binaryOpts: Required<MongoBinaryOpts>;
38+
39+
// TODO: for an major version, remove the compat get/set
40+
// the following get/set are to not break existing stuff
41+
42+
get checkMD5(): boolean {
43+
return this.binaryOpts.checkMD5;
44+
}
45+
46+
set checkMD5(val: boolean) {
47+
this.binaryOpts.checkMD5 = val;
48+
}
49+
50+
get downloadDir(): string {
51+
return this.binaryOpts.downloadDir;
52+
}
53+
54+
set downloadDir(val: string) {
55+
this.binaryOpts.downloadDir = val;
56+
}
57+
58+
get arch(): string {
59+
return this.binaryOpts.arch;
60+
}
61+
62+
set arch(val: string) {
63+
this.binaryOpts.arch = val;
64+
}
65+
66+
get version(): string {
67+
return this.binaryOpts.version;
68+
}
69+
70+
set version(val: string) {
71+
this.binaryOpts.version = val;
72+
}
73+
74+
get platform(): string {
75+
return this.binaryOpts.platform;
76+
}
77+
78+
set platform(val: string) {
79+
this.binaryOpts.platform = val;
80+
}
81+
82+
// end get/set backwards compat section
83+
84+
constructor(opts: MongoBinaryOpts) {
85+
assertion(typeof opts.downloadDir === 'string', new Error('An DownloadDir must be specified!'));
86+
const version = opts.version ?? resolveConfig(ResolveConfigVariables.VERSION);
4687
assertion(
4788
typeof version === 'string',
4889
new Error('An MongoDB Binary version must be specified!')
4990
);
50-
assertion(typeof downloadDir === 'string', new Error('An DownloadDir must be specified!'));
51-
this.version = version;
52-
this.downloadDir = downloadDir;
53-
this.checkMD5 = checkMD5 ?? envToBool(resolveConfig(ResolveConfigVariables.MD5_CHECK));
91+
92+
// DryMongoBinary.generateOptions cannot be used here, because its async
93+
this.binaryOpts = {
94+
platform: opts.platform ?? os.platform(),
95+
arch: opts.arch ?? os.arch(),
96+
version: version,
97+
downloadDir: opts.downloadDir,
98+
checkMD5: opts.checkMD5 ?? envToBool(resolveConfig(ResolveConfigVariables.MD5_CHECK)),
99+
systemBinary: opts.systemBinary ?? '',
100+
os: opts.os ?? { os: 'unknown' },
101+
};
102+
54103
this.dlProgress = {
55104
current: 0,
56105
length: 0,
@@ -64,11 +113,7 @@ export class MongoBinaryDownload {
64113
* @return Absoulte Path with FileName
65114
*/
66115
protected async getPath(): Promise<string> {
67-
const opts = await DryMongoBinary.generateOptions({
68-
version: this.version,
69-
arch: this.arch,
70-
downloadDir: this.downloadDir,
71-
});
116+
const opts = await DryMongoBinary.generateOptions(this.binaryOpts);
72117

73118
return DryMongoBinary.combineBinaryName(this.downloadDir, DryMongoBinary.getBinaryName(opts));
74119
}
@@ -104,11 +149,7 @@ export class MongoBinaryDownload {
104149
*/
105150
async startDownload(): Promise<string> {
106151
log('startDownload');
107-
const mbdUrl = new MongoBinaryDownloadUrl({
108-
platform: this.platform,
109-
arch: this.arch,
110-
version: this.version,
111-
});
152+
const mbdUrl = new MongoBinaryDownloadUrl(this.binaryOpts);
112153

113154
await mkdirp(this.downloadDir);
114155

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { promises as fspromises } from 'fs';
22
import md5file from 'md5-file';
33
import * as mkdirp from 'mkdirp';
44
import { DryMongoBinary } from '../DryMongoBinary';
5+
import { MongoBinaryOpts } from '../MongoBinary';
56
import MongoBinaryDownload from '../MongoBinaryDownload';
67
import MongoBinaryDownloadUrl from '../MongoBinaryDownloadUrl';
78
import { envName, ResolveConfigVariables } from '../resolveConfig';
@@ -270,4 +271,23 @@ describe('MongoBinaryDownload', () => {
270271
expect(console.error).toHaveBeenCalledTimes(1);
271272
}
272273
});
274+
275+
it('should passthrough options to MongoBinaryDownloadUrl', async () => {
276+
const options: MongoBinaryOpts = {
277+
arch: 'x64',
278+
downloadDir: '/path/to/somewhere',
279+
os: {
280+
os: 'linux',
281+
dist: 'custom',
282+
release: '100.0',
283+
},
284+
version: '4.0.0',
285+
};
286+
const mbd = new MongoBinaryDownload(options);
287+
jest.spyOn(DryMongoBinary, 'generateOptions');
288+
// @ts-expect-error "getPath" is protected
289+
const path = await mbd.getPath();
290+
expect(DryMongoBinary.generateOptions).toBeCalledWith(expect.objectContaining(options));
291+
expect(path).toMatchSnapshot();
292+
});
273293
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`MongoBinaryDownload should passthrough options to MongoBinaryDownloadUrl 1`] = `"/path/to/somewhere/mongod-x64-custom-4.0.0"`;

0 commit comments

Comments
 (0)