From 5f0c897788d85b790f79532a8ed48ecf848b85c0 Mon Sep 17 00:00:00 2001 From: Benjamin Lu Date: Wed, 23 Apr 2025 14:31:52 -0400 Subject: [PATCH] Fix split for empty dirs --- app/user_manager.py | 7 ++++- .../prompt_server_test/user_manager_test.py | 31 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/app/user_manager.py b/app/user_manager.py index 77839c0c3..4b1ef08b7 100644 --- a/app/user_manager.py +++ b/app/user_manager.py @@ -187,7 +187,12 @@ class UserManager(): rel_path = os.path.relpath(full_path, path).replace(os.sep, '/') if split_path: - return [rel_path] + rel_path.split('/') + if os.path.isdir(full_path): + dirname, basename = rel_path, "" + else: + head, tail = rel_path.rsplit('/', 1) if '/' in rel_path else ("", rel_path) + dirname, basename = head, tail + return [rel_path, dirname, basename] return rel_path diff --git a/tests-unit/prompt_server_test/user_manager_test.py b/tests-unit/prompt_server_test/user_manager_test.py index 27a83c9bd..86c254a1c 100644 --- a/tests-unit/prompt_server_test/user_manager_test.py +++ b/tests-unit/prompt_server_test/user_manager_test.py @@ -205,6 +205,37 @@ async def test_listuserdata_full_info_include_empty_dirs(aiohttp_client, app, tm assert "size" in info assert "modified" in info +async def test_listuserdata_recurse_split_include_empty_dirs(aiohttp_client, app, tmp_path): + # Arrange + test_dir = tmp_path / "test_dir" + file1 = test_dir / "file1.txt" + empty = test_dir / "empty_subdir" + occupying_dir = test_dir / "occupied_directory" + another_occupying_dir = occupying_dir / "another_occupied_directory" + file2 = another_occupying_dir / "file2.txt" + os.makedirs(test_dir) + os.makedirs(occupying_dir) + os.makedirs(another_occupying_dir) + os.makedirs(empty) + with open(file1, "w") as f: + f.write("content") + with open(file2, "w") as f: + f.write("nested content") + + client = await aiohttp_client(app) + + # Act + resp = await client.get("/userdata?dir=test_dir&split=true&emptyDirs=true&recurse=true") + + # Assert + assert resp.status == 200 + result = await resp.json() + assert set(tuple(r) for r in result) == { + ("file1.txt", "", "file1.txt"), + ("empty_subdir", "empty_subdir", ""), + ("occupied_directory/another_occupied_directory/file2.txt", "occupied_directory/another_occupied_directory", "file2.txt"), + } + async def test_post_userdata_new_file(aiohttp_client, app, tmp_path): client = await aiohttp_client(app) content = b"test content"