Skip to content

Commit

Permalink
Drop dependency on testpath. (#1723)
Browse files Browse the repository at this point in the history
Co-authored-by: Steven Silvester <steven.silvester@ieee.org>
  • Loading branch information
anntzer and blink1073 committed Apr 2, 2022
1 parent 9d6d83d commit a957175
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
6 changes: 4 additions & 2 deletions nbconvert/exporters/pdf.py
Expand Up @@ -6,11 +6,13 @@
import subprocess
import os
import sys
from tempfile import TemporaryDirectory

import shutil
from traitlets import Integer, List, Bool, Instance, Unicode, default
from testpath.tempdir import TemporaryWorkingDirectory
from typing import Optional
from .latex import LatexExporter
from ..utils import _contextlib_chdir

class LatexFailed(IOError):
"""Exception for failed latex run
Expand Down Expand Up @@ -174,7 +176,7 @@ def from_notebook_node(self, nb, resources=None, **kw):
self.texinputs = os.getcwd()

self._captured_outputs = []
with TemporaryWorkingDirectory():
with TemporaryDirectory() as td, _contextlib_chdir.chdir(td):
notebook_name = 'notebook'
resources['output_extension'] = '.tex'
tex_file = self.writer.write(latex, resources, notebook_name=notebook_name)
Expand Down
19 changes: 10 additions & 9 deletions nbconvert/exporters/tests/test_templateexporter.py
Expand Up @@ -14,14 +14,15 @@
from nbformat import v4
from unittest.mock import patch
from concurrent.futures import ProcessPoolExecutor
from tempfile import TemporaryDirectory

from .base import ExportersTestsBase
from .cheese import CheesePreprocessor
from ..templateexporter import TemplateExporter
from ..rst import RSTExporter
from ..html import HTMLExporter
from ..markdown import MarkdownExporter
from testpath import tempdir
from ...utils import _contextlib_chdir

import pytest

Expand Down Expand Up @@ -128,7 +129,7 @@ def test_pickle(self):
assert len(output) > 0

def test_absolute_template_file(self):
with tempdir.TemporaryDirectory() as td:
with TemporaryDirectory() as td:
template = os.path.join(td, 'abstemplate.ext.j2')
test_output = 'absolute!'
with open(template, 'w') as f:
Expand All @@ -140,7 +141,7 @@ def test_absolute_template_file(self):
assert os.path.dirname(template) in exporter.template_paths

def test_relative_template_file(self):
with tempdir.TemporaryWorkingDirectory() as td:
with TemporaryDirectory() as td, _contextlib_chdir.chdir(td):
with patch('os.getcwd', return_value=os.path.abspath(td)):
template = os.path.join('relative', 'relative_template.ext.j2')
template_abs = os.path.abspath(os.path.join(td, template))
Expand All @@ -155,7 +156,7 @@ def test_relative_template_file(self):
assert os.path.dirname(template_abs) in [os.path.abspath(d) for d in exporter.template_paths]

def test_absolute_template_file_compatibility(self):
with tempdir.TemporaryDirectory() as td:
with TemporaryDirectory() as td:
template = os.path.join(td, 'abstemplate.tpl')
test_output = 'absolute!'
with open(template, 'w') as f:
Expand All @@ -168,7 +169,7 @@ def test_absolute_template_file_compatibility(self):
assert os.path.dirname(template) in exporter.template_paths

def test_relative_template_file_compatibility(self):
with tempdir.TemporaryWorkingDirectory() as td:
with TemporaryDirectory() as td, _contextlib_chdir.chdir(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))
Expand All @@ -184,7 +185,7 @@ def test_relative_template_file_compatibility(self):
assert os.path.dirname(template_abs) in [os.path.abspath(d) for d in exporter.template_paths]

def test_absolute_template_name_tpl_compatibility(self):
with tempdir.TemporaryDirectory() as td:
with TemporaryDirectory() as td:
template = os.path.join(td, 'abstemplate.tpl')
test_output = 'absolute!'
with open(template, 'w') as f:
Expand Down Expand Up @@ -218,7 +219,7 @@ def test_absolute_template_name_5x_compatibility_display_priority(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 TemporaryDirectory() as td, _contextlib_chdir.chdir(td):
with patch('os.getcwd', return_value=os.path.abspath(td)):
template_abs = os.path.abspath(os.path.join(td, template))
dirname = os.path.dirname(template_abs)
Expand Down Expand Up @@ -248,7 +249,7 @@ 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:
with TemporaryDirectory() as td:
template = 'mytemplate'
template_file = os.path.join(td, template, 'index.py.j2')
template_dir = os.path.dirname(template_file)
Expand All @@ -265,7 +266,7 @@ def test_absolute_template_dir(self):
assert os.path.join(td, template) in exporter.template_paths

def test_local_template_dir(self):
with tempdir.TemporaryWorkingDirectory() as td:
with TemporaryDirectory() as td, _contextlib_chdir.chdir(td):
with patch('os.getcwd', return_value=os.path.abspath(td)):
template = 'mytemplate'
template_file = os.path.join(template, 'index.py.j2')
Expand Down
18 changes: 9 additions & 9 deletions nbconvert/tests/base.py
Expand Up @@ -3,6 +3,7 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

import contextlib
import io
import os
import glob
Expand All @@ -12,9 +13,11 @@
import unittest
import nbconvert
from subprocess import Popen, PIPE
from tempfile import TemporaryDirectory

from nbformat import v4, write
from testpath.tempdir import TemporaryWorkingDirectory

from ..utils import _contextlib_chdir


class TestsBase(unittest.TestCase):
Expand Down Expand Up @@ -86,15 +89,12 @@ def recursive_replace(self, text, search, replacement):
text = text.replace(search, replacement)
return text

@contextlib.contextmanager
def create_temp_cwd(self, copy_filenames=None):
temp_dir = TemporaryWorkingDirectory()

#Copy the files if requested.
if copy_filenames is not None:
self.copy_files_to(copy_filenames, dest=temp_dir.name)

#Return directory handler
return temp_dir
with TemporaryDirectory() as td, _contextlib_chdir.chdir(td):
if copy_filenames is not None: # Copy the files if requested.
self.copy_files_to(copy_filenames, dest=td)
yield td # Return directory handler

@classmethod
def merge_dicts(cls, *dict_args):
Expand Down
20 changes: 20 additions & 0 deletions nbconvert/utils/_contextlib_chdir.py
@@ -0,0 +1,20 @@
"""Backport of Python 3.11's contextlib.chdir."""


from contextlib import AbstractContextManager
import os


class chdir(AbstractContextManager):
"""Non thread-safe context manager to change the current working directory."""

def __init__(self, path):
self.path = path
self._old_cwd = []

def __enter__(self):
self._old_cwd.append(os.getcwd())
os.chdir(self.path)

def __exit__(self, *excinfo):
os.chdir(self._old_cwd.pop())
1 change: 0 additions & 1 deletion setup.py
Expand Up @@ -222,7 +222,6 @@ def get_data_files():
'entrypoints>=0.2.2',
'bleach',
'pandocfilters>=1.4.1',
'testpath',
'defusedxml',
'beautifulsoup4',
'nbclient>=0.5.0,<0.6.0',
Expand Down

0 comments on commit a957175

Please sign in to comment.