1
0
Fork 0

fix(tests): cover source read and write

This commit is contained in:
ssube 2019-11-13 19:32:20 -06:00 committed by Sean Sube
parent b7118f11da
commit 838f87baf5
3 changed files with 89 additions and 16 deletions

View File

@ -5,7 +5,7 @@ import { loadConfig } from './config';
import { CONFIG_ARGS_NAME, CONFIG_ARGS_PATH, MODE, parseArgs, VALID_MODES } from './config/args';
import { YamlParser } from './parser/YamlParser';
import { createRuleSelector, createRuleSources, loadRules, resolveRules, visitRules } from './rule';
import { loadSource, writeSource } from './source';
import { readSource, writeSource } from './source';
import { VERSION_INFO } from './version';
import { VisitorContext } from './visitor/VisitorContext';
@ -60,7 +60,7 @@ export async function main(argv: Array<string>): Promise<number> {
}
const parser = new YamlParser();
const source = await loadSource(args.source);
const source = await readSource(args.source);
const docs = parser.parse(source);
for (const data of docs) {

View File

@ -8,11 +8,9 @@ export const readDir = promisify(readdir);
export const readFile = promisify(readBack);
export const writeFile = promisify(writeBack);
export async function loadSource(path: string): Promise<string> {
export async function readSource(path: string, stream = process.stdin): Promise<string> {
if (path === '-') {
return readFile(0, {
encoding: FILE_ENCODING,
});
return readStream(stream, FILE_ENCODING);
} else {
return readFile(path, {
encoding: FILE_ENCODING,
@ -20,20 +18,36 @@ export async function loadSource(path: string): Promise<string> {
}
}
export async function writeSource(path: string, data: string): Promise<void> {
if (path === '-') {
return new Promise((res, rej) => {
process.stdout.write(data, (err: Error | null | undefined) => {
if (isNil(err)) {
res();
} else {
rej(err);
}
});
export function readStream(stream: NodeJS.ReadStream, encoding: string): Promise<string> {
return new Promise((res, rej) => {
const chunks: Array<Buffer> = [];
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('end', () => {
const result = Buffer.concat(chunks).toString(encoding);
res(result);
});
stream.on('error', (err) => rej(err));
});
}
export async function writeSource(path: string, data: string, stream = process.stdout): Promise<void> {
if (path === '-') {
return writeStream(stream, data);
} else {
return writeFile(path, data, {
encoding: FILE_ENCODING,
});
}
}
export function writeStream(stream: NodeJS.WriteStream, data: string): Promise<void> {
return new Promise((res, rej) => {
stream.write(data, (err: Error | null | undefined) => {
if (isNil(err)) {
res();
} else {
rej(err);
}
});
});
}

59
test/TestSource.ts Normal file
View File

@ -0,0 +1,59 @@
import { expect } from 'chai';
import mockFs from 'mock-fs';
import { spy } from 'sinon';
import { PassThrough } from 'stream';
import { readSource, writeSource } from '../src/source';
import { describeLeaks, itLeaks } from './helpers/async';
export const TEST_STRING = 'hello world';
describeLeaks('load source helper', async () => {
itLeaks('should read from stdin', async () => {
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 () => {
mockFs({
test: TEST_STRING,
});
const source = await readSource('test');
mockFs.restore();
expect(source).to.equal(TEST_STRING);
});
});
describeLeaks('write source helper', async () => {
it('should write to a file', async () => {
mockFs({
test: 'empty',
});
await writeSource('test', TEST_STRING);
const source = await readSource('test');
mockFs.restore();
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);
});
});