class TestHowOriginalLocationIsStored:
    def test_for_absolute_paths(self):
        fs = Mock()
        self.dir = TrashDirectoryForPut("/volume/.Trash", "/volume", None, fs)
        self.dir.store_absolute_paths()

        self.assert_path_for_trashinfo_is("/file", "/file")
        self.assert_path_for_trashinfo_is("/file", "/dir/../file")
        self.assert_path_for_trashinfo_is("/outside/file", "/outside/file")
        self.assert_path_for_trashinfo_is("/volume/file", "/volume/file")
        self.assert_path_for_trashinfo_is("/volume/dir/file", "/volume/dir/file")

    def test_for_relative_paths(self):
        self.dir = TrashDirectoryForPut("/volume/.Trash", "/volume", None, Mock())
        self.dir.store_relative_paths()

        self.assert_path_for_trashinfo_is("/file", "/file")
        self.assert_path_for_trashinfo_is("/file", "/dir/../file")
        self.assert_path_for_trashinfo_is("/outside/file", "/outside/file")
        self.assert_path_for_trashinfo_is("file", "/volume/file")
        self.assert_path_for_trashinfo_is("dir/file", "/volume/dir/file")

    def assert_path_for_trashinfo_is(self, expected_value, file_to_be_trashed):
        result = self.dir.path_for_trash_info.for_file(file_to_be_trashed)
        assert_equals(expected_value, result)
Example #2
0
class TestHowOriginalLocationIsStored:
    def test_for_absolute_paths(self):
        fs = Mock()
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume', fs = fs)
        self.dir.store_absolute_paths()

        self.assert_path_for_trashinfo_is('/file'            , '/file')
        self.assert_path_for_trashinfo_is('/file'            , '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file'    , '/outside/file')
        self.assert_path_for_trashinfo_is('/volume/file'     , '/volume/file')
        self.assert_path_for_trashinfo_is('/volume/dir/file' , '/volume/dir/file')

    def test_for_relative_paths(self):
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume')
        self.dir.store_relative_paths()

        self.assert_path_for_trashinfo_is('/file'         , '/file')
        self.assert_path_for_trashinfo_is('/file'         , '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file' , '/outside/file')
        self.assert_path_for_trashinfo_is('file'          , '/volume/file')
        self.assert_path_for_trashinfo_is('dir/file'      , '/volume/dir/file')

    def assert_path_for_trashinfo_is(self, expected_value, file_to_be_trashed):
        result = self.dir.path_for_trash_info.for_file(file_to_be_trashed)
        assert_equals(expected_value, result)
Example #3
0
    def setUp(self):
        self.trashdirectory_base_dir = os.path.realpath(
            "./sandbox/testTrashDirectory")
        require_empty_dir(self.trashdirectory_base_dir)

        self.instance = TrashDirectoryForPut(self.trashdirectory_base_dir, "/",
                                             None, RealFs())
Example #4
0
 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'])
Example #5
0
    def test_for_relative_paths(self):
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume')
        self.dir.store_relative_paths()

        self.assert_path_for_trashinfo_is('/file'         , '/file')
        self.assert_path_for_trashinfo_is('/file'         , '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file' , '/outside/file')
        self.assert_path_for_trashinfo_is('file'          , '/volume/file')
        self.assert_path_for_trashinfo_is('dir/file'      , '/volume/dir/file')
Example #6
0
 def setUp(self):
     self.now = Mock()
     self.fs = Mock()
     self.trashdir = TrashDirectoryForPut('~/.Trash', '/', self.now,
                                          self.fs)
     self.trashdir.store_relative_paths()
     path_for_trash_info = Mock()
     path_for_trash_info.for_file.return_value = 'foo'
     self.trashdir.path_for_trash_info = path_for_trash_info
Example #7
0
    def test_for_relative_paths(self):
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume', Mock())
        self.dir.path_maker = TopDirRelativePaths('/volume')

        self.assert_path_for_trashinfo_is('/file', '/file')
        self.assert_path_for_trashinfo_is('/file', '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file', '/outside/file')
        self.assert_path_for_trashinfo_is('file', '/volume/file')
        self.assert_path_for_trashinfo_is('dir/file', '/volume/dir/file')
Example #8
0
    def test_for_absolute_paths(self):
        fs = Mock()
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume', fs = fs)
        self.dir.store_absolute_paths()

        self.assert_path_for_trashinfo_is('/file'            , '/file')
        self.assert_path_for_trashinfo_is('/file'            , '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file'    , '/outside/file')
        self.assert_path_for_trashinfo_is('/volume/file'     , '/volume/file')
        self.assert_path_for_trashinfo_is('/volume/dir/file' , '/volume/dir/file')
 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_for_absolute_paths(self):
        fs = Mock()
        path_maker = AbsolutePaths(None)
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume', fs,
                                        path_maker, None)

        self.assert_path_for_trashinfo_is('/file'            , '/file')
        self.assert_path_for_trashinfo_is('/file'            , '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file'    , '/outside/file')
        self.assert_path_for_trashinfo_is('/volume/file'     , '/volume/file')
        self.assert_path_for_trashinfo_is('/volume/dir/file' , '/volume/dir/file')
Example #11
0
    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')
    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)
Example #13
0
class TestTrashing:
    def setUp(self):
        self.now = Mock()
        self.fs = Mock()
        self.trashdir = TrashDirectoryForPut('~/.Trash', '/', self.now,
                                             self.fs)
        self.trashdir.store_relative_paths()
        path_for_trash_info = Mock()
        path_for_trash_info.for_file.return_value = 'foo'
        self.trashdir.path_for_trash_info = path_for_trash_info

    @istest
    def the_file_should_be_moved_in_trash_dir(self):

        self.trashdir.trash('foo')

        self.fs.move.assert_called_with('foo', '~/.Trash/files/foo')

    @istest
    def test_should_create_a_trashinfo(self):

        self.trashdir.trash('foo')

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

    @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.trash('foo')

        self.fs.atomic_write.assert_called_with(
            ANY, b'[Trash Info]\n'
            b'Path=foo\n'
            b'DeletionDate=2012-09-25T21:47:39\n')

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

        try:
            self.trashdir.trash('foo')
        except IOError:
            pass

        self.fs.remove_file.assert_called_with('~/.Trash/info/foo.trashinfo')
class TestHowOriginalLocationIsStored:
    def test_for_absolute_paths(self):
        fs = Mock()
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume', fs)
        self.dir.path_maker = AbsolutePaths(None)

        self.assert_path_for_trashinfo_is('/file'            , '/file')
        self.assert_path_for_trashinfo_is('/file'            , '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file'    , '/outside/file')
        self.assert_path_for_trashinfo_is('/volume/file'     , '/volume/file')
        self.assert_path_for_trashinfo_is('/volume/dir/file' , '/volume/dir/file')

    def test_for_relative_paths(self):
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume', Mock())
        self.dir.path_maker = TopDirRelativePaths('/volume')

        self.assert_path_for_trashinfo_is('/file'         , '/file')
        self.assert_path_for_trashinfo_is('/file'         , '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file' , '/outside/file')
        self.assert_path_for_trashinfo_is('file'          , '/volume/file')
        self.assert_path_for_trashinfo_is('dir/file'      , '/volume/dir/file')

    def assert_path_for_trashinfo_is(self, expected_value, file_to_be_trashed):
        result = self.dir.path_for_trash_info_for_file(file_to_be_trashed)
        assert_equals(expected_value, result)
class TestHowOriginalLocationIsStored:
    def test_for_absolute_paths(self):
        fs = Mock()
        paths = AbsolutePaths()
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume', fs,
                                        paths, None)

        self.assert_path_for_trashinfo_is('/file', '/file')
        self.assert_path_for_trashinfo_is('/file', '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file', '/outside/file')
        self.assert_path_for_trashinfo_is('/volume/file', '/volume/file')
        self.assert_path_for_trashinfo_is('/volume/dir/file', '/volume/dir/file')

    def test_for_relative_paths(self):
        paths = TopDirRelativePaths('/volume')
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume', Mock(),
                                        paths, None)

        self.assert_path_for_trashinfo_is('/file', '/file')
        self.assert_path_for_trashinfo_is('/file', '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file', '/outside/file')
        self.assert_path_for_trashinfo_is('file', '/volume/file')
        self.assert_path_for_trashinfo_is('dir/file', '/volume/dir/file')

    def assert_path_for_trashinfo_is(self, expected_value, file_to_be_trashed):
        result = self.dir.path_for_trash_info_for_file(file_to_be_trashed)
        assert expected_value == result
Example #16
0
    def setUp(self):
        self.trashdirectory_base_dir = os.path.realpath(
                "./sandbox/testTrashDirectory")
        require_empty_dir(self.trashdirectory_base_dir)

        self.instance = TrashDirectoryForPut(
                self.trashdirectory_base_dir,
                "/")
Example #17
0
 def setUp(self):
     self.now = Mock()
     self.fs = Mock()
     self.trashdir = TrashDirectoryForPut("~/.Trash", "/", now=self.now, fs=self.fs)
     self.trashdir.store_relative_paths()
     path_for_trash_info = Mock()
     path_for_trash_info.for_file.return_value = "foo"
     self.trashdir.path_for_trash_info = path_for_trash_info
Example #18
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')
        ]
    def test_for_relative_paths(self):
        self.dir = TrashDirectoryForPut("/volume/.Trash", "/volume", None, Mock())
        self.dir.store_relative_paths()

        self.assert_path_for_trashinfo_is("/file", "/file")
        self.assert_path_for_trashinfo_is("/file", "/dir/../file")
        self.assert_path_for_trashinfo_is("/outside/file", "/outside/file")
        self.assert_path_for_trashinfo_is("file", "/volume/file")
        self.assert_path_for_trashinfo_is("dir/file", "/volume/dir/file")
    def test_for_relative_paths(self):
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume', Mock())
        self.dir.path_maker = TopDirRelativePaths('/volume')

        self.assert_path_for_trashinfo_is('/file'         , '/file')
        self.assert_path_for_trashinfo_is('/file'         , '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file' , '/outside/file')
        self.assert_path_for_trashinfo_is('file'          , '/volume/file')
        self.assert_path_for_trashinfo_is('dir/file'      , '/volume/dir/file')
    def test_for_absolute_paths(self):
        fs = Mock()
        self.dir = TrashDirectoryForPut("/volume/.Trash", "/volume", None, fs)
        self.dir.store_absolute_paths()

        self.assert_path_for_trashinfo_is("/file", "/file")
        self.assert_path_for_trashinfo_is("/file", "/dir/../file")
        self.assert_path_for_trashinfo_is("/outside/file", "/outside/file")
        self.assert_path_for_trashinfo_is("/volume/file", "/volume/file")
        self.assert_path_for_trashinfo_is("/volume/dir/file", "/volume/dir/file")
    def test_for_absolute_paths(self):
        fs = Mock()
        self.dir = TrashDirectoryForPut('/volume/.Trash', '/volume', fs)
        self.dir.path_maker = AbsolutePaths(None)

        self.assert_path_for_trashinfo_is('/file'            , '/file')
        self.assert_path_for_trashinfo_is('/file'            , '/dir/../file')
        self.assert_path_for_trashinfo_is('/outside/file'    , '/outside/file')
        self.assert_path_for_trashinfo_is('/volume/file'     , '/volume/file')
        self.assert_path_for_trashinfo_is('/volume/dir/file' , '/volume/dir/file')
Example #23
0
class TestTrashing:
    def setUp(self):
        self.now = Mock()
        self.fs = Mock()
        self.trashdir = TrashDirectoryForPut("~/.Trash", "/", now=self.now, fs=self.fs)
        self.trashdir.store_relative_paths()
        path_for_trash_info = Mock()
        path_for_trash_info.for_file.return_value = "foo"
        self.trashdir.path_for_trash_info = path_for_trash_info

    @istest
    def the_file_should_be_moved_in_trash_dir(self):

        self.trashdir.trash("foo")

        self.fs.move.assert_called_with("foo", "~/.Trash/files/foo")

    @istest
    def test_should_create_a_trashinfo(self):

        self.trashdir.trash("foo")

        self.fs.atomic_write.assert_called_with("~/.Trash/info/foo.trashinfo", ANY)

    @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.trash("foo")

        self.fs.atomic_write.assert_called_with(ANY, "[Trash Info]\n" "Path=foo\n" "DeletionDate=2012-09-25T21:47:39\n")

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

        try:
            self.trashdir.trash("foo")
        except IOError:
            pass

        self.fs.remove_file.assert_called_with("~/.Trash/info/foo.trashinfo")
class TestTrashing(unittest.TestCase):
    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'])

    def test_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.')

    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.')

    def test_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.')

    def test_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.')
Example #25
0
class TestTrashDirectory_persit_trash_info:
    def setUp(self):
        self.trashdirectory_base_dir = os.path.realpath(
                "./sandbox/testTrashDirectory")
        require_empty_dir(self.trashdirectory_base_dir)

        self.instance = TrashDirectoryForPut(
                self.trashdirectory_base_dir,
                "/",
                RealFs())
        self.logger = Mock()

    def persist_trash_info(self, basename, content):
        return self.instance.persist_trash_info(basename,
                                                content,
                                                self.logger)

    def test_persist_trash_info_first_time(self):

        trash_info_file = self.persist_trash_info('dummy-path', b'content')
        assert_equal(join(self.trashdirectory_base_dir,'info', 'dummy-path.trashinfo'), trash_info_file)

        assert_equal('content', read(trash_info_file))

    def test_persist_trash_info_first_100_times(self):
        self.test_persist_trash_info_first_time()

        for i in range(1,100) :
            content=b'trashinfo content'
            trash_info_file = self.persist_trash_info('dummy-path', content)

            assert_equal("dummy-path_%s.trashinfo" % i,
                    os.path.basename(trash_info_file))
            assert_equal('trashinfo content', read(trash_info_file))

    def test_persist_trash_info_other_times(self):
        self.test_persist_trash_info_first_100_times()

        for i in range(101,200) :
            trash_info_file = self.persist_trash_info('dummy-path',b'content')
            trash_info_id = os.path.basename(trash_info_file)
            assert_true(trash_info_id.startswith("dummy-path_"))
            assert_equal('content', read(trash_info_file))
    test_persist_trash_info_first_100_times.stress_test = True
    test_persist_trash_info_other_times.stress_test = True
Example #26
0
class TestTrashDirectory_persit_trash_info:
    def setUp(self):
        self.trashdirectory_base_dir = os.path.realpath(
                "./sandbox/testTrashDirectory")
        require_empty_dir(self.trashdirectory_base_dir)

        self.instance = TrashDirectoryForPut(
                self.trashdirectory_base_dir,
                "/",
                None,
                RealFs())

    def persist_trash_info(self, basename, content):
        return self.instance.persist_trash_info(
                self.instance.info_dir, basename,content)

    def test_persist_trash_info_first_time(self):

        trash_info_file = self.persist_trash_info('dummy-path', 'content')
        assert_equals(join(self.trashdirectory_base_dir,'info', 'dummy-path.trashinfo'), trash_info_file)

        assert_equals('content', read(trash_info_file))

    def test_persist_trash_info_first_100_times(self):
        self.test_persist_trash_info_first_time()

        for i in range(1,100) :
            content='trashinfo content'
            trash_info_file = self.persist_trash_info('dummy-path', content)

            assert_equals("dummy-path_%s.trashinfo" % i,
                    os.path.basename(trash_info_file))
            assert_equals('trashinfo content', read(trash_info_file))

    def test_persist_trash_info_other_times(self):
        self.test_persist_trash_info_first_100_times()

        for i in range(101,200) :
            trash_info_file = self.persist_trash_info('dummy-path','content')
            trash_info_id = os.path.basename(trash_info_file)
            assert_true(trash_info_id.startswith("dummy-path_"))
            assert_equals('content', read(trash_info_file))
    test_persist_trash_info_first_100_times.stress_test = True
    test_persist_trash_info_other_times.stress_test = True