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