##// END OF EJS Templates
Add test for when stdin is disabled
Jessica B. Hamrick -
Show More
@@ -0,0 +1,36 b''
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {
7 "collapsed": false
8 },
9 "outputs": [],
10 "source": [
11 "name = input(\"name: \")"
12 ]
13 }
14 ],
15 "metadata": {
16 "kernelspec": {
17 "display_name": "Python 3",
18 "language": "python",
19 "name": "python3"
20 },
21 "language_info": {
22 "codemirror_mode": {
23 "name": "ipython",
24 "version": 3
25 },
26 "file_extension": ".py",
27 "mimetype": "text/x-python",
28 "name": "python",
29 "nbconvert_exporter": "python",
30 "pygments_lexer": "ipython3",
31 "version": "3.4.2"
32 }
33 },
34 "nbformat": 4,
35 "nbformat_minor": 0
36 }
@@ -1,92 +1,106 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 io
10 import io
11 import os
11 import os
12 import re
12 import re
13
13
14 from IPython import nbformat
14 from IPython import nbformat
15
15
16 from .base import PreprocessorTestsBase
16 from .base import PreprocessorTestsBase
17 from ..execute import ExecutePreprocessor
17 from ..execute import ExecutePreprocessor
18
18
19 from IPython.nbconvert.filters import strip_ansi
19 from IPython.nbconvert.filters import strip_ansi
20
20
21 addr_pat = re.compile(r'0x[0-9a-f]{7,9}')
21 addr_pat = re.compile(r'0x[0-9a-f]{7,9}')
22
22
23 class TestExecute(PreprocessorTestsBase):
23 class TestExecute(PreprocessorTestsBase):
24 """Contains test functions for execute.py"""
24 """Contains test functions for execute.py"""
25
25
26 @staticmethod
26 @staticmethod
27 def normalize_output(output):
27 def normalize_output(output):
28 """
28 """
29 Normalizes outputs for comparison.
29 Normalizes outputs for comparison.
30 """
30 """
31 output = dict(output)
31 output = dict(output)
32 if 'metadata' in output:
32 if 'metadata' in output:
33 del output['metadata']
33 del output['metadata']
34 if 'text' in output:
34 if 'text' in output:
35 output['text'] = re.sub(addr_pat, '<HEXADDR>', output['text'])
35 output['text'] = re.sub(addr_pat, '<HEXADDR>', output['text'])
36 if 'text/plain' in output.get('data', {}):
36 if 'text/plain' in output.get('data', {}):
37 output['data']['text/plain'] = \
37 output['data']['text/plain'] = \
38 re.sub(addr_pat, '<HEXADDR>', output['data']['text/plain'])
38 re.sub(addr_pat, '<HEXADDR>', output['data']['text/plain'])
39 if 'traceback' in output:
39 if 'traceback' in output:
40 tb = []
40 tb = []
41 for line in output['traceback']:
41 for line in output['traceback']:
42 tb.append(strip_ansi(line))
42 tb.append(strip_ansi(line))
43 output['traceback'] = tb
43 output['traceback'] = tb
44
44
45 return output
45 return output
46
46
47
47
48 def assert_notebooks_equal(self, expected, actual):
48 def assert_notebooks_equal(self, expected, actual):
49 expected_cells = expected['cells']
49 expected_cells = expected['cells']
50 actual_cells = actual['cells']
50 actual_cells = actual['cells']
51 self.assertEqual(len(expected_cells), len(actual_cells))
51 self.assertEqual(len(expected_cells), len(actual_cells))
52
52
53 for expected_cell, actual_cell in zip(expected_cells, actual_cells):
53 for expected_cell, actual_cell in zip(expected_cells, actual_cells):
54 expected_outputs = expected_cell.get('outputs', [])
54 expected_outputs = expected_cell.get('outputs', [])
55 actual_outputs = actual_cell.get('outputs', [])
55 actual_outputs = actual_cell.get('outputs', [])
56 normalized_expected_outputs = list(map(self.normalize_output, expected_outputs))
56 normalized_expected_outputs = list(map(self.normalize_output, expected_outputs))
57 normalized_actual_outputs = list(map(self.normalize_output, actual_outputs))
57 normalized_actual_outputs = list(map(self.normalize_output, actual_outputs))
58 self.assertEqual(normalized_expected_outputs, normalized_actual_outputs)
58 self.assertEqual(normalized_expected_outputs, normalized_actual_outputs)
59
59
60 expected_execution_count = expected_cell.get('execution_count', None)
60 expected_execution_count = expected_cell.get('execution_count', None)
61 actual_execution_count = actual_cell.get('execution_count', None)
61 actual_execution_count = actual_cell.get('execution_count', None)
62 self.assertEqual(expected_execution_count, actual_execution_count)
62 self.assertEqual(expected_execution_count, actual_execution_count)
63
63
64
64
65 def build_preprocessor(self):
65 def build_preprocessor(self):
66 """Make an instance of a preprocessor"""
66 """Make an instance of a preprocessor"""
67 preprocessor = ExecutePreprocessor()
67 preprocessor = ExecutePreprocessor()
68 preprocessor.enabled = True
68 preprocessor.enabled = True
69 return preprocessor
69 return preprocessor
70
70
71
71
72 def test_constructor(self):
72 def test_constructor(self):
73 """Can a ExecutePreprocessor be constructed?"""
73 """Can a ExecutePreprocessor be constructed?"""
74 self.build_preprocessor()
74 self.build_preprocessor()
75
75
76
76
77 def test_run_notebooks(self):
77 def test_run_notebooks(self):
78 """Runs a series of test notebooks and compares them to their actual output"""
78 """Runs a series of test notebooks and compares them to their actual output"""
79 current_dir = os.path.dirname(__file__)
79 current_dir = os.path.dirname(__file__)
80 input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb'))
80 input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb'))
81 for filename in input_files:
81 for filename in input_files:
82 with io.open(os.path.join(current_dir, 'files', filename)) as f:
82 with io.open(os.path.join(current_dir, 'files', filename)) as f:
83 input_nb = nbformat.read(f, 4)
83 input_nb = nbformat.read(f, 4)
84 res = self.build_resources()
84 res = self.build_resources()
85 preprocessor = self.build_preprocessor()
85 preprocessor = self.build_preprocessor()
86 cleaned_input_nb = copy.deepcopy(input_nb)
86 cleaned_input_nb = copy.deepcopy(input_nb)
87 for cell in cleaned_input_nb.cells:
87 for cell in cleaned_input_nb.cells:
88 if 'execution_count' in cell:
88 if 'execution_count' in cell:
89 del cell['execution_count']
89 del cell['execution_count']
90 cell['outputs'] = []
90 cell['outputs'] = []
91 output_nb, _ = preprocessor(cleaned_input_nb, res)
91 output_nb, _ = preprocessor(cleaned_input_nb, res)
92 self.assert_notebooks_equal(output_nb, input_nb)
92
93 if os.path.basename(filename) == "Disable Stdin.ipynb":
94 # We need to special-case this particular notebook, because the
95 # traceback contains machine-specific stuff like where IPython
96 # is installed. It is sufficient here to just check that an error
97 # was thrown, and that it was a StdinNotImplementedError
98 self.assertEqual(len(output_nb['cells']), 1)
99 self.assertEqual(len(output_nb['cells'][0]['outputs']), 1)
100 output = output_nb['cells'][0]['outputs'][0]
101 self.assertEqual(output['output_type'], 'error')
102 self.assertEqual(output['ename'], 'StdinNotImplementedError')
103 self.assertEqual(output['evalue'], 'raw_input was called, but this frontend does not support input requests.')
104
105 else:
106 self.assert_notebooks_equal(output_nb, input_nb)
General Comments 0
You need to be logged in to leave comments. Login now