@util.js/node-files 0.41.5 → 0.41.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Files.js +16 -2
- package/package.json +8 -4
- package/test/FilesTest.js +0 -1134
- package/test/Files_sanitizeFilename_Test.js +0 -24
package/lib/Files.js
CHANGED
|
@@ -92,8 +92,22 @@ module.exports = class Files {
|
|
|
92
92
|
return path().dirname(...arguments);
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
exists() {
|
|
96
|
-
|
|
95
|
+
exists(path, callback) {
|
|
96
|
+
if (callback) {
|
|
97
|
+
return fs().exists(path, callback);
|
|
98
|
+
}
|
|
99
|
+
return new Promise((resolve, reject) => {
|
|
100
|
+
// From https://nodejs.org/api/fs.html#fsexistspath-callback:
|
|
101
|
+
// The parameters for this callback are not consistent with other Node.js callbacks.
|
|
102
|
+
// Normally, the first parameter to a Node.js callback is an err parameter, optionally
|
|
103
|
+
// followed by other parameters. The fs.exists() callback has only one boolean
|
|
104
|
+
// parameter.
|
|
105
|
+
try {
|
|
106
|
+
fs().exists(path, resolve);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
reject(err);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
97
111
|
}
|
|
98
112
|
|
|
99
113
|
existsSync() {
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Chris Topher <c@creemama.com>",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@util.js/objects": "0.41.
|
|
5
|
-
"@util.js/promises": "0.41.
|
|
6
|
-
"@util.js/strings": "0.41.
|
|
4
|
+
"@util.js/objects": "0.41.7",
|
|
5
|
+
"@util.js/promises": "0.41.7",
|
|
6
|
+
"@util.js/strings": "0.41.7",
|
|
7
7
|
"graceful-fs": "4.2.11",
|
|
8
8
|
"mkdirp": "3.0.1",
|
|
9
9
|
"ncp": "2.0.0",
|
|
@@ -16,6 +16,10 @@
|
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"async-waterfall": "0.1.5"
|
|
18
18
|
},
|
|
19
|
+
"files": [
|
|
20
|
+
"index.js",
|
|
21
|
+
"lib"
|
|
22
|
+
],
|
|
19
23
|
"keywords": [
|
|
20
24
|
"file",
|
|
21
25
|
"file system",
|
|
@@ -38,5 +42,5 @@
|
|
|
38
42
|
"type": "git",
|
|
39
43
|
"url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-node-files"
|
|
40
44
|
},
|
|
41
|
-
"version": "0.41.
|
|
45
|
+
"version": "0.41.7"
|
|
42
46
|
}
|
package/test/FilesTest.js
DELETED
|
@@ -1,1134 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { assert, expect } = require("chai");
|
|
4
|
-
const files = require("..");
|
|
5
|
-
|
|
6
|
-
describe("Files", function () {
|
|
7
|
-
const waterfall = require("async-waterfall");
|
|
8
|
-
const targetDir = files.join(__dirname, "..", "target");
|
|
9
|
-
const emptyDir = targetDir + "/emptyDir";
|
|
10
|
-
const jsonDir = targetDir + "/filesWithExtension";
|
|
11
|
-
files.mkdirpSync(targetDir);
|
|
12
|
-
files.mkdirpSync(emptyDir);
|
|
13
|
-
|
|
14
|
-
async function prepareDirWithJsonFiles() {
|
|
15
|
-
await files.mkdirp(jsonDir);
|
|
16
|
-
await files.touch(jsonDir + "/.DS_Store");
|
|
17
|
-
await files.touch(jsonDir + "/.gitignore");
|
|
18
|
-
await files.mkdirp(jsonDir + "/json");
|
|
19
|
-
await files.mkdirp(jsonDir + "/lib");
|
|
20
|
-
await files.mkdirp(jsonDir + "/node_modules");
|
|
21
|
-
await files.touch(jsonDir + "/package.json");
|
|
22
|
-
await files.mkdirp(jsonDir + "/public");
|
|
23
|
-
await files.touch(jsonDir + "/server.json");
|
|
24
|
-
await files.mkdirp(jsonDir + "/target");
|
|
25
|
-
await files.touch(jsonDir + "/tasks.JSON");
|
|
26
|
-
await files.mkdirp(jsonDir + "/test");
|
|
27
|
-
await files.mkdirp(jsonDir + "/views");
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
describe("#readLastLines()", () => {
|
|
31
|
-
it("should read the last n lines of a file", async function () {
|
|
32
|
-
let lines;
|
|
33
|
-
|
|
34
|
-
lines = await files.readLastLines(__filename, 3);
|
|
35
|
-
expect(lines).to.eql(" });\n });\n});\n");
|
|
36
|
-
|
|
37
|
-
lines = await files.readLastLines(__filename, 3, "utf8");
|
|
38
|
-
expect(lines).to.eql(" });\n });\n});\n");
|
|
39
|
-
|
|
40
|
-
lines = await files.readLastLines(__filename, 3, "buffer");
|
|
41
|
-
expect(lines).to.be.an.instanceof(Buffer);
|
|
42
|
-
expect(lines.toString("utf8")).to.eql(" });\n });\n});\n");
|
|
43
|
-
|
|
44
|
-
if (global.URL) {
|
|
45
|
-
lines = await files.readLastLines(
|
|
46
|
-
new URL("file://" + __filename),
|
|
47
|
-
3,
|
|
48
|
-
"utf8",
|
|
49
|
-
);
|
|
50
|
-
expect(lines).to.eql(" });\n });\n});\n");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
lines = await files.readLastLines(
|
|
54
|
-
Buffer.from(__filename, "utf8"),
|
|
55
|
-
3,
|
|
56
|
-
"utf8",
|
|
57
|
-
);
|
|
58
|
-
expect(lines).to.eql(" });\n });\n});\n");
|
|
59
|
-
|
|
60
|
-
lines = await files.readLastLines(
|
|
61
|
-
Buffer.from(__filename, "utf8"),
|
|
62
|
-
3,
|
|
63
|
-
Buffer.from("utf8", "utf8"),
|
|
64
|
-
);
|
|
65
|
-
expect(lines).to.eql(" });\n });\n});\n");
|
|
66
|
-
});
|
|
67
|
-
it("should read the last n lines of a file without an encoding and with a callback", (callback) => {
|
|
68
|
-
files.readLastLines(__filename, 3, (error, lines) => {
|
|
69
|
-
if (error) {
|
|
70
|
-
callback(error);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
try {
|
|
74
|
-
expect(lines).to.eql(" });\n });\n});\n");
|
|
75
|
-
callback();
|
|
76
|
-
} catch (e) {
|
|
77
|
-
callback(e);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
it("should read the last n lines of a file with an encoding and with a callback", (callback) => {
|
|
82
|
-
files.readLastLines(__filename, 3, "utf8", (error, lines) => {
|
|
83
|
-
if (error) {
|
|
84
|
-
callback(error);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
try {
|
|
88
|
-
expect(lines).to.eql(" });\n });\n});\n");
|
|
89
|
-
callback();
|
|
90
|
-
} catch (e) {
|
|
91
|
-
callback(e);
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
it("should read the last n lines with weird maxLineCounts", async function () {
|
|
96
|
-
let lines;
|
|
97
|
-
|
|
98
|
-
const src = targetDir + "/readLastLines.txt";
|
|
99
|
-
await files.mkdirp(targetDir);
|
|
100
|
-
await files.writeFile(src, "a", "utf8");
|
|
101
|
-
|
|
102
|
-
lines = await files.readLastLines(src, Buffer.from("3", "utf8"));
|
|
103
|
-
expect(lines).to.eql("a");
|
|
104
|
-
|
|
105
|
-
lines = await files.readLastLines(src, null);
|
|
106
|
-
expect(lines).to.eql("");
|
|
107
|
-
|
|
108
|
-
lines = await files.readLastLines(src, false);
|
|
109
|
-
expect(lines).to.eql("");
|
|
110
|
-
|
|
111
|
-
lines = await files.readLastLines(src, undefined);
|
|
112
|
-
expect(lines).to.eql("a");
|
|
113
|
-
|
|
114
|
-
lines = await files.readLastLines(src, "5");
|
|
115
|
-
expect(lines).to.eql("a");
|
|
116
|
-
|
|
117
|
-
lines = await files.readLastLines(src, "0");
|
|
118
|
-
expect(lines).to.eql("");
|
|
119
|
-
|
|
120
|
-
lines = await files.readLastLines(src, files);
|
|
121
|
-
expect(lines).to.eql("a");
|
|
122
|
-
|
|
123
|
-
lines = await files.readLastLines(src, Number.NEGATIVE_INFINITY);
|
|
124
|
-
expect(lines).to.eql("");
|
|
125
|
-
|
|
126
|
-
lines = await files.readLastLines(__filename, -1);
|
|
127
|
-
expect(lines).to.eql("");
|
|
128
|
-
|
|
129
|
-
lines = await files.readLastLines(__filename, 0);
|
|
130
|
-
expect(lines).to.eql("");
|
|
131
|
-
|
|
132
|
-
lines = await files.readLastLines(__filename, 2.3);
|
|
133
|
-
expect(lines).to.eql(" });\n });\n});\n");
|
|
134
|
-
|
|
135
|
-
lines = await files.readLastLines(src, Number.POSITIVE_INFINITY);
|
|
136
|
-
expect(lines).to.eql("a");
|
|
137
|
-
|
|
138
|
-
lines = await files.readLastLines(src, NaN);
|
|
139
|
-
expect(lines).to.eql("a");
|
|
140
|
-
});
|
|
141
|
-
it("should read the last n lines with weird encodings", async function () {
|
|
142
|
-
const lines = await files.readLastLines(__filename, 3, null);
|
|
143
|
-
expect(lines).to.eql(" });\n });\n});\n");
|
|
144
|
-
});
|
|
145
|
-
it("should fail if the path argument is invalid", async function () {
|
|
146
|
-
// The method fails if a URL as a string, not a URL object.
|
|
147
|
-
await files
|
|
148
|
-
.readLastLines("file://" + __filename, 3)
|
|
149
|
-
.then((lines) => assert.fail())
|
|
150
|
-
.catch((error) => expect(error.message).to.eql("file does not exist"));
|
|
151
|
-
|
|
152
|
-
// The method fails if a URL does not use the file scheme.
|
|
153
|
-
await files
|
|
154
|
-
.readLastLines("http://www.google.com", 3)
|
|
155
|
-
.then((lines) => assert.fail())
|
|
156
|
-
.catch((error) => expect(error.message).to.eql("file does not exist"));
|
|
157
|
-
if (global.URL)
|
|
158
|
-
await files
|
|
159
|
-
.readLastLines(new URL("http://www.google.com"), 3)
|
|
160
|
-
.then((lines) => assert.fail())
|
|
161
|
-
.catch((error) =>
|
|
162
|
-
expect(error.message).to.eql("The URL must be of scheme file"),
|
|
163
|
-
);
|
|
164
|
-
|
|
165
|
-
// The method fails if path is a directory.
|
|
166
|
-
await files
|
|
167
|
-
.readLastLines(__dirname, 3)
|
|
168
|
-
.then((lines) => assert.fail())
|
|
169
|
-
.catch((error) =>
|
|
170
|
-
expect(error.message).to.eql(
|
|
171
|
-
"EISDIR: illegal operation on a directory, read",
|
|
172
|
-
),
|
|
173
|
-
);
|
|
174
|
-
|
|
175
|
-
// The method fails if path is one of the primitive types other than string.
|
|
176
|
-
await files
|
|
177
|
-
.readLastLines(true, 3)
|
|
178
|
-
.then((lines) => assert.fail())
|
|
179
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
180
|
-
await files
|
|
181
|
-
.readLastLines(null, 3)
|
|
182
|
-
.then((lines) => assert.fail())
|
|
183
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
184
|
-
await files
|
|
185
|
-
.readLastLines(undefined, 3)
|
|
186
|
-
.then((lines) => assert.fail())
|
|
187
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
188
|
-
await files
|
|
189
|
-
.readLastLines(1, 3)
|
|
190
|
-
.then((lines) => assert.fail())
|
|
191
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
192
|
-
await files
|
|
193
|
-
.readLastLines(Symbol(__filename), 3)
|
|
194
|
-
.then((lines) => assert.fail())
|
|
195
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
196
|
-
});
|
|
197
|
-
it("should fail with a callback if the path argument is invalid", (callback) => {
|
|
198
|
-
files.readLastLines(__dirname, 3, (error, lines) => {
|
|
199
|
-
try {
|
|
200
|
-
expect(error.message).to.eql(
|
|
201
|
-
"EISDIR: illegal operation on a directory, read",
|
|
202
|
-
);
|
|
203
|
-
callback();
|
|
204
|
-
} catch (e) {
|
|
205
|
-
callback(e);
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
it("should fail if the maxLineCount is invalid", async function () {
|
|
210
|
-
await files
|
|
211
|
-
.readLastLines(__filename, Symbol("5"))
|
|
212
|
-
.then((lines) => assert.fail())
|
|
213
|
-
.catch((error) =>
|
|
214
|
-
expect(error.message).to.eql(
|
|
215
|
-
"Cannot convert a Symbol value to a number",
|
|
216
|
-
),
|
|
217
|
-
);
|
|
218
|
-
});
|
|
219
|
-
it("should fail if the encoding is invalid", async function () {
|
|
220
|
-
await files
|
|
221
|
-
.readLastLines(__filename, 3, false)
|
|
222
|
-
.then((lines) => assert.fail())
|
|
223
|
-
.catch((error) =>
|
|
224
|
-
expect(error.message).to.eql("Unknown encoding: false"),
|
|
225
|
-
);
|
|
226
|
-
|
|
227
|
-
await files
|
|
228
|
-
.readLastLines(__filename, 3, NaN)
|
|
229
|
-
.then((lines) => assert.fail())
|
|
230
|
-
.catch((error) =>
|
|
231
|
-
expect(error.message).to.eql("Unknown encoding: NaN"),
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
await files
|
|
235
|
-
.readLastLines(__filename, 3, 0)
|
|
236
|
-
.then((lines) => assert.fail())
|
|
237
|
-
.catch((error) => expect(error.message).to.eql("Unknown encoding: 0"));
|
|
238
|
-
|
|
239
|
-
await files
|
|
240
|
-
.readLastLines(__filename, 3, "")
|
|
241
|
-
.then((lines) => assert.fail())
|
|
242
|
-
.catch((error) => expect(error.message).to.eql("Unknown encoding: "));
|
|
243
|
-
|
|
244
|
-
await files
|
|
245
|
-
.readLastLines(__filename, 3, "frog")
|
|
246
|
-
.then((lines) => assert.fail())
|
|
247
|
-
.catch((error) =>
|
|
248
|
-
expect(error.message).to.eql("Unknown encoding: frog"),
|
|
249
|
-
);
|
|
250
|
-
|
|
251
|
-
await files
|
|
252
|
-
.readLastLines(__filename, 3, Symbol("utf8"))
|
|
253
|
-
.then((lines) => assert.fail())
|
|
254
|
-
.catch((error) =>
|
|
255
|
-
expect(error.message).to.eql(
|
|
256
|
-
"Cannot convert a Symbol value to a string",
|
|
257
|
-
),
|
|
258
|
-
);
|
|
259
|
-
});
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
describe("#cp(src, dest)", function () {
|
|
263
|
-
it("should successfully copy a file", async function () {
|
|
264
|
-
const src = targetDir + "/cp.txt";
|
|
265
|
-
const dest = targetDir + "/cp2.txt";
|
|
266
|
-
await files.rmrf(src);
|
|
267
|
-
await files.rmrf(dest);
|
|
268
|
-
await files.mkdirp(targetDir);
|
|
269
|
-
await files.writeFile(src, "cp", "utf8");
|
|
270
|
-
await files.cp(src, dest);
|
|
271
|
-
expect(await files.readFile(dest, "utf8")).to.eql("cp");
|
|
272
|
-
await files.rmrf(src);
|
|
273
|
-
await files.rmrf(dest);
|
|
274
|
-
});
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
describe("#cpr(src, dest)", function () {
|
|
278
|
-
it("should successfully copy a directory", async function () {
|
|
279
|
-
const src = targetDir + "/cprA";
|
|
280
|
-
const src0 = src + "/0.txt";
|
|
281
|
-
const src1 = src + "/1.txt";
|
|
282
|
-
const src2 = src + "/2.txt";
|
|
283
|
-
const dest = targetDir + "/cprB";
|
|
284
|
-
const dest0 = dest + "/0.txt";
|
|
285
|
-
await files.rmrf(src);
|
|
286
|
-
await files.rmrf(dest);
|
|
287
|
-
await files.mkdirp(src);
|
|
288
|
-
await files.mkdirp(dest);
|
|
289
|
-
await files.writeFile(src0, "cp", "utf8");
|
|
290
|
-
await files.touch(src1);
|
|
291
|
-
await files.touch(src2);
|
|
292
|
-
await files.cpr(src, dest);
|
|
293
|
-
expect(await files.readFile(dest0, "utf8")).to.eql("cp");
|
|
294
|
-
expect(await files.readdir(dest)).to.eql(["0.txt", "1.txt", "2.txt"]);
|
|
295
|
-
await files.rmrf(src);
|
|
296
|
-
await files.rmrf(dest);
|
|
297
|
-
});
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
describe("#diff(pathA, pathB, cb)", function () {
|
|
301
|
-
it("should error if pathA is undefined", (callback) => {
|
|
302
|
-
let pathA;
|
|
303
|
-
files.diff(pathA, "/not/a/path", (error, result) => {
|
|
304
|
-
expect(error).to.be.an.instanceof(TypeError);
|
|
305
|
-
callback();
|
|
306
|
-
});
|
|
307
|
-
});
|
|
308
|
-
it("should error if pathA is null", (callback) => {
|
|
309
|
-
files.diff(null, "/not/a/path", (error, result) => {
|
|
310
|
-
expect(error).to.be.an.instanceof(TypeError);
|
|
311
|
-
callback();
|
|
312
|
-
});
|
|
313
|
-
});
|
|
314
|
-
it("should error if pathB is undefined", (callback) => {
|
|
315
|
-
let pathB;
|
|
316
|
-
files.diff("/not/a/path", pathB, (error, result) => {
|
|
317
|
-
expect(error).to.be.an.instanceof(TypeError);
|
|
318
|
-
callback();
|
|
319
|
-
});
|
|
320
|
-
});
|
|
321
|
-
it("should error if pathB is null", (callback) => {
|
|
322
|
-
files.diff("/not/a/path", null, (error, result) => {
|
|
323
|
-
expect(error).to.be.an.instanceof(TypeError);
|
|
324
|
-
callback();
|
|
325
|
-
});
|
|
326
|
-
});
|
|
327
|
-
it("should return false if neither path exists", (callback) => {
|
|
328
|
-
files.diff("/not/a/path", "/not/a/path", (error, result) => {
|
|
329
|
-
expect(error).to.be.null;
|
|
330
|
-
expect(result).to.be.false;
|
|
331
|
-
callback();
|
|
332
|
-
});
|
|
333
|
-
});
|
|
334
|
-
it("should return true if both paths are the same directory", (callback) => {
|
|
335
|
-
files.diff(__dirname, __dirname, (error, result) => {
|
|
336
|
-
expect(error).to.be.null;
|
|
337
|
-
expect(result).to.be.ok;
|
|
338
|
-
callback();
|
|
339
|
-
});
|
|
340
|
-
});
|
|
341
|
-
it("should return true if both paths are the same file", (callback) => {
|
|
342
|
-
files.diff(__filename, __filename, (error, result) => {
|
|
343
|
-
expect(error).to.be.null;
|
|
344
|
-
expect(result).to.be.ok;
|
|
345
|
-
callback();
|
|
346
|
-
});
|
|
347
|
-
});
|
|
348
|
-
it("should resolve to true if both paths are the same file", async function () {
|
|
349
|
-
expect(await files.diff(__filename, __filename)).to.be.ok;
|
|
350
|
-
});
|
|
351
|
-
it("should return false if both paths are different files", (callback) => {
|
|
352
|
-
const pathA = files.join(__dirname, "FilesTest.js");
|
|
353
|
-
const pathB = files.join(__dirname, "..", "index.js");
|
|
354
|
-
expect(files.isFile(pathA)).to.be.ok;
|
|
355
|
-
expect(files.isFile(pathB)).to.be.ok;
|
|
356
|
-
files.diff(pathA, pathB, (error, result) => {
|
|
357
|
-
expect(error).to.be.null;
|
|
358
|
-
expect(result).to.be.false;
|
|
359
|
-
callback();
|
|
360
|
-
});
|
|
361
|
-
});
|
|
362
|
-
it("should return false if both paths are different directories", (callback) => {
|
|
363
|
-
files.diff(__dirname, files.join(__dirname, ".."), (error, result) => {
|
|
364
|
-
expect(error).to.be.null;
|
|
365
|
-
expect(result).to.be.false;
|
|
366
|
-
callback();
|
|
367
|
-
});
|
|
368
|
-
});
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
describe("#filesWithExtension()", () => {
|
|
372
|
-
it("should throw a TypeError if caller does not specify any arguments", () => {
|
|
373
|
-
return files
|
|
374
|
-
.filesWithExtension()
|
|
375
|
-
.then(() => expect.fail())
|
|
376
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
377
|
-
});
|
|
378
|
-
it("should error if a callback is specified and caller does not specify any arguments", (done) => {
|
|
379
|
-
files.filesWithExtension(null, (error) => {
|
|
380
|
-
try {
|
|
381
|
-
expect(error).to.be.an.instanceof(TypeError);
|
|
382
|
-
done();
|
|
383
|
-
} catch (err) {
|
|
384
|
-
done(err);
|
|
385
|
-
}
|
|
386
|
-
});
|
|
387
|
-
});
|
|
388
|
-
it("should error if params is empty", () => {
|
|
389
|
-
return files
|
|
390
|
-
.filesWithExtension({})
|
|
391
|
-
.then(() => expect.fail())
|
|
392
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
393
|
-
});
|
|
394
|
-
it("should error if callback is specified and params is empty", (done) => {
|
|
395
|
-
files.filesWithExtension({}, (error) => {
|
|
396
|
-
try {
|
|
397
|
-
expect(error).to.be.an.instanceof(TypeError);
|
|
398
|
-
done();
|
|
399
|
-
} catch (err) {
|
|
400
|
-
done(err);
|
|
401
|
-
}
|
|
402
|
-
});
|
|
403
|
-
});
|
|
404
|
-
it("should error if dir and ext are null", () => {
|
|
405
|
-
return files
|
|
406
|
-
.filesWithExtension({ dir: null, ext: null })
|
|
407
|
-
.then(() => expect.fail())
|
|
408
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
409
|
-
});
|
|
410
|
-
it("should return an error if callback is specified and dir and ext are null", (done) => {
|
|
411
|
-
files.filesWithExtension({ dir: null, ext: null }, (error) => {
|
|
412
|
-
try {
|
|
413
|
-
expect(error).to.be.an.instanceof(TypeError);
|
|
414
|
-
done();
|
|
415
|
-
} catch (err) {
|
|
416
|
-
done(err);
|
|
417
|
-
}
|
|
418
|
-
});
|
|
419
|
-
});
|
|
420
|
-
it("should error if only ext is null", () => {
|
|
421
|
-
return files
|
|
422
|
-
.filesWithExtension({ dir: ".", ext: null })
|
|
423
|
-
.then(() => expect.fail())
|
|
424
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
425
|
-
});
|
|
426
|
-
it("should error if callback is specified and only ext is null", (done) => {
|
|
427
|
-
files.filesWithExtension({ dir: ".", ext: null }, (error) => {
|
|
428
|
-
try {
|
|
429
|
-
expect(error).to.be.an.instanceof(TypeError);
|
|
430
|
-
done();
|
|
431
|
-
} catch (err) {
|
|
432
|
-
done(err);
|
|
433
|
-
}
|
|
434
|
-
});
|
|
435
|
-
});
|
|
436
|
-
it("should throw an exception if only dir is null", () => {
|
|
437
|
-
return files
|
|
438
|
-
.filesWithExtension({ dir: null, ext: "json" })
|
|
439
|
-
.then(() => expect.fail())
|
|
440
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
441
|
-
});
|
|
442
|
-
it("should error if only dir is null", (done) => {
|
|
443
|
-
files.filesWithExtension({ dir: null, ext: "json" }, (error) => {
|
|
444
|
-
try {
|
|
445
|
-
expect(error).to.be.an.instanceof(TypeError);
|
|
446
|
-
done();
|
|
447
|
-
} catch (err) {
|
|
448
|
-
done(err);
|
|
449
|
-
}
|
|
450
|
-
});
|
|
451
|
-
});
|
|
452
|
-
it("should throw an exception if dir does not exist", () => {
|
|
453
|
-
return files
|
|
454
|
-
.filesWithExtension({ dir: "/unlikely/to/exist/dir", ext: "json" })
|
|
455
|
-
.then(() => expect.fail())
|
|
456
|
-
.catch((error) => {
|
|
457
|
-
expect(error).to.be.an.instanceof(Error);
|
|
458
|
-
expect(error.toString()).to.include("ENOENT");
|
|
459
|
-
});
|
|
460
|
-
});
|
|
461
|
-
it("should error if dir does not exist", (done) => {
|
|
462
|
-
files.filesWithExtension(
|
|
463
|
-
{ dir: "/unlikely/to/exist/dir", ext: "json" },
|
|
464
|
-
(error) => {
|
|
465
|
-
try {
|
|
466
|
-
expect(error).to.be.an.instanceof(Error);
|
|
467
|
-
expect(error.toString()).to.include("ENOENT");
|
|
468
|
-
done();
|
|
469
|
-
} catch (err) {
|
|
470
|
-
done(err);
|
|
471
|
-
}
|
|
472
|
-
},
|
|
473
|
-
);
|
|
474
|
-
});
|
|
475
|
-
it("should return [] if dir is empty", (done) => {
|
|
476
|
-
files.filesWithExtension(
|
|
477
|
-
{ dir: emptyDir, ext: "json" },
|
|
478
|
-
(error, result) => {
|
|
479
|
-
if (error) {
|
|
480
|
-
done(error);
|
|
481
|
-
return;
|
|
482
|
-
}
|
|
483
|
-
try {
|
|
484
|
-
expect(result).to.have.members([]);
|
|
485
|
-
done();
|
|
486
|
-
} catch (err) {
|
|
487
|
-
done(err);
|
|
488
|
-
}
|
|
489
|
-
},
|
|
490
|
-
);
|
|
491
|
-
});
|
|
492
|
-
it("should have a forward slash separating directory and filename in returned filenames if dir does not contain a slash at the end", async function () {
|
|
493
|
-
await prepareDirWithJsonFiles();
|
|
494
|
-
const result = await files.filesWithExtension({
|
|
495
|
-
dir: jsonDir,
|
|
496
|
-
ext: "json",
|
|
497
|
-
});
|
|
498
|
-
expect(result).to.have.members([
|
|
499
|
-
jsonDir + "/package.json",
|
|
500
|
-
jsonDir + "/server.json",
|
|
501
|
-
jsonDir + "/tasks.JSON",
|
|
502
|
-
]);
|
|
503
|
-
});
|
|
504
|
-
it("should not have two forward slashes in returned filenames if dir contains a slash at the end", (done) => {
|
|
505
|
-
prepareDirWithJsonFiles().then(() => {
|
|
506
|
-
files.filesWithExtension(
|
|
507
|
-
{ dir: jsonDir + "/", ext: "json" },
|
|
508
|
-
(error, result) => {
|
|
509
|
-
if (error) {
|
|
510
|
-
done(error);
|
|
511
|
-
return;
|
|
512
|
-
}
|
|
513
|
-
try {
|
|
514
|
-
expect(result).to.have.members([
|
|
515
|
-
jsonDir + "/package.json",
|
|
516
|
-
jsonDir + "/server.json",
|
|
517
|
-
jsonDir + "/tasks.JSON",
|
|
518
|
-
]);
|
|
519
|
-
done();
|
|
520
|
-
} catch (err) {
|
|
521
|
-
done(err);
|
|
522
|
-
}
|
|
523
|
-
},
|
|
524
|
-
);
|
|
525
|
-
});
|
|
526
|
-
});
|
|
527
|
-
it("should return [] if there are files in dir matching ext but ext starts with a period", async function () {
|
|
528
|
-
await prepareDirWithJsonFiles();
|
|
529
|
-
const result = await files.filesWithExtension({
|
|
530
|
-
dir: jsonDir,
|
|
531
|
-
ext: ".json",
|
|
532
|
-
});
|
|
533
|
-
expect(result).to.have.members([]);
|
|
534
|
-
});
|
|
535
|
-
it("should return [] if ext is empty", (done) => {
|
|
536
|
-
prepareDirWithJsonFiles().then(() => {
|
|
537
|
-
files.filesWithExtension({ dir: jsonDir, ext: "" }, (error, result) => {
|
|
538
|
-
if (error) {
|
|
539
|
-
done(error);
|
|
540
|
-
return;
|
|
541
|
-
}
|
|
542
|
-
try {
|
|
543
|
-
expect(result).to.have.members([]);
|
|
544
|
-
done();
|
|
545
|
-
} catch (err) {
|
|
546
|
-
done(err);
|
|
547
|
-
}
|
|
548
|
-
});
|
|
549
|
-
});
|
|
550
|
-
});
|
|
551
|
-
});
|
|
552
|
-
|
|
553
|
-
describe("#filesWithExtensionSync()", function () {
|
|
554
|
-
it("should throw a TypeError if caller does not specify any arguments", function () {
|
|
555
|
-
expect(function () {
|
|
556
|
-
files.filesWithExtensionSync();
|
|
557
|
-
}).to.throw(TypeError);
|
|
558
|
-
});
|
|
559
|
-
it("should throw an exception if opts is empty", function () {
|
|
560
|
-
expect(function () {
|
|
561
|
-
files.filesWithExtensionSync({});
|
|
562
|
-
}).to.throw(TypeError);
|
|
563
|
-
});
|
|
564
|
-
it("should throw an exception if dir and ext are null", function () {
|
|
565
|
-
expect(function () {
|
|
566
|
-
files.filesWithExtensionSync({ dir: null, ext: null });
|
|
567
|
-
}).to.throw(TypeError);
|
|
568
|
-
});
|
|
569
|
-
it("should throw an exception if only ext is null", function () {
|
|
570
|
-
expect(function () {
|
|
571
|
-
files.filesWithExtensionSync({ dir: ".", ext: null });
|
|
572
|
-
}).to.throw(TypeError);
|
|
573
|
-
});
|
|
574
|
-
it("should throw an exception if only dir is null", function () {
|
|
575
|
-
expect(function () {
|
|
576
|
-
files.filesWithExtensionSync({ dir: null, ext: "json" });
|
|
577
|
-
}).to.throw(TypeError);
|
|
578
|
-
});
|
|
579
|
-
it("should throw an exception if dir does not exist", function () {
|
|
580
|
-
expect(function () {
|
|
581
|
-
files.filesWithExtensionSync({
|
|
582
|
-
dir: "/unlikely/to/exist/dir",
|
|
583
|
-
ext: "json",
|
|
584
|
-
});
|
|
585
|
-
}).to.throw(/ENOENT.*/);
|
|
586
|
-
});
|
|
587
|
-
it("should return [] if dir is empty", function () {
|
|
588
|
-
expect(
|
|
589
|
-
files.filesWithExtensionSync({ dir: emptyDir, ext: "json" }),
|
|
590
|
-
).to.have.members([]);
|
|
591
|
-
});
|
|
592
|
-
it("should have a forward slash separating directory and filename in returned filenames if dir does not contain a slash at the end", function () {
|
|
593
|
-
return prepareDirWithJsonFiles().then(() => {
|
|
594
|
-
expect(
|
|
595
|
-
files.filesWithExtensionSync({ dir: jsonDir, ext: "json" }),
|
|
596
|
-
).to.have.members([
|
|
597
|
-
jsonDir + "/package.json",
|
|
598
|
-
jsonDir + "/server.json",
|
|
599
|
-
jsonDir + "/tasks.JSON",
|
|
600
|
-
]);
|
|
601
|
-
});
|
|
602
|
-
});
|
|
603
|
-
it("should not have two forward slashes in returned filenames if dir contains a slash at the end", function () {
|
|
604
|
-
return prepareDirWithJsonFiles().then(() => {
|
|
605
|
-
expect(
|
|
606
|
-
files.filesWithExtensionSync({ dir: jsonDir, ext: "json" }),
|
|
607
|
-
).to.have.members([
|
|
608
|
-
jsonDir + "/package.json",
|
|
609
|
-
jsonDir + "/server.json",
|
|
610
|
-
jsonDir + "/tasks.JSON",
|
|
611
|
-
]);
|
|
612
|
-
});
|
|
613
|
-
});
|
|
614
|
-
it("should return [] if there are files in dir matching ext but ext starts with a period", function () {
|
|
615
|
-
return prepareDirWithJsonFiles().then(() => {
|
|
616
|
-
expect(
|
|
617
|
-
files.filesWithExtensionSync({ dir: jsonDir, ext: ".json" }),
|
|
618
|
-
).to.have.members([]);
|
|
619
|
-
});
|
|
620
|
-
});
|
|
621
|
-
it("should return [] if ext is empty", function () {
|
|
622
|
-
return prepareDirWithJsonFiles().then(() => {
|
|
623
|
-
expect(
|
|
624
|
-
files.filesWithExtensionSync({ dir: jsonDir, ext: "" }),
|
|
625
|
-
).to.have.members([]);
|
|
626
|
-
});
|
|
627
|
-
});
|
|
628
|
-
});
|
|
629
|
-
|
|
630
|
-
describe("#isDirectory", function () {
|
|
631
|
-
it("should be true for the working directory", (done) => {
|
|
632
|
-
files.isDirectory(".", function (error, truth) {
|
|
633
|
-
try {
|
|
634
|
-
expect(error).to.be.null;
|
|
635
|
-
expect(truth).to.be.ok;
|
|
636
|
-
done();
|
|
637
|
-
} catch (err) {
|
|
638
|
-
done(err);
|
|
639
|
-
}
|
|
640
|
-
});
|
|
641
|
-
});
|
|
642
|
-
it("should be true for the directory containing FilesTest.js", async function () {
|
|
643
|
-
const truth = await files.isDirectory(__dirname);
|
|
644
|
-
expect(truth).to.be.ok;
|
|
645
|
-
});
|
|
646
|
-
it("should be false for FilesTest.js", (done) => {
|
|
647
|
-
files.isDirectory(
|
|
648
|
-
files.join(__dirname, "FilesTest.js"),
|
|
649
|
-
function (error, truth) {
|
|
650
|
-
try {
|
|
651
|
-
expect(error).to.be.null;
|
|
652
|
-
expect(truth).to.be.false;
|
|
653
|
-
done();
|
|
654
|
-
} catch (err) {
|
|
655
|
-
done(err);
|
|
656
|
-
}
|
|
657
|
-
},
|
|
658
|
-
);
|
|
659
|
-
});
|
|
660
|
-
it("should error for UnlikelyToExist.js", async function () {
|
|
661
|
-
try {
|
|
662
|
-
await files.isDirectory(files.join(__dirname, "UnlikelyToExist.js"));
|
|
663
|
-
expect.fail("We expected #isDirectory to throw an exception.");
|
|
664
|
-
} catch (error) {
|
|
665
|
-
expect(error).to.be.an.instanceof(Error);
|
|
666
|
-
}
|
|
667
|
-
});
|
|
668
|
-
it("should error for /unlikely/to/exist/dir/", (done) => {
|
|
669
|
-
files.isDirectory("/unlikely/to/exist/dir/", (error, truth) => {
|
|
670
|
-
try {
|
|
671
|
-
expect(error).to.be.an.instanceof(Error);
|
|
672
|
-
expect(truth).to.be.undefined;
|
|
673
|
-
done();
|
|
674
|
-
} catch (err) {
|
|
675
|
-
done(err);
|
|
676
|
-
}
|
|
677
|
-
});
|
|
678
|
-
});
|
|
679
|
-
});
|
|
680
|
-
|
|
681
|
-
describe("#isDirectorySync", () => {
|
|
682
|
-
it("should return true for the working directory", () => {
|
|
683
|
-
expect(files.isDirectorySync(".")).to.be.ok;
|
|
684
|
-
});
|
|
685
|
-
it("should return true for the directory containing FilesTest.js", () => {
|
|
686
|
-
expect(files.isDirectorySync(__dirname)).to.be.ok;
|
|
687
|
-
});
|
|
688
|
-
it("should return false for FilesTest.js", () => {
|
|
689
|
-
expect(files.isDirectorySync(files.join(__dirname, "FilesTest.js"))).to.be
|
|
690
|
-
.false;
|
|
691
|
-
});
|
|
692
|
-
it("should throw an exception for UnlikelyToExist.js", () => {
|
|
693
|
-
expect(() => {
|
|
694
|
-
files.isDirectorySync(files.join(__dirname, "UnlikelyToExist.js"));
|
|
695
|
-
}).to.throw(/ENOENT.*/);
|
|
696
|
-
});
|
|
697
|
-
it("should throw an exception for /unlikely/to/exist/dir/", () => {
|
|
698
|
-
expect(() => {
|
|
699
|
-
files.isDirectorySync("/unlikely/to/exist/dir/");
|
|
700
|
-
}).to.throw(/ENOENT.*/);
|
|
701
|
-
});
|
|
702
|
-
});
|
|
703
|
-
|
|
704
|
-
describe("#isFile", function () {
|
|
705
|
-
it("should be false for the working directory", (done) => {
|
|
706
|
-
files.isFile(".", function (error, truth) {
|
|
707
|
-
try {
|
|
708
|
-
expect(error).to.be.null;
|
|
709
|
-
expect(truth).to.be.false;
|
|
710
|
-
done();
|
|
711
|
-
} catch (err) {
|
|
712
|
-
done(err);
|
|
713
|
-
}
|
|
714
|
-
});
|
|
715
|
-
});
|
|
716
|
-
it("should be false for the directory containing FilesTest.js", async function () {
|
|
717
|
-
const truth = await files.isFile(__dirname);
|
|
718
|
-
expect(truth).to.be.false;
|
|
719
|
-
});
|
|
720
|
-
it("should be true for FilesTest.js", (done) => {
|
|
721
|
-
files.isFile(files.join(__dirname, "FilesTest.js"), (error, truth) => {
|
|
722
|
-
try {
|
|
723
|
-
expect(error).to.be.null;
|
|
724
|
-
expect(truth).to.be.ok;
|
|
725
|
-
done();
|
|
726
|
-
} catch (err) {
|
|
727
|
-
done(err);
|
|
728
|
-
}
|
|
729
|
-
});
|
|
730
|
-
});
|
|
731
|
-
it("should error for UnlikelyToExist.js", async function () {
|
|
732
|
-
try {
|
|
733
|
-
await files.isFile(files.join(__dirname, "UnlikelyToExist.js"));
|
|
734
|
-
expect.fail("We expected #isFile to throw an exception.");
|
|
735
|
-
} catch (error) {
|
|
736
|
-
expect(error).to.be.an.instanceof(Error);
|
|
737
|
-
}
|
|
738
|
-
});
|
|
739
|
-
it("should error for /unlikely/to/exist/dir/", (done) => {
|
|
740
|
-
files.isFile("/unlikely/to/exist/dir/", function (error, truth) {
|
|
741
|
-
try {
|
|
742
|
-
expect(error).to.be.an.instanceof(Error);
|
|
743
|
-
expect(truth).to.be.undefined;
|
|
744
|
-
done();
|
|
745
|
-
} catch (err) {
|
|
746
|
-
done(err);
|
|
747
|
-
}
|
|
748
|
-
});
|
|
749
|
-
});
|
|
750
|
-
});
|
|
751
|
-
|
|
752
|
-
describe("#isFileSync", () => {
|
|
753
|
-
it("should return false for the working directory", () => {
|
|
754
|
-
expect(files.isFileSync(".")).to.be.false;
|
|
755
|
-
});
|
|
756
|
-
it("should return false for the directory containing FilesTest.js", () => {
|
|
757
|
-
expect(files.isFileSync(__dirname)).to.be.false;
|
|
758
|
-
});
|
|
759
|
-
it("should return false for FilesTest.js", () => {
|
|
760
|
-
expect(files.isFileSync(files.join(__dirname, "FilesTest.js"))).to.be.ok;
|
|
761
|
-
});
|
|
762
|
-
it("should throw an exception for UnlikelyToExist.js", () => {
|
|
763
|
-
expect(function () {
|
|
764
|
-
files.isFileSync(files.join(__dirname, "UnlikelyToExist.js"));
|
|
765
|
-
}).to.throw(/ENOENT.*/);
|
|
766
|
-
});
|
|
767
|
-
it("should throw an exception for /unlikely/to/exist/dir/", () => {
|
|
768
|
-
expect(function () {
|
|
769
|
-
files.isFileSync("/unlikely/to/exist/dir/");
|
|
770
|
-
}).to.throw(/ENOENT.*/);
|
|
771
|
-
});
|
|
772
|
-
});
|
|
773
|
-
|
|
774
|
-
describe("#mkdirp and #rmrf", function () {
|
|
775
|
-
it("should create and destroy a new directory hierarchy", function (done) {
|
|
776
|
-
const testDir = targetDir + "/this/is/a/nested/dir";
|
|
777
|
-
waterfall(
|
|
778
|
-
[
|
|
779
|
-
function (cb) {
|
|
780
|
-
cb(null, targetDir);
|
|
781
|
-
},
|
|
782
|
-
files.rmrf,
|
|
783
|
-
function (cb) {
|
|
784
|
-
expect(function () {
|
|
785
|
-
files.isDirectorySync(targetDir);
|
|
786
|
-
}).to.throw(/ENOENT.*/);
|
|
787
|
-
cb(null);
|
|
788
|
-
},
|
|
789
|
-
function (cb) {
|
|
790
|
-
cb(null, testDir);
|
|
791
|
-
},
|
|
792
|
-
files.mkdirp,
|
|
793
|
-
function (make, cb) {
|
|
794
|
-
expect(files.isDirectorySync(testDir)).to.be.true;
|
|
795
|
-
cb(null);
|
|
796
|
-
},
|
|
797
|
-
function (cb) {
|
|
798
|
-
cb(null, targetDir);
|
|
799
|
-
},
|
|
800
|
-
files.rmrf,
|
|
801
|
-
function (cb) {
|
|
802
|
-
expect(function () {
|
|
803
|
-
files.isDirectorySync(testDir);
|
|
804
|
-
}).to.throw(/ENOENT.*/);
|
|
805
|
-
expect(function () {
|
|
806
|
-
files.isDirectorySync(targetDir);
|
|
807
|
-
}).to.throw(/ENOENT.*/);
|
|
808
|
-
cb(null);
|
|
809
|
-
},
|
|
810
|
-
],
|
|
811
|
-
done,
|
|
812
|
-
);
|
|
813
|
-
});
|
|
814
|
-
});
|
|
815
|
-
|
|
816
|
-
describe("#rmrf", function () {
|
|
817
|
-
it("should delete files", function (done) {
|
|
818
|
-
const testFile = targetDir + "/frog.txt";
|
|
819
|
-
files.mkdirpSync(targetDir);
|
|
820
|
-
files.closeSync(files.openSync(testFile, "w"));
|
|
821
|
-
waterfall(
|
|
822
|
-
[
|
|
823
|
-
function (cb) {
|
|
824
|
-
cb(null, testFile);
|
|
825
|
-
},
|
|
826
|
-
files.isFile.bind(files),
|
|
827
|
-
function (truth, cb) {
|
|
828
|
-
expect(truth).to.be.true;
|
|
829
|
-
cb(null);
|
|
830
|
-
},
|
|
831
|
-
function (cb) {
|
|
832
|
-
cb(null, testFile);
|
|
833
|
-
},
|
|
834
|
-
files.rmrf,
|
|
835
|
-
function (cb) {
|
|
836
|
-
expect(function () {
|
|
837
|
-
files.isFileSync(testFile);
|
|
838
|
-
}).to.throw(/ENOENT.*/);
|
|
839
|
-
cb();
|
|
840
|
-
},
|
|
841
|
-
],
|
|
842
|
-
done,
|
|
843
|
-
);
|
|
844
|
-
});
|
|
845
|
-
});
|
|
846
|
-
|
|
847
|
-
describe("#mkdirpSync and #rmrfSync", function () {
|
|
848
|
-
it("should create and destroy a new directory hierarchy", function () {
|
|
849
|
-
const testDir = targetDir + "/this/is/a/nested/dir";
|
|
850
|
-
files.rmrfSync(targetDir);
|
|
851
|
-
expect(function () {
|
|
852
|
-
files.isDirectorySync(targetDir);
|
|
853
|
-
}).to.throw(/ENOENT.*/);
|
|
854
|
-
files.mkdirpSync(testDir);
|
|
855
|
-
expect(files.isDirectorySync(testDir)).to.be.ok;
|
|
856
|
-
files.rmrfSync(targetDir);
|
|
857
|
-
expect(function () {
|
|
858
|
-
files.isDirectorySync(testDir);
|
|
859
|
-
}).to.throw(/ENOENT.*/);
|
|
860
|
-
expect(function () {
|
|
861
|
-
files.isDirectorySync(targetDir);
|
|
862
|
-
}).to.throw(/ENOENT.*/);
|
|
863
|
-
});
|
|
864
|
-
});
|
|
865
|
-
|
|
866
|
-
describe("#readFile(path[, options, callback]", () => {
|
|
867
|
-
it("should read a file and notify a callback", (callback) => {
|
|
868
|
-
files.readFile(__filename, "utf8", (error, string) => {
|
|
869
|
-
if (error) return callback(error);
|
|
870
|
-
expect(string.includes("use stict")).to.be.true;
|
|
871
|
-
callback();
|
|
872
|
-
});
|
|
873
|
-
});
|
|
874
|
-
it("should read a file and resolve a Promise", async function () {
|
|
875
|
-
const string = await files.readFile(__filename, "utf8");
|
|
876
|
-
expect(string.includes("use stict")).to.be.true;
|
|
877
|
-
});
|
|
878
|
-
});
|
|
879
|
-
|
|
880
|
-
describe("#readFiles(files, options, callback)", function (done) {
|
|
881
|
-
before(function (done) {
|
|
882
|
-
waterfall(
|
|
883
|
-
[
|
|
884
|
-
function (cb) {
|
|
885
|
-
cb(null, targetDir);
|
|
886
|
-
},
|
|
887
|
-
files.rmrf,
|
|
888
|
-
function (cb) {
|
|
889
|
-
cb(null, targetDir);
|
|
890
|
-
},
|
|
891
|
-
files.mkdirp,
|
|
892
|
-
function (made, cb) {
|
|
893
|
-
cb(null, targetDir + "/a.txt", "a", "utf8");
|
|
894
|
-
},
|
|
895
|
-
files.writeFile,
|
|
896
|
-
function (cb) {
|
|
897
|
-
cb(null, targetDir + "/b.txt", "b", "utf8");
|
|
898
|
-
},
|
|
899
|
-
files.writeFile,
|
|
900
|
-
function (cb) {
|
|
901
|
-
cb(null, targetDir + "/c.txt", "c", "utf8");
|
|
902
|
-
},
|
|
903
|
-
files.writeFile,
|
|
904
|
-
],
|
|
905
|
-
done,
|
|
906
|
-
);
|
|
907
|
-
});
|
|
908
|
-
|
|
909
|
-
after(function (done) {
|
|
910
|
-
files.rmrf(targetDir, done);
|
|
911
|
-
});
|
|
912
|
-
|
|
913
|
-
it("should successfully read three files when called without options", (done) => {
|
|
914
|
-
files.readFiles(
|
|
915
|
-
[targetDir + "/a.txt", targetDir + "/b.txt", targetDir + "/c.txt"],
|
|
916
|
-
(error, string) => {
|
|
917
|
-
try {
|
|
918
|
-
expect(error).to.be.null;
|
|
919
|
-
expect(string).to.equal("abc");
|
|
920
|
-
done();
|
|
921
|
-
} catch (err) {
|
|
922
|
-
done(err);
|
|
923
|
-
}
|
|
924
|
-
},
|
|
925
|
-
);
|
|
926
|
-
});
|
|
927
|
-
it("should successfully read three files when called with option 'utf8'", (done) => {
|
|
928
|
-
files.readFiles(
|
|
929
|
-
[targetDir + "/b.txt", targetDir + "/a.txt", targetDir + "/c.txt"],
|
|
930
|
-
"utf8",
|
|
931
|
-
(error, string) => {
|
|
932
|
-
try {
|
|
933
|
-
expect(error).to.be.null;
|
|
934
|
-
expect(string).to.equal("bac");
|
|
935
|
-
done();
|
|
936
|
-
} catch (err) {
|
|
937
|
-
done(err);
|
|
938
|
-
}
|
|
939
|
-
},
|
|
940
|
-
);
|
|
941
|
-
});
|
|
942
|
-
it("should successfully read three files when called with options", async function () {
|
|
943
|
-
const string = await files.readFiles(
|
|
944
|
-
[targetDir + "/c.txt", targetDir + "/a.txt", targetDir + "/c.txt"],
|
|
945
|
-
{ encoding: "utf8", flag: "r" },
|
|
946
|
-
);
|
|
947
|
-
expect(string).to.equal("cac");
|
|
948
|
-
});
|
|
949
|
-
it("should successfully read three files when called with null options", (done) => {
|
|
950
|
-
files.readFiles(
|
|
951
|
-
[targetDir + "/c.txt", targetDir + "/a.txt", targetDir + "/c.txt"],
|
|
952
|
-
null,
|
|
953
|
-
(error, string) => {
|
|
954
|
-
try {
|
|
955
|
-
expect(error).to.be.null;
|
|
956
|
-
expect(string).to.equal("cac");
|
|
957
|
-
done();
|
|
958
|
-
} catch (err) {
|
|
959
|
-
done(err);
|
|
960
|
-
}
|
|
961
|
-
},
|
|
962
|
-
);
|
|
963
|
-
});
|
|
964
|
-
it("should return a Promise if callback is null", async function () {
|
|
965
|
-
const string = await files.readFiles([
|
|
966
|
-
targetDir + "/c.txt",
|
|
967
|
-
targetDir + "/a.txt",
|
|
968
|
-
targetDir + "/c.txt",
|
|
969
|
-
]);
|
|
970
|
-
expect(string).to.equal("cac");
|
|
971
|
-
});
|
|
972
|
-
it("should throw an error if files argument is null", (done) => {
|
|
973
|
-
files.readFiles(null, (error, string) => {
|
|
974
|
-
try {
|
|
975
|
-
expect(error).to.be.an.instanceof(TypeError);
|
|
976
|
-
expect(string).to.be.undefined;
|
|
977
|
-
done();
|
|
978
|
-
} catch (err) {
|
|
979
|
-
done(err);
|
|
980
|
-
}
|
|
981
|
-
});
|
|
982
|
-
});
|
|
983
|
-
it("should return an empty string if files argument is empty", (done) => {
|
|
984
|
-
files.readFiles([], null, (error, string) => {
|
|
985
|
-
try {
|
|
986
|
-
expect(error).to.be.null;
|
|
987
|
-
expect(string).to.equal("");
|
|
988
|
-
done();
|
|
989
|
-
} catch (err) {
|
|
990
|
-
done(err);
|
|
991
|
-
}
|
|
992
|
-
});
|
|
993
|
-
});
|
|
994
|
-
it("should return an empty string when given the empty string as input", (done) => {
|
|
995
|
-
files.readFiles("", (error, string) => {
|
|
996
|
-
try {
|
|
997
|
-
expect(error).to.be.null;
|
|
998
|
-
expect(string).to.eql("");
|
|
999
|
-
done();
|
|
1000
|
-
} catch (err) {
|
|
1001
|
-
done(err);
|
|
1002
|
-
}
|
|
1003
|
-
});
|
|
1004
|
-
});
|
|
1005
|
-
it("should return with an error if one of the files does not exist", (done) => {
|
|
1006
|
-
files.readFiles(
|
|
1007
|
-
[
|
|
1008
|
-
targetDir + "/c.txt",
|
|
1009
|
-
targetDir + "/nonexistent.txt",
|
|
1010
|
-
targetDir + "/b.txt",
|
|
1011
|
-
],
|
|
1012
|
-
null,
|
|
1013
|
-
(error, string) => {
|
|
1014
|
-
try {
|
|
1015
|
-
expect(error).to.not.be.null;
|
|
1016
|
-
expect(error).to.not.be.undefined;
|
|
1017
|
-
expect(string).to.be.undefined;
|
|
1018
|
-
done();
|
|
1019
|
-
} catch (err) {
|
|
1020
|
-
done(err);
|
|
1021
|
-
}
|
|
1022
|
-
},
|
|
1023
|
-
);
|
|
1024
|
-
});
|
|
1025
|
-
});
|
|
1026
|
-
|
|
1027
|
-
describe("#stat(path[, options, cb])", () => {
|
|
1028
|
-
it("should notify a callback with stats", (callback) => {
|
|
1029
|
-
files.stat(__filename, (error, stats) => {
|
|
1030
|
-
if (error) return callback(error);
|
|
1031
|
-
expect(stats).to.be.an.instanceof(files.Stats);
|
|
1032
|
-
callback();
|
|
1033
|
-
});
|
|
1034
|
-
});
|
|
1035
|
-
it("should resolve a Promise with stats", async function () {
|
|
1036
|
-
const stats = await files.stat(__filename);
|
|
1037
|
-
expect(stats).to.be.an.instanceof(files.Stats);
|
|
1038
|
-
});
|
|
1039
|
-
});
|
|
1040
|
-
|
|
1041
|
-
describe("#touch(filename, options, cb)", function () {
|
|
1042
|
-
before(function (done) {
|
|
1043
|
-
waterfall(
|
|
1044
|
-
[
|
|
1045
|
-
function (cb) {
|
|
1046
|
-
cb(null, targetDir);
|
|
1047
|
-
},
|
|
1048
|
-
files.rmrf,
|
|
1049
|
-
function (cb) {
|
|
1050
|
-
cb(null, targetDir);
|
|
1051
|
-
},
|
|
1052
|
-
files.mkdirp,
|
|
1053
|
-
],
|
|
1054
|
-
done,
|
|
1055
|
-
);
|
|
1056
|
-
});
|
|
1057
|
-
|
|
1058
|
-
after(function (done) {
|
|
1059
|
-
files.rmrf(targetDir, done);
|
|
1060
|
-
});
|
|
1061
|
-
|
|
1062
|
-
it("should create an empty file", function (done) {
|
|
1063
|
-
const touchFile = targetDir + "/foo.bar";
|
|
1064
|
-
expect(function () {
|
|
1065
|
-
files.isFileSync(touchFile);
|
|
1066
|
-
}).to.throw(/ENOENT.*/);
|
|
1067
|
-
waterfall(
|
|
1068
|
-
[
|
|
1069
|
-
function (cb) {
|
|
1070
|
-
cb(null, touchFile, null);
|
|
1071
|
-
},
|
|
1072
|
-
files.touch,
|
|
1073
|
-
function (res, cb) {
|
|
1074
|
-
expect(res).to.be.undefined;
|
|
1075
|
-
cb(null, touchFile);
|
|
1076
|
-
},
|
|
1077
|
-
files.isFile.bind(files),
|
|
1078
|
-
function (truth, cb) {
|
|
1079
|
-
expect(truth).to.be.ok;
|
|
1080
|
-
cb(null, touchFile);
|
|
1081
|
-
},
|
|
1082
|
-
files.rmrf,
|
|
1083
|
-
function (cb) {
|
|
1084
|
-
cb(null, touchFile);
|
|
1085
|
-
},
|
|
1086
|
-
files.touch,
|
|
1087
|
-
function (res, cb) {
|
|
1088
|
-
expect(res).to.be.undefined;
|
|
1089
|
-
cb(null, touchFile);
|
|
1090
|
-
},
|
|
1091
|
-
files.isFile.bind(files),
|
|
1092
|
-
],
|
|
1093
|
-
done,
|
|
1094
|
-
);
|
|
1095
|
-
});
|
|
1096
|
-
it("should throw TypeError when filename is null", async function () {
|
|
1097
|
-
await files
|
|
1098
|
-
.touch()
|
|
1099
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
1100
|
-
await files
|
|
1101
|
-
.touch(null)
|
|
1102
|
-
.catch((error) => expect(error).to.be.an.instanceof(TypeError));
|
|
1103
|
-
});
|
|
1104
|
-
});
|
|
1105
|
-
|
|
1106
|
-
describe("#writeFile(file, data[, options, callback]", () => {
|
|
1107
|
-
it("should write a file and notify a callback", (callback) => {
|
|
1108
|
-
const testFile = targetDir + "/writeFileTestCallback.txt";
|
|
1109
|
-
files.rmrf(testFile, (error) => {
|
|
1110
|
-
if (error) return callback(error);
|
|
1111
|
-
files.mkdirp(targetDir, (erro) => {
|
|
1112
|
-
if (erro) return callback(erro);
|
|
1113
|
-
files.writeFile(testFile, "writeFileTest", "utf8", (err) => {
|
|
1114
|
-
if (err) return callback(err);
|
|
1115
|
-
files.readFile(testFile, "utf8", (er, string) => {
|
|
1116
|
-
if (er) return callback(er);
|
|
1117
|
-
expect(string).to.eql("writeFileTest");
|
|
1118
|
-
files.rmrf(testFile, callback);
|
|
1119
|
-
});
|
|
1120
|
-
});
|
|
1121
|
-
});
|
|
1122
|
-
});
|
|
1123
|
-
});
|
|
1124
|
-
it("should write a file and resolve a Promise", async function () {
|
|
1125
|
-
const testFile = targetDir + "/writeFileTestPromise.txt";
|
|
1126
|
-
await files.rmrf(testFile);
|
|
1127
|
-
await files.mkdirp(targetDir);
|
|
1128
|
-
await files.writeFile(testFile, "writeFileTest", "utf8");
|
|
1129
|
-
const string = await files.readFile(testFile, "utf8");
|
|
1130
|
-
expect(string).to.eql("writeFileTest");
|
|
1131
|
-
await files.rmrf(testFile);
|
|
1132
|
-
});
|
|
1133
|
-
});
|
|
1134
|
-
});
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { expect } = require("chai");
|
|
4
|
-
const files = require("..");
|
|
5
|
-
|
|
6
|
-
describe("Files#sanitizeFilename", () => {
|
|
7
|
-
it("should sanitize filenames", () => {
|
|
8
|
-
const expected = "~.sshauthorized_keys";
|
|
9
|
-
const actual = files.sanitizeFilename("~/.\u0000ssh/authorized_keys");
|
|
10
|
-
expect(actual).to.eql(expected);
|
|
11
|
-
});
|
|
12
|
-
it("should sanitize filenames with a replacement", () => {
|
|
13
|
-
const expected = "~_._ssh_authorized_keys";
|
|
14
|
-
const actual = files.sanitizeFilename("~/.\u0000ssh/authorized_keys", {
|
|
15
|
-
replacement: "_",
|
|
16
|
-
});
|
|
17
|
-
expect(actual).to.eql(expected);
|
|
18
|
-
});
|
|
19
|
-
it("should throw an error if the filename is not a string", () => {
|
|
20
|
-
expect(() =>
|
|
21
|
-
files.sanitizeFilename(["~/.\u0000ssh/authorized_keys"]),
|
|
22
|
-
).to.throw(Error);
|
|
23
|
-
});
|
|
24
|
-
});
|