Skip to content

Commit

Permalink
More coverage and polish.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jan 6, 2019
1 parent dfb6d07 commit fa5690c
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 48 deletions.
6 changes: 4 additions & 2 deletions packages/core/src/Console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class Console extends Emitter {
this.tool = tool;
this.writers = testWriters;

/* istanbul ignore next */
// istanbul ignore next
if (process.env.NODE_ENV !== 'test') {
process
.on('SIGINT', this.handleSignal)
Expand Down Expand Up @@ -236,7 +236,7 @@ export default class Console extends Emitter {
/**
* Handle the final rendering of all output before stopping.
*/
renderFinalOutput(error: Error | null = null) {
renderFinalOutput(error: Error | null) {
this.debug('Rendering final console output');

// Mark all output as final
Expand Down Expand Up @@ -356,6 +356,7 @@ export default class Console extends Emitter {
/**
* Unwrap the native console and reset it back to normal.
*/
// istanbul ignore next
unwrapStreams() {
this.debug('Unwrapping `stderr` and `stdout` streams');

Expand All @@ -371,6 +372,7 @@ export default class Console extends Emitter {
* Wrap the `stdout` and `stderr` streams and buffer the output as
* to not collide with our render loop.
*/
// istanbul ignore next
wrapStreams() {
this.debug('Wrapping `stderr` and `stdout` streams');

Expand Down
44 changes: 22 additions & 22 deletions packages/core/src/CrashLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class CrashLogger {

logPath: string;

// istanbul ignore next
constructor(tool: Tool<any>) {
// @ts-ignore Allow private access of pluginTypes
const { config, console, options, pluginTypes } = tool;
Expand All @@ -28,31 +29,9 @@ export default class CrashLogger {
try {
this.add('Yarn', String(execa.shellSync('yarn --version').stdout));
} catch {
// istanbul ignore next
this.add('Yarn', '(Not installed)');
}

this.addTitle('Tool Instance');
this.add('App name', options.appName);
this.add('App path', options.appPath);
this.add('Plugin types', Object.keys(pluginTypes).join(', '));
this.add('Scoped package', options.scoped ? 'Yes' : 'No');
this.add('Root', options.root);
this.add('Config name', options.configName);
this.add('Package path', path.join(options.root, 'package.json'));
this.add('Workspaces root', options.workspaceRoot || '(Not enabled)');
this.add(
'Extending configs',
config.extends.length > 0 ? util.inspect(config.extends) : '(Not extending)',
);

this.addTitle('Console Instance');
this.add('Logs', console.logs.length > 0 ? console.logs.join('\n ') : '(No logs)');
this.add(
'Error logs',
console.errorLogs.length > 0 ? console.errorLogs.join('\n ') : '(No error logs)',
);

this.addTitle('Process');
this.add('ID', process.pid);
this.add('Title', process.title);
Expand All @@ -73,6 +52,27 @@ export default class CrashLogger {
this.add('Group ID', process.getgid());
this.add('User ID', process.getuid());
}

this.addTitle('Tool Instance');
this.add('App name', options.appName);
this.add('App path', options.appPath);
this.add('Plugin types', Object.keys(pluginTypes).join(', '));
this.add('Scoped package', options.scoped ? 'Yes' : 'No');
this.add('Root', options.root);
this.add('Config name', options.configName);
this.add('Package path', path.join(options.root, 'package.json'));
this.add('Workspaces root', options.workspaceRoot || '(Not enabled)');
this.add(
'Extending configs',
config.extends.length > 0 ? util.inspect(config.extends) : '(Not extending)',
);

this.addTitle('Console Instance');
this.add('Logs', console.logs.length > 0 ? console.logs.join('\n ') : '(No logs)');
this.add(
'Error logs',
console.errorLogs.length > 0 ? console.errorLogs.join('\n ') : '(No error logs)',
);
}

add(label: string, message: string | number) {
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/Emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ export type EventListener = (...args: EventArguments) => false | void;
export default class Emitter {
listeners: { [eventName: string]: Set<EventListener> } = {};

/**
* Remove all listeners for the defined event name.
*/
clearListeners(eventName: string): this {
this.getListeners(eventName).clear();

return this;
}

/**
* Syncronously execute listeners for the defined event and arguments.
*/
Expand All @@ -37,15 +46,6 @@ export default class Emitter {
// return value;
// }

/**
* Remove all listeners for the defined event name.
*/
clearListeners(eventName: string): this {
this.getListeners(eventName).clear();

return this;
}

/**
* Return all event names with registered listeners.
*/
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/Reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { Color, ColorType, ColorPalette, OutputLevel } from './types';
export const SLOW_THRESHOLD = 10000; // ms

export default class Reporter<Options = {}> extends Plugin<Options> {
// eslint-disable-next-line no-magic-numbers
static BUFFER = 10;

static OUTPUT_COMPACT = 1;

static OUTPUT_NORMAL = 2;
Expand Down Expand Up @@ -98,11 +101,9 @@ export default class Reporter<Options = {}> extends Plugin<Options> {
return 'success';
} else if (task.hasFailed()) {
return 'failure';
} else if (task.isPending() || task.isRunning()) {
return 'pending';
}

return 'default';
return 'pending';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default class Tool<
loadBoostModules: true,
});

/* istanbul ignore next */
// istanbul ignore next
if (process.env.NODE_ENV !== 'test') {
// Add a reporter to catch errors during initialization
this.addPlugin('reporter', new ErrorReporter());
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/reporters/BoostReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import Reporter from '../Reporter';
import Routine from '../Routine';

const BUFFER = 10;

export interface LineParts {
prefix: string;
suffix: string;
Expand Down Expand Up @@ -89,7 +87,7 @@ export default class BoostReporter extends Reporter {

// Routine line
output += prefix;
output += this.truncate(title, columns - prefixLength - suffixLength - BUFFER);
output += this.truncate(title, columns - prefixLength - suffixLength - Reporter.BUFFER);

if (suffix) {
output += ' ';
Expand All @@ -101,15 +99,15 @@ export default class BoostReporter extends Reporter {
// Active task lines
routine.tasks.forEach(task => {
if (task.isRunning()) {
let line = this.strip(task.statusText || task.title);
let line = this.strip(task.statusText || task.title).trim();

if (this.getOutputLevel() >= Reporter.OUTPUT_NORMAL) {
line += ` [${task.metadata.index}/${task.parent!.tasks.length}]`;
}

output += this.truncate(
this.indent(prefixLength) + this.style(line, 'pending'),
columns - BUFFER,
columns - Reporter.BUFFER,
);
output += '\n';
}
Expand Down
20 changes: 14 additions & 6 deletions packages/core/tests/Console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,13 @@ describe('Console', () => {

describe('logLive()', () => {
it('adds a log via `console.log`', () => {
const old = console.log.bind(console);
const spy = jest.fn();

console.log = spy;
const spy = jest.spyOn(process.stdout, 'write');

cli.logLive('foo');

expect(spy).toHaveBeenCalledWith('foo');

console.log = old;
spy.mockRestore();
});
});

Expand Down Expand Up @@ -271,7 +268,9 @@ describe('Console', () => {
});
});

describe.skip('renderFinalOutput()', () => {});
describe.skip('renderFinalOutput()', () => {
// TODO
});

describe('resetCursor()', () => {
it('writes ansi escape code', () => {
Expand Down Expand Up @@ -359,6 +358,15 @@ describe('Console', () => {
expect(cli.stopping).toBe(true);
});

it('only triggers once if stopping', () => {
const spy = jest.spyOn(cli, 'renderFinalOutput');

cli.stop();
cli.stop();

expect(spy).toHaveBeenCalledTimes(1);
});

it('calls `stop` with null (no error)', () => {
const spy = jest.fn();

Expand Down
13 changes: 13 additions & 0 deletions packages/core/tests/CrashLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,17 @@ describe('CrashLogger', () => {
'utf8',
);
});

it('with console information', () => {
tool.console.log('Log');
tool.console.logError('Error');

logger.log(new Error());

expect(spy).toHaveBeenCalledWith(
path.join(tool.options.root, 'test-boost-error.log'),
expect.anything(),
'utf8',
);
});
});
13 changes: 13 additions & 0 deletions packages/core/tests/Emitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ describe('Emitter', () => {
emitter = new Emitter();
});

describe('clearListeners()', () => {
it('deletes all listeners', () => {
emitter.on('foo', () => {});
emitter.on('foo', () => {});

expect(emitter.getListeners('foo').size).toBe(2);

emitter.clearListeners('foo');

expect(emitter.getListeners('foo').size).toBe(0);
});
});

describe('emit()', () => {
it('executes listeners in order', () => {
let output = '';
Expand Down
14 changes: 14 additions & 0 deletions packages/core/tests/Reporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,20 @@ describe('Reporter', () => {
});
});

describe('getRootParent()', () => {
it('returns the root parent of a routine tree', () => {
const a = createTestRoutine(reporter.tool as any);
const b = createTestRoutine(reporter.tool as any);
const c = createTestRoutine(reporter.tool as any);

a.pipe(b.pipe(c));

expect(reporter.getRootParent(c)).toBe(a);
expect(reporter.getRootParent(b)).toBe(a);
expect(reporter.getRootParent(a)).toBe(a);
});
});

describe('handleBaseStart()', () => {
const oldCI = process.env.CI;

Expand Down

0 comments on commit fa5690c

Please sign in to comment.