-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
161 lines (140 loc) · 4.16 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
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
'use strict';
const {join} = require('path');
const {promisify} = require('util');
const {URL} = require('url');
const fileUrl = require('file-url');
const {readFile} = require('graceful-fs');
const rmfr = require('rmfr');
const test = require('tape');
const writeFileAtomically = require('.');
const promisifiedReadFile = promisify(readFile);
test('writeFileAtomically()', async t => {
t.plan(13);
t.on('end', async () => rmfr('tmp*', {glob: true}));
(async () => {
const filename = 'tmp_0.txt';
await writeFileAtomically(filename, Buffer.from('foo'));
t.equal(
await promisifiedReadFile(filename, 'utf8'),
'foo',
'should write a file.'
);
})();
(async () => {
const filename = Buffer.from('tmp_1.txt');
await writeFileAtomically(filename, 'a0', {encoding: 'hex'});
t.equal(
await promisifiedReadFile(filename, 'hex'),
'a0',
'should support write-file-atomic options.'
);
})();
(async () => {
const filename = new URL(`${fileUrl(join(__dirname, 'tmp_2.txt'))}?query_params=should_be_ignored`);
await writeFileAtomically(filename, 'a0', 'hex');
t.equal(
await promisifiedReadFile(filename, 'hex'),
'a0',
'should accept a string as its third argument.'
);
})();
const fail = t.fail.bind(t, 'Unexpectedly succceeded.');
(async () => {
try {
await writeFileAtomically(__dirname, '_');
fail();
} catch ({code}) {
t.equal(code, 'EISDIR', 'should fail when it cannot write a file.');
}
})();
try {
await writeFileAtomically(1, '_');
fail();
} catch (err) {
t.equal(
err.toString(),
'TypeError: Expected a file path (<string|Buffer|URL>) to write data, but got an invalid value 1 (number).',
'should fail when it takes a non-string path.'
);
}
try {
await writeFileAtomically('_', '_', new WeakMap());
fail();
} catch (err) {
t.equal(
err.toString(),
'TypeError: Expected the third argument to be a `write-file-atomic` option (plain <Object>) or a valid encoding (<string>), but got WeakMap {}.',
'should fail when it takes a non-plain options object.'
);
}
try {
await writeFileAtomically('_', '_', '123');
fail();
} catch ({message}) {
t.equal(
message,
'Expected the third argument to be a `write-file-atomic` option (plain <Object>) or a valid encoding (<string>), but got an invalid encoding string \'123\'.',
'should fail when it takes an invalid encoding string.'
);
}
try {
await writeFileAtomically('_', '_', '');
fail();
} catch (err) {
t.equal(
err.toString(),
'Error: Expected the third argument to be a `write-file-atomic` option (plain <Object>) or a valid encoding (<string>), but got an empty string.',
'should fail when it takes an empty encoding string.'
);
}
try {
await writeFileAtomically('_', '_', {encoding: new Uint8Array()});
fail();
} catch (err) {
t.equal(
err.toString(),
'TypeError: Expected `encoding` option to be a valid encoding (<string>), but got a non-string value Uint8Array [ ].',
'should fail when it takes a non-string encoding option.'
);
}
try {
await writeFileAtomically('_', '_', {encoding: ''});
fail();
} catch (err) {
t.equal(
err.toString(),
'Error: Expected `encoding` option to be a valid encoding (<string>), but got an empty string.',
'should fail when it takes an empty encoding option.'
);
}
try {
await writeFileAtomically('_', '_', {encoding: 'utf7'});
fail();
} catch (err) {
t.equal(
err.toString(),
'Error: Expected `encoding` option to be a valid encoding, but got an unknown one \'utf7\'.',
'should fail when it takes an unknown encoding option.'
);
}
try {
await writeFileAtomically();
fail();
} catch (err) {
t.equal(
err.toString(),
'RangeError: Expected 2 or 3 arguments (path: <string>, data: <string|Buffer|Uint8Array>[, options: <Object|string>]), but got no arguments.',
'should fail when it takes no arguments.'
);
}
try {
await writeFileAtomically('_', '_', '_', '_');
fail();
} catch (err) {
t.equal(
err.toString(),
'RangeError: Expected 2 or 3 arguments (path: <string>, data: <string|Buffer|Uint8Array>[, options: <Object|string>]), but got 4 arguments.',
'should fail when it takes too many arguments.'
);
}
});