Skip to content

Commit 13ba300

Browse files
authored
Merge pull request #93 from vidartf/color
Respect the pytest --color flag
2 parents b8531c0 + f3ee89d commit 13ba300

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

nbval/plugin.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ class bcolors:
4242
FAIL = '\033[91m'
4343
ENDC = '\033[0m'
4444

45+
class nocolors:
46+
HEADER = ''
47+
OKBLUE = ''
48+
OKGREEN = ''
49+
WARNING = ''
50+
FAIL = ''
51+
ENDC = ''
52+
53+
4554

4655
class NbCellError(Exception):
4756
""" custom exception for error reporting. """
@@ -319,6 +328,8 @@ def __init__(self, name, parent, cell_num, cell, options):
319328
self.options = options
320329
self.config = parent.parent.config
321330
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
322333
# _pytest.skipping assumes all pytest.Item have this attribute:
323334
self.obj = Dummy()
324335

@@ -329,20 +340,21 @@ def __init__(self, name, parent, cell_num, cell, options):
329340
def repr_failure(self, excinfo):
330341
""" called when self.runtest() raises an exception. """
331342
exc = excinfo.value
343+
cc = self.colors
332344
if isinstance(exc, NbCellError):
333345
msg_items = [
334-
bcolors.FAIL + "Notebook cell execution failed" + bcolors.ENDC]
346+
cc.FAIL + "Notebook cell execution failed" + cc.ENDC]
335347
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")
338350
msg_items.append(formatstring % (
339351
exc.cell_num,
340352
str(exc),
341353
exc.source
342354
))
343355
if exc.inner_traceback:
344356
msg_items.append((
345-
bcolors.OKBLUE + "Traceback:" + bcolors.ENDC + "\n%s\n") %
357+
cc.OKBLUE + "Traceback:" + cc.ENDC + "\n%s\n") %
346358
exc.inner_traceback)
347359
return "\n".join(msg_items)
348360
else:
@@ -359,6 +371,9 @@ def compare_outputs(self, test, ref, skip_compare=None):
359371
test = transform_streams_for_comparison(test)
360372
ref = transform_streams_for_comparison(ref)
361373

374+
# Color codes to use for reporting
375+
cc = self.colors
376+
362377
# We reformat outputs into a dictionaries where
363378
# key:
364379
# - all keys on output except 'data' and those in skip_compare
@@ -409,18 +424,18 @@ def compare_outputs(self, test, ref, skip_compare=None):
409424

410425
if ref_keys - test_keys:
411426
self.comparison_traceback.append(
412-
bcolors.FAIL
427+
cc.FAIL
413428
+ "Missing output fields from running code: %s"
414429
% (ref_keys - test_keys)
415-
+ bcolors.ENDC
430+
+ cc.ENDC
416431
)
417432
return False
418433
elif test_keys - ref_keys:
419434
self.comparison_traceback.append(
420-
bcolors.FAIL
435+
cc.FAIL
421436
+ "Unexpected output fields from running code: %s"
422437
% (test_keys - ref_keys)
423-
+ bcolors.ENDC
438+
+ cc.ENDC
424439
)
425440
return False
426441

@@ -435,24 +450,24 @@ def compare_outputs(self, test, ref, skip_compare=None):
435450
if len(test_values) != len(ref_values):
436451
# The number of outputs for a specific MIME type differs
437452
self.comparison_traceback.append(
438-
bcolors.OKBLUE
453+
cc.OKBLUE
439454
+ 'dissimilar number of outputs for key "%s"' % key
440-
+ bcolors.FAIL
455+
+ cc.FAIL
441456
+ "<<<<<<<<<<<< Reference outputs from ipynb file:"
442-
+ bcolors.ENDC
457+
+ cc.ENDC
443458
)
444459
for val in ref_values:
445460
self.comparison_traceback.append(_trim_base64(val))
446461
self.comparison_traceback.append(
447-
bcolors.FAIL
462+
cc.FAIL
448463
+ '============ disagrees with newly computed (test) output:'
449-
+ bcolors.ENDC)
464+
+ cc.ENDC)
450465
for val in test_values:
451466
self.comparison_traceback.append(_trim_base64(val))
452467
self.comparison_traceback.append(
453-
bcolors.FAIL
468+
cc.FAIL
454469
+ '>>>>>>>>>>>>'
455-
+ bcolors.ENDC)
470+
+ cc.ENDC)
456471
return False
457472

458473
for ref_out, test_out in zip(ref_values, test_values):
@@ -469,10 +484,12 @@ def format_output_compare(self, key, left, right):
469484
if isinstance(right, six.string_types):
470485
right = _trim_base64(right)
471486

487+
cc = self.colors
488+
472489
self.comparison_traceback.append(
473-
bcolors.OKBLUE
490+
cc.OKBLUE
474491
+ " mismatch '%s'" % key
475-
+ bcolors.FAIL)
492+
+ cc.FAIL)
476493

477494
# Use comparison repr from pytest:
478495
hook_result = self.ihook.pytest_assertrepr_compare(
@@ -487,17 +504,17 @@ def format_output_compare(self, key, left, right):
487504
# Fallback repr:
488505
self.comparison_traceback.append(
489506
" <<<<<<<<<<<< Reference output from ipynb file:"
490-
+ bcolors.ENDC)
507+
+ cc.ENDC)
491508
self.comparison_traceback.append(_indent(left))
492509
self.comparison_traceback.append(
493-
bcolors.FAIL
510+
cc.FAIL
494511
+ ' ============ disagrees with newly computed (test) output:'
495-
+ bcolors.ENDC)
512+
+ cc.ENDC)
496513
self.comparison_traceback.append(_indent(right))
497514
self.comparison_traceback.append(
498-
bcolors.FAIL
515+
cc.FAIL
499516
+ ' >>>>>>>>>>>>')
500-
self.comparison_traceback.append(bcolors.ENDC)
517+
self.comparison_traceback.append(cc.ENDC)
501518

502519

503520
""" *****************************************************

0 commit comments

Comments
 (0)