'use strict';
const childProcess = require('child_process');
const {EOL} = require('os');
const {expect} = require('chai');
function getColorCmd(cmd, args, {shortFlags} = {}) {
const flags = Object.entries(args || {}).reduce((s, [flag, value]) => {
return s + `${shortFlags ? '-' : '--'}${flag} ${value} `;
}, '');
const cmds = {
darwin: `CLICOLOR_FORCE="1" ${cmd} | node lib/cli ${flags}`,
linux: `CLICOLOR="1" ${cmd} | node lib/cli ${flags}`,
// for win32 compatibility, make sure there is no space between the cmd and the pipe
win32: `${cmd}| node lib/cli ${flags}`
};
return cmds[process.platform];
}
function echo(str) {
if (process.platform === 'win32') {
return `echo ${str}`;
}
return `echo "${str}"`;
}
function runCLI (cmd, args) {
return new Promise((resolve, reject) => {
childProcess.exec(cmd, {
...args,
timeout: 10000
}, (err, stdout, stderr) => {
if (err) {
return reject(err);
}
if (stderr) {
return reject(stderr);
}
resolve(stdout);
});
});
}
function runColorCLI (data, args, opts) {
return runCLI(getColorCmd(data, args, opts));
}
describe('cli', function () {
it('converts colors', async function () {
const data = echo('what\u001b[0;31m what?');
const result = `what what?${EOL}`;
const stdout = await runColorCLI(data);
expect(stdout).to.equal(result);
});
it('works with flags', async function () {
const data = echo('test\ntest\n');
// Has an additional line break relative to programmatic, due to `echo`
const result = 'test
test
';
const stdout = await runColorCLI(data, {newline: ''});
expect(stdout).to.equal(result);
});
it('works with multiple flags', async function () {
const data = echo('test\n