1
1
/* eslint no-control-regex: 0 */
2
- import formatRaw from './format-output' ;
2
+ import type { FormatOptions } from './format-output' ;
3
+ import { formatOutput } from './format-output' ;
3
4
import { expect } from 'chai' ;
4
5
5
6
for ( const colors of [ false , true ] ) {
6
7
describe ( `formatOutput with 'colors' set to ${ colors } ` , function ( ) {
7
- const format = ( value : any ) : string => formatRaw ( value , { colors } ) ;
8
+ const format = (
9
+ value : { value : unknown ; type ?: string | null } ,
10
+ opts : Partial < FormatOptions > = { }
11
+ ) : string => formatOutput ( value , { colors, ...opts } ) ;
8
12
const stripAnsiColors = colors
9
13
? ( str : string ) : string => str . replace ( / \x1B [ [ ( ? ) ; ] { 0 , 2 } ( ; ? \d ) * ./ g, '' )
10
14
: ( str : string ) : string => str ;
@@ -15,6 +19,31 @@ for (const colors of [false, true]) {
15
19
} ) ;
16
20
} ) ;
17
21
22
+ context (
23
+ 'when the result is a string that only contains simple special characters' ,
24
+ function ( ) {
25
+ it ( 'returns the output' , function ( ) {
26
+ expect ( format ( { value : 'test\n\ttest' } ) ) . to . equal ( 'test\n\ttest' ) ;
27
+ } ) ;
28
+ }
29
+ ) ;
30
+
31
+ context (
32
+ 'when the result is a string that contains special characters' ,
33
+ function ( ) {
34
+ it ( 'returns the output' , function ( ) {
35
+ expect ( stripAnsiColors ( format ( { value : 'test\bfooo' } ) ) ) . to . equal (
36
+ "'test\\bfooo'"
37
+ ) ;
38
+ } ) ;
39
+ it ( 'returns the raw value if control characters are allowed' , function ( ) {
40
+ expect (
41
+ format ( { value : 'test\bfooo' } , { allowControlCharacters : true } )
42
+ ) . to . equal ( 'test\bfooo' ) ;
43
+ } ) ;
44
+ }
45
+ ) ;
46
+
18
47
context ( 'when the result is undefined' , function ( ) {
19
48
it ( 'returns the output' , function ( ) {
20
49
expect ( format ( { value : undefined } ) ) . to . equal ( '' ) ;
@@ -253,10 +282,34 @@ for (const colors of [false, true]) {
253
282
'\rError: Something went wrong\nCaused by: \n\rError: Something else went wrong'
254
283
) ;
255
284
} ) ;
285
+
286
+ it ( 'escapes the message name if the error can be server-generated' , function ( ) {
287
+ const output = stripAnsiColors (
288
+ format ( {
289
+ value : Object . assign ( new Error ( 'foo\bbar.' ) , {
290
+ name : 'MongoServerError' ,
291
+ } ) ,
292
+ type : 'Error' ,
293
+ } )
294
+ ) ;
295
+
296
+ expect ( output ) . to . equal ( "\rMongoServerError: 'foo\\bbar.'" ) ;
297
+ } ) ;
298
+
299
+ it ( 'does not escape the message name if the error is a generic one' , function ( ) {
300
+ const output = stripAnsiColors (
301
+ format ( {
302
+ value : Object . assign ( new Error ( 'foo\bbar.' ) , { name : 'FooError' } ) ,
303
+ type : 'Error' ,
304
+ } )
305
+ ) ;
306
+
307
+ expect ( output ) . to . equal ( '\rFooError: foo\bbar.' ) ;
308
+ } ) ;
256
309
} ) ;
257
310
258
311
context ( 'when the result is ShowDatabasesResult' , function ( ) {
259
- it ( 'returns the help text ' , function ( ) {
312
+ it ( 'returns the database list ' , function ( ) {
260
313
const output = stripAnsiColors (
261
314
format ( {
262
315
value : [
@@ -265,25 +318,27 @@ for (const colors of [false, true]) {
265
318
{ name : 'supplies' , sizeOnDisk : 2236416 , empty : false } ,
266
319
{ name : 'test' , sizeOnDisk : 5664768 , empty : false } ,
267
320
{ name : 'test' , sizeOnDisk : 599999768000 , empty : false } ,
321
+ { name : 'ab\bdef' , sizeOnDisk : 1234 , empty : false } ,
268
322
] ,
269
323
type : 'ShowDatabasesResult' ,
270
324
} )
271
325
) ;
272
326
273
- expect ( output ) . to . equal (
327
+ expect ( output . replace ( / + / g , ' ' ) ) . to . equal (
274
328
`
275
- admin 44.00 KiB
276
- dxl 8.00 KiB
277
- supplies 2.13 MiB
278
- test 5.40 MiB
279
- test 558.79 GiB
329
+ admin 44.00 KiB
330
+ dxl 8.00 KiB
331
+ supplies 2.13 MiB
332
+ test 5.40 MiB
333
+ test 558.79 GiB
334
+ 'ab\\bdef' 1.21 KiB
280
335
` . trim ( )
281
336
) ;
282
337
} ) ;
283
338
} ) ;
284
339
285
340
context ( 'when the result is ShowCollectionsResult' , function ( ) {
286
- it ( 'returns the help text ' , function ( ) {
341
+ it ( 'returns the collections list ' , function ( ) {
287
342
const output = stripAnsiColors (
288
343
format ( {
289
344
value : [
@@ -292,13 +347,19 @@ test 558.79 GiB
292
347
{ name : 'coll' , badge : '' } ,
293
348
{ name : 'people_imported' , badge : '[view]' } ,
294
349
{ name : 'cats' , badge : '[time-series]' } ,
350
+ { name : 'cats\bcats' , badge : '' } ,
295
351
] ,
296
352
type : 'ShowCollectionsResult' ,
297
353
} )
298
354
) ;
299
355
300
- expect ( output ) . to . equal (
301
- 'nested_documents\ndecimal128\ncoll\npeople_imported [view]\ncats [time-series]'
356
+ expect ( output . replace ( / + / g, ' ' ) ) . to . equal (
357
+ 'nested_documents\n' +
358
+ 'decimal128\n' +
359
+ 'coll\n' +
360
+ 'people_imported [view]\n' +
361
+ 'cats [time-series]\n' +
362
+ "'cats\\bcats'"
302
363
) ;
303
364
} ) ;
304
365
} ) ;
@@ -319,15 +380,41 @@ test 558.79 GiB
319
380
'c1\n{ metadata: 1 }\n---\nc2\n{ metadata: 2 }'
320
381
) ;
321
382
} ) ;
383
+ it ( 'accounts for special characters and escapes them' , function ( ) {
384
+ const output = stripAnsiColors (
385
+ format ( {
386
+ value : {
387
+ [ 'c\b1' ] : { metadata : 1 } ,
388
+ c2 : { metadata : 2 } ,
389
+ } ,
390
+ type : 'StatsResult' ,
391
+ } )
392
+ ) ;
393
+
394
+ expect ( output ) . to . contain (
395
+ "'c\\b1'\n{ metadata: 1 }\n---\nc2\n{ metadata: 2 }"
396
+ ) ;
397
+ } ) ;
322
398
} ) ;
323
399
324
400
context ( 'when the result is ListCommandsResult' , function ( ) {
325
401
it ( 'returns the formatted list' , function ( ) {
326
402
const output = stripAnsiColors (
327
403
format ( {
328
404
value : {
329
- c1 : { metadata1 : 1 , help : 'help1' } ,
330
- c2 : { metadata2 : 2 , help : 'help2' } ,
405
+ c1 : { metadata1 : true , help : 'help1' } ,
406
+ c2 : {
407
+ metadata2 : true ,
408
+ help : 'help2' ,
409
+ apiVersions : [ ] ,
410
+ deprecatedApiVersions : [ ] ,
411
+ } ,
412
+ c3 : {
413
+ metadata2 : true ,
414
+ help : 'help2' ,
415
+ apiVersions : [ '1' ] ,
416
+ deprecatedApiVersions : [ '0' ] ,
417
+ } ,
331
418
} ,
332
419
type : 'ListCommandsResult' ,
333
420
} )
@@ -543,5 +630,20 @@ test 558.79 GiB
543
630
expect ( output ) . to . equal ( '------\n Header\n foo\n bar\n------\n' ) ;
544
631
} ) ;
545
632
} ) ;
633
+ it ( 'returns a formatted banner with escaped special characters' , function ( ) {
634
+ const output = stripAnsiColors (
635
+ format ( {
636
+ value : {
637
+ header : 'Heade\br' ,
638
+ content : 'foo\bbar\n' ,
639
+ } ,
640
+ type : 'ShowBannerResult' ,
641
+ } )
642
+ ) ;
643
+
644
+ expect ( output ) . to . equal (
645
+ "------\n 'Heade\\br'\n 'foo\\bbar\\n'\n------\n"
646
+ ) ;
647
+ } ) ;
546
648
} ) ;
547
649
}
0 commit comments