Fixing bug where similarly named files could not be deleted.

This commit is contained in:
Roland 2026-03-14 13:18:07 -06:00
parent 79a210bb6c
commit 8455955e69
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 27 additions and 1 deletions

View file

@ -230,7 +230,7 @@ async def delete(*paths: Union[str, pathlib.Path]):
for p2 in paths:
if p1 == p2:
continue
if str(p2).startswith(str(p1)):
if p1 != p2 and p1 in p2.parents:
raise InvalidFile(f'Cannot deleted nested paths')
with get_db_session() as session:
# Search for any files that have been tagged.

View file

@ -74,6 +74,19 @@ async def test_delete_file_multiple(async_client, test_session, make_files_struc
assert not baz.is_file()
@pytest.mark.asyncio
async def test_delete_file_missing(async_client, test_session, make_files_structure, test_directory):
"""Deleting a mix of existing and nonexistent files should succeed, deleting only the existing files."""
foo, bar = make_files_structure([
'config/foo.txt',
'config/bar.txt',
])
await lib.delete('config/foo.txt', 'config/._bar.txt', 'config/._baz.txt')
assert not foo.is_file()
assert bar.is_file()
@pytest.mark.asyncio
async def test_delete_file_names(async_client, test_session, make_files_structure, test_directory, tag_factory):
"""Will not refuse to delete a file that shares the name of a nearby file when they are in different FileGroups."""
@ -135,6 +148,19 @@ async def test_delete_nested(test_session, make_files_structure):
await lib.delete('foo', 'foo/bar')
@pytest.mark.asyncio
async def test_delete_similar_names(async_client, test_session, make_files_structure):
"""Deleting sibling files where one filename is a prefix of another should not be considered nested."""
make_files_structure([
'config/._wrolpi.yaml',
'config/._wrolpi.yaml.sb-98fa34a1-MG7phT',
'config/._wrolpi.yaml.sb-98fa34a1-N4k8NB',
])
await lib.delete('config/._wrolpi.yaml', 'config/._wrolpi.yaml.sb-98fa34a1-MG7phT',
'config/._wrolpi.yaml.sb-98fa34a1-N4k8NB')
@pytest.mark.parametrize(
'path,full,expected',
[