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
9 changes: 5 additions & 4 deletions IPython/core/tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import tempfile
from pathlib import Path

import nose.tools as nt

Expand Down Expand Up @@ -33,15 +34,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 +62,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
31 changes: 16 additions & 15 deletions IPython/core/tests/test_completerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys
import tempfile
import unittest
from os.path import join
from pathlib import Path

import nose.tools as nt

Expand All @@ -33,12 +33,12 @@ 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()
os.chdir(self.BASETESTDIR)
Expand Down Expand Up @@ -77,21 +77,22 @@ 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)) if Path(f).suffix
else str(self.BASETESTDIR.joinpath(f)) + os.sep
for f in (u"a.py", u"aao.py", u"aao.txt", u"adir/")})
kir0ul marked this conversation as resolved.
Show resolved Hide resolved

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()
os.chdir(self.BASETESTDIR)
Expand Down Expand Up @@ -135,8 +136,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 +151,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 +172,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