2019-11-14 01:32:20 +00:00
|
|
|
import { expect } from 'chai';
|
2021-07-14 05:13:21 +00:00
|
|
|
import { vol } from 'memfs';
|
2019-11-15 23:45:23 +00:00
|
|
|
import { BaseError } from 'noicejs';
|
|
|
|
import { spy, stub } from 'sinon';
|
2019-11-14 01:32:20 +00:00
|
|
|
import { PassThrough } from 'stream';
|
|
|
|
|
2022-02-02 05:45:57 +00:00
|
|
|
import { Filesystem, readSource, setFs, writeSource } from '../src/source.js';
|
2019-11-14 01:32:20 +00:00
|
|
|
|
|
|
|
export const TEST_STRING = 'hello world';
|
|
|
|
|
2020-04-02 23:58:57 +00:00
|
|
|
describe('load source helper', async () => {
|
2021-07-14 05:13:21 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
vol.reset();
|
|
|
|
});
|
|
|
|
|
2020-04-02 23:58:57 +00:00
|
|
|
it('should read from stdin', async () => {
|
2019-11-14 01:32:20 +00:00
|
|
|
const pt = new PassThrough();
|
|
|
|
|
|
|
|
const futureSource = readSource('-', pt);
|
|
|
|
pt.emit('data', Buffer.from(TEST_STRING));
|
|
|
|
pt.end();
|
|
|
|
pt.destroy();
|
|
|
|
|
|
|
|
const source = await futureSource;
|
|
|
|
expect(source).to.equal(TEST_STRING);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should read from a file', async () => {
|
2021-07-14 05:13:21 +00:00
|
|
|
vol.fromJSON({
|
2019-11-14 01:32:20 +00:00
|
|
|
test: TEST_STRING,
|
|
|
|
});
|
|
|
|
|
2021-07-14 05:13:21 +00:00
|
|
|
const restore = setFs(vol.promises as Filesystem);
|
|
|
|
|
2019-11-14 01:32:20 +00:00
|
|
|
const source = await readSource('test');
|
2021-07-14 05:13:21 +00:00
|
|
|
|
|
|
|
restore();
|
2019-11-14 01:32:20 +00:00
|
|
|
|
|
|
|
expect(source).to.equal(TEST_STRING);
|
|
|
|
});
|
2019-11-15 23:45:23 +00:00
|
|
|
|
|
|
|
it('should handle errors reading from stdin', async () => {
|
|
|
|
const pt = new PassThrough();
|
|
|
|
const futureSource = readSource('-', pt);
|
|
|
|
pt.emit('error', new BaseError('terribad!'));
|
|
|
|
|
|
|
|
return expect(futureSource).to.eventually.be.rejectedWith(BaseError);
|
|
|
|
});
|
2019-11-14 01:32:20 +00:00
|
|
|
});
|
|
|
|
|
2020-04-02 23:58:57 +00:00
|
|
|
describe('write source helper', async () => {
|
2019-11-14 01:32:20 +00:00
|
|
|
it('should write to a file', async () => {
|
2021-07-14 05:13:21 +00:00
|
|
|
vol.fromJSON({
|
2019-11-14 01:32:20 +00:00
|
|
|
test: 'empty',
|
|
|
|
});
|
|
|
|
|
2021-07-14 05:13:21 +00:00
|
|
|
const restore = setFs(vol.promises as Filesystem);
|
|
|
|
|
2019-11-14 01:32:20 +00:00
|
|
|
await writeSource('test', TEST_STRING);
|
|
|
|
const source = await readSource('test');
|
2021-07-14 05:13:21 +00:00
|
|
|
|
|
|
|
restore();
|
2019-11-14 01:32:20 +00:00
|
|
|
|
|
|
|
expect(source).to.equal(TEST_STRING);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should write to stdout', async () => {
|
|
|
|
const pt = new PassThrough();
|
|
|
|
const writeSpy = spy(pt, 'write');
|
|
|
|
|
|
|
|
await writeSource('-', TEST_STRING, pt);
|
|
|
|
pt.end();
|
|
|
|
pt.destroy();
|
|
|
|
|
|
|
|
expect(writeSpy).to.have.been.calledWith(TEST_STRING);
|
|
|
|
});
|
2019-11-15 23:45:23 +00:00
|
|
|
|
|
|
|
it('should handle errors writing to stdout', async () => {
|
|
|
|
const pt = new PassThrough();
|
|
|
|
stub(pt, 'write').yields(new BaseError('terribad!'));
|
|
|
|
|
|
|
|
return expect(writeSource('-', 'test', pt)).to.eventually.be.rejectedWith(BaseError);
|
|
|
|
});
|
2019-11-14 01:32:20 +00:00
|
|
|
});
|