Exemple #1
0
class TestTrashing:
    def setUp(self):
        self.now = Mock()
        self.fs = Mock()
        self.trashdir = TrashDirectoryForPut('~/.Trash', '/', self.fs)
        self.trashdir.path_maker = TopDirRelativePaths('/')
        path_maker = Mock()
        path_maker.calc_parent_path.return_value = ''
        self.trashdir.path_maker = path_maker
        self.logger = Mock(['debug'])

    @istest
    def the_file_should_be_moved_in_trash_dir(self):

        self.trashdir.trash2('foo', self.now, self.logger)

        self.fs.move.assert_called_with('foo', '~/.Trash/files/foo')
        self.logger.debug.assert_called_with(
            '.trashinfo created as ~/.Trash/info/foo.trashinfo.')

    @istest
    def test_should_create_a_trashinfo(self):

        self.trashdir.trash2('foo', self.now, self.logger)

        self.fs.atomic_write.assert_called_with('~/.Trash/info/foo.trashinfo',
                                                ANY)
        self.logger.debug.assert_called_with(
            '.trashinfo created as ~/.Trash/info/foo.trashinfo.')

    @istest
    def trashinfo_should_contains_original_location_and_deletion_date(self):
        from datetime import datetime

        self.now.return_value = datetime(2012, 9, 25, 21, 47, 39)
        self.trashdir.trash2('foo', self.now, self.logger)

        self.fs.atomic_write.assert_called_with(
            ANY, b'[Trash Info]\n'
            b'Path=foo\n'
            b'DeletionDate=2012-09-25T21:47:39\n')
        self.logger.debug.assert_called_with(
            '.trashinfo created as ~/.Trash/info/foo.trashinfo.')

    @istest
    def should_rollback_trashinfo_creation_on_problems(self):
        self.fs.move.side_effect = IOError

        try:
            self.trashdir.trash2('foo', self.now, self.logger)
        except IOError:
            pass

        self.fs.remove_file.assert_called_with('~/.Trash/info/foo.trashinfo')
        self.logger.debug.assert_called_with(
            '.trashinfo created as ~/.Trash/info/foo.trashinfo.')
Exemple #2
0
class TestTrashing(unittest.TestCase):
    def setUp(self):
        self.now = lambda: datetime.datetime(1970, 1, 1)
        self.fs = Mock()
        path_maker = TopDirRelativePaths('/')
        self.info_dir = Mock(['persist_trash_info'])
        self.info_dir.persist_trash_info.return_value = 'info_file'
        self.trashdir = TrashDirectoryForPut('~/.Trash', '/', self.fs,
                                             path_maker, self.info_dir)
        path_maker = Mock()
        path_maker.calc_parent_path.return_value = ''
        self.trashdir.path_maker = path_maker

    def test_the_file_should_be_moved_in_trash_dir(self):

        self.trashdir.trash2('foo', self.now)

        assert self.fs.mock_calls == [
            call.ensure_dir('~/.Trash/files', 0o700),
            call.move('foo', 'files/')
        ]
        assert self.info_dir.mock_calls == [
            call.persist_trash_info(
                'foo',
                b'[Trash Info]\nPath=foo\nDeletionDate=1970-01-01T00:00:00\n')
        ]

    def test_should_rollback_trashinfo_creation_on_problems(self):
        self.fs.move.side_effect = IOError

        try:
            self.trashdir.trash2('foo', self.now)
        except IOError:
            pass

        assert self.fs.mock_calls == [
            call.ensure_dir('~/.Trash/files', 448),
            call.move('foo', 'files/'),
            call.remove_file('info_file')
        ]
        assert self.info_dir.mock_calls == [
            call.persist_trash_info(
                'foo',
                b'[Trash Info]\nPath=foo\nDeletionDate=1970-01-01T00:00:00\n')
        ]