##// END OF EJS Templates
strip ANSI colors from tracebacks when normalizing outputs
MinRK -
Show More
@@ -1,83 +1,87 b''
1 1 """
2 2 Module with tests for the execute preprocessor.
3 3 """
4 4
5 5 # Copyright (c) IPython Development Team.
6 6 # Distributed under the terms of the Modified BSD License.
7 7
8 #-----------------------------------------------------------------------------
9 # Imports
10 #-----------------------------------------------------------------------------
11 8 import copy
12 9 import os
13 10 import re
14 11
15 12 from IPython.nbformat import current as nbformat
16 13
17 14 from .base import PreprocessorTestsBase
18 15 from ..execute import ExecutePreprocessor
19 16
17 from IPython.nbconvert.filters import strip_ansi
18
19
20 addr_pat = re.compile(r'0x[0-9a-f]{7,9}')
20 21
21 #-----------------------------------------------------------------------------
22 # Class
23 #-----------------------------------------------------------------------------
24 22
25 23 class TestExecute(PreprocessorTestsBase):
26 24 """Contains test functions for execute.py"""
27 25
28 26 @staticmethod
29 27 def normalize_cell(cell):
30 28 """
31 29 Normalizes cells for comparison.
32 30 """
33 31 cell = dict(cell)
34 32 if 'metadata' in cell:
35 33 del cell['metadata']
36 34 if 'text' in cell:
37 cell['text'] = re.sub('0x[0-9a-f]{7,9}', '<HEXADDR>', cell['text'])
35 cell['text'] = re.sub(addr_pat, '<HEXADDR>', cell['text'])
38 36 if 'svg' in cell:
39 37 del cell['text']
38 if 'traceback' in cell:
39 tb = []
40 for line in cell['traceback']:
41 tb.append(strip_ansi(line))
42 cell['traceback'] = tb
43
40 44 return cell
41 45
42 46
43 47 def assert_notebooks_equal(self, expected, actual):
44 48 expected_cells = expected['worksheets'][0]['cells']
45 49 actual_cells = actual['worksheets'][0]['cells']
46 50 assert len(expected_cells) == len(actual_cells)
47 51
48 52 for expected_cell, actual_cell in zip(expected_cells, actual_cells):
49 53 expected_outputs = expected_cell.get('outputs', [])
50 54 actual_outputs = actual_cell.get('outputs', [])
51 55 normalized_expected_outputs = list(map(self.normalize_cell, expected_outputs))
52 56 normalized_actual_outputs = list(map(self.normalize_cell, actual_outputs))
53 57 assert normalized_expected_outputs == normalized_actual_outputs
54 58
55 59
56 60 def build_preprocessor(self):
57 61 """Make an instance of a preprocessor"""
58 62 preprocessor = ExecutePreprocessor()
59 63 preprocessor.enabled = True
60 64 return preprocessor
61 65
62 66
63 67 def test_constructor(self):
64 68 """Can a ExecutePreprocessor be constructed?"""
65 69 self.build_preprocessor()
66 70
67 71
68 72 def test_run_notebooks(self):
69 73 """Runs a series of test notebooks and compares them to their actual output"""
70 74 current_dir = os.path.dirname(__file__)
71 75 input_files = os.listdir(os.path.join(current_dir, 'input'))
72 76 for filename in input_files:
73 77 if not filename.endswith(".ipynb"):
74 78 continue
75 79 with open(os.path.join(current_dir, 'input', filename)) as f:
76 80 input_nb = nbformat.read(f, 'ipynb')
77 81 with open(os.path.join(current_dir, 'expected', filename)) as f:
78 82 expected_nb = nbformat.read(f, 'ipynb')
79 83 res = self.build_resources()
80 84 preprocessor = self.build_preprocessor()
81 85 output_nb, _ = preprocessor(input_nb, res)
82 86 self.assert_notebooks_equal(output_nb, expected_nb)
83 87
General Comments 0
You need to be logged in to leave comments. Login now