From d01bac15bfa5c26be30a67348994fcee4461d9f7 Mon Sep 17 00:00:00 2001 From: krassowski <5832902+krassowski@users.noreply.github.com> Date: Wed, 10 Jul 2024 14:21:58 +0100 Subject: [PATCH] Add a test ensuring correct mode of index dir --- .../jupyter_ai/chat_handlers/learn.py | 6 ++++-- .../jupyter_ai/tests/test_handlers.py | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py index 2d09aa9a5..33d3ceca8 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py @@ -97,11 +97,13 @@ def __init__(self, *args, **kwargs): self.metadata = IndexMetadata(dirs=[]) self.prev_em_id = None + self._ensure_dirs() + self._load() + + def _ensure_dirs(self): if not os.path.exists(INDEX_SAVE_DIR): os.makedirs(INDEX_SAVE_DIR) - self._load() - def _load(self): """Loads the vector store.""" if self.index is not None: diff --git a/packages/jupyter-ai/jupyter_ai/tests/test_handlers.py b/packages/jupyter-ai/jupyter_ai/tests/test_handlers.py index 0194dec73..496cde371 100644 --- a/packages/jupyter-ai/jupyter_ai/tests/test_handlers.py +++ b/packages/jupyter-ai/jupyter_ai/tests/test_handlers.py @@ -1,3 +1,24 @@ +import os +import stat +from unittest import mock + +from jupyter_ai.chat_handlers import learn + + +class MockLearnHandler(learn.LearnChatHandler): + def __init__(self): + pass + + +def test_learn_index_permissions(tmp_path): + test_dir = tmp_path / "test" + with mock.patch.object(learn, "INDEX_SAVE_DIR", new=test_dir): + handler = MockLearnHandler() + handler._ensure_dirs() + mode = os.stat(test_dir).st_mode + assert stat.filemode(mode) == "drwx------" + + # TODO # import json