Skip to content

Commit 4e3f935

Browse files
authored
Fixed pathed template files to behave correctly for local relative paths without a dot (#1381)
1 parent 3c9a67a commit 4e3f935

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

nbconvert/exporters/templateexporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def _template_file_changed(self, change):
207207
# rather than a name already on template_path
208208
full_path = os.path.abspath(new)
209209
if os.path.isfile(full_path):
210-
directory, self.template_file = os.path.split(self.template_file)
210+
directory, self.template_file = os.path.split(full_path)
211211
# While not strictly an invalid template file name, the extension hints that there isn't a template directory involved
212212
if self.template_file.endswith('.tpl'):
213213
warnings.warn(

nbconvert/exporters/tests/test_templateexporter.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,14 @@ def test_absolute_template_name_tpl_compatibility(self):
175175
assert exporter.template.filename == template
176176
assert os.path.dirname(template) in exporter.template_paths
177177

178-
def test_relative_template_name_tpl_compatibility(self):
178+
# Can't use @pytest.mark.parametrize without removing all self.assert calls in all tests... repeating some here
179+
def relative_template_test(self, template):
179180
with tempdir.TemporaryWorkingDirectory() as td:
180181
with patch('os.getcwd', return_value=os.path.abspath(td)):
181-
template = os.path.join('relative', 'relative_template.tpl')
182182
template_abs = os.path.abspath(os.path.join(td, template))
183-
os.mkdir(os.path.dirname(template_abs))
183+
dirname = os.path.dirname(template_abs)
184+
if not os.path.exists(dirname):
185+
os.mkdir(dirname)
184186
test_output = 'relative!'
185187
with open(template_abs, 'w') as f:
186188
f.write(test_output)
@@ -192,6 +194,18 @@ def test_relative_template_name_tpl_compatibility(self):
192194
assert os.path.abspath(exporter.template.filename) == template_abs
193195
assert os.path.dirname(template_abs) in [os.path.abspath(d) for d in exporter.template_paths]
194196

197+
def test_relative_template_name_tpl_compatibility_local(self):
198+
self.relative_template_test('relative_template.tpl')
199+
200+
def test_relative_template_name_tpl_compatibility_nested(self):
201+
self.relative_template_test(os.path.join('relative', 'relative_template.tpl'))
202+
203+
def test_relative_template_name_tpl_compatibility_dot(self):
204+
self.relative_template_test(os.path.join('.', 'relative_template.tpl'))
205+
206+
def test_relative_template_name_tpl_compatibility_dot_nested(self):
207+
self.relative_template_test(os.path.join('.', 'relative', 'relative_template.tpl'))
208+
195209
def test_absolute_template_dir(self):
196210
with tempdir.TemporaryDirectory() as td:
197211
template = 'mytemplate'

0 commit comments

Comments
 (0)