When renaming authors fix empty author folder not being removed if it contained file explorer metadata but was otherwise empty

This commit is contained in:
Kovid Goyal 2024-11-01 16:52:01 +05:30
parent 803c684049
commit becebc1c0e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 20 additions and 1 deletions

View File

@ -1997,7 +1997,7 @@ class DB:
copy_tree(os.path.abspath(spath), tpath, delete_source=True, transform_destination_filename=transform_format_filenames) copy_tree(os.path.abspath(spath), tpath, delete_source=True, transform_destination_filename=transform_format_filenames)
parent = os.path.dirname(spath) parent = os.path.dirname(spath)
with suppress(OSError): with suppress(OSError):
os.rmdir(parent) # remove empty parent directory remove_dir_if_empty(parent, ignore_metadata_caches=True)
else: else:
os.makedirs(tpath) os.makedirs(tpath)
update_paths_in_db() update_paths_in_db()

View File

@ -589,6 +589,24 @@ class WritingTest(BaseTest):
def test_rename_items(self): # {{{ def test_rename_items(self): # {{{
' Test renaming of many-(many,one) items ' ' Test renaming of many-(many,one) items '
# Test renaming authors removes folders with junk in them
cl = self.cloned_library
cache = self.init_cache(cl)
fmtpath = cache.format_abspath(1, 'FMT1')
bookpath = os.path.dirname(fmtpath)
authorpath = os.path.dirname(bookpath)
self.assertTrue(os.path.exists(authorpath))
author = cache.field_for('authors', 1)[0]
os.mkdir(os.path.join(authorpath, '.DS_Store'))
open(os.path.join(authorpath, 'Thumbs.db'), 'wb').close()
amap = {v:k for k, v in cache.get_id_map('authors').items()}
cache.rename_items('authors', {amap[author]: 'renamed'})
try:
items = os.listdir(authorpath)
except FileNotFoundError:
items = []
self.assertFalse(items, 'Items in author folder: ' + ' '.join(items))
cl = self.cloned_library cl = self.cloned_library
cache = self.init_cache(cl) cache = self.init_cache(cl)
# Check that renaming authors updates author sort and path # Check that renaming authors updates author sort and path
@ -681,6 +699,7 @@ class WritingTest(BaseTest):
self.assertEqual(c.field_for('tags', 1), ('r', 'q', 'c')) self.assertEqual(c.field_for('tags', 1), ('r', 'q', 'c'))
self.assertEqual(c.field_for('tags', 2), ('X', 'y', 'z')) self.assertEqual(c.field_for('tags', 2), ('X', 'y', 'z'))
self.assertEqual(c.field_for('tags', 3), ('a', 'X', 'z')) self.assertEqual(c.field_for('tags', 3), ('a', 'X', 'z'))
# }}} # }}}
def test_composite_cache(self): # {{{ def test_composite_cache(self): # {{{