Skip to content

Commit 3e2d7cd

Browse files
authoredApr 22, 2020
Merge pull request #72 from brettz9/cli-coverage
Cli coverage
2 parents 6e25fb6 + ec29bb8 commit 3e2d7cd

File tree

4 files changed

+96
-24
lines changed

4 files changed

+96
-24
lines changed
 

‎.eslintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"node": true,
66
"mocha": true
77
},
8+
"parserOptions": {
9+
"ecmaVersion": "2018"
10+
},
811
"extends": "eslint:recommended",
912
"rules": {
1013
"strict": ["error"],

‎src/cli.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ const args = {
2121
let file = null,
2222
skip = false;
2323

24+
const argv = process.argv.slice(2);
2425
for (
25-
let i = 2, j = 2, ref = process.argv.length;
26-
2 <= ref ? j < ref : j > ref;
27-
i = 2 <= ref ? ++j : --j
26+
let i = 0, len = argv.length;
27+
i < len;
28+
++i
2829
) {
2930
if (skip) {
3031
skip = false;
3132
continue;
3233
}
33-
34-
switch (process.argv[i]) {
34+
switch (argv[i]) {
3535
case '-n':
3636
case '--newline':
3737
args.newline = true;
@@ -42,26 +42,28 @@ for (
4242
break;
4343
case '-f':
4444
case '--fg':
45-
args.fg = process.argv[i + 1];
45+
args.fg = argv[i + 1];
4646
skip = true;
4747
break;
4848
case '-b':
4949
case '--bg':
50-
args.bg = process.argv[i + 1];
50+
args.bg = argv[i + 1];
5151
skip = true;
5252
break;
5353
case '-v':
5454
case '--version':
5555
console.log(require(__dirname + '/../package.json').version);
5656
process.exit(0);
57+
// istanbul ignore next
5758
break;
5859
case '-h':
5960
case '--help':
6061
console.log(help);
6162
process.exit(0);
63+
// istanbul ignore next
6264
break;
6365
default:
64-
file = process.argv[i];
66+
file = argv[i];
6567
}
6668
}
6769

‎test/cli.js

+81-16
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ const childProcess = require('child_process');
33
const {EOL} = require('os');
44
const {expect} = require('chai');
55

6-
function getColorCmd(cmd) {
6+
function getColorCmd(cmd, args, {shortFlags} = {}) {
7+
const flags = Object.entries(args || {}).reduce((s, [flag, value]) => {
8+
return s + `${shortFlags ? '-' : '--'}${flag} ${value} `;
9+
}, '');
710
const cmds = {
8-
darwin: `CLICOLOR_FORCE="1" ${cmd} | node lib/cli`,
9-
linux: `CLICOLOR="1" ${cmd} | node lib/cli`,
11+
darwin: `CLICOLOR_FORCE="1" ${cmd} | node lib/cli ${flags}`,
12+
linux: `CLICOLOR="1" ${cmd} | node lib/cli ${flags}`,
1013
// for win32 compatibility, make sure there is no space between the cmd and the pipe
11-
win32: `${cmd}| node lib/cli`
14+
win32: `${cmd}| node lib/cli ${flags}`
1215
};
13-
1416
return cmds[process.platform];
1517
}
1618

@@ -21,25 +23,88 @@ function echo(str) {
2123
return `echo "${str}"`;
2224
}
2325

24-
describe('cli', function () {
25-
it('converts colors', function (done) {
26-
const data = echo('what\u001b[0;31m what?');
27-
const result = `what<span style="color:#A00"> what?${EOL}</span>`;
28-
29-
childProcess.exec(getColorCmd(data), {
26+
function runCLI (cmd, args) {
27+
return new Promise((resolve, reject) => {
28+
childProcess.exec(cmd, {
29+
...args,
3030
timeout: 10000
3131
}, (err, stdout, stderr) => {
3232
if (err) {
33-
return done(err);
33+
return reject(err);
3434
}
3535

3636
if (stderr) {
37-
return done(stderr);
37+
return reject(stderr);
3838
}
3939

40-
expect(stdout).to.equal(result);
41-
42-
done();
40+
resolve(stdout);
4341
});
4442
});
43+
}
44+
45+
function runColorCLI (data, args, opts) {
46+
return runCLI(getColorCmd(data, args, opts));
47+
}
48+
49+
describe('cli', function () {
50+
it('converts colors', async function () {
51+
const data = echo('what\u001b[0;31m what?');
52+
const result = `what<span style="color:#A00"> what?${EOL}</span>`;
53+
54+
const stdout = await runColorCLI(data);
55+
expect(stdout).to.equal(result);
56+
});
57+
58+
it('works with flags', async function () {
59+
const data = echo('test\ntest\n');
60+
// Has an additional line break relative to programmatic, due to `echo`
61+
const result = 'test<br/>test<br/><br/>';
62+
63+
const stdout = await runColorCLI(data, {newline: ''});
64+
expect(stdout).to.equal(result);
65+
});
66+
67+
it('works with multiple flags', async function () {
68+
const data = echo('test\n<test\n');
69+
// Has an additional line break relative to programmatic, due to `echo`
70+
const result = 'test<br/>&lt;test<br/><br/>';
71+
72+
let stdout = await runColorCLI(
73+
data, {
74+
newline: '', escapeXML: '', fg: '"#FAF"', bg: '"#0F0"'
75+
}
76+
);
77+
expect(stdout).to.equal(result);
78+
stdout = await runColorCLI(
79+
data, {
80+
n: '', x: '', f: '"#FAF"', b: '"#0F0"'
81+
},
82+
{shortFlags: true}
83+
);
84+
expect(stdout).to.equal(result);
85+
});
86+
87+
it('prints version', async function () {
88+
const result = '0.6.14\n';
89+
let stdout = await runCLI('node lib/cli --version');
90+
expect(stdout).to.equal(result);
91+
stdout = await runCLI('node lib/cli -v');
92+
expect(stdout).to.equal(result);
93+
});
94+
95+
it('prints help', async function () {
96+
const result = 'ansi-to-html [options] [file]';
97+
let stdout = await runCLI('node lib/cli --help');
98+
expect(stdout).to.contain(result);
99+
stdout = await runCLI('node lib/cli -h');
100+
expect(stdout).to.contain(result);
101+
});
102+
103+
it('works with a file', async function () {
104+
// Has an additional line break relative to programmatic, due to `echo`
105+
const result = 'test\ntest\n';
106+
107+
const stdout = await runCLI('node lib/cli test/fixtures/data.txt');
108+
expect(stdout).to.equal(result);
109+
});
45110
});

‎test/fixtures/data.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test
2+
test

0 commit comments

Comments
 (0)
Please sign in to comment.