Skip to content

Commit 6c81035

Browse files
committed
test: remove usage of "tmp" package
1 parent 6911daf commit 6c81035

File tree

9 files changed

+123
-164
lines changed

9 files changed

+123
-164
lines changed

packages/mongodb-memory-server-core/src/__tests__/MongoMemoryReplSet-restart.test.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import MongoMemoryReplSet, { MongoMemoryReplSetOpts } from '../MongoMemoryReplSet';
2-
import * as tmp from 'tmp';
2+
import { createTmpDir, removeDir } from '../util/utils';
33

4-
tmp.setGracefulCleanup();
5-
let tmpDir: tmp.DirResult;
6-
beforeEach(() => {
7-
tmpDir = tmp.dirSync({ prefix: 'reuse-mongo-mem-', unsafeCleanup: true });
4+
let tmpDir: string;
5+
beforeEach(async () => {
6+
tmpDir = await createTmpDir('reuse-mongo-mem-');
87
});
98

10-
afterEach(() => {
11-
tmpDir.removeCallback();
9+
afterEach(async () => {
10+
await removeDir(tmpDir);
1211
});
1312

1413
const sleep = (ms: number) => {
@@ -24,7 +23,7 @@ describe('Restart single MongoMemoryReplSet instance', () => {
2423
instanceOpts: [
2524
{
2625
port: 27017,
27-
dbPath: tmpDir.name,
26+
dbPath: tmpDir,
2827
},
2928
],
3029
} as MongoMemoryReplSetOpts;

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
22
import { MongoClient, MongoServerError } from 'mongodb';
3-
import * as tmp from 'tmp';
43
import MongoMemoryServer, {
54
CreateUser,
65
MongoInstanceData,
@@ -15,7 +14,6 @@ import { assertIsError } from './testUtils/test_utils';
1514
import { promises as fspromises } from 'fs';
1615
import * as path from 'path';
1716

18-
tmp.setGracefulCleanup();
1917
jest.setTimeout(100000); // 10s
2018

2119
afterEach(() => {
@@ -770,7 +768,7 @@ describe('MongoMemoryServer', () => {
770768

771769
describe('cleanup()', () => {
772770
/** Cleanup the created tmp-dir, even if the cleanup test tested to not clean it up */
773-
let tmpdir: tmp.DirResult | undefined;
771+
let tmpdir: string | undefined;
774772

775773
// "beforeAll" dosnt work here, thanks to the top-level "afterAll" hook
776774
beforeEach(() => {
@@ -779,9 +777,9 @@ describe('MongoMemoryServer', () => {
779777
jest.spyOn(semver.default, 'lt'); // it needs to be ".default" otherwise "lt" is only an getter
780778
});
781779

782-
afterEach(() => {
780+
afterEach(async () => {
783781
if (!utils.isNullOrUndefined(tmpdir)) {
784-
tmpdir.removeCallback();
782+
await utils.removeDir(tmpdir);
785783

786784
tmpdir = undefined; // reset, just to be sure its clean
787785
}
@@ -820,8 +818,8 @@ describe('MongoMemoryServer', () => {
820818
});
821819

822820
it('should properly cleanup with force (without tmpDir) (old)', async () => {
823-
const tmpDir = tmp.dirSync({ prefix: 'mongo-mem-cleanup-', unsafeCleanup: true });
824-
const mongoServer = await MongoMemoryServer.create({ instance: { dbPath: tmpDir.name } });
821+
const tmpDir = await utils.createTmpDir('mongo-mem-cleanup-');
822+
const mongoServer = await MongoMemoryServer.create({ instance: { dbPath: tmpDir } });
825823
const dbPath = mongoServer.instanceInfo!.dbPath;
826824

827825
tmpdir = mongoServer.instanceInfo?.tmpDir;
@@ -873,8 +871,8 @@ describe('MongoMemoryServer', () => {
873871
});
874872

875873
it('should properly cleanup with force (without tmpDir) (new)', async () => {
876-
const tmpDir = tmp.dirSync({ prefix: 'mongo-mem-cleanup-', unsafeCleanup: true });
877-
const mongoServer = await MongoMemoryServer.create({ instance: { dbPath: tmpDir.name } });
874+
const tmpDir = await utils.createTmpDir('mongo-mem-cleanup-');
875+
const mongoServer = await MongoMemoryServer.create({ instance: { dbPath: tmpDir } });
878876
const dbPath = mongoServer.instanceInfo!.dbPath;
879877

880878
const cleanupSpy = jest.spyOn(mongoServer, 'cleanup');
@@ -926,10 +924,10 @@ describe('MongoMemoryServer', () => {
926924

927925
it('should resolve "isNew" to "true" and set "createAuth" to "true" when dbPath is set, but empty', async () => {
928926
const readdirSpy = jest.spyOn(fspromises, 'readdir');
929-
const tmpDbPath = tmp.dirSync({ prefix: 'mongo-mem-getStartOptions1-', unsafeCleanup: true });
927+
const tmpDbPath = await utils.createTmpDir('mongo-mem-getStartOptions1-');
930928

931929
const mongoServer = new MongoMemoryServer({
932-
instance: { dbPath: tmpDbPath.name },
930+
instance: { dbPath: tmpDbPath },
933931
auth: {},
934932
});
935933

@@ -942,22 +940,22 @@ describe('MongoMemoryServer', () => {
942940
new Error('Expected "options.data.dbPath" to be defined')
943941
);
944942
expect(await utils.pathExists(options.data.dbPath)).toEqual(true);
945-
expect(options.data.dbPath).toEqual(tmpDbPath.name);
943+
expect(options.data.dbPath).toEqual(tmpDbPath);
946944
expect(readdirSpy).toHaveBeenCalledTimes(1);
947945
expect(options.createAuth).toEqual(true);
948946

949-
tmpDbPath.removeCallback();
947+
await utils.removeDir(tmpDbPath);
950948
});
951949

952950
it('should resolve "isNew" to "false" and set "createAuth" to "false" when dbPath is set, but not empty', async () => {
953951
const readdirSpy = jest.spyOn(fspromises, 'readdir');
954-
const tmpDbPath = tmp.dirSync({ prefix: 'mongo-mem-getStartOptions1-', unsafeCleanup: true });
952+
const tmpDbPath = await utils.createTmpDir('mongo-mem-getStartOptions2-');
955953

956954
// create dummy file, to make the directory non-empty
957-
await fspromises.writeFile(path.resolve(tmpDbPath.name, 'testfile'), '');
955+
await fspromises.writeFile(path.resolve(tmpDbPath, 'testfile'), '');
958956

959957
const mongoServer = new MongoMemoryServer({
960-
instance: { dbPath: tmpDbPath.name },
958+
instance: { dbPath: tmpDbPath },
961959
auth: {},
962960
});
963961

@@ -970,11 +968,11 @@ describe('MongoMemoryServer', () => {
970968
new Error('Expected "options.data.dbPath" to be defined')
971969
);
972970
expect(await utils.pathExists(options.data.dbPath)).toEqual(true);
973-
expect(options.data.dbPath).toEqual(tmpDbPath.name);
971+
expect(options.data.dbPath).toEqual(tmpDbPath);
974972
expect(readdirSpy).toHaveBeenCalledTimes(1);
975973
expect(options.createAuth).toEqual(false);
976974

977-
tmpDbPath.removeCallback();
975+
await utils.removeDir(tmpDbPath);
978976
});
979977

980978
it('should generate a port when no suggestion is defined', async () => {
@@ -1027,17 +1025,17 @@ describe('MongoMemoryServer', () => {
10271025
});
10281026

10291027
it('"getDbPath" should return the dbPath', async () => {
1030-
const tmpDir = tmp.dirSync({ prefix: 'mongo-mem-getDbPath-', unsafeCleanup: true });
1028+
const tmpDir = await utils.createTmpDir('mongo-mem-getDbPath-');
10311029
const mongoServer = new MongoMemoryServer({
1032-
instance: { dbPath: tmpDir.name },
1030+
instance: { dbPath: tmpDir },
10331031
});
10341032

10351033
await mongoServer.start();
10361034

1037-
expect(mongoServer.instanceInfo!.dbPath).toEqual(tmpDir.name);
1035+
expect(mongoServer.instanceInfo!.dbPath).toEqual(tmpDir);
10381036

10391037
await mongoServer.stop();
1040-
tmpDir.removeCallback();
1038+
await utils.removeDir(tmpDir);
10411039
});
10421040

10431041
it('"state" should return correct state', () => {

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as tmp from 'tmp';
21
import os from 'os';
32
import MongoBinary, { MongoBinaryOpts } from '../MongoBinary';
43
import MongoBinaryDownload from '../MongoBinaryDownload';
@@ -8,8 +7,6 @@ import { DryMongoBinary } from '../DryMongoBinary';
87
import * as childProcess from 'child_process';
98
import { assertIsError } from '../../__tests__/testUtils/test_utils';
109

11-
tmp.setGracefulCleanup();
12-
1310
const mockedPath = '/path/to/binary';
1411
const mockGetMongodPath = jest.fn().mockResolvedValue(mockedPath);
1512

@@ -22,16 +19,16 @@ jest.mock('../MongoBinaryDownload', () => {
2219
jest.mock('child_process');
2320

2421
describe('MongoBinary', () => {
25-
let tmpDir: tmp.DirResult;
22+
let tmpDir: string;
2623

27-
beforeEach(() => {
28-
tmpDir = tmp.dirSync({ prefix: 'mongo-mem-bin-', unsafeCleanup: true });
24+
beforeEach(async () => {
25+
tmpDir = await utils.createTmpDir('mongo-mem-bin-');
2926
DryMongoBinary.binaryCache.clear();
3027
});
3128

3229
// cleanup
33-
afterEach(() => {
34-
tmpDir.removeCallback();
30+
afterEach(async () => {
31+
await utils.removeDir(tmpDir);
3532
jest.restoreAllMocks(); // restore all mocked functions to original
3633
DryMongoBinary.binaryCache.clear();
3734
delete process.env[envName(ResolveConfigVariables.RUNTIME_DOWNLOAD)];
@@ -42,7 +39,7 @@ describe('MongoBinary', () => {
4239
const version = resolveConfig(ResolveConfigVariables.VERSION);
4340
utils.assertion(typeof version === 'string', new Error('Expected "version" to be an string'));
4441
const binPath = await MongoBinary.download({
45-
downloadDir: tmpDir.name,
42+
downloadDir: tmpDir,
4643
version,
4744
arch: 'x64',
4845
platform: 'linux',
@@ -51,7 +48,7 @@ describe('MongoBinary', () => {
5148

5249
// eg. /tmp/mongo-mem-bin-33990ScJTSRNSsFYf/mongodb-download/a811facba94753a2eba574f446561b7e/mongodb-macOS-x86_64-3.5.5-13-g00ee4f5/
5350
expect(MongoBinaryDownload).toHaveBeenCalledWith({
54-
downloadDir: tmpDir.name,
51+
downloadDir: tmpDir,
5552
platform: os.platform(),
5653
arch: os.arch(),
5754
version,

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import MongoBinaryDownload from '../MongoBinaryDownload';
88
import MongoBinaryDownloadUrl from '../MongoBinaryDownloadUrl';
99
import { envName, ResolveConfigVariables } from '../resolveConfig';
1010
import * as utils from '../utils';
11-
import * as tmp from 'tmp';
1211
import * as path from 'path';
1312
import * as yazl from 'yazl';
1413
import { pack } from 'tar-stream';
1514
import { createGzip } from 'zlib';
1615

17-
tmp.setGracefulCleanup();
1816
jest.mock('md5-file');
1917

2018
describe('MongoBinaryDownload', () => {
@@ -305,21 +303,18 @@ describe('MongoBinaryDownload', () => {
305303
});
306304

307305
describe('extract()', () => {
308-
let tmpdir: tmp.DirResult;
306+
let tmpdir: string;
309307

310-
beforeEach(() => {
311-
tmpdir = tmp.dirSync({
312-
prefix: 'mongo-mem-test-extract-',
313-
unsafeCleanup: true,
314-
});
308+
beforeEach(async () => {
309+
tmpdir = await utils.createTmpDir('mongo-mem-test-extract-');
315310
});
316-
afterEach(() => {
317-
tmpdir.removeCallback();
311+
afterEach(async () => {
312+
await utils.removeDir(tmpdir);
318313
});
319314

320315
it('should extract zip archives', async () => {
321-
const zipPath = path.join(tmpdir.name, 'archive.zip');
322-
const outPath = path.join(tmpdir.name, 'binary.exe');
316+
const zipPath = path.join(tmpdir, 'archive.zip');
317+
const outPath = path.join(tmpdir, 'binary.exe');
323318
const options: MongoBinaryOpts = {
324319
arch: 'x64',
325320
downloadDir: path.join(outPath, 'downloadDir'),
@@ -368,8 +363,8 @@ describe('MongoBinaryDownload', () => {
368363
});
369364

370365
it('should extract tar.gz archives', async () => {
371-
const tarPath = path.join(tmpdir.name, 'archive.tgz');
372-
const outPath = path.join(tmpdir.name, 'binary');
366+
const tarPath = path.join(tmpdir, 'archive.tgz');
367+
const outPath = path.join(tmpdir, 'binary');
373368
const options: MongoBinaryOpts = {
374369
arch: 'x64',
375370
downloadDir: path.join(outPath, 'downloadDir'),

0 commit comments

Comments
 (0)