Show More
@@ -1,82 +1,90 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 | import copy |
|
8 | import copy | |
9 | import glob |
|
9 | import glob | |
10 | import os |
|
10 | import os | |
11 | import re |
|
11 | import re | |
12 |
|
12 | |||
13 | from IPython.nbformat import current as nbformat |
|
13 | from IPython.nbformat import current as nbformat | |
14 |
|
14 | |||
15 | from .base import PreprocessorTestsBase |
|
15 | from .base import PreprocessorTestsBase | |
16 | from ..execute import ExecutePreprocessor |
|
16 | from ..execute import ExecutePreprocessor | |
17 |
|
17 | |||
18 | from IPython.nbconvert.filters import strip_ansi |
|
18 | from IPython.nbconvert.filters import strip_ansi | |
19 |
|
19 | |||
20 | addr_pat = re.compile(r'0x[0-9a-f]{7,9}') |
|
20 | addr_pat = re.compile(r'0x[0-9a-f]{7,9}') | |
21 |
|
21 | |||
22 | class TestExecute(PreprocessorTestsBase): |
|
22 | class TestExecute(PreprocessorTestsBase): | |
23 | """Contains test functions for execute.py""" |
|
23 | """Contains test functions for execute.py""" | |
24 |
|
24 | |||
25 | @staticmethod |
|
25 | @staticmethod | |
26 | def normalize_output(output): |
|
26 | def normalize_output(output): | |
27 | """ |
|
27 | """ | |
28 | Normalizes outputs for comparison. |
|
28 | Normalizes outputs for comparison. | |
29 | """ |
|
29 | """ | |
30 | output = dict(output) |
|
30 | output = dict(output) | |
31 | if 'metadata' in output: |
|
31 | if 'metadata' in output: | |
32 | del output['metadata'] |
|
32 | del output['metadata'] | |
33 | if 'text' in output: |
|
33 | if 'text' in output: | |
34 | output['text'] = re.sub(addr_pat, '<HEXADDR>', output['text']) |
|
34 | output['text'] = re.sub(addr_pat, '<HEXADDR>', output['text']) | |
35 | if 'svg' in output: |
|
35 | if 'svg' in output: | |
36 | del output['text'] |
|
36 | del output['text'] | |
37 | if 'traceback' in output: |
|
37 | if 'traceback' in output: | |
38 | tb = [] |
|
38 | tb = [] | |
39 | for line in output['traceback']: |
|
39 | for line in output['traceback']: | |
40 | tb.append(strip_ansi(line)) |
|
40 | tb.append(strip_ansi(line)) | |
41 | output['traceback'] = tb |
|
41 | output['traceback'] = tb | |
42 |
|
42 | |||
43 | return output |
|
43 | return output | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | def assert_notebooks_equal(self, expected, actual): |
|
46 | def assert_notebooks_equal(self, expected, actual): | |
47 | expected_cells = expected['worksheets'][0]['cells'] |
|
47 | expected_cells = expected['worksheets'][0]['cells'] | |
48 | actual_cells = actual['worksheets'][0]['cells'] |
|
48 | actual_cells = actual['worksheets'][0]['cells'] | |
49 | assert len(expected_cells) == len(actual_cells) |
|
49 | assert len(expected_cells) == len(actual_cells) | |
50 |
|
50 | |||
51 | for expected_cell, actual_cell in zip(expected_cells, actual_cells): |
|
51 | for expected_cell, actual_cell in zip(expected_cells, actual_cells): | |
52 | expected_outputs = expected_cell.get('outputs', []) |
|
52 | expected_outputs = expected_cell.get('outputs', []) | |
53 | actual_outputs = actual_cell.get('outputs', []) |
|
53 | actual_outputs = actual_cell.get('outputs', []) | |
54 | normalized_expected_outputs = list(map(self.normalize_output, expected_outputs)) |
|
54 | normalized_expected_outputs = list(map(self.normalize_output, expected_outputs)) | |
55 | normalized_actual_outputs = list(map(self.normalize_output, actual_outputs)) |
|
55 | normalized_actual_outputs = list(map(self.normalize_output, actual_outputs)) | |
56 | assert normalized_expected_outputs == normalized_actual_outputs |
|
56 | assert normalized_expected_outputs == normalized_actual_outputs | |
57 |
|
57 | |||
|
58 | expected_prompt_number = expected_cell.get('prompt_number', None) | |||
|
59 | actual_prompt_number = actual_cell.get('prompt_number', None) | |||
|
60 | assert expected_prompt_number == actual_prompt_number | |||
|
61 | ||||
58 |
|
62 | |||
59 | def build_preprocessor(self): |
|
63 | def build_preprocessor(self): | |
60 | """Make an instance of a preprocessor""" |
|
64 | """Make an instance of a preprocessor""" | |
61 | preprocessor = ExecutePreprocessor() |
|
65 | preprocessor = ExecutePreprocessor() | |
62 | preprocessor.enabled = True |
|
66 | preprocessor.enabled = True | |
63 | return preprocessor |
|
67 | return preprocessor | |
64 |
|
68 | |||
65 |
|
69 | |||
66 | def test_constructor(self): |
|
70 | def test_constructor(self): | |
67 | """Can a ExecutePreprocessor be constructed?""" |
|
71 | """Can a ExecutePreprocessor be constructed?""" | |
68 | self.build_preprocessor() |
|
72 | self.build_preprocessor() | |
69 |
|
73 | |||
70 |
|
74 | |||
71 | def test_run_notebooks(self): |
|
75 | def test_run_notebooks(self): | |
72 | """Runs a series of test notebooks and compares them to their actual output""" |
|
76 | """Runs a series of test notebooks and compares them to their actual output""" | |
73 | current_dir = os.path.dirname(__file__) |
|
77 | current_dir = os.path.dirname(__file__) | |
74 | input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb')) |
|
78 | input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb')) | |
75 | for filename in input_files: |
|
79 | for filename in input_files: | |
76 | with open(os.path.join(current_dir, 'files', filename)) as f: |
|
80 | with open(os.path.join(current_dir, 'files', filename)) as f: | |
77 | input_nb = nbformat.read(f, 'ipynb') |
|
81 | input_nb = nbformat.read(f, 'ipynb') | |
78 | res = self.build_resources() |
|
82 | res = self.build_resources() | |
79 | preprocessor = self.build_preprocessor() |
|
83 | preprocessor = self.build_preprocessor() | |
80 |
|
|
84 | cleaned_input_nb = copy.deepcopy(input_nb) | |
|
85 | for cell in cleaned_input_nb.worksheets[0].cells: | |||
|
86 | if 'prompt_number' in cell: | |||
|
87 | del cell['prompt_number'] | |||
|
88 | cell['outputs'] = [] | |||
|
89 | output_nb, _ = preprocessor(cleaned_input_nb, res) | |||
81 | self.assert_notebooks_equal(output_nb, input_nb) |
|
90 | self.assert_notebooks_equal(output_nb, input_nb) | |
82 |
|
General Comments 0
You need to be logged in to leave comments.
Login now