From b327455fac758a1d2c748eff917544d80374d19e Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Thu, 3 May 2012 16:59:56 -0700 Subject: [PATCH] prune empty directories when moving (#266) --- beets/importer.py | 5 +++++ test/test_importer.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/beets/importer.py b/beets/importer.py index 8a48cb326..369d03402 100644 --- a/beets/importer.py +++ b/beets/importer.py @@ -731,7 +731,12 @@ def apply_choices(config): if config.copy or config.move: if config.move: # Just move the file. + old_path = item.path lib.move(item, False) + # Clean up empty parent directory. + if task.toppath: + util.prune_dirs(os.path.dirname(old_path), + task.toppath) else: # If it's a reimport, move the file. Otherwise, copy # and keep track of the old path. diff --git a/test/test_importer.py b/test/test_importer.py index 3a0875599..730565c3c 100644 --- a/test/test_importer.py +++ b/test/test_importer.py @@ -307,6 +307,25 @@ class ImportApplyTest(unittest.TestCase, _common.ExtraAsserts): # not exist. self.assertEqual(task.old_paths, []) + def test_apply_with_move(self): + config = _common.iconfig(self.lib, move=True) + applyc = importer.apply_choices(config) + applyc.next() + finalize = importer.finalize(config) + finalize.next() + _call_apply([applyc], [self.i], self.info) + self.assertExists(list(self.lib.items())[0].path) + self.assertNotExists(self.srcpath) + + def test_apply_with_move_prunes_empty_directory(self): + config = _common.iconfig(self.lib, move=True) + applyc = importer.apply_choices(config) + applyc.next() + finalize = importer.finalize(config) + finalize.next() + _call_apply([applyc], [self.i], self.info, self.srcdir) + self.assertNotExists(os.path.dirname(self.srcpath)) + class AsIsApplyTest(unittest.TestCase): def setUp(self): self.dbpath = os.path.join(_common.RSRC, 'templib.blb')