4
4
// License text available at https://opensource.org/licenses/MIT
5
5
6
6
import { TestSandbox , expect } from '../..' ;
7
- import { existsSync , readFile as readFile_ } from 'fs' ;
8
7
import { resolve } from 'path' ;
9
8
import { createHash } from 'crypto' ;
10
9
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' ;
14
11
15
12
describe ( 'TestSandbox integration tests' , ( ) => {
16
13
let sandbox : TestSandbox ;
@@ -28,43 +25,43 @@ describe('TestSandbox integration tests', () => {
28
25
beforeEach ( givenPath ) ;
29
26
afterEach ( deleteSandbox ) ;
30
27
31
- it ( 'returns path of sandbox and it exists' , ( ) => {
28
+ it ( 'returns path of sandbox and it exists' , async ( ) => {
32
29
expect ( path ) . to . be . a . String ( ) ;
33
- expect ( existsSync ( path ) ) . to . be . True ( ) ;
30
+ expect ( await pathExists ( path ) ) . to . be . True ( ) ;
34
31
} ) ;
35
32
36
33
it ( 'creates a directory in the sandbox' , async ( ) => {
37
34
const dir = 'controllers' ;
38
35
await sandbox . mkdir ( dir ) ;
39
- expect ( existsSync ( resolve ( path , dir ) ) ) . to . be . True ( ) ;
36
+ expect ( await pathExists ( resolve ( path , dir ) ) ) . to . be . True ( ) ;
40
37
} ) ;
41
38
42
39
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 ( ) ;
45
42
await compareFiles ( resolve ( path , COPY_FILE ) ) ;
46
43
} ) ;
47
44
48
45
it ( 'copies and renames the file to the sandbox' , async ( ) => {
49
46
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 ( ) ;
53
50
await compareFiles ( resolve ( path , rename ) ) ;
54
51
} ) ;
55
52
56
53
it ( 'copies file to a directory' , async ( ) => {
57
54
const dir = 'test' ;
58
55
await sandbox . mkdir ( dir ) ;
59
56
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 ( ) ;
62
59
await compareFiles ( resolve ( path , rename ) ) ;
63
60
} ) ;
64
61
65
62
it ( 'deletes the test sandbox' , async ( ) => {
66
63
await sandbox . delete ( ) ;
67
- expect ( existsSync ( path ) ) . to . be . False ( ) ;
64
+ expect ( await pathExists ( path ) ) . to . be . False ( ) ;
68
65
} ) ;
69
66
70
67
describe ( 'after deleting sandbox' , ( ) => {
@@ -82,7 +79,7 @@ describe('TestSandbox integration tests', () => {
82
79
} ) ;
83
80
84
81
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 ) ;
86
83
} ) ;
87
84
88
85
it ( 'throws an error when trying to call reset()' , async ( ) => {
@@ -111,13 +108,9 @@ describe('TestSandbox integration tests', () => {
111
108
path = sandbox . getPath ( ) ;
112
109
}
113
110
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 ( ) ) ;
121
114
}
122
115
123
116
async function getCopyFileContents ( ) {
0 commit comments