Skip to content

Commit 39326fe

Browse files
authored
Fix preferred_dir for sync contents manager (#1173)
1 parent 9070acb commit 39326fe

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

jupyter_server/services/contents/manager.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ def emit(self, data):
7979
def _validate_preferred_dir(self, proposal):
8080
value = proposal["value"].strip("/")
8181
try:
82-
dir_exists = run_sync(self.dir_exists)(value)
82+
import inspect
83+
84+
if inspect.iscoroutinefunction(self.dir_exists):
85+
dir_exists = run_sync(self.dir_exists)(value)
86+
else:
87+
dir_exists = self.dir_exists(value)
8388
except HTTPError as e:
8489
raise TraitError(e.log_message) from e
8590
if not dir_exists:

tests/test_serverapp.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@
1818
list_running_servers,
1919
random_ports,
2020
)
21+
from jupyter_server.services.contents.filemanager import (
22+
AsyncFileContentsManager,
23+
FileContentsManager,
24+
)
2125
from jupyter_server.utils import pathname2url, urljoin
2226

2327

28+
@pytest.fixture(params=[FileContentsManager, AsyncFileContentsManager])
29+
def jp_file_contents_manager_class(request, tmp_path):
30+
return request.param
31+
32+
2433
def test_help_output():
2534
"""jupyter server --help-all works"""
2635
check_help_all_output("jupyter_server")
@@ -291,11 +300,28 @@ def test_valid_preferred_dir_does_not_exist(tmp_path, jp_configurable_serverapp)
291300
path = str(tmp_path)
292301
path_subdir = str(tmp_path / "subdir")
293302
with pytest.raises(TraitError) as error:
294-
app = jp_configurable_serverapp(root_dir=path, preferred_dir=path_subdir)
303+
jp_configurable_serverapp(root_dir=path, preferred_dir=path_subdir)
295304

296305
assert "No such preferred dir:" in str(error)
297306

298307

308+
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
309+
def test_preferred_dir_validation_sync_regression(
310+
tmp_path, jp_configurable_serverapp, jp_file_contents_manager_class
311+
):
312+
path = str(tmp_path)
313+
path_subdir = str(tmp_path / "subdir")
314+
os.makedirs(path_subdir, exist_ok=True)
315+
app = jp_configurable_serverapp(
316+
root_dir=path,
317+
contents_manager_class=jp_file_contents_manager_class,
318+
)
319+
app.contents_manager.preferred_dir = path_subdir
320+
assert app.preferred_dir == path_subdir
321+
assert app.preferred_dir.startswith(app.root_dir)
322+
assert app.contents_manager.preferred_dir == "subdir"
323+
324+
299325
# This tests some deprecated behavior as well
300326
@pytest.mark.filterwarnings("ignore::FutureWarning")
301327
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)