gulp-spawn
Version:
spawn plugin for gulp
159 lines (141 loc) • 3.76 kB
JavaScript
import Stream from 'node:stream';
import assert from 'node:assert';
import es from 'event-stream';
import Vinyl from 'vinyl';
import gSpawn from '../index.js';
describe('gulp-spawn', () => {
describe('in stream mode', () => {
it('should work with async contents streams', done => {
const stream = gSpawn({
cmd: 'cat',
});
const inputStream = new Stream.PassThrough({objectMode: true});
const outputStream = new Stream.PassThrough({objectMode: true});
let n = 0;
const fakeFile = new Vinyl({
cwd: './',
base: 'test',
path: 'test/file.js',
contents: new Stream.PassThrough(),
});
const fakeFile2 = new Vinyl({
cwd: './',
base: 'test',
path: 'test/file2.js',
contents: new Stream.PassThrough(),
});
inputStream.pipe(stream).pipe(outputStream);
outputStream.on('readable', () => {
let newFile;
while ((newFile = outputStream.read())) {
assert(newFile);
assert.equal(newFile.cwd, './');
assert.equal(newFile.base, 'test');
assert(newFile.contents instanceof Stream);
if (++n === 1) {
assert.equal(newFile.path, 'test/file.js');
newFile.contents.pipe(
es.wait((error, data) => {
assert.equal(data, 'plipplap');
}),
);
} else {
assert.equal(newFile.path, 'test/file2.js');
newFile.contents.pipe(
es.wait((error, data) => {
assert.equal(data, 'plopplup');
}),
);
}
}
});
outputStream.on('end', () => {
assert.equal(n, 2);
done();
});
inputStream.write(fakeFile);
inputStream.write(fakeFile2);
inputStream.end();
setImmediate(() => {
fakeFile.contents.write('plip');
setImmediate(() => {
fakeFile.contents.write('plap');
fakeFile.contents.end();
});
});
setImmediate(() => {
fakeFile2.contents.write('plop');
setImmediate(() => {
fakeFile2.contents.write('plup');
fakeFile2.contents.end();
});
});
});
it('should work with async files streams', done => {
const stream = gSpawn({
cmd: 'cat',
});
const inputStream = new Stream.PassThrough({objectMode: true});
const outputStream = new Stream.PassThrough({objectMode: true});
let n = 0;
const fakeFile = new Vinyl({
cwd: './',
base: 'test',
path: 'test/file.js',
contents: new Stream.PassThrough(),
});
const fakeFile2 = new Vinyl({
cwd: './',
base: 'test',
path: 'test/file2.js',
contents: new Stream.PassThrough(),
});
inputStream.pipe(stream).pipe(outputStream);
outputStream.on('readable', () => {
let newFile;
while ((newFile = outputStream.read())) {
assert(newFile);
assert.equal(newFile.cwd, './');
assert.equal(newFile.base, 'test');
assert(newFile.contents instanceof Stream);
if (++n === 1) {
assert.equal(newFile.path, 'test/file.js');
newFile.contents.pipe(
es.wait((error, data) => {
assert.equal(data, 'plipplap');
}),
);
} else {
assert.equal(newFile.path, 'test/file2.js');
newFile.contents.pipe(
es.wait((error, data) => {
assert.equal(data, 'plopplup');
}),
);
}
}
});
outputStream.on('end', () => {
assert.equal(n, 2);
done();
});
setImmediate(() => {
inputStream.write(fakeFile);
fakeFile.contents.write('plip');
setImmediate(() => {
fakeFile.contents.write('plap');
fakeFile.contents.end();
});
setImmediate(() => {
inputStream.write(fakeFile2);
inputStream.end();
fakeFile2.contents.write('plop');
setImmediate(() => {
fakeFile2.contents.write('plup');
fakeFile2.contents.end();
});
});
});
});
});
});