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