def test(self): parent_path = lambda _:None volume_of = lambda _:'/volume' realpath = lambda _: None fs = Mock(['move', 'atomic_write', 'remove_file', 'ensure_dir', 'isdir', 'islink', 'has_sticky_bit']) fs.islink.side_effect = lambda path: { '/volume/.Trash':False }[path] fs.has_sticky_bit.side_effect = lambda path: { '/volume/.Trash':False }[path] reporter = Mock(['volume_of_file', 'found_unsecure_trash_dir_unsticky', 'trash_dir_with_volume', 'file_has_been_trashed_in_as']) trashcan = GlobalTrashCan({}, volume_of, reporter, fs, lambda :'uid', datetime.now, parent_path, realpath, Mock()) trashcan.trash('') assert_equal([ call('', '/volume/.Trash-uid') ], reporter.file_has_been_trashed_in_as.mock_calls)
class TestHomeFallback: def setUp(self): self.reporter = Mock() mount_points = ['/', 'sandbox/other_partition'] self.fs = Mock() self.trashcan = GlobalTrashCan( reporter = self.reporter, getuid = lambda: 123, volume_of = self.fake_volume_of(mount_points), now = None, environ = dict(), fs = self.fs) def test_should_skip_top_trash_if_does_not_exists(self): self.fs.mock_add_spec(['isdir', 'islink', 'move', 'atomic_write', 'remove_file', 'ensure_dir']) self.fs.isdir.side_effect = lambda x:['.Trash'][False] self.fs.islink.side_effect = lambda x:['.Trash'][False] self.trashcan.trash('sandbox/foo') assert_equals([ call.isdir('.Trash'), call.islink('.Trash'), call.ensure_dir('.Trash/123/info', 448), call.atomic_write('.Trash/123/info/foo.trashinfo', ANY), call.ensure_dir('.Trash/123/files', 448), call.move('sandbox/foo', '.Trash/123/files/foo') ], self.fs.mock_calls) def fake_volume_of(self, volumes): fstab = FakeFstab() for vol in volumes: fstab.add_mount(vol) return fstab.volume_of
def test(self): parent_path = lambda _:None volume_of = lambda _:'/volume' realpath = lambda _: None fs = Mock(['move', 'atomic_write', 'remove_file', 'ensure_dir', 'isdir', 'islink', 'has_sticky_bit']) fs.islink.side_effect = lambda path: { '/volume/.Trash':False }[path] fs.has_sticky_bit.side_effect = lambda path: { '/volume/.Trash':False }[path] reporter = Mock(['volume_of_file', 'found_unsecure_trash_dir_unsticky', 'trash_dir_with_volume', 'file_has_been_trashed_in_as']) trashcan = GlobalTrashCan({}, volume_of, reporter, fs, lambda :'uid', datetime.now, parent_path, realpath, Mock()) trashcan.trash('') assert_equals([ call('', '/volume/.Trash-uid') ], reporter.file_has_been_trashed_in_as.mock_calls)
class TestHomeFallback: def setUp(self): self.reporter = Mock() mount_points = ['/', 'sandbox/other_partition'] self.fs = Mock() self.trashcan = GlobalTrashCan( reporter=self.reporter, getuid=lambda: 123, volume_of=self.fake_volume_of(mount_points), now=None, environ=dict(), fs=self.fs, parent_path=os.path.dirname, realpath=lambda x: x) def test_should_skip_top_trash_if_does_not_exists(self): self.fs.mock_add_spec([ 'isdir', 'islink', 'move', 'atomic_write', 'remove_file', 'ensure_dir' ]) self.fs.isdir.side_effect = lambda x: ['.Trash'][False] self.fs.islink.side_effect = lambda x: ['.Trash'][False] self.trashcan.trash('sandbox/foo') assert_equals([ call.isdir('.Trash'), call.islink('.Trash'), call.ensure_dir('.Trash/123/info', 448), call.atomic_write('.Trash/123/info/foo.trashinfo', ANY), call.ensure_dir('.Trash/123/files', 448), call.move('sandbox/foo', '.Trash/123/files/foo') ], self.fs.mock_calls) def fake_volume_of(self, volumes): fstab = FakeFstab() for vol in volumes: fstab.add_mount(vol) return fstab.volume_of
class TestGlobalTrashCan: def setUp(self): self.reporter = Mock() self.fs = Mock() self.volume_of = Mock() self.volume_of.return_value = '/' self.trashcan = GlobalTrashCan( volume_of = self.volume_of, reporter = self.reporter, getuid = lambda:123, now = None, environ = dict(), fs = self.fs) @istest def should_report_when_trash_fail(self): self.fs.move.side_effect = IOError self.trashcan.trash('non-existent') self.reporter.unable_to_trash_file.assert_called_with('non-existent') @istest def should_not_delete_a_dot_entru(self): self.trashcan.trash('.') self.reporter.unable_to_trash_dot_entries.assert_called_with('.') @istest def bug(self): self.fs.mock_add_spec([ 'move', 'atomic_write', 'remove_file', 'ensure_dir', 'isdir', 'islink', 'has_sticky_bit', ], True) self.fs.islink.side_effect = (lambda path: { '/.Trash':False }[path]) self.volume_of.side_effect = (lambda path: { '/foo': '/', '': '/', '/.Trash/123': '/', }[path]) self.trashcan.trash('foo')
class TestGlobalTrashCan(unittest.TestCase): def setUp(self): self.reporter = Mock() self.fs = Mock() self.volume_of = Mock() self.volume_of.return_value = '/' self.trashcan = GlobalTrashCan(volume_of=self.volume_of, reporter=self.reporter, getuid=lambda: 123, now=datetime.now, environ=dict(), fs=self.fs, parent_path=os.path.dirname, realpath=lambda x: x, logger=Mock()) def test_log_volume(self): self.trashcan.trash('a-dir/with-a-file') self.reporter.volume_of_file.assert_called_with('/') def test_should_report_when_trash_fail(self): self.fs.move.side_effect = IOError self.trashcan.trash('non-existent') self.reporter.unable_to_trash_file.assert_called_with('non-existent') def test_should_not_delete_a_dot_entru(self): self.trashcan.trash('.') self.reporter.unable_to_trash_dot_entries.assert_called_with('.') def test_bug(self): self.fs.mock_add_spec([ 'move', 'atomic_write', 'remove_file', 'ensure_dir', 'isdir', 'islink', 'has_sticky_bit', ], True) self.fs.islink.side_effect = (lambda path: {'/.Trash': False}[path]) self.volume_of.side_effect = (lambda path: { '/foo': '/', '': '/', '/.Trash/123': '/', }[path]) self.trashcan.trash('foo') def test_what_happen_when_trashing_with_trash_dir(self): from trashcli.put import TrashDirectoryForPut fs = Mock() now = Mock() fs.mock_add_spec([ 'move', 'atomic_write', 'remove_file', 'ensure_dir', ], True) from unittest import SkipTest raise SkipTest("") trash_dir = TrashDirectoryForPut('/path', '/volume', fs) trash_dir.trash('garbage', now)
class TestHomeFallback(unittest.TestCase): def setUp(self): self.reporter = Mock() mount_points = ['/', 'sandbox/other_partition'] self.fs = Mock() self.trashcan = GlobalTrashCan( reporter=self.reporter, getuid=lambda: 123, volume_of=self.fake_volume_of(mount_points), now=datetime.now, environ=dict(), fs=self.fs, parent_path=os.path.dirname, realpath=lambda x: x, logger=Mock()) def test_use_of_top_trash_dir_when_sticky(self): self.fs.mock_add_spec([ 'isdir', 'islink', 'has_sticky_bit', 'move', 'atomic_write', 'remove_file', 'ensure_dir' ]) self.fs.isdir.return_value = True self.fs.islink.return_value = False self.fs.has_sticky_bit.return_value = True self.trashcan.trash('sandbox/foo') assert [ call.isdir('.Trash'), call.islink('.Trash'), call.has_sticky_bit('.Trash'), call.ensure_dir('.Trash/123/info', 448), call.atomic_write('.Trash/123/info/foo.trashinfo', ANY), call.ensure_dir('.Trash/123/files', 448), call.move('sandbox/foo', '.Trash/123/files/foo') ] == self.fs.mock_calls def test_bug_will_use_top_trashdir_even_with_not_sticky(self): self.fs.mock_add_spec([ 'isdir', 'islink', 'has_sticky_bit', 'move', 'atomic_write', 'remove_file', 'ensure_dir' ]) self.fs.isdir.return_value = True self.fs.islink.return_value = False self.fs.has_sticky_bit.return_value = False self.trashcan.trash('sandbox/foo') assert [ call.isdir('.Trash'), call.islink('.Trash'), call.has_sticky_bit('.Trash'), call.ensure_dir('.Trash-123/info', 448), call.atomic_write('.Trash-123/info/foo.trashinfo', ANY), call.ensure_dir('.Trash-123/files', 448), call.move('sandbox/foo', '.Trash-123/files/foo') ] == self.fs.mock_calls, self.fs.mock_calls def fake_volume_of(self, volumes): fstab = FakeFstab() for vol in volumes: fstab.add_mount(vol) return fstab.volume_of
class TestHomeFallback: def setUp(self): self.reporter = Mock() mount_points = ['/', 'sandbox/other_partition'] self.fs = Mock() self.trashcan = GlobalTrashCan( reporter = self.reporter, getuid = lambda: 123, volume_of = self.fake_volume_of(mount_points), now = datetime.now, environ = dict(), fs = self.fs, parent_path = os.path.dirname, realpath = lambda x:x, logger = Mock()) def test_use_of_top_trash_dir_when_sticky(self): self.fs.mock_add_spec(['isdir', 'islink', 'has_sticky_bit', 'move', 'atomic_write', 'remove_file', 'ensure_dir']) self.fs.isdir.return_value = True self.fs.islink.return_value = False self.fs.has_sticky_bit.return_value = True self.trashcan.trash('sandbox/foo') assert_equals([ call.isdir('.Trash'), call.islink('.Trash'), call.has_sticky_bit('.Trash'), call.ensure_dir('.Trash/123/info', 448), call.atomic_write('.Trash/123/info/foo.trashinfo', ANY), call.ensure_dir('.Trash/123/files', 448), call.move('sandbox/foo', '.Trash/123/files/foo') ], self.fs.mock_calls) def test_bug_will_use_top_trashdir_even_with_not_sticky(self): self.fs.mock_add_spec(['isdir', 'islink', 'has_sticky_bit', 'move', 'atomic_write', 'remove_file', 'ensure_dir']) self.fs.isdir.return_value = True self.fs.islink.return_value = False self.fs.has_sticky_bit.return_value = False self.trashcan.trash('sandbox/foo') assert_equals([ call.isdir('.Trash'), call.islink('.Trash'), call.has_sticky_bit('.Trash'), call.ensure_dir('.Trash-123/info', 448), call.atomic_write('.Trash-123/info/foo.trashinfo', ANY), call.ensure_dir('.Trash-123/files', 448), call.move('sandbox/foo', '.Trash-123/files/foo') ], self.fs.mock_calls, self.fs.mock_calls) def fake_volume_of(self, volumes): fstab = FakeFstab() for vol in volumes: fstab.add_mount(vol) return fstab.volume_of
class TestGlobalTrashCan: def setUp(self): self.reporter = Mock() self.fs = Mock() self.volume_of = Mock() self.volume_of.return_value = '/' self.trashcan = GlobalTrashCan( volume_of = self.volume_of, reporter = self.reporter, getuid = lambda:123, now = None, environ = dict(), fs = self.fs) @istest def should_report_when_trash_fail(self): self.fs.move.side_effect = IOError self.trashcan.trash('non-existent') self.reporter.unable_to_trash_file.assert_called_with('non-existent') @istest def should_not_delete_a_dot_entru(self): self.trashcan.trash('.') self.reporter.unable_to_trash_dot_entries.assert_called_with('.') @istest def bug(self): self.fs.mock_add_spec([ 'move', 'atomic_write', 'remove_file', 'ensure_dir', 'isdir', 'islink', 'has_sticky_bit', ], True) self.fs.islink.side_effect = (lambda path: { '/.Trash':False }[path]) self.volume_of.side_effect = (lambda path: { '/foo': '/', '': '/', '/.Trash/123': '/', }[path]) self.trashcan.trash('foo') def test_what_happen_when_trashing_with_trash_dir(self): from trashcli.put import TrashDirectoryForPut fs = Mock() now = Mock() fs.mock_add_spec([ 'move', 'atomic_write', 'remove_file', 'ensure_dir', ], True) from nose import SkipTest raise SkipTest() trash_dir = TrashDirectoryForPut('/path', '/volume', now, fs) trash_dir.trash('garbage')