-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
MongoBinary-test.js
89 lines (70 loc) · 2.45 KB
/
MongoBinary-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* @flow */
import tmp from 'tmp';
import fs from 'fs';
import os from 'os';
import MongoBinary from '../MongoBinary';
const MongoBinaryDownload: any = require('../MongoBinaryDownload');
tmp.setGracefulCleanup();
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
const mockGetMongodPath = jest.fn().mockResolvedValue('/temp/path');
jest.mock('../MongoBinaryDownload', () => {
return jest.fn().mockImplementation(() => {
return { getMongodPath: mockGetMongodPath };
});
});
describe('MongoBinary', () => {
let tmpDir;
beforeEach(() => {
tmpDir = tmp.dirSync({ prefix: 'mongo-mem-bin-', unsafeCleanup: true });
});
// cleanup
afterEach(() => {
tmpDir.removeCallback();
MongoBinaryDownload.mockClear();
mockGetMongodPath.mockClear();
MongoBinary.cache = {};
});
describe('getPath', () => {
it('should get system binary from the environment', async () => {
const accessSpy = jest.spyOn(fs, 'access');
process.env.MONGOMS_SYSTEM_BINARY = '/usr/local/bin/mongod';
await MongoBinary.getPath();
expect(accessSpy).toHaveBeenCalledWith('/usr/local/bin/mongod');
accessSpy.mockClear();
});
});
describe('getDownloadPath', () => {
it('should download binary and keep it in cache', async () => {
// download
const version = 'latest';
const binPath = await MongoBinary.getPath({
downloadDir: tmpDir.name,
version,
});
// eg. /tmp/mongo-mem-bin-33990ScJTSRNSsFYf/mongodb-download/a811facba94753a2eba574f446561b7e/mongodb-macOS-x86_64-3.5.5-13-g00ee4f5/
expect(MongoBinaryDownload).toHaveBeenCalledWith({
downloadDir: tmpDir.name,
platform: os.platform(),
arch: os.arch(),
version,
});
expect(mockGetMongodPath).toHaveBeenCalledTimes(1);
expect(MongoBinary.cache[version]).toBeDefined();
expect(MongoBinary.cache[version]).toEqual(binPath);
});
});
describe('getCachePath', () => {
it('should get the cache', async () => {
MongoBinary.cache['3.4.2'] = '/bin/mongod';
await expect(MongoBinary.getCachePath('3.4.2')).resolves.toEqual('/bin/mongod');
});
});
describe('getSystemPath', () => {
it('should use system binary if option is passed.', async () => {
const accessSpy = jest.spyOn(fs, 'access');
await MongoBinary.getSystemPath('/usr/bin/mongod');
expect(accessSpy).toHaveBeenCalledWith('/usr/bin/mongod');
accessSpy.mockClear();
});
});
});