Skip to content

Commit 06cdaaf

Browse files
authored
fix(testlab): switch to fs-extra instead of using fs (#905)
1 parent 72afddd commit 06cdaaf

File tree

4 files changed

+26
-100
lines changed

4 files changed

+26
-100
lines changed

packages/testlab/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@
2121
"license": "MIT",
2222
"dependencies": {
2323
"@loopback/openapi-spec": "^4.0.0-alpha.20",
24-
"@types/rimraf": "^2.0.2",
24+
"@types/fs-extra": "^5.0.0",
2525
"@types/shot": "^3.4.0",
2626
"@types/sinon": "^2.3.7",
2727
"@types/supertest": "^2.0.0",
2828
"@types/swagger-parser": "^4.0.1",
29-
"rimraf": "^2.6.2",
29+
"fs-extra": "^5.0.0",
3030
"shot": "^4.0.3",
3131
"should": "^13.1.3",
3232
"sinon": "^4.1.2",
3333
"supertest": "^3.0.0",
34-
"swagger-parser": "^4.0.1",
35-
"util.promisify": "^1.0.0"
34+
"swagger-parser": "^4.0.1"
3635
},
3736
"devDependencies": {
3837
"@loopback/build": "^4.0.0-alpha.9"

packages/testlab/src/test-sandbox.ts

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,7 @@ import {tmpdir} from 'os';
77
import {createHash} from 'crypto';
88
import {resolve, join, parse} from 'path';
99
import * as util from 'util';
10-
import {mkdirSync, existsSync, mkdir, copyFile, readFile, writeFile} from 'fs';
11-
const promisify = util.promisify || require('util.promisify/implementation');
12-
const rimrafAsync = promisify(require('rimraf'));
13-
const mkdirAsync = promisify(mkdir);
14-
15-
// tslint:disable-next-line:no-any
16-
let copyFileAsync: any;
17-
if (copyFile) {
18-
copyFileAsync = promisify(copyFile);
19-
} else {
20-
/**
21-
* Taranveer: fs.copyFile wasn't made available till Node 8.5.0. As such this
22-
* polyfill is needed for versions of Node prior to that.
23-
*/
24-
copyFileAsync = async function(src: string, target: string) {
25-
const readFileAsync = promisify(readFile);
26-
const writeFileAsync = promisify(writeFile);
27-
const data = await readFileAsync(src);
28-
await writeFileAsync(target, data);
29-
};
30-
}
10+
import {copy, ensureDirSync, emptyDir, remove, ensureDir} from 'fs-extra';
3111

3212
/**
3313
* TestSandbox class provides a convenient way to get a reference to a
@@ -46,18 +26,7 @@ export class TestSandbox {
4626
constructor(path: string) {
4727
// resolve ensures path is absolute / makes it absolute (relative to cwd())
4828
this.path = resolve(path);
49-
// Create directory if it doesn't already exist.
50-
if (!existsSync(this.path)) {
51-
this.create();
52-
}
53-
}
54-
55-
/**
56-
* Syncronously creates the TestSandbox directory. It is syncronous because
57-
* it is called by the constructor.
58-
*/
59-
private create(): void {
60-
mkdirSync(this.path);
29+
ensureDirSync(this.path);
6130
}
6231

6332
/**
@@ -84,16 +53,15 @@ export class TestSandbox {
8453
*/
8554
async reset(): Promise<void> {
8655
this.validateInst();
87-
await rimrafAsync(this.path);
88-
this.create();
56+
await emptyDir(this.path);
8957
}
9058

9159
/**
9260
* Deletes the TestSandbox.
9361
*/
9462
async delete(): Promise<void> {
9563
this.validateInst();
96-
await rimrafAsync(this.path);
64+
await remove(this.path);
9765
delete this.path;
9866
}
9967

@@ -103,7 +71,7 @@ export class TestSandbox {
10371
*/
10472
async mkdir(dir: string): Promise<void> {
10573
this.validateInst();
106-
await mkdirAsync(resolve(this.path, dir));
74+
await ensureDir(resolve(this.path, dir));
10775
}
10876

10977
/**
@@ -112,11 +80,11 @@ export class TestSandbox {
11280
* @param [dest] Optional. Destination filename of the copy operation
11381
* (relative to TestSandbox). Original filename used if not specified.
11482
*/
115-
async copy(src: string, dest?: string): Promise<void> {
83+
async copyFile(src: string, dest?: string): Promise<void> {
11684
this.validateInst();
11785
dest = dest
11886
? resolve(this.path, dest)
11987
: resolve(this.path, parse(src).base);
120-
await copyFileAsync(src, dest);
88+
await copy(src, dest);
12189
}
12290
}

packages/testlab/test/integration/test-sandbox.integration.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
// License text available at https://opensource.org/licenses/MIT
55

66
import {TestSandbox, expect} from '../..';
7-
import {existsSync, readFile as readFile_} from 'fs';
87
import {resolve} from 'path';
98
import {createHash} from 'crypto';
109
import * as util from 'util';
11-
const promisify = util.promisify || require('util.promisify/implementation');
12-
const rimraf = require('rimraf');
13-
const readFile = promisify(readFile_);
10+
import {remove, pathExists, readFile} from 'fs-extra';
1411

1512
describe('TestSandbox integration tests', () => {
1613
let sandbox: TestSandbox;
@@ -28,43 +25,43 @@ describe('TestSandbox integration tests', () => {
2825
beforeEach(givenPath);
2926
afterEach(deleteSandbox);
3027

31-
it('returns path of sandbox and it exists', () => {
28+
it('returns path of sandbox and it exists', async () => {
3229
expect(path).to.be.a.String();
33-
expect(existsSync(path)).to.be.True();
30+
expect(await pathExists(path)).to.be.True();
3431
});
3532

3633
it('creates a directory in the sandbox', async () => {
3734
const dir = 'controllers';
3835
await sandbox.mkdir(dir);
39-
expect(existsSync(resolve(path, dir))).to.be.True();
36+
expect(await pathExists(resolve(path, dir))).to.be.True();
4037
});
4138

4239
it('copies a file to the sandbox', async () => {
43-
await sandbox.copy(COPY_FILE_PATH);
44-
expect(existsSync(resolve(path, COPY_FILE))).to.be.True();
40+
await sandbox.copyFile(COPY_FILE_PATH);
41+
expect(await pathExists(resolve(path, COPY_FILE))).to.be.True();
4542
await compareFiles(resolve(path, COPY_FILE));
4643
});
4744

4845
it('copies and renames the file to the sandbox', async () => {
4946
const rename = 'copy.me.js';
50-
await sandbox.copy(COPY_FILE_PATH, rename);
51-
expect(existsSync(resolve(path, COPY_FILE))).to.be.False();
52-
expect(existsSync(resolve(path, rename))).to.be.True();
47+
await sandbox.copyFile(COPY_FILE_PATH, rename);
48+
expect(await pathExists(resolve(path, COPY_FILE))).to.be.False();
49+
expect(await pathExists(resolve(path, rename))).to.be.True();
5350
await compareFiles(resolve(path, rename));
5451
});
5552

5653
it('copies file to a directory', async () => {
5754
const dir = 'test';
5855
await sandbox.mkdir(dir);
5956
const rename = `${dir}/${COPY_FILE}`;
60-
await sandbox.copy(COPY_FILE_PATH, rename);
61-
expect(existsSync(resolve(path, rename))).to.be.True();
57+
await sandbox.copyFile(COPY_FILE_PATH, rename);
58+
expect(await pathExists(resolve(path, rename))).to.be.True();
6259
await compareFiles(resolve(path, rename));
6360
});
6461

6562
it('deletes the test sandbox', async () => {
6663
await sandbox.delete();
67-
expect(existsSync(path)).to.be.False();
64+
expect(await pathExists(path)).to.be.False();
6865
});
6966

7067
describe('after deleting sandbox', () => {
@@ -82,7 +79,7 @@ describe('TestSandbox integration tests', () => {
8279
});
8380

8481
it('throws an error when trying to call copy()', async () => {
85-
await expect(sandbox.copy(COPY_FILE_PATH)).to.be.rejectedWith(ERR);
82+
await expect(sandbox.copyFile(COPY_FILE_PATH)).to.be.rejectedWith(ERR);
8683
});
8784

8885
it('throws an error when trying to call reset()', async () => {
@@ -111,13 +108,9 @@ describe('TestSandbox integration tests', () => {
111108
path = sandbox.getPath();
112109
}
113110

114-
function deleteSandbox() {
115-
if (!existsSync(path)) return;
116-
try {
117-
rimraf.sync(sandbox.getPath());
118-
} catch (err) {
119-
console.log(`Failed to delete sandbox because: ${err}`);
120-
}
111+
async function deleteSandbox() {
112+
if (!await pathExists(path)) return;
113+
await remove(sandbox.getPath());
121114
}
122115

123116
async function getCopyFileContents() {

packages/testlab/test/unit/test-sandbox.unit.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)