From 87500cf5c40ec8f8d1a1d07ecf1313ba6b0ac563 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 1 Jan 2011 10:49:16 -0800 Subject: [PATCH] fix copying when file is already where it needs to be --- NEWS | 2 ++ beets/library.py | 9 +++++---- test/test_files.py | 12 ++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index ecf574ea3..2a986a1fb 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ * Fixed a spurious crash when LANG or a related environment variable is set to an invalid value (such as 'UTF-8' on some installations of Mac OS X). +* Fixed an error when trying to copy a file that is already at its + destination. 1.0b5 ----- diff --git a/beets/library.py b/beets/library.py index 67f41604a..684291a90 100644 --- a/beets/library.py +++ b/beets/library.py @@ -328,10 +328,11 @@ class Item(object): # Create necessary ancestry for the move. _mkdirall(dest) - if copy: - shutil.copy(self.path, dest) - else: - shutil.move(self.path, dest) + if not shutil._samefile(self.path, dest): + if copy: + shutil.copy(self.path, dest) + else: + shutil.move(self.path, dest) # Either copying or moving succeeded, so update the stored path. self.path = dest diff --git a/test/test_files.py b/test/test_files.py index 6382062bb..7cc665054 100644 --- a/test/test_files.py +++ b/test/test_files.py @@ -72,6 +72,18 @@ class MoveTest(unittest.TestCase): def test_move_changes_path(self): self.i.move(self.lib) self.assertEqual(self.i.path, beets.library._normpath(self.dest)) + + def test_copy_already_at_destination(self): + self.i.move(self.lib) + old_path = self.i.path + self.i.move(self.lib, copy=True) + self.assertEqual(self.i.path, old_path) + + def test_move_already_at_destination(self): + self.i.move(self.lib) + old_path = self.i.path + self.i.move(self.lib, copy=False) + self.assertEqual(self.i.path, old_path) class HelperTest(unittest.TestCase): def test_ancestry_works_on_file(self):