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