-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
44 lines (37 loc) · 1.56 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
'use strict';
const test = require('ava');
const del = require('del');
const path = require('path');
const fs = require('fs');
const fsu = require('./index.js');
test.after.always(() => del([ './test' ]));
test.serial('write unique file', async t => {
const filePath = await fsu.writeFileUnique(path.join('test', 'test{_file###}.txt'), 'test', { force: true });
t.true(filePath.endsWith('test.txt'));
});
test.serial('write file with a file name without pattern', async t => {
const filePath = await fsu.writeFileUnique(path.join('test', 'test1.txt'), 'test', { force: true });
t.true(filePath.endsWith('test1.txt'));
});
test.serial.cb('writes unique file with fs stream', t => {
const stream = fsu.createWriteStreamUnique(path.join('test', 'test{_stream###}.txt'), { force: true });
fs.createReadStream('readme.md').pipe(stream).on('finish', () => {
t.true(stream.path.endsWith('test_stream001.txt'));
t.end();
});
});
test.serial.cb('fails to write unique file with a stream to non existed directory', t => {
const stream = fsu.createWriteStreamUnique(path.join('nonexisted', 'test{_stream###}.txt'));
fs.createReadStream('readme.md').pipe(stream)
.on('finish', () => t.fail())
.on('error', () => t.end())
});
test.serial.cb('writes big unique file with fs stream', t => {
const stream = fsu.createWriteStreamUnique(path.join('test', 'test{_stream###}.txt'), { force: true });
fs.createReadStream('package-lock.json')
.pipe(stream)
.on('finish', () => {
t.true(stream.path.endsWith('test_stream002.txt'));
t.end();
});
});