Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Pathlib in test_application.py and test_completerlib.py #12641

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
10 changes: 5 additions & 5 deletions IPython/core/tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import os
import tempfile
from pathlib import Path

import nose.tools as nt

from traitlets import Unicode

from IPython.core.application import BaseIPythonApplication
Expand Down Expand Up @@ -33,15 +33,15 @@ def test_unicode_cwd():
@dec.onlyif_unicode_paths
def test_unicode_ipdir():
"""Check that IPython starts with non-ascii characters in the IP dir."""
ipdir = tempfile.mkdtemp(suffix=u"€")
ipdir = Path(tempfile.mkdtemp(suffix=u"€"))

# Create the config file, so it tries to load it.
with open(os.path.join(ipdir, 'ipython_config.py'), "w") as f:
with ipdir.joinpath("ipython_config.py").open("w") as f:
pass
kir0ul marked this conversation as resolved.
Show resolved Hide resolved

old_ipdir1 = os.environ.pop("IPYTHONDIR", None)
old_ipdir2 = os.environ.pop("IPYTHON_DIR", None)
os.environ["IPYTHONDIR"] = ipdir
os.environ["IPYTHONDIR"] = str(ipdir)
try:
app = BaseIPythonApplication()
# The lines below are copied from Application.initialize()
Expand All @@ -61,7 +61,7 @@ class TestApp(BaseIPythonApplication):
test = Unicode().tag(config=True)

# Create the config file, so it tries to load it.
with open(os.path.join(td, 'ipython_config.py'), "w") as f:
with Path(td).joinpath("ipython_config.py").open("w") as f:
f.write("c.TestApp.test = 'config file'")

app = TestApp()
Expand Down
44 changes: 26 additions & 18 deletions IPython/core/tests/test_completerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import sys
import tempfile
import unittest
from os.path import join
from pathlib import Path

import nose.tools as nt

from IPython.core.completerlib import magic_run_completer, module_completion, try_import
from IPython.utils.tempdir import TemporaryDirectory
from IPython.testing.decorators import onlyif_unicode_paths
from IPython.utils.tempdir import TemporaryDirectory


class MockEvent(object):
Expand All @@ -33,14 +33,14 @@ class Test_magic_run_completer(unittest.TestCase):
dirs = [u"adir/", "bdir/"]

def setUp(self):
self.BASETESTDIR = tempfile.mkdtemp()
self.BASETESTDIR = Path(tempfile.mkdtemp())
for fil in self.files:
with open(join(self.BASETESTDIR, fil), "w") as sfile:
with self.BASETESTDIR.joinpath(fil).open("w") as sfile:
sfile.write("pass\n")
for d in self.dirs:
os.mkdir(join(self.BASETESTDIR, d))
os.mkdir(self.BASETESTDIR.joinpath(d))

self.oldpath = os.getcwd()
self.oldpath = Path.cwd()
os.chdir(self.BASETESTDIR)

def tearDown(self):
Expand Down Expand Up @@ -77,23 +77,31 @@ def test_completion_more_args(self):

def test_completion_in_dir(self):
# Github issue #3459
event = MockEvent(u'%run a.py {}'.format(join(self.BASETESTDIR, 'a')))
event = MockEvent(u"%run a.py {}".format(self.BASETESTDIR.joinpath("a")))
print(repr(event.line))
match = set(magic_run_completer(None, event))
# We specifically use replace here rather than normpath, because
# at one point there were duplicates 'adir' and 'adir/', and normpath
# would hide the failure for that.
self.assertEqual(match, {join(self.BASETESTDIR, f).replace('\\','/')
for f in (u'a.py', u'aao.py', u'aao.txt', u'adir/')})
self.assertEqual(
match,
{
str(self.BASETESTDIR.joinpath(f)).replace("\\", "/")
if Path(f).suffix
else (str(self.BASETESTDIR.joinpath(f)) + os.sep).replace("\\", "/")
for f in (u"a.py", u"aao.py", u"aao.txt", u"adir/")
},
)


class Test_magic_run_completer_nonascii(unittest.TestCase):
@onlyif_unicode_paths
def setUp(self):
self.BASETESTDIR = tempfile.mkdtemp()
self.BASETESTDIR = Path(tempfile.mkdtemp())
for fil in [u"aaø.py", u"a.py", u"b.py"]:
with open(join(self.BASETESTDIR, fil), "w") as sfile:
with self.BASETESTDIR.joinpath(fil).open("w") as sfile:
sfile.write("pass\n")
self.oldpath = os.getcwd()
self.oldpath = Path.cwd()
os.chdir(self.BASETESTDIR)

def tearDown(self):
Expand Down Expand Up @@ -135,8 +143,8 @@ def test_import_invalid_module():
with TemporaryDirectory() as tmpdir:
sys.path.insert( 0, tmpdir )
for name in invalid_module_names | valid_module_names:
filename = os.path.join(tmpdir, name + '.py')
open(filename, 'w').close()
filename = Path(tmpdir).joinpath(name + ".py")
filename.open("w").close()

s = set( module_completion('import foo') )
intersection = s.intersection(invalid_module_names)
Expand All @@ -150,15 +158,15 @@ def test_bad_module_all():

https://github.com/ipython/ipython/issues/9678
"""
testsdir = os.path.dirname(__file__)
sys.path.insert(0, testsdir)
testsdir = Path(__file__).parent
sys.path.insert(0, str(testsdir))
try:
results = module_completion('from bad_all import ')
nt.assert_in('puppies', results)
for r in results:
nt.assert_is_instance(r, str)
finally:
sys.path.remove(testsdir)
sys.path.remove(str(testsdir))


def test_module_without_init():
Expand All @@ -171,7 +179,7 @@ def test_module_without_init():
with TemporaryDirectory() as tmpdir:
sys.path.insert(0, tmpdir)
try:
os.makedirs(os.path.join(tmpdir, fake_module_name))
Path(tmpdir).joinpath(fake_module_name).mkdir()
s = try_import(mod=fake_module_name)
assert s == []
finally:
Expand Down
7 changes: 3 additions & 4 deletions IPython/testing/globalipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
import sys
import types
import warnings

from pathlib import Path

from . import tools

from IPython.core import page
from IPython.utils import io
from IPython.terminal.interactiveshell import TerminalInteractiveShell
from IPython.utils import io

from . import tools


class StreamProxy(io.IOStream):
Expand Down