Skip to content

Commit eb31253

Browse files
committed
Remove leading "/" from preferred_dir
Also validates set value to strip any leading slashes
1 parent 70f65a2 commit eb31253

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

jupyter_server/services/contents/filemanager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ def _default_preferred_dir(self):
6868
else:
6969
if value is not None:
7070
warnings.warn(
71-
"ServerApp.preferred_dir config is deprecated in jupyter-server 2.0. Use ContentsManager.preferred_dir with a relative path instead",
71+
"ServerApp.preferred_dir config is deprecated in jupyter-server 2.0. Use FileContentsManager.preferred_dir instead",
7272
FutureWarning,
7373
stacklevel=3,
7474
)
7575
try:
7676
path = Path(value)
77-
return "/" + path.relative_to(self.root_dir).as_posix()
77+
return path.relative_to(self.root_dir).as_posix()
7878
except ValueError:
7979
raise TraitError("%s is outside root contents directory" % value)
80-
return "/"
80+
return ""
8181

8282
@default("checkpoints_class")
8383
def _checkpoints_class_default(self):

jupyter_server/services/contents/manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ def emit(self, data):
6666
root_dir = Unicode("/", config=True)
6767

6868
preferred_dir = Unicode(
69-
"/",
69+
"",
7070
config=True,
7171
help=_i18n(
72-
"Preferred starting directory to use for notebooks, relative to the server root dir."
72+
"Preferred starting directory to use for notebooks. This is an API path (`/` separated, relative to root dir)"
7373
),
7474
)
7575

7676
@validate("preferred_dir")
7777
def _validate_preferred_dir(self, proposal):
78-
value = proposal["value"]
78+
value = proposal["value"].strip("/")
7979
try:
8080
dir_exists = run_sync(self.dir_exists)(value)
8181
except HTTPError as e:

tests/test_serverapp.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def test_valid_preferred_dir(tmp_path, jp_configurable_serverapp):
271271
assert app.preferred_dir == path
272272
assert app.root_dir == app.preferred_dir
273273
assert app.contents_manager.root_dir == path
274-
assert app.contents_manager.preferred_dir == "/"
274+
assert app.contents_manager.preferred_dir == ""
275275

276276

277277
@pytest.mark.filterwarnings("ignore::FutureWarning")
@@ -283,7 +283,7 @@ def test_valid_preferred_dir_is_root_subdir(tmp_path, jp_configurable_serverapp)
283283
assert app.root_dir == path
284284
assert app.preferred_dir == path_subdir
285285
assert app.preferred_dir.startswith(app.root_dir)
286-
assert app.contents_manager.preferred_dir == "/subdir"
286+
assert app.contents_manager.preferred_dir == "subdir"
287287

288288

289289
def test_valid_preferred_dir_does_not_exist(tmp_path, jp_configurable_serverapp):
@@ -332,8 +332,9 @@ def test_preferred_dir_validation(
332332

333333
os_preferred_dir = str(tmp_path / "subdir")
334334
os.makedirs(os_preferred_dir, exist_ok=True)
335-
config_preferred_dir = os_preferred_dir if config_target == "ServerApp" else "/subdir"
336-
expected_preferred_dir = "/subdir"
335+
config_preferred_dir = os_preferred_dir if config_target == "ServerApp" else "subdir"
336+
config_preferred_dir = config_preferred_dir + "/" # add trailing slash to ensure it is removed
337+
expected_preferred_dir = "subdir"
337338

338339
argv = []
339340
kwargs = {"root_dir": None}
@@ -355,7 +356,7 @@ def test_preferred_dir_validation(
355356
if preferred_dir_loc == "config":
356357
config_lines.append(f'c.{config_target}.preferred_dir = r"{config_preferred_dir}"')
357358
if preferred_dir_loc == "default":
358-
expected_preferred_dir = "/"
359+
expected_preferred_dir = ""
359360

360361
if config_file is not None:
361362
config_file.write_text("\n".join(config_lines))

0 commit comments

Comments
 (0)