Repository: jupyter/jupyter_client
Pull request #260 (2017-05-18 17:50:49)

test find_connection_file with local and absolute paths
- find_connection_file returns abspath
- add missing cwd to `--existing` search path


Patch:
diff --git a/jupyter_client/connect.py b/jupyter_client/connect.py
index 7d42b13..042904f 100644
--- a/jupyter_client/connect.py
+++ b/jupyter_client/connect.py
@@ -207,6 +207,7 @@ def find_connection_file(filename='kernel-*.json', path=None, profile=None):
     for p in path:
         matches.extend(glob.glob(os.path.join(p, pat)))
     
+    matches = [ os.path.abspath(m) for m in matches ]
     if not matches:
         raise IOError("Could not find %r in %r" % (filename, path))
     elif len(matches) == 1:
diff --git a/jupyter_client/consoleapp.py b/jupyter_client/consoleapp.py
index 2c57840..ce2ead4 100644
--- a/jupyter_client/consoleapp.py
+++ b/jupyter_client/consoleapp.py
@@ -161,7 +161,7 @@ class JupyterConsoleApp(ConnectionFileMixin):
         """
         if self.existing:
             try:
-                cf = find_connection_file(self.existing, [self.runtime_dir])
+                cf = find_connection_file(self.existing, ['.', self.runtime_dir])
             except Exception:
                 self.log.critical("Could not find existing kernel connection file %s", self.existing)
                 self.exit(1)


Test patch:
diff --git a/jupyter_client/tests/test_connect.py b/jupyter_client/tests/test_connect.py
index bd6e7c6..e1985e7 100644
--- a/jupyter_client/tests/test_connect.py
+++ b/jupyter_client/tests/test_connect.py
@@ -8,6 +8,7 @@ import os
 
 from traitlets.config import Config
 from jupyter_core.application import JupyterApp
+from jupyter_core.paths import jupyter_runtime_dir
 from ipython_genutils.tempdir import TemporaryDirectory, TemporaryWorkingDirectory
 from ipython_genutils.py3compat import str_to_bytes
 from jupyter_client import connect, KernelClient
@@ -110,11 +111,9 @@ def test_load_connection_info():
 
 
 def test_find_connection_file():
-    cfg = Config()
     with TemporaryDirectory() as d:
-        cfg.ProfileDir.location = d
         cf = 'kernel.json'
-        app = DummyConsoleApp(config=cfg, connection_file=cf)
+        app = DummyConsoleApp(runtime_dir=d, connection_file=cf)
         app.initialize()
 
         security_dir = app.runtime_dir
@@ -131,5 +130,45 @@ def test_find_connection_file():
             ):
             assert connect.find_connection_file(query, path=security_dir) == profile_cf
 
-        JupyterApp._instance = None
+
+def test_find_connection_file_local():
+    with TemporaryWorkingDirectory() as d:
+        cf = 'test.json'
+        abs_cf = os.path.abspath(cf)
+        with open(cf, 'w') as f:
+            f.write('{}')
+        
+        for query in (
+            'test.json',
+            'test',
+            abs_cf,
+            os.path.join('.', 'test.json'),
+        ):
+            assert connect.find_connection_file(query, path=['.', jupyter_runtime_dir()]) == abs_cf
+
+
+def test_find_connection_file_relative():
+    with TemporaryWorkingDirectory() as d:
+        cf = 'test.json'
+        os.mkdir('subdir')
+        cf = os.path.join('subdir', 'test.json')
+        abs_cf = os.path.abspath(cf)
+        with open(cf, 'w') as f:
+            f.write('{}')
+        
+        for query in (
+            os.path.join('.', 'subdir', 'test.json'),
+            os.path.join('subdir', 'test.json'),
+            abs_cf,
+        ):
+            assert connect.find_connection_file(query, path=['.', jupyter_runtime_dir()]) == abs_cf
+
+
+def test_find_connection_file_abspath():
+    with TemporaryDirectory() as d:
+        cf = 'absolute.json'
+        abs_cf = os.path.abspath(cf)
+        with open(cf, 'w') as f:
+            f.write('{}')
+        assert connect.find_connection_file(abs_cf, path=jupyter_runtime_dir()) == abs_cf