Skip to content

Commit

Permalink
Unify icon symbol for stdout pipeline (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed Apr 14, 2018
1 parent 37714ad commit a710f53
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 110 deletions.
56 changes: 38 additions & 18 deletions lib/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,40 @@ const cliTruncate = require('cli-truncate');
const stringWidth = require('string-width');
const ansiEscapes = require('ansi-escapes');
const isCI = require('ci-info').isCI;
const isWin = !isCI && require('is-windows')();
const icons = isWin && !process.env.VSCODE_PID ? {
// https://github.com/sindresorhus/log-symbols
success: '√',
info: 'i',
warn: '‼',
error: '×',
} : require('./icons');
const icons = require('./icons');
let fixIcon;

if (!isCI && process.stdout.isTTY && require('is-windows')()) {
if (process.env.VSCODE_PID) {
fixIcon = str => {
if (str === icons.error) {
str = str[0] + ' ';
} else if (str !== icons.info) {
str += ' ';
}
return str;
};
} else if (process.env.ConEmuPID) {
fixIcon = str => {
if (str === icons.info) {
str = '‼';
}
return str;
};
} else {
fixIcon = str => {
str = str[0];
return str;
};
}
} else {
fixIcon = str => {
if (str === icons.error) {
str = str[0];
}
return str;
};
}

const msgColor = {
success: chalk.green,
Expand All @@ -24,11 +50,11 @@ const msgColor = {

let padNum;
if (String.prototype.padStart) {
padNum = function (number, targetLength) {
padNum = (number, targetLength) => {
return String(number).padStart(targetLength, '0');
};
} else {
padNum = function (number, targetLength) {
padNum = (number, targetLength) => {
number = String(number);
return ('0').repeat(Math.max(targetLength - number.length, 0)) + number;
};
Expand All @@ -47,6 +73,7 @@ function justify (left, right, width) {
}
return left + ' '.repeat(Math.max(width, left && right ? 1 : 0)) + right;
}

function timeFormat (timestamp) {
timestamp = new Date(timestamp * 1000);
return [
Expand Down Expand Up @@ -138,14 +165,7 @@ function formatError (error, fmtOpts) {
message.push(mainMessage);
}

if (isWin && process.env.VSCODE_PID) {
message[0] = message[0].replace(/\S\uFE0F/g, str => {
if (str !== icons.info) {
str += ' ';
}
return str;
});
}
message[0] = message[0].replace(/\S\uFE0F/g, fixIcon);

if (fmtOpts.source && error.source) {
let source = fmtOpts.sourceCache[error.source];
Expand Down
205 changes: 113 additions & 92 deletions test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,106 +367,127 @@ describe('API', () => {
]);
});

it('mock Windows', () => {
describe('mock Windows', () => {
const fileName = path.join(__dirname, 'fixtures/testcase');
const VSCODE_PID = process.env.VSCODE_PID;
const ConEmuPID = process.env.ConEmuPID;
const isTTY = process.stdout.isTTY;
before(() => {
process.stdout.isTTY = true;
});
after(() => {
if (VSCODE_PID) {
process.env.VSCODE_PID = VSCODE_PID;
} else {
delete process.env.VSCODE_PID;
}
if (ConEmuPID) {
process.env.ConEmuPID = ConEmuPID;
} else {
delete process.env.ConEmuPID;
}
if (isTTY) {
process.stdout.isTTY = isTTY;
} else {
delete process.stdout.isTTY;
}
});
function splitLog (log) {
/* eslint-disable-next-line no-control-regex */
return stripAnsi(log.replace(/\u001b]50;\w+=.+?\u0007/g, '')).split('\n');
}
const VSCODE_PID = process.env.VSCODE_PID;
const fileName = path.join(__dirname, 'fixtures/testcase');
function formatter (env, error) {
delete process.env.VSCODE_PID;
delete process.env.ConEmuPID;
Object.assign(process.env, env);
const formatter = proxyquire('../lib/formatter', {
'ci-info': {
isCI: false,
},
'is-windows': () => true,
});
return splitLog(formatter({
cwd: __dirname,
path: fileName,
report: {
errors: [Object.assign({}, {
message: 'testcase message.',
source: 'testcase source',
fileName,
}, error)],
},
}, {
blame: false,
_termColumns: 60,
}));
}

process.env.VSCODE_PID = 'mock_pid';
let formatter = proxyquire('../lib/formatter', {
'ci-info': {
isCI: false,
},
'is-windows': () => true,
it('vscode', () => {
assert.deepEqual(formatter({
VSCODE_PID: 'mock_pid',
}), [
'fixtures/testcase',
' 01:01 \u{2714}\u{FE0F} testcase message.',
]);
assert.deepEqual(formatter({
VSCODE_PID: 'mock_pid',
}, {
severity: 'error',
}), [
'fixtures/testcase',
' 01:01 \u{274C} testcase message.',
]);
assert.deepEqual(formatter({
VSCODE_PID: 'mock_pid',
}, {
severity: 'info',
}), [
'fixtures/testcase',
' 01:01 \u{2139}\u{FE0F} testcase message.',
]);
});

assert.deepEqual(splitLog(formatter({
cwd: __dirname,
path: fileName,
report: {
errors: [{
message: 'testcase message.',
source: 'testcase source',
fileName,
}],
},
}, {
blame: false,
_termColumns: 60,
})), [
'fixtures/testcase',
' 01:01 \u{2714}\u{FE0F} testcase message.',
]);

assert.deepEqual(splitLog(formatter({
cwd: __dirname,
path: fileName,
report: {
errors: [{
message: 'testcase message.',
source: 'testcase source',
severity: 'info',
fileName,
}],
},
}, {
blame: false,
_termColumns: 60,
})), [
'fixtures/testcase',
' 01:01 \u{2139}\u{FE0F} testcase message.',
]);

delete process.env.VSCODE_PID;
formatter = proxyquire('../lib/formatter', {
'ci-info': {
isCI: false,
},
'is-windows': () => true,
it('ConEmu', () => {
assert.deepEqual(formatter({
ConEmuPID: 'mock_pid',
}, {
}), [
'fixtures/testcase',
' 01:01 \u{2714}\u{FE0F} testcase message.',
]);
assert.deepEqual(formatter({
ConEmuPID: 'mock_pid',
}, {
severity: 'error',
}), [
'fixtures/testcase',
' 01:01 \u{274C}\u{FE0F} testcase message.',
]);
assert.deepEqual(formatter({
ConEmuPID: 'mock_pid',
}, {
severity: 'info',
}), [
'fixtures/testcase',
' 01:01 ‼ testcase message.',
]);
});
it('cmd', () => {
assert.deepEqual(formatter({
}, {
severity: 'error',
}), [
'fixtures/testcase',
' 01:01 \u{274C} testcase message.',
]);
assert.deepEqual(formatter({
}, {
severity: 'info',
}), [
'fixtures/testcase',
' 01:01 \u{2139} testcase message.',
]);
});

assert.deepEqual(splitLog(formatter({
cwd: __dirname,
path: fileName,
report: {
errors: [{
message: 'testcase message.',
source: 'testcase source',
fileName,
}],
},
}, {
blame: false,
_termColumns: 60,
})), [
'fixtures/testcase',
' 01:01 √ testcase message.',
]);

assert.deepEqual(splitLog(formatter({
cwd: __dirname,
path: fileName,
report: {
errors: [{
message: 'testcase message.',
source: 'testcase source',
fileName,
}],
},
}, {
blame: false,
_termColumns: 60,
})), [
'fixtures/testcase',
' 01:01 √ testcase message.',
]);

if (VSCODE_PID) {
process.env.VSCODE_PID = VSCODE_PID;
}
});
});

Expand Down

0 comments on commit a710f53

Please sign in to comment.