-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest.js
More file actions
183 lines (157 loc) · 4.88 KB
/
test.js
File metadata and controls
183 lines (157 loc) · 4.88 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
/*!
* delete <https://github.com/jonschlinkert/delete>
*
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
require('mocha');
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var del = require('./');
function writeFixtures(names) {
if (typeof names === 'string') names = [names];
for (var i = 0; i < names.length; i++) {
fs.writeFileSync(names[i], 'fixture');
}
}
function exists(names) {
for (var i = 0; i < names.length; i++) {
if (!fs.existsSync(names[i])) {
return false;
}
}
return true;
}
describe('delete:', function() {
afterEach(function() {
assert(!exists(['a.txt', 'b.txt', 'c.txt']));
});
describe('async:', function() {
it('should do nothing when pattern is empty and no cwd is passed', function(cb) {
del('', function(err, files) {
assert.equal(files.length, 0);
cb();
});
});
it('should not delete the cwd:', function(cb) {
del('.', function(err, files) {
assert(err);
assert.equal(files.length, 0);
assert(/force/.test(err.message));
cb();
});
});
it('should not delete parent directories:', function(cb) {
del('../', function(err, files) {
assert(err);
assert.equal(files.length, 0);
assert(/force/.test(err.message));
cb();
});
});
it('should delete a file', function(cb) {
writeFixtures('a.txt');
del('a.txt', function(err, files) {
assert.ifError(err);
assert.equal(files.length, 1);
cb();
});
});
it('should delete an array of files', function(cb) {
writeFixtures(['a.txt', 'b.txt']);
assert(exists(['a.txt', 'b.txt']));
del(['a.txt', 'b.txt'], cb);
});
it('should add deleted files to callback', function(cb) {
writeFixtures(['a.txt', 'b.txt']);
assert(exists(['a.txt', 'b.txt']));
del(['a.txt', 'b.txt'], function(err, files) {
assert.ifError(err);
assert.equal(files[0], path.resolve('a.txt'));
assert.equal(files[1], path.resolve('b.txt'));
cb();
});
});
it('should not add files that did not exist', function(cb) {
writeFixtures(['a.txt', 'b.txt']);
assert(exists(['a.txt', 'b.txt']));
del(['a.txt', 'b.txt', 'c.txt'], function(err, files) {
assert.ifError(err);
assert.equal(files.length, 2);
assert.equal(files[0], path.resolve('a.txt'));
assert.equal(files[1], path.resolve('b.txt'));
cb();
});
});
it('should delete a glob of files asynchronously', function(cb) {
writeFixtures(['a.txt', 'b.txt', 'c.txt']);
assert(exists(['a.txt', 'b.txt', 'c.txt']));
del(['*.txt'], function(err) {
assert.ifError(err);
assert(!fs.existsSync('a.txt'));
cb();
});
});
});
describe('sync:', function() {
it('should not delete the current working directory', function() {
assert.throws(function() {
del.sync('.');
});
});
it('should not delete parent directories', function() {
assert.throws(function() {
del.sync('../');
});
});
it('should delete files synchronously', function() {
writeFixtures(['a.txt']);
assert(exists(['a.txt']));
del.sync('a.txt');
});
it('should delete a glob of files synchronously', function() {
writeFixtures(['a.txt', 'b.txt', 'c.txt']);
assert(exists(['a.txt', 'b.txt', 'c.txt']));
del.sync(['*.txt']);
});
it('should return an array of deleted files', function() {
writeFixtures(['a.txt', 'b.txt', 'c.txt']);
assert(exists(['a.txt', 'b.txt', 'c.txt']));
var deleted = del.sync(['*.txt']);
assert.equal(deleted.length, 3);
});
});
describe('promise:', function() {
it('should do nothing when pattern is an empty string', function() {
return del('')
.then(function(files) {
assert.equal(files.length, 0);
});
});
it('should delete files with `del.promise`', function() {
writeFixtures(['a.txt']);
assert(exists(['a.txt']));
return del.promise('a.txt');
});
it('should delete a glob of files with `del.promise`', function() {
writeFixtures(['a.txt', 'b.txt', 'c.txt']);
assert(exists(['a.txt', 'b.txt', 'c.txt']));
return del.promise(['*.txt']);
});
it('should return list of deleted files', function() {
writeFixtures(['a.txt', 'b.txt', 'c.txt']);
assert(exists(['a.txt', 'b.txt', 'c.txt']));
return del(['*.txt'])
.then(function(files) {
assert.equal(files.length, 3);
})
});
it('should use the promise method when no callback is given', function() {
writeFixtures(['a.txt', 'b.txt', 'c.txt']);
assert(exists(['a.txt', 'b.txt', 'c.txt']));
return del(['*.txt']);
});
});
});