@@ -3,14 +3,16 @@ const childProcess = require('child_process');
3
3
const { EOL } = require ( 'os' ) ;
4
4
const { expect} = require ( 'chai' ) ;
5
5
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
+ } , '' ) ;
7
10
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 } ` ,
10
13
// 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 } `
12
15
} ;
13
-
14
16
return cmds [ process . platform ] ;
15
17
}
16
18
@@ -21,25 +23,88 @@ function echo(str) {
21
23
return `echo "${ str } "` ;
22
24
}
23
25
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 ,
30
30
timeout : 10000
31
31
} , ( err , stdout , stderr ) => {
32
32
if ( err ) {
33
- return done ( err ) ;
33
+ return reject ( err ) ;
34
34
}
35
35
36
36
if ( stderr ) {
37
- return done ( stderr ) ;
37
+ return reject ( stderr ) ;
38
38
}
39
39
40
- expect ( stdout ) . to . equal ( result ) ;
41
-
42
- done ( ) ;
40
+ resolve ( stdout ) ;
43
41
} ) ;
44
42
} ) ;
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/><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
+ } ) ;
45
110
} ) ;
0 commit comments