-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.js
47 lines (34 loc) · 1.71 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
const prependFile = require('.');
const fs = require('fs');
const test = require('ava');
const {promises: fsP} = fs;
const temporaryFile = '.temp1';
const syncTemporaryFile = '.temp2';
test.after.always(async () => {
await fsP.unlink(temporaryFile);
await fsP.unlink(syncTemporaryFile);
});
test('main', async t => {
await prependFile(temporaryFile, 'World');
t.is(await fsP.readFile(temporaryFile, 'utf8'), 'World');
await prependFile(temporaryFile, 'Hello ');
t.is(await fsP.readFile(temporaryFile, 'utf8'), 'Hello World');
await prependFile(temporaryFile, Buffer.from('Yes '));
t.is(await fsP.readFile(temporaryFile, 'utf8'), 'Yes Hello World');
await prependFile(temporaryFile, Buffer.from('\uFEFF'));
t.is(encodeURIComponent(await fsP.readFile(temporaryFile, 'utf8')), encodeURIComponent('\uFEFFYes Hello World'));
await prependFile(temporaryFile, Buffer.from('What '));
t.is(encodeURIComponent(await fsP.readFile(temporaryFile, 'utf8')), encodeURIComponent('\uFEFFWhat Yes Hello World'));
});
test('.sync', t => {
prependFile.sync(syncTemporaryFile, 'World');
t.is(fs.readFileSync(syncTemporaryFile, 'utf8'), 'World');
prependFile.sync(syncTemporaryFile, 'Hello ');
t.is(fs.readFileSync(syncTemporaryFile, 'utf8'), 'Hello World');
prependFile.sync(syncTemporaryFile, Buffer.from('Yes '));
t.is(fs.readFileSync(syncTemporaryFile, 'utf8'), 'Yes Hello World');
prependFile.sync(syncTemporaryFile, '\uFEFF');
t.is(encodeURIComponent(fs.readFileSync(syncTemporaryFile, 'utf8')), encodeURIComponent('\uFEFFYes Hello World'));
prependFile.sync(syncTemporaryFile, 'What ');
t.is(encodeURIComponent(fs.readFileSync(syncTemporaryFile, 'utf8')), encodeURIComponent('\uFEFFWhat Yes Hello World'));
});