Skip to content

Commit

Permalink
Fixed pathed template files to behave correctly for local relative pa…
Browse files Browse the repository at this point in the history
…ths without a dot (#1381)
  • Loading branch information
MSeal authored Sep 12, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 3c9a67a commit 4e3f935
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion nbconvert/exporters/templateexporter.py
Original file line number Diff line number Diff line change
@@ -207,7 +207,7 @@ def _template_file_changed(self, change):
# rather than a name already on template_path
full_path = os.path.abspath(new)
if os.path.isfile(full_path):
directory, self.template_file = os.path.split(self.template_file)
directory, self.template_file = os.path.split(full_path)
# While not strictly an invalid template file name, the extension hints that there isn't a template directory involved
if self.template_file.endswith('.tpl'):
warnings.warn(
20 changes: 17 additions & 3 deletions nbconvert/exporters/tests/test_templateexporter.py
Original file line number Diff line number Diff line change
@@ -175,12 +175,14 @@ def test_absolute_template_name_tpl_compatibility(self):
assert exporter.template.filename == template
assert os.path.dirname(template) in exporter.template_paths

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

def test_relative_template_name_tpl_compatibility_local(self):
self.relative_template_test('relative_template.tpl')

def test_relative_template_name_tpl_compatibility_nested(self):
self.relative_template_test(os.path.join('relative', 'relative_template.tpl'))

def test_relative_template_name_tpl_compatibility_dot(self):
self.relative_template_test(os.path.join('.', 'relative_template.tpl'))

def test_relative_template_name_tpl_compatibility_dot_nested(self):
self.relative_template_test(os.path.join('.', 'relative', 'relative_template.tpl'))

def test_absolute_template_dir(self):
with tempdir.TemporaryDirectory() as td:
template = 'mytemplate'

0 comments on commit 4e3f935

Please sign in to comment.