Skip to content

Commit 7b97de8

Browse files
committed
verify file existence in get_path()
1 parent 7ff6cb9 commit 7b97de8

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

jupyter_server/pytest_plugin.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,12 @@ def delete_fid_db(fid_db_path):
477477
@pytest.fixture
478478
def fid_manager(fid_db_path, tmp_path):
479479
"""Fixture returning a test-configured instance of `FileIdManager`."""
480-
return FileIdManager(db_path=fid_db_path, root_dir=str(tmp_path))
480+
fid_manager = FileIdManager(db_path=fid_db_path, root_dir=str(tmp_path))
481+
# disable journal so no temp journal file is created under `tmp_path`.
482+
# reduces test flakiness since sometimes journal file has same ino and
483+
# crtime as a deleted file, so FID manager detects it wrongly as a move
484+
fid_manager.con.execute("PRAGMA journal_mode = OFF")
485+
return fid_manager
481486

482487

483488
@pytest.fixture

jupyter_server/services/contents/fileidmanager.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,13 @@ def get_path(self, id):
298298
self._sync_all()
299299
self.con.commit()
300300
row = self.con.execute("SELECT path FROM Files WHERE id = ?", (id,)).fetchone()
301-
return row[0] if row else None
301+
path = row and row[0]
302+
303+
# if no record associated with ID or if file no longer exists at path, return None
304+
if path is None or self._stat(path) is None:
305+
return None
306+
307+
return path
302308

303309
def move(self, old_path, new_path, recursive=False):
304310
"""Handles file moves by updating the file path of the associated file

tests/services/contents/test_fileidmanager.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,20 @@ def test_index_already_indexed(fid_manager, test_path):
6868
assert id == fid_manager.index(test_path)
6969

7070

71+
# test out-of-band move detection for FIM.index()
72+
def test_index_oob_move(fid_manager, old_path, new_path):
73+
id = fid_manager.index(old_path)
74+
os.rename(old_path, new_path)
75+
assert fid_manager.index(new_path) == id
76+
77+
7178
def test_getters_indexed(fid_manager, test_path):
7279
id = fid_manager.index(test_path)
7380

7481
assert fid_manager.get_id(test_path) == id
7582
assert fid_manager.get_path(id) == test_path
7683

7784

78-
def test_get_id_unindexed(fid_manager, test_path_child):
79-
assert fid_manager.get_id(test_path_child) == None
80-
81-
8285
def test_getters_nonnormalized(fid_manager, test_path):
8386
path1 = os.path.join(test_path, "file")
8487
path2 = os.path.join(test_path, "some_dir", "..", "file")
@@ -92,11 +95,17 @@ def test_getters_nonnormalized(fid_manager, test_path):
9295
assert fid_manager.get_id(path3) == id
9396

9497

95-
# test out-of-band move detection for FIM.index()
96-
def test_index_oob_move(fid_manager, old_path, new_path):
97-
id = fid_manager.index(old_path)
98-
os.rename(old_path, new_path)
99-
assert fid_manager.index(new_path) == id
98+
def test_getters_oob_delete(fid_manager, test_path):
99+
id = fid_manager.index(test_path)
100+
os.rmdir(test_path)
101+
print(fid_manager.con.execute("select * from Files").fetchall())
102+
assert id is not None
103+
assert fid_manager.get_id(test_path) == None
104+
assert fid_manager.get_path(id) == None
105+
106+
107+
def test_get_id_unindexed(fid_manager, test_path_child):
108+
assert fid_manager.get_id(test_path_child) == None
100109

101110

102111
# test out-of-band move detection for FIM.get_id()

0 commit comments

Comments
 (0)