Skip to content

Commit eeaa621

Browse files
committed
test find_connection_file with local and absolute paths
- find_connection_file returns abspath - add missing cwd to `--existing` search path
1 parent e7a256b commit eeaa621

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

jupyter_client/connect.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def find_connection_file(filename='kernel-*.json', path=None, profile=None):
207207
for p in path:
208208
matches.extend(glob.glob(os.path.join(p, pat)))
209209

210+
matches = [ os.path.abspath(m) for m in matches ]
210211
if not matches:
211212
raise IOError("Could not find %r in %r" % (filename, path))
212213
elif len(matches) == 1:

jupyter_client/consoleapp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def init_connection_file(self):
161161
"""
162162
if self.existing:
163163
try:
164-
cf = find_connection_file(self.existing, [self.runtime_dir])
164+
cf = find_connection_file(self.existing, ['.', self.runtime_dir])
165165
except Exception:
166166
self.log.critical("Could not find existing kernel connection file %s", self.existing)
167167
self.exit(1)

jupyter_client/tests/test_connect.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from traitlets.config import Config
1010
from jupyter_core.application import JupyterApp
11+
from jupyter_core.paths import jupyter_runtime_dir
1112
from ipython_genutils.tempdir import TemporaryDirectory, TemporaryWorkingDirectory
1213
from ipython_genutils.py3compat import str_to_bytes
1314
from jupyter_client import connect, KernelClient
@@ -110,11 +111,9 @@ def test_load_connection_info():
110111

111112

112113
def test_find_connection_file():
113-
cfg = Config()
114114
with TemporaryDirectory() as d:
115-
cfg.ProfileDir.location = d
116115
cf = 'kernel.json'
117-
app = DummyConsoleApp(config=cfg, connection_file=cf)
116+
app = DummyConsoleApp(runtime_dir=d, connection_file=cf)
118117
app.initialize()
119118

120119
security_dir = app.runtime_dir
@@ -131,5 +130,45 @@ def test_find_connection_file():
131130
):
132131
assert connect.find_connection_file(query, path=security_dir) == profile_cf
133132

134-
JupyterApp._instance = None
133+
134+
def test_find_connection_file_local():
135+
with TemporaryWorkingDirectory() as d:
136+
cf = 'test.json'
137+
abs_cf = os.path.abspath(cf)
138+
with open(cf, 'w') as f:
139+
f.write('{}')
140+
141+
for query in (
142+
'test.json',
143+
'test',
144+
abs_cf,
145+
os.path.join('.', 'test.json'),
146+
):
147+
assert connect.find_connection_file(query, path=['.', jupyter_runtime_dir()]) == abs_cf
148+
149+
150+
def test_find_connection_file_relative():
151+
with TemporaryWorkingDirectory() as d:
152+
cf = 'test.json'
153+
os.mkdir('subdir')
154+
cf = os.path.join('subdir', 'test.json')
155+
abs_cf = os.path.abspath(cf)
156+
with open(cf, 'w') as f:
157+
f.write('{}')
158+
159+
for query in (
160+
os.path.join('.', 'subdir', 'test.json'),
161+
os.path.join('subdir', 'test.json'),
162+
abs_cf,
163+
):
164+
assert connect.find_connection_file(query, path=['.', jupyter_runtime_dir()]) == abs_cf
165+
166+
167+
def test_find_connection_file_abspath():
168+
with TemporaryDirectory() as d:
169+
cf = 'absolute.json'
170+
abs_cf = os.path.abspath(cf)
171+
with open(cf, 'w') as f:
172+
f.write('{}')
173+
assert connect.find_connection_file(abs_cf, path=jupyter_runtime_dir()) == abs_cf
135174

0 commit comments

Comments
 (0)