Skip to content

Commit

Permalink
Fix error prefix length calculation. Expose test run errors via embed…
Browse files Browse the repository at this point in the history
…ding utils for testing purposes. Bump version. (#623)
  • Loading branch information
inikulin authored and AlexanderMoskovkin committed Jun 28, 2016
1 parent 97329b2 commit d395986
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "testcafe",
"version": "0.0.18",
"version": "0.0.19",
"main": "lib/index",
"bin": {
"testcafe": "./bin/testcafe"
Expand Down
2 changes: 1 addition & 1 deletion src/cli/argument-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import promisify from '../utils/promisify';
import { GeneralError } from '../errors/runtime';
import MESSAGE from '../errors/runtime/message';
import getViewPortWidth from '../utils/get-viewport-width';
import wordWrap from '../utils/word-wrap';
import { wordWrap } from '../utils/string';

var ensureDir = promisify(mkdirp);
var stat = promisify(fs.stat);
Expand Down
14 changes: 14 additions & 0 deletions src/embedding-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import ReporterPluginHost from './reporter/plugin-host';
import TestRunErrorFormattableAdapter from './errors/test-run/formattable-adapter';
import testRunErrors from './errors/test-run';

export default {
TestRunErrorFormattableAdapter,
testRunErrors,

buildReporterPlugin (pluginFactory, outStream) {
var plugin = pluginFactory();

return new ReporterPluginHost(plugin, outStream);
}
};
13 changes: 2 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import TestCafe from './testcafe';
import ReporterPluginHost from './reporter/plugin-host';
import TestRunErrorFormattableAdapter from './errors/test-run/formattable-adapter';
import * as endpointUtils from 'endpoint-utils';
import { GeneralError } from './errors/runtime';
import MESSAGE from './errors/runtime/message';
import embeddingUtils from './embedding-utils';
import commonAPI from './api/common';

// Validations
Expand Down Expand Up @@ -45,15 +44,7 @@ async function createTestCafe (hostname, port1, port2) {
}

// Embedding utils
createTestCafe.embeddingUtils = {
TestRunErrorFormattableAdapter: TestRunErrorFormattableAdapter,

buildReporterPlugin (pluginFactory, outStream) {
var plugin = pluginFactory();

return new ReporterPluginHost(plugin, outStream);
}
};
createTestCafe.embeddingUtils = embeddingUtils;

// Common API
Object.keys(commonAPI).forEach(key => createTestCafe[key] = commonAPI[key]);
Expand Down
4 changes: 2 additions & 2 deletions src/reporter/plugin-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { identity, escape as escapeHtml, assignIn } from 'lodash';
import moment from 'moment';
import 'moment-duration-format';
import OS from 'os-family';
import wordWrap from '../utils/word-wrap';
import { wordWrap, removeTTYColors } from '../utils/string';
import getViewportWidth from '../utils/get-viewport-width';

// NOTE: we should not expose internal state to
Expand Down Expand Up @@ -91,7 +91,7 @@ export default class ReporterPluginHost {
}

formatError (err, prefix = '') {
var maxMsgLength = this[viewportWidth] - this[indent] - prefix.length;
var maxMsgLength = this[viewportWidth] - this[indent] - removeTTYColors(prefix).length;
var msg = err.formatMessage(this[errorDecorator], maxMsgLength);

if (this[wordWrapEnabled])
Expand Down
14 changes: 7 additions & 7 deletions src/utils/word-wrap.js → src/utils/string.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import indentString from 'indent-string';

function cutTtyColors (str) {
return str.replace(/\033\[[0-9;]*m/g, '');
}

function rtrim (str) {
return str.replace(/\s+$/, '');
}

export default function (str, indent, width) {
export function removeTTYColors (str) {
return str.replace(/\033\[[0-9;]*m/g, '');
}

export function wordWrap (str, indent, width) {
var curStr = '';
var wrappedMsg = '';

if (cutTtyColors(str).length <= width - indent)
if (removeTTYColors(str).length <= width - indent)
return indentString(str, ' ', indent);

str = str.replace(/(\r\n)/gm, '\n')
Expand All @@ -23,7 +23,7 @@ export default function (str, indent, width) {
str.forEach(word => {
var newStr = curStr + word;

if (cutTtyColors(newStr).length > width - indent) {
if (removeTTYColors(newStr).length > width - indent) {
wrappedMsg += `${rtrim(curStr)}\n`;
curStr = word;
}
Expand Down

0 comments on commit d395986

Please sign in to comment.