forked from shinnn/empty-file
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
executable file
·69 lines (58 loc) · 1.78 KB
/
test.js
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
'use strict';
const emptyFile = require('.');
const pify = require('pify');
const test = require('tape');
const {
readFile: readFilePromise,
stat: statPromise,
unlink: unlinkPromise
} = pify.all(require('graceful-fs'));
test('emptyFile()', t => {
t.plan(8);
t.strictEqual(emptyFile.name, 'emptyFile', 'should have a function name.');
emptyFile('tmp0')
.then(() => readFilePromise('tmp0', 'utf8'))
.then(content => t.strictEqual(content, '', 'should write an empty file.'))
.then(() => unlinkPromise('tmp0'))
.catch(t.fail);
emptyFile('tmp1', {mode: 33261})
.then(() => statPromise('tmp1'))
.then(stats => t.strictEqual(stats.mode, 33261, 'should support fs.writeFile options.'))
.then(() => unlinkPromise('tmp1'))
.catch(t.fail);
emptyFile('node_modules', null)
.then(t.fail, err => t.ok(err, 'should fail when it cannot write a file.'))
.catch(t.fail);
emptyFile('__', 'utf8')
.then(t.fail, err => {
t.ok(
/TypeError.*Encoding string is not supported since empty-file writes an empty file\./.test(err),
'should not accept encoding string.'
);
})
.catch(t.fail);
emptyFile('___', {encoding: 'base64'})
.then(t.fail, err => {
t.ok(
/TypeError.*Encoding option is not supported since empty-file writes an empty file\./.test(err),
'should not accept encoding option.'
);
})
.catch(t.fail);
emptyFile(null)
.then(t.fail, err => {
t.ok(
/TypeError.*path/.test(err),
'should fail when the first argument is not a string.'
);
})
.catch(t.fail);
emptyFile('foo', 123)
.then(t.fail, err => {
t.ok(
/TypeError.*Expected options to be either an object or a string/.test(err),
'should fail when the second argument is neither an object nor a string.'
);
})
.catch(t.fail);
});