@@ -42,6 +42,15 @@ class bcolors:
42
42
FAIL = '\033 [91m'
43
43
ENDC = '\033 [0m'
44
44
45
+ class nocolors :
46
+ HEADER = ''
47
+ OKBLUE = ''
48
+ OKGREEN = ''
49
+ WARNING = ''
50
+ FAIL = ''
51
+ ENDC = ''
52
+
53
+
45
54
46
55
class NbCellError (Exception ):
47
56
""" custom exception for error reporting. """
@@ -319,6 +328,8 @@ def __init__(self, name, parent, cell_num, cell, options):
319
328
self .options = options
320
329
self .config = parent .parent .config
321
330
self .output_timeout = 5
331
+ # Disable colors if we have been explicitly asked to
332
+ self .colors = bcolors if self .config .option .color != 'no' else nocolors
322
333
# _pytest.skipping assumes all pytest.Item have this attribute:
323
334
self .obj = Dummy ()
324
335
@@ -329,20 +340,21 @@ def __init__(self, name, parent, cell_num, cell, options):
329
340
def repr_failure (self , excinfo ):
330
341
""" called when self.runtest() raises an exception. """
331
342
exc = excinfo .value
343
+ cc = self .colors
332
344
if isinstance (exc , NbCellError ):
333
345
msg_items = [
334
- bcolors .FAIL + "Notebook cell execution failed" + bcolors .ENDC ]
346
+ cc .FAIL + "Notebook cell execution failed" + cc .ENDC ]
335
347
formatstring = (
336
- bcolors .OKBLUE + "Cell %d: %s\n \n " +
337
- "Input:\n " + bcolors .ENDC + "%s\n " )
348
+ cc .OKBLUE + "Cell %d: %s\n \n " +
349
+ "Input:\n " + cc .ENDC + "%s\n " )
338
350
msg_items .append (formatstring % (
339
351
exc .cell_num ,
340
352
str (exc ),
341
353
exc .source
342
354
))
343
355
if exc .inner_traceback :
344
356
msg_items .append ((
345
- bcolors .OKBLUE + "Traceback:" + bcolors .ENDC + "\n %s\n " ) %
357
+ cc .OKBLUE + "Traceback:" + cc .ENDC + "\n %s\n " ) %
346
358
exc .inner_traceback )
347
359
return "\n " .join (msg_items )
348
360
else :
@@ -359,6 +371,9 @@ def compare_outputs(self, test, ref, skip_compare=None):
359
371
test = transform_streams_for_comparison (test )
360
372
ref = transform_streams_for_comparison (ref )
361
373
374
+ # Color codes to use for reporting
375
+ cc = self .colors
376
+
362
377
# We reformat outputs into a dictionaries where
363
378
# key:
364
379
# - all keys on output except 'data' and those in skip_compare
@@ -409,18 +424,18 @@ def compare_outputs(self, test, ref, skip_compare=None):
409
424
410
425
if ref_keys - test_keys :
411
426
self .comparison_traceback .append (
412
- bcolors .FAIL
427
+ cc .FAIL
413
428
+ "Missing output fields from running code: %s"
414
429
% (ref_keys - test_keys )
415
- + bcolors .ENDC
430
+ + cc .ENDC
416
431
)
417
432
return False
418
433
elif test_keys - ref_keys :
419
434
self .comparison_traceback .append (
420
- bcolors .FAIL
435
+ cc .FAIL
421
436
+ "Unexpected output fields from running code: %s"
422
437
% (test_keys - ref_keys )
423
- + bcolors .ENDC
438
+ + cc .ENDC
424
439
)
425
440
return False
426
441
@@ -435,24 +450,24 @@ def compare_outputs(self, test, ref, skip_compare=None):
435
450
if len (test_values ) != len (ref_values ):
436
451
# The number of outputs for a specific MIME type differs
437
452
self .comparison_traceback .append (
438
- bcolors .OKBLUE
453
+ cc .OKBLUE
439
454
+ 'dissimilar number of outputs for key "%s"' % key
440
- + bcolors .FAIL
455
+ + cc .FAIL
441
456
+ "<<<<<<<<<<<< Reference outputs from ipynb file:"
442
- + bcolors .ENDC
457
+ + cc .ENDC
443
458
)
444
459
for val in ref_values :
445
460
self .comparison_traceback .append (_trim_base64 (val ))
446
461
self .comparison_traceback .append (
447
- bcolors .FAIL
462
+ cc .FAIL
448
463
+ '============ disagrees with newly computed (test) output:'
449
- + bcolors .ENDC )
464
+ + cc .ENDC )
450
465
for val in test_values :
451
466
self .comparison_traceback .append (_trim_base64 (val ))
452
467
self .comparison_traceback .append (
453
- bcolors .FAIL
468
+ cc .FAIL
454
469
+ '>>>>>>>>>>>>'
455
- + bcolors .ENDC )
470
+ + cc .ENDC )
456
471
return False
457
472
458
473
for ref_out , test_out in zip (ref_values , test_values ):
@@ -469,10 +484,12 @@ def format_output_compare(self, key, left, right):
469
484
if isinstance (right , six .string_types ):
470
485
right = _trim_base64 (right )
471
486
487
+ cc = self .colors
488
+
472
489
self .comparison_traceback .append (
473
- bcolors .OKBLUE
490
+ cc .OKBLUE
474
491
+ " mismatch '%s'" % key
475
- + bcolors .FAIL )
492
+ + cc .FAIL )
476
493
477
494
# Use comparison repr from pytest:
478
495
hook_result = self .ihook .pytest_assertrepr_compare (
@@ -487,17 +504,17 @@ def format_output_compare(self, key, left, right):
487
504
# Fallback repr:
488
505
self .comparison_traceback .append (
489
506
" <<<<<<<<<<<< Reference output from ipynb file:"
490
- + bcolors .ENDC )
507
+ + cc .ENDC )
491
508
self .comparison_traceback .append (_indent (left ))
492
509
self .comparison_traceback .append (
493
- bcolors .FAIL
510
+ cc .FAIL
494
511
+ ' ============ disagrees with newly computed (test) output:'
495
- + bcolors .ENDC )
512
+ + cc .ENDC )
496
513
self .comparison_traceback .append (_indent (right ))
497
514
self .comparison_traceback .append (
498
- bcolors .FAIL
515
+ cc .FAIL
499
516
+ ' >>>>>>>>>>>>' )
500
- self .comparison_traceback .append (bcolors .ENDC )
517
+ self .comparison_traceback .append (cc .ENDC )
501
518
502
519
503
520
""" *****************************************************
0 commit comments