##// END OF EJS Templates
test `nbconvert --output` with and without extension
MinRK -
Show More
@@ -1,202 +1,212 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Test NbConvertApp"""
2 """Test NbConvertApp"""
3
3
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 import os
7 import os
8 import glob
8 import glob
9 import sys
9 import sys
10
10
11 from .base import TestsBase
11 from .base import TestsBase
12 from ..postprocessors import PostProcessorBase
12 from ..postprocessors import PostProcessorBase
13
13
14 import IPython.testing.tools as tt
14 import IPython.testing.tools as tt
15 from IPython.testing import decorators as dec
15 from IPython.testing import decorators as dec
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Classes and functions
18 # Classes and functions
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 class DummyPost(PostProcessorBase):
21 class DummyPost(PostProcessorBase):
22 def postprocess(self, filename):
22 def postprocess(self, filename):
23 print("Dummy:%s" % filename)
23 print("Dummy:%s" % filename)
24
24
25 class TestNbConvertApp(TestsBase):
25 class TestNbConvertApp(TestsBase):
26 """Collection of NbConvertApp tests"""
26 """Collection of NbConvertApp tests"""
27
27
28
28
29 def test_notebook_help(self):
29 def test_notebook_help(self):
30 """Will help show if no notebooks are specified?"""
30 """Will help show if no notebooks are specified?"""
31 with self.create_temp_cwd():
31 with self.create_temp_cwd():
32 out, err = self.call('nbconvert --log-level 0', ignore_return_code=True)
32 out, err = self.call('nbconvert --log-level 0', ignore_return_code=True)
33 self.assertIn("see '--help-all'", out)
33 self.assertIn("see '--help-all'", out)
34
34
35 def test_help_output(self):
35 def test_help_output(self):
36 """ipython nbconvert --help-all works"""
36 """ipython nbconvert --help-all works"""
37 tt.help_all_output_test('nbconvert')
37 tt.help_all_output_test('nbconvert')
38
38
39 def test_glob(self):
39 def test_glob(self):
40 """
40 """
41 Do search patterns work for notebook names?
41 Do search patterns work for notebook names?
42 """
42 """
43 with self.create_temp_cwd(['notebook*.ipynb']):
43 with self.create_temp_cwd(['notebook*.ipynb']):
44 self.call('nbconvert --to python *.ipynb --log-level 0')
44 self.call('nbconvert --to python *.ipynb --log-level 0')
45 assert os.path.isfile('notebook1.py')
45 assert os.path.isfile('notebook1.py')
46 assert os.path.isfile('notebook2.py')
46 assert os.path.isfile('notebook2.py')
47
47
48
48
49 def test_glob_subdir(self):
49 def test_glob_subdir(self):
50 """
50 """
51 Do search patterns work for subdirectory notebook names?
51 Do search patterns work for subdirectory notebook names?
52 """
52 """
53 with self.create_temp_cwd():
53 with self.create_temp_cwd():
54 self.copy_files_to(['notebook*.ipynb'], 'subdir/')
54 self.copy_files_to(['notebook*.ipynb'], 'subdir/')
55 self.call('nbconvert --to python --log-level 0 ' +
55 self.call('nbconvert --to python --log-level 0 ' +
56 os.path.join('subdir', '*.ipynb'))
56 os.path.join('subdir', '*.ipynb'))
57 assert os.path.isfile('notebook1.py')
57 assert os.path.isfile('notebook1.py')
58 assert os.path.isfile('notebook2.py')
58 assert os.path.isfile('notebook2.py')
59
59
60
60
61 def test_explicit(self):
61 def test_explicit(self):
62 """
62 """
63 Do explicit notebook names work?
63 Do explicit notebook names work?
64 """
64 """
65 with self.create_temp_cwd(['notebook*.ipynb']):
65 with self.create_temp_cwd(['notebook*.ipynb']):
66 self.call('nbconvert --log-level 0 --to python notebook2')
66 self.call('nbconvert --log-level 0 --to python notebook2')
67 assert not os.path.isfile('notebook1.py')
67 assert not os.path.isfile('notebook1.py')
68 assert os.path.isfile('notebook2.py')
68 assert os.path.isfile('notebook2.py')
69
69
70
70
71 @dec.onlyif_cmds_exist('pdflatex')
71 @dec.onlyif_cmds_exist('pdflatex')
72 @dec.onlyif_cmds_exist('pandoc')
72 @dec.onlyif_cmds_exist('pandoc')
73 def test_filename_spaces(self):
73 def test_filename_spaces(self):
74 """
74 """
75 Generate PDFs with graphics if notebooks have spaces in the name?
75 Generate PDFs with graphics if notebooks have spaces in the name?
76 """
76 """
77 with self.create_temp_cwd(['notebook2.ipynb']):
77 with self.create_temp_cwd(['notebook2.ipynb']):
78 os.rename('notebook2.ipynb', 'notebook with spaces.ipynb')
78 os.rename('notebook2.ipynb', 'notebook with spaces.ipynb')
79 self.call('nbconvert --log-level 0 --to pdf'
79 self.call('nbconvert --log-level 0 --to pdf'
80 ' "notebook with spaces"'
80 ' "notebook with spaces"'
81 ' --PDFExporter.latex_count=1'
81 ' --PDFExporter.latex_count=1'
82 ' --PDFExporter.verbose=True'
82 ' --PDFExporter.verbose=True'
83 )
83 )
84 assert os.path.isfile('notebook with spaces.pdf')
84 assert os.path.isfile('notebook with spaces.pdf')
85
85
86 def test_post_processor(self):
86 def test_post_processor(self):
87 """Do post processors work?"""
87 """Do post processors work?"""
88 with self.create_temp_cwd(['notebook1.ipynb']):
88 with self.create_temp_cwd(['notebook1.ipynb']):
89 out, err = self.call('nbconvert --log-level 0 --to python notebook1 '
89 out, err = self.call('nbconvert --log-level 0 --to python notebook1 '
90 '--post IPython.nbconvert.tests.test_nbconvertapp.DummyPost')
90 '--post IPython.nbconvert.tests.test_nbconvertapp.DummyPost')
91 self.assertIn('Dummy:notebook1.py', out)
91 self.assertIn('Dummy:notebook1.py', out)
92
92
93 @dec.onlyif_cmds_exist('pandoc')
93 @dec.onlyif_cmds_exist('pandoc')
94 def test_spurious_cr(self):
94 def test_spurious_cr(self):
95 """Check for extra CR characters"""
95 """Check for extra CR characters"""
96 with self.create_temp_cwd(['notebook2.ipynb']):
96 with self.create_temp_cwd(['notebook2.ipynb']):
97 self.call('nbconvert --log-level 0 --to latex notebook2')
97 self.call('nbconvert --log-level 0 --to latex notebook2')
98 assert os.path.isfile('notebook2.tex')
98 assert os.path.isfile('notebook2.tex')
99 with open('notebook2.tex') as f:
99 with open('notebook2.tex') as f:
100 tex = f.read()
100 tex = f.read()
101 self.call('nbconvert --log-level 0 --to html notebook2')
101 self.call('nbconvert --log-level 0 --to html notebook2')
102 assert os.path.isfile('notebook2.html')
102 assert os.path.isfile('notebook2.html')
103 with open('notebook2.html') as f:
103 with open('notebook2.html') as f:
104 html = f.read()
104 html = f.read()
105 self.assertEqual(tex.count('\r'), tex.count('\r\n'))
105 self.assertEqual(tex.count('\r'), tex.count('\r\n'))
106 self.assertEqual(html.count('\r'), html.count('\r\n'))
106 self.assertEqual(html.count('\r'), html.count('\r\n'))
107
107
108 @dec.onlyif_cmds_exist('pandoc')
108 @dec.onlyif_cmds_exist('pandoc')
109 def test_png_base64_html_ok(self):
109 def test_png_base64_html_ok(self):
110 """Is embedded png data well formed in HTML?"""
110 """Is embedded png data well formed in HTML?"""
111 with self.create_temp_cwd(['notebook2.ipynb']):
111 with self.create_temp_cwd(['notebook2.ipynb']):
112 self.call('nbconvert --log-level 0 --to HTML '
112 self.call('nbconvert --log-level 0 --to HTML '
113 'notebook2.ipynb --template full')
113 'notebook2.ipynb --template full')
114 assert os.path.isfile('notebook2.html')
114 assert os.path.isfile('notebook2.html')
115 with open('notebook2.html') as f:
115 with open('notebook2.html') as f:
116 assert "data:image/png;base64,b'" not in f.read()
116 assert "data:image/png;base64,b'" not in f.read()
117
117
118 @dec.onlyif_cmds_exist('pandoc')
118 @dec.onlyif_cmds_exist('pandoc')
119 def test_template(self):
119 def test_template(self):
120 """
120 """
121 Do export templates work?
121 Do export templates work?
122 """
122 """
123 with self.create_temp_cwd(['notebook2.ipynb']):
123 with self.create_temp_cwd(['notebook2.ipynb']):
124 self.call('nbconvert --log-level 0 --to slides '
124 self.call('nbconvert --log-level 0 --to slides '
125 'notebook2.ipynb')
125 'notebook2.ipynb')
126 assert os.path.isfile('notebook2.slides.html')
126 assert os.path.isfile('notebook2.slides.html')
127 with open('notebook2.slides.html') as f:
127 with open('notebook2.slides.html') as f:
128 assert '/reveal.css' in f.read()
128 assert '/reveal.css' in f.read()
129
129
130 def test_output_ext(self):
131 """test --output=outputfile[.ext]"""
132 with self.create_temp_cwd(['notebook1.ipynb']):
133 self.call('nbconvert --log-level 0 --to python '
134 'notebook1.ipynb --output nb.py')
135 assert os.path.exists('nb.py')
136
137 self.call('nbconvert --log-level 0 --to python '
138 'notebook1.ipynb --output nb2')
139 assert os.path.exists('nb2.py')
130
140
131 def test_glob_explicit(self):
141 def test_glob_explicit(self):
132 """
142 """
133 Can a search pattern be used along with matching explicit notebook names?
143 Can a search pattern be used along with matching explicit notebook names?
134 """
144 """
135 with self.create_temp_cwd(['notebook*.ipynb']):
145 with self.create_temp_cwd(['notebook*.ipynb']):
136 self.call('nbconvert --log-level 0 --to python '
146 self.call('nbconvert --log-level 0 --to python '
137 '*.ipynb notebook1.ipynb notebook2.ipynb')
147 '*.ipynb notebook1.ipynb notebook2.ipynb')
138 assert os.path.isfile('notebook1.py')
148 assert os.path.isfile('notebook1.py')
139 assert os.path.isfile('notebook2.py')
149 assert os.path.isfile('notebook2.py')
140
150
141
151
142 def test_explicit_glob(self):
152 def test_explicit_glob(self):
143 """
153 """
144 Can explicit notebook names be used and then a matching search pattern?
154 Can explicit notebook names be used and then a matching search pattern?
145 """
155 """
146 with self.create_temp_cwd(['notebook*.ipynb']):
156 with self.create_temp_cwd(['notebook*.ipynb']):
147 self.call('nbconvert --log-level 0 --to=python '
157 self.call('nbconvert --log-level 0 --to=python '
148 'notebook1.ipynb notebook2.ipynb *.ipynb')
158 'notebook1.ipynb notebook2.ipynb *.ipynb')
149 assert os.path.isfile('notebook1.py')
159 assert os.path.isfile('notebook1.py')
150 assert os.path.isfile('notebook2.py')
160 assert os.path.isfile('notebook2.py')
151
161
152
162
153 def test_default_config(self):
163 def test_default_config(self):
154 """
164 """
155 Does the default config work?
165 Does the default config work?
156 """
166 """
157 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py']):
167 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py']):
158 self.call('nbconvert --log-level 0')
168 self.call('nbconvert --log-level 0')
159 assert os.path.isfile('notebook1.py')
169 assert os.path.isfile('notebook1.py')
160 assert not os.path.isfile('notebook2.py')
170 assert not os.path.isfile('notebook2.py')
161
171
162
172
163 def test_override_config(self):
173 def test_override_config(self):
164 """
174 """
165 Can the default config be overriden?
175 Can the default config be overriden?
166 """
176 """
167 with self.create_temp_cwd(['notebook*.ipynb',
177 with self.create_temp_cwd(['notebook*.ipynb',
168 'ipython_nbconvert_config.py',
178 'ipython_nbconvert_config.py',
169 'override.py']):
179 'override.py']):
170 self.call('nbconvert --log-level 0 --config="override.py"')
180 self.call('nbconvert --log-level 0 --config="override.py"')
171 assert not os.path.isfile('notebook1.py')
181 assert not os.path.isfile('notebook1.py')
172 assert os.path.isfile('notebook2.py')
182 assert os.path.isfile('notebook2.py')
173
183
174 def test_accents_in_filename(self):
184 def test_accents_in_filename(self):
175 """
185 """
176 Can notebook names include accents?
186 Can notebook names include accents?
177 """
187 """
178 with self.create_temp_cwd():
188 with self.create_temp_cwd():
179 self.create_empty_notebook(u'nb1_análisis.ipynb')
189 self.create_empty_notebook(u'nb1_análisis.ipynb')
180 self.call('nbconvert --log-level 0 --to python nb1_*')
190 self.call('nbconvert --log-level 0 --to python nb1_*')
181 assert os.path.isfile(u'nb1_análisis.py')
191 assert os.path.isfile(u'nb1_análisis.py')
182
192
183 @dec.onlyif_cmds_exist('pdflatex', 'pandoc')
193 @dec.onlyif_cmds_exist('pdflatex', 'pandoc')
184 def test_filename_accent_pdf(self):
194 def test_filename_accent_pdf(self):
185 """
195 """
186 Generate PDFs if notebooks have an accent in their name?
196 Generate PDFs if notebooks have an accent in their name?
187 """
197 """
188 with self.create_temp_cwd():
198 with self.create_temp_cwd():
189 self.create_empty_notebook(u'nb1_análisis.ipynb')
199 self.create_empty_notebook(u'nb1_análisis.ipynb')
190 self.call('nbconvert --log-level 0 --to pdf "nb1_*"'
200 self.call('nbconvert --log-level 0 --to pdf "nb1_*"'
191 ' --PDFExporter.latex_count=1'
201 ' --PDFExporter.latex_count=1'
192 ' --PDFExporter.verbose=True')
202 ' --PDFExporter.verbose=True')
193 assert os.path.isfile(u'nb1_análisis.pdf')
203 assert os.path.isfile(u'nb1_análisis.pdf')
194
204
195 def test_cwd_plugin(self):
205 def test_cwd_plugin(self):
196 """
206 """
197 Verify that an extension in the cwd can be imported.
207 Verify that an extension in the cwd can be imported.
198 """
208 """
199 with self.create_temp_cwd(['hello.py']):
209 with self.create_temp_cwd(['hello.py']):
200 self.create_empty_notebook(u'empty.ipynb')
210 self.create_empty_notebook(u'empty.ipynb')
201 self.call('nbconvert empty --to html --NbConvertApp.writer_class=\'hello.HelloWriter\'')
211 self.call('nbconvert empty --to html --NbConvertApp.writer_class=\'hello.HelloWriter\'')
202 assert os.path.isfile(u'hello.txt')
212 assert os.path.isfile(u'hello.txt')
General Comments 0
You need to be logged in to leave comments. Login now