diff --git a/notebook/notebookapp.py b/notebook/notebookapp.py index 8b8facceb..9946f6e90 100644 --- a/notebook/notebookapp.py +++ b/notebook/notebookapp.py @@ -754,15 +754,19 @@ class NotebookApp(JupyterApp): else: return py3compat.getcwd() + def _notebook_dir_validate(self, value, trait): + # Strip any trailing slashes + value = value.rstrip(os.sep) + + if not os.path.isabs(value): + # If we receive a non-absolute path, make it absolute. + value = os.path.abspath(value) + if not os.path.isdir(value): + raise TraitError("No such notebook dir: %r" % value) + return value + def _notebook_dir_changed(self, name, old, new): """Do a bit of validation of the notebook dir.""" - if not os.path.isabs(new): - # If we receive a non-absolute path, make it absolute. - self.notebook_dir = os.path.abspath(new) - return - if not os.path.isdir(new): - raise TraitError("No such notebook dir: %r" % new) - # setting App.notebook_dir implies setting notebook and kernel dirs as well self.config.FileContentsManager.root_dir = new self.config.MappingKernelManager.root_dir = new diff --git a/notebook/tests/test_notebookapp.py b/notebook/tests/test_notebookapp.py index 4119d0858..daf4c4654 100644 --- a/notebook/tests/test_notebookapp.py +++ b/notebook/tests/test_notebookapp.py @@ -62,6 +62,11 @@ def test_invalid_nb_dir(): with nt.assert_raises(TraitError): app.notebook_dir = tf +def test_nb_dir_with_slash(): + with TemporaryDirectory(suffix="_slash/") as td: + app = NotebookApp(notebook_dir=td) + nt.assert_false(app.notebook_dir.endswith("/")) + def test_generate_config(): with TemporaryDirectory() as td: app = NotebookApp(config_dir=td) @@ -69,4 +74,4 @@ def test_generate_config(): with nt.assert_raises(NoStart): app.start() assert os.path.exists(os.path.join(td, 'jupyter_notebook_config.py')) - \ No newline at end of file +