Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test on CI * fix unit test * fix prettier * check that system binary exists * fix unit tests * refactor getPath method to consume several smaller methods * update tests to excersize each smaller method individually this also updates tests to use a mocked version of the MongoBinaryDownloader so that we have a truelly issolated unit test... * fix flow typing issues * check system binary version against requested version this is a fairly simple check and doesn't attempt to do any complex version matching or resolution. It simply takes the value provided by the system and checks strict equality with the version requested. * update the README.md to include new option also added a section explaining how to use mongodb-memory-server on a system not officially supported by mongodb * check that systemBinary exists before checking versions
- Loading branch information
Showing
4 changed files
with
196 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,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', () => { | ||
it('should download binary and keep it in cache', async () => { | ||
const tmpDir = tmp.dirSync({ prefix: 'mongo-mem-bin-', unsafeCleanup: true }); | ||
|
||
// download | ||
const version = 'latest'; | ||
const binPath = await MongoBinary.getPath({ | ||
downloadDir: tmpDir.name, | ||
version, | ||
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(); | ||
}); | ||
// eg. /tmp/mongo-mem-bin-33990ScJTSRNSsFYf/mongodb-download/a811facba94753a2eba574f446561b7e/mongodb-macOS-x86_64-3.5.5-13-g00ee4f5/ | ||
expect(binPath).toMatch(/mongo-mem-bin-.*\/.*\/mongod$/); | ||
|
||
// reuse cache | ||
expect(MongoBinary.cache[version]).toBeDefined(); | ||
expect(MongoBinary.cache[version]).toEqual(binPath); | ||
const binPathAgain = await MongoBinary.getPath({ | ||
downloadDir: tmpDir.name, | ||
version, | ||
}); | ||
|
||
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); | ||
}); | ||
expect(binPathAgain).toEqual(binPath); | ||
}); | ||
|
||
// cleanup | ||
tmpDir.removeCallback(); | ||
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'); | ||
}); | ||
}); | ||
|
||
it('should use cache', async () => { | ||
MongoBinary.cache['3.4.2'] = '/bin/mongod'; | ||
await expect(MongoBinary.getPath({ version: '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(); | ||
}); | ||
}); | ||
}); |