class TestHomeFallback(unittest.TestCase): def setUp(self): self.reporter = Mock() mount_points = ['/', 'sandbox/other_partition'] self.fs = Mock() self.trashcan = TrashPutCmd( stdout=None, stderr=None, getuid=lambda: 123, volumes=create_fake_volume_of(mount_points), now=datetime.now, environ=dict(), fs=self.fs, parent_path=os.path.dirname, realpath=lambda x: x) self.trashcan.reporter = self.reporter self.trashcan.logger = Mock() self.trashcan.ignore_missing = False 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', False) 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', False) 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
class TestGlobalTrashCan(unittest.TestCase): def setUp(self): self.reporter = Mock() self.fs = Mock() self.volumes = Mock() self.volumes.volume_of.return_value = '/' self.trashcan = TrashPutCmd(stdout=None, stderr=None, volumes=self.volumes, getuid=lambda: 123, now=datetime.now, environ=dict(), fs=self.fs, parent_path=os.path.dirname, realpath=lambda x: x) self.trashcan.reporter = self.reporter self.trashcan.logger = Mock() self.trashcan.ignore_missing = False def test_log_volume(self): self.trashcan.trash('a-dir/with-a-file', False) 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', False) self.reporter.unable_to_trash_file.assert_called_with('non-existent') def test_should_not_delete_a_dot_entru(self): self.trashcan.trash('.', False) 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.volumes.volume_of.side_effect = (lambda path: { '/foo': '/', '': '/', '/.Trash/123': '/', }[path]) self.trashcan.trash('foo', False)
def test(self): parent_path = lambda _:None class MyVolumes: def volume_of(self, _path): return '/volume' volumes = MyVolumes() 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 = TrashPutCmd(stdout=None, stderr=None, environ={}, volumes=volumes, fs=fs, getuid=lambda: 'uid', now=datetime.now, parent_path=parent_path, realpath=realpath) trashcan.reporter = reporter trashcan.ignore_missing = False trashcan.logger = Mock() trashcan.trash('') assert [ call('', '/volume/.Trash-uid') ] == reporter.file_has_been_trashed_in_as.mock_calls