-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
95 lines (76 loc) · 2.52 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
import promisify from 'promisify-node';
import fs from 'fs';
const access = promisify(fs.access);
import test from 'ava';
import File from 'vinyl';
import pEvent from 'p-event';
import path from 'path';
import mkdirp from 'mkdirp-promise';
import del from 'del';
import multiDest from './index';
import through from 'through2';
import _ from 'lodash';
const testOutputDirectory = './.test-output';
test('write files to all specified locations', async t => {
const destinations = [
path.join(testOutputDirectory, 'test1/dest1'),
path.join(testOutputDirectory, 'test1/dest2')
];
await del(destinations);
for (let dest of destinations) {
await mkdirp(dest);
}
const fakeFileName = 'test-file.txt';
const fakeFile = new File({
cwd: '/home',
base: '/fake/path',
path: `/fake/path/${fakeFileName}`,
contents: new Buffer('test content')
});
const pluginStream = multiDest(destinations);
const finish = pEvent(pluginStream, 'finish');
pluginStream.end(fakeFile);
await finish;
for (let dest of destinations) {
let filePath = path.join(dest, fakeFileName);
let err = await access(filePath);
t.ifError(err, `File ${filePath} was not created`);
}
});
test('all written files are piped through', async t => {
const destinations = [
path.join(testOutputDirectory, 'test2/dest1'),
path.join(testOutputDirectory, 'test2/dest2')
];
const createdFiles = [];
await del(destinations);
for (let dest of destinations) {
await mkdirp(dest);
}
const fakeFileName = 'test-file.txt';
const fakeFile = new File({
cwd: '/home',
base: '/fake/path',
path: `/fake/path/${fakeFileName}`,
contents: new Buffer('test content')
});
const pluginStream = multiDest(destinations);
const finish = pEvent(pluginStream, 'finish');
pluginStream
.pipe(through.obj(function (file, encoding, done) {
let transform = this;
createdFiles.push(file.path);
transform.push(file);
done();
}));
pluginStream.end(fakeFile);
await finish;
t.is(createdFiles.length, 2);
let filesThatShouldHaveBeenCreated = destinations.map(dest => {
return path.resolve(path.join(dest, fakeFileName));
});
const allFilesWereCreated = _.every(createdFiles, filePath => {
return _.includes(filesThatShouldHaveBeenCreated, path.resolve(filePath));
})
t.is(allFilesWereCreated, true);
});