Example #1
0
class TestTrashRestoreCmd:
    def setUp(self):
        self.stdout = StringIO()
        self.stderr = StringIO()
        self.cmd = RestoreCmd(stdout=self.stdout,
                              stderr=self.stderr,
                              environ={},
                              exit = self.capture_exit_status,
                              input =lambda x: self.user_reply,
                              version = None)

    def capture_exit_status(self, exit_status):
        self.exit_status = exit_status

    def test_should_print_version(self):
        self.cmd.version = '1.2.3'
        self.cmd.run(['trash-restore', '--version'])

        assert_equals('trash-restore 1.2.3\n', self.stdout.getvalue())

    def test_with_no_args_and_no_files_in_trashcan(self):
        self.cmd.curdir = lambda: "cwd"

        self.cmd.run(['trash-restore'])

        assert_equals("No files trashed from current dir ('cwd')\n",
                self.stdout.getvalue())

    def test_until_the_restore(self):
        from trashcli.fs import remove_file
        from trashcli.fs import contents_of
        self.user_reply = '0'
        file('orig_file', 'w').write('original')
        file('info_file', 'w').write('')
        remove_file('parent/path')
        remove_file('parent')

        trashed_file = TrashedFile(
                'parent/path',
                None,
                'info_file',
                'orig_file')

        self.cmd.restore_asking_the_user([trashed_file])

        assert_equals('', self.stdout.getvalue())
        assert_equals('', self.stderr.getvalue())
        assert_true(not os.path.exists('info_file'))
        assert_true(not os.path.exists('orig_file'))
        assert_true(os.path.exists('parent/path'))
        assert_equals('original', contents_of('parent/path'))

    def test_until_the_restore_unit(self):
        from mock import Mock, call
        trashed_file = TrashedFile(
                'parent/path',
                None,
                'info_file',
                'orig_file')
        fs = Mock()
        self.cmd.fs = fs
        self.cmd.path_exists = lambda _: False

        self.user_reply = '0'
        self.cmd.restore_asking_the_user([trashed_file])

        assert_equals('', self.stdout.getvalue())
        assert_equals('', self.stderr.getvalue())
        assert_equals([
            call.mkdirs('parent')
            , call.move('orig_file', 'parent/path')
            , call.remove_file('info_file')
            ], fs.mock_calls)

    def test_with_no_args_and_files_in_trashcan(self):
        class thing(object):
            pass
        def some_files(append, _):
            t = thing()
            t.deletion_date = None
            t.path = None
            append(t)
        self.cmd.curdir = lambda: "cwd"
        self.cmd.for_all_trashed_file_in_dir = some_files
        self.cmd.input = lambda _ : ""

        self.cmd.run(['trash-restore'])

        assert_equals("   0 None None\n"
                "Exiting\n",
                self.stdout.getvalue())
    def test_when_user_reply_with_empty_string(self):
        self.user_reply = ''

        self.cmd.restore_asking_the_user([])

        assert_equals('Exiting\n', self.stdout.getvalue())

    def test_when_user_reply_with_empty_string(self):
        self.user_reply = 'non numeric'

        self.cmd.restore_asking_the_user([])

        assert_equals('Invalid entry\n', self.stderr.getvalue())
        assert_equals('', self.stdout.getvalue())
        assert_equals(1, self.exit_status)

    def test_when_user_reply_with_out_of_range_number(self):
        self.user_reply = '100'

        self.cmd.restore_asking_the_user([])

        assert_equals('Invalid entry\n', self.stderr.getvalue())
        assert_equals('', self.stdout.getvalue())
        assert_equals(1, self.exit_status)
Example #2
0
class TestTrashRestoreCmd:
    def setUp(self):
        self.stdout = StringIO()
        self.stderr = StringIO()
        self.cmd = RestoreCmd(stdout=self.stdout,
                              stderr=self.stderr,
                              environ={},
                              exit=self.capture_exit_status,
                              input=lambda x: self.user_reply,
                              version=None)

    def capture_exit_status(self, exit_status):
        self.exit_status = exit_status

    def test_should_print_version(self):
        self.cmd.version = '1.2.3'
        self.cmd.run(['trash-restore', '--version'])

        assert_equals('trash-restore 1.2.3\n', self.stdout.getvalue())

    def test_with_no_args_and_no_files_in_trashcan(self):
        self.cmd.curdir = lambda: "cwd"

        self.cmd.run(['trash-restore'])

        assert_equals("No files trashed from current dir ('cwd')\n",
                      self.stdout.getvalue())

    def test_until_the_restore_intgration(self):
        from trashcli.fs import remove_file
        from trashcli.fs import contents_of
        self.user_reply = '0'
        open('orig_file', 'w').write('original')
        open('info_file', 'w').write('')
        remove_file('parent/path')
        remove_file('parent')

        trashed_file = TrashedFile('parent/path', None, 'info_file',
                                   'orig_file')

        self.cmd.restore_asking_the_user([trashed_file])

        assert_equals('', self.stdout.getvalue())
        assert_equals('', self.stderr.getvalue())
        assert_true(not os.path.exists('info_file'))
        assert_true(not os.path.exists('orig_file'))
        assert_true(os.path.exists('parent/path'))
        assert_equals('original', contents_of('parent/path'))

    def test_until_the_restore_unit(self):
        trashed_file = TrashedFile('parent/path', None, 'info_file',
                                   'orig_file')
        fs = Mock()
        self.cmd.fs = fs
        self.cmd.path_exists = lambda _: False

        self.user_reply = '0'
        self.cmd.restore_asking_the_user([trashed_file])

        assert_equals('', self.stdout.getvalue())
        assert_equals('', self.stderr.getvalue())
        assert_equals([
            call.mkdirs('parent'),
            call.move('orig_file', 'parent/path'),
            call.remove_file('info_file')
        ], fs.mock_calls)

    def test_when_user_reply_with_empty_string(self):
        self.user_reply = ''

        self.cmd.restore_asking_the_user([])

        assert_equals('Exiting\n', self.stdout.getvalue())

    def test_when_user_reply_with_not_number(self):
        self.user_reply = 'non numeric'

        self.cmd.restore_asking_the_user([])

        assert_equals('Invalid entry\n', self.stderr.getvalue())
        assert_equals('', self.stdout.getvalue())
        assert_equals(1, self.exit_status)

    def test_when_user_reply_with_an_out_of_range_number(self):
        self.user_reply = '100'

        self.cmd.restore_asking_the_user([])

        assert_equals('Invalid entry\n', self.stderr.getvalue())
        assert_equals('', self.stdout.getvalue())
        assert_equals(1, self.exit_status)
class TestTrashRestoreCmd(unittest.TestCase):
    def setUp(self):
        self.stdout = StringIO()
        self.stderr = StringIO()
        trash_directories = make_trash_directories()
        self.logger = Mock(spec=[])
        trashed_files = TrashedFiles(self.logger,
                                     trash_directories,
                                     TrashDirectory(),
                                     contents_of)
        self.fs = Mock(spec=restore.FileSystem)
        self.cmd = RestoreCmd(stdout=self.stdout,
                              stderr=self.stderr,
                              exit = self.capture_exit_status,
                              input =lambda x: self.user_reply,
                              version=None,
                              trashed_files=trashed_files,
                              mount_points=os_mount_points,
                              fs=self.fs)

    def capture_exit_status(self, exit_status):
        self.exit_status = exit_status

    def test_should_print_version(self):
        self.cmd.version = '1.2.3'
        self.cmd.run(['trash-restore', '--version'])

        assert 'trash-restore 1.2.3\n' == self.stdout.getvalue()

    def test_with_no_args_and_no_files_in_trashcan(self):
        self.cmd.curdir = lambda: "cwd"

        self.cmd.run(['trash-restore'])

        assert ("No files trashed from current dir ('cwd')\n" ==
                self.stdout.getvalue())

    def test_until_the_restore_unit(self):
        self.fs.path_exists.return_value = False
        trashed_file = TrashedFile(
                'parent/path',
                None,
                'info_file',
                'orig_file')

        self.user_reply = '0'
        self.cmd.restore_asking_the_user([trashed_file])

        assert '' == self.stdout.getvalue()
        assert '' == self.stderr.getvalue()
        assert [call.path_exists('parent/path'),
                      call.mkdirs('parent'),
                      call.move('orig_file', 'parent/path'),
                      call.remove_file('info_file')] == self.fs.mock_calls

    def test_when_user_reply_with_empty_string(self):
        self.user_reply = ''

        self.cmd.restore_asking_the_user([])

        assert 'Exiting\n' == self.stdout.getvalue()

    def test_when_user_reply_with_not_number(self):
        self.user_reply = 'non numeric'

        self.cmd.restore_asking_the_user([])

        assert 'Invalid entry: not an index: non numeric\n' == \
               self.stderr.getvalue()
        assert '' == self.stdout.getvalue()
        assert 1 == self.exit_status