Show More
@@ -1,212 +1,234 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): |
|
130 | def test_output_ext(self): | |
131 | """test --output=outputfile[.ext]""" |
|
131 | """test --output=outputfile[.ext]""" | |
132 | with self.create_temp_cwd(['notebook1.ipynb']): |
|
132 | with self.create_temp_cwd(['notebook1.ipynb']): | |
133 | self.call('nbconvert --log-level 0 --to python ' |
|
133 | self.call('nbconvert --log-level 0 --to python ' | |
134 | 'notebook1.ipynb --output nb.py') |
|
134 | 'notebook1.ipynb --output nb.py') | |
135 | assert os.path.exists('nb.py') |
|
135 | assert os.path.exists('nb.py') | |
136 |
|
136 | |||
137 | self.call('nbconvert --log-level 0 --to python ' |
|
137 | self.call('nbconvert --log-level 0 --to python ' | |
138 | 'notebook1.ipynb --output nb2') |
|
138 | 'notebook1.ipynb --output nb2') | |
139 | assert os.path.exists('nb2.py') |
|
139 | assert os.path.exists('nb2.py') | |
140 |
|
140 | |||
141 | def test_glob_explicit(self): |
|
141 | def test_glob_explicit(self): | |
142 | """ |
|
142 | """ | |
143 | 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? | |
144 | """ |
|
144 | """ | |
145 | with self.create_temp_cwd(['notebook*.ipynb']): |
|
145 | with self.create_temp_cwd(['notebook*.ipynb']): | |
146 | self.call('nbconvert --log-level 0 --to python ' |
|
146 | self.call('nbconvert --log-level 0 --to python ' | |
147 | '*.ipynb notebook1.ipynb notebook2.ipynb') |
|
147 | '*.ipynb notebook1.ipynb notebook2.ipynb') | |
148 | assert os.path.isfile('notebook1.py') |
|
148 | assert os.path.isfile('notebook1.py') | |
149 | assert os.path.isfile('notebook2.py') |
|
149 | assert os.path.isfile('notebook2.py') | |
150 |
|
150 | |||
151 |
|
151 | |||
152 | def test_explicit_glob(self): |
|
152 | def test_explicit_glob(self): | |
153 | """ |
|
153 | """ | |
154 | 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? | |
155 | """ |
|
155 | """ | |
156 | with self.create_temp_cwd(['notebook*.ipynb']): |
|
156 | with self.create_temp_cwd(['notebook*.ipynb']): | |
157 | self.call('nbconvert --log-level 0 --to=python ' |
|
157 | self.call('nbconvert --log-level 0 --to=python ' | |
158 | 'notebook1.ipynb notebook2.ipynb *.ipynb') |
|
158 | 'notebook1.ipynb notebook2.ipynb *.ipynb') | |
159 | assert os.path.isfile('notebook1.py') |
|
159 | assert os.path.isfile('notebook1.py') | |
160 | assert os.path.isfile('notebook2.py') |
|
160 | assert os.path.isfile('notebook2.py') | |
161 |
|
161 | |||
162 |
|
162 | |||
163 | def test_default_config(self): |
|
163 | def test_default_config(self): | |
164 | """ |
|
164 | """ | |
165 | Does the default config work? |
|
165 | Does the default config work? | |
166 | """ |
|
166 | """ | |
167 | with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py']): |
|
167 | with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py']): | |
168 | self.call('nbconvert --log-level 0') |
|
168 | self.call('nbconvert --log-level 0') | |
169 | assert os.path.isfile('notebook1.py') |
|
169 | assert os.path.isfile('notebook1.py') | |
170 | assert not os.path.isfile('notebook2.py') |
|
170 | assert not os.path.isfile('notebook2.py') | |
171 |
|
171 | |||
172 |
|
172 | |||
173 | def test_override_config(self): |
|
173 | def test_override_config(self): | |
174 | """ |
|
174 | """ | |
175 | Can the default config be overriden? |
|
175 | Can the default config be overriden? | |
176 | """ |
|
176 | """ | |
177 | with self.create_temp_cwd(['notebook*.ipynb', |
|
177 | with self.create_temp_cwd(['notebook*.ipynb', | |
178 | 'ipython_nbconvert_config.py', |
|
178 | 'ipython_nbconvert_config.py', | |
179 | 'override.py']): |
|
179 | 'override.py']): | |
180 | self.call('nbconvert --log-level 0 --config="override.py"') |
|
180 | self.call('nbconvert --log-level 0 --config="override.py"') | |
181 | assert not os.path.isfile('notebook1.py') |
|
181 | assert not os.path.isfile('notebook1.py') | |
182 | assert os.path.isfile('notebook2.py') |
|
182 | assert os.path.isfile('notebook2.py') | |
183 |
|
183 | |||
184 | def test_accents_in_filename(self): |
|
184 | def test_accents_in_filename(self): | |
185 | """ |
|
185 | """ | |
186 | Can notebook names include accents? |
|
186 | Can notebook names include accents? | |
187 | """ |
|
187 | """ | |
188 | with self.create_temp_cwd(): |
|
188 | with self.create_temp_cwd(): | |
189 | self.create_empty_notebook(u'nb1_anΓ‘lisis.ipynb') |
|
189 | self.create_empty_notebook(u'nb1_anΓ‘lisis.ipynb') | |
190 | self.call('nbconvert --log-level 0 --to python nb1_*') |
|
190 | self.call('nbconvert --log-level 0 --to python nb1_*') | |
191 | assert os.path.isfile(u'nb1_anΓ‘lisis.py') |
|
191 | assert os.path.isfile(u'nb1_anΓ‘lisis.py') | |
192 |
|
192 | |||
193 | @dec.onlyif_cmds_exist('pdflatex', 'pandoc') |
|
193 | @dec.onlyif_cmds_exist('pdflatex', 'pandoc') | |
194 | def test_filename_accent_pdf(self): |
|
194 | def test_filename_accent_pdf(self): | |
195 | """ |
|
195 | """ | |
196 | Generate PDFs if notebooks have an accent in their name? |
|
196 | Generate PDFs if notebooks have an accent in their name? | |
197 | """ |
|
197 | """ | |
198 | with self.create_temp_cwd(): |
|
198 | with self.create_temp_cwd(): | |
199 | self.create_empty_notebook(u'nb1_anΓ‘lisis.ipynb') |
|
199 | self.create_empty_notebook(u'nb1_anΓ‘lisis.ipynb') | |
200 | self.call('nbconvert --log-level 0 --to pdf "nb1_*"' |
|
200 | self.call('nbconvert --log-level 0 --to pdf "nb1_*"' | |
201 | ' --PDFExporter.latex_count=1' |
|
201 | ' --PDFExporter.latex_count=1' | |
202 | ' --PDFExporter.verbose=True') |
|
202 | ' --PDFExporter.verbose=True') | |
203 | assert os.path.isfile(u'nb1_anΓ‘lisis.pdf') |
|
203 | assert os.path.isfile(u'nb1_anΓ‘lisis.pdf') | |
204 |
|
204 | |||
205 | def test_cwd_plugin(self): |
|
205 | def test_cwd_plugin(self): | |
206 | """ |
|
206 | """ | |
207 | Verify that an extension in the cwd can be imported. |
|
207 | Verify that an extension in the cwd can be imported. | |
208 | """ |
|
208 | """ | |
209 | with self.create_temp_cwd(['hello.py']): |
|
209 | with self.create_temp_cwd(['hello.py']): | |
210 | self.create_empty_notebook(u'empty.ipynb') |
|
210 | self.create_empty_notebook(u'empty.ipynb') | |
211 | self.call('nbconvert empty --to html --NbConvertApp.writer_class=\'hello.HelloWriter\'') |
|
211 | self.call('nbconvert empty --to html --NbConvertApp.writer_class=\'hello.HelloWriter\'') | |
212 | assert os.path.isfile(u'hello.txt') |
|
212 | assert os.path.isfile(u'hello.txt') | |
|
213 | ||||
|
214 | def test_output_suffix(self): | |||
|
215 | """ | |||
|
216 | Verify that the output suffix is applied | |||
|
217 | """ | |||
|
218 | with self.create_temp_cwd(): | |||
|
219 | self.create_empty_notebook('empty.ipynb') | |||
|
220 | self.call('nbconvert empty.ipynb --to notebook') | |||
|
221 | assert os.path.isfile('empty.nbconvert.ipynb') | |||
|
222 | ||||
|
223 | def test_no_output_suffix(self): | |||
|
224 | """ | |||
|
225 | Verify that the output suffix is not applied | |||
|
226 | """ | |||
|
227 | with self.create_temp_cwd(): | |||
|
228 | self.create_empty_notebook('empty.ipynb') | |||
|
229 | os.mkdir('output') | |||
|
230 | self.call( | |||
|
231 | 'nbconvert empty.ipynb --to notebook ' | |||
|
232 | '--FilesWriter.build_directory=output ' | |||
|
233 | '--no-output-suffix') | |||
|
234 | assert os.path.isfile('output/empty.ipynb') |
General Comments 0
You need to be logged in to leave comments.
Login now