Пример #1
0
class TrashPutTest:
    def run(self, *arg):
        self.stderr = StringIO()
        self.stdout = StringIO()
        args = ['trash-put'] + list(arg)
        cmd = TrashPutCmd(self.stdout, self.stderr, None, None, None, None,
                          None, None, None)
        self._collect_exit_code(lambda: cmd.run(args))

    def _collect_exit_code(self, main_function):
        self.exit_code = 0
        result = main_function()
        if result is not None:
            self.exit_code = result

    def stderr_should_be(self, expected_err):
        assert_equals_with_unidiff(expected_err, self._actual_stderr())

    def stdout_should_be(self, expected_out):
        assert_equals_with_unidiff(expected_out, self._actual_stdout())

    def _actual_stderr(self):
        return self.stderr.getvalue()

    def _actual_stdout(self):
        return self.stdout.getvalue()
Пример #2
0
class TrashPutTest:
    def run(self, *arg):
        self.stderr = StringIO()
        self.stdout = StringIO()
        args = ['trash-put'] + list(arg)
        cmd = TrashPutCmd(self.stdout,
                          self.stderr,
                          None,
                          None,
                          None,
                          None,
                          None,
                          None,
                          None)
        self._collect_exit_code(lambda:cmd.run(args))

    def _collect_exit_code(self, main_function):
        self.exit_code = 0
        result=main_function()
        if result is not None:
            self.exit_code=result

    def stderr_should_be(self, expected_err):
        assert_equals_with_unidiff(expected_err, self._actual_stderr())

    def stdout_should_be(self, expected_out):
        assert_equals_with_unidiff(expected_out, self._actual_stdout())

    def _actual_stderr(self):
        return self.stderr.getvalue()

    def _actual_stdout(self):
        return self.stdout.getvalue()
class TestTrashPutIssueMessage:

    def setUp(self):
        self.out = StringIO()

    def test_trash_put_last_line(self):
        from trashcli.put import TrashPutCmd

        cmd = TrashPutCmd(self.out,
                          StringIO(),
                          None,
                          None,
                          None,
                          None,
                          None,
                          None,
                          None)
        cmd.run(['', '--help'])

        self.assert_last_line_of_output_is(
                'Report bugs to https://github.com/andreafrancia/trash-cli/issues')

    def test_trash_empty_last_line(self):
        from trashcli.empty import EmptyCmd
        from trashcli.trash import FileSystemReader

        cmd = EmptyCmd(self.out, StringIO(), [], lambda:[],
                       now = None,
                       file_reader = FileSystemReader(),
                       getuid = None,
                       file_remover = None,
                       version = None,
                       )
        cmd.run('', '--help')

        self.assert_last_line_of_output_is(
                'Report bugs to https://github.com/andreafrancia/trash-cli/issues')

    def test_trash_list_last_line(self):
        from trashcli.list import ListCmd

        cmd = ListCmd(self.out, None, None, None, None)
        cmd.run('', '--help')

        self.assert_last_line_of_output_is(
                'Report bugs to https://github.com/andreafrancia/trash-cli/issues')

    def assert_last_line_of_output_is(self, expected):
        output = self.out.getvalue()
        if len(output.splitlines()) > 0:
            last_line = output.splitlines()[-1]
        else:
            last_line = ''
        assert_equals(expected, last_line,
                ('Last line of output should be:\n\n%s\n\n' % expected +
                'but the output is\n\n%s' % output))
Пример #4
0
class RestoreTrashUser:
    def __init__(self, XDG_DATA_HOME):
        self.XDG_DATA_HOME = XDG_DATA_HOME
        self.out = StringIO()
        self.err = StringIO()

    def chdir(self, dir):
        self.current_dir = dir

    def run_restore(self, with_user_typing=''):
        environ = {'XDG_DATA_HOME': self.XDG_DATA_HOME}
        trash_directories = TrashDirectories(volume_of, os.getuid, environ)
        trash_directories2 = TrashDirectories2(volume_of, trash_directories)
        trashed_files = TrashedFiles(trash_directories2, TrashDirectory(),
                                     contents_of)
        RestoreCmd(
            stdout  = self.out,
            stderr  = self.err,
            exit    = [].append,
            input   = lambda msg: with_user_typing,
            curdir  = lambda: self.current_dir,
            trashed_files=trashed_files,
            mount_points=os_mount_points
        ).run([])

    def having_a_file_trashed_from_current_dir(self, filename):
        self.having_a_trashed_file(os.path.join(os.getcwd(), filename))
        remove_file(filename)
        assert not os.path.exists(filename)

    def having_a_trashed_file(self, path):
        write_file('%s/info/foo.trashinfo' % self._trash_dir(),
                   a_trashinfo(path))
        write_file('%s/files/foo' % self._trash_dir())

    def _trash_dir(self):
        return "%s/Trash" % self.XDG_DATA_HOME

    def stdout(self):
        return self.out.getvalue()

    def stderr(self):
        return self.err.getvalue()
Пример #5
0
class describe_trash_empty_command_line__on_invalid_options():
    def setUp(self):
        self.err, self.out = StringIO(), StringIO()
        self.cmd = EmptyCmd(
            err=self.err,
            out=self.out,
            environ={},
            list_volumes=no_volumes,
            now=None,
            file_reader=FileSystemReader(),
            getuid=None,
            file_remover=None,
            version=None,
        )

    def it_should_fail(self):

        self.exit_code = self.cmd.run('trash-empty', '-2')

        exit_code_for_command_line_usage = 64
        assert_equal(exit_code_for_command_line_usage, self.exit_code)

    def it_should_complain_to_the_standard_error(self):

        self.exit_code = self.cmd.run('trash-empty', '-2')

        assert_equal(
            self.err.getvalue(),
            dedent("""\
                trash-empty: invalid option -- '2'
                """))

    def test_with_a_different_option(self):

        self.cmd.run('trash-empty', '-3')

        assert_equal(
            self.err.getvalue(),
            dedent("""\
                trash-empty: invalid option -- '3'
                """))
Пример #6
0
class TestTrashPutIssueMessage(unittest.TestCase):
    def setUp(self):
        self.out = StringIO()

    def test_trash_put_last_line(self):
        from trashcli.put import TrashPutCmd

        cmd = TrashPutCmd(self.out, StringIO(), None, None, None, None, None,
                          None, None)
        cmd.run(['', '--help'])

        self.assert_last_line_of_output_is(
            'Report bugs to https://github.com/andreafrancia/trash-cli/issues')

    def test_trash_empty_last_line(self):
        from trashcli.empty import EmptyCmd
        from trashcli.fs import FileSystemReader

        cmd = EmptyCmd(
            self.out,
            StringIO(),
            [],
            lambda: [],
            now=None,
            file_reader=FileSystemReader(),
            getuid=None,
            file_remover=None,
            version=None,
        )
        cmd.run('', '--help')

        self.assert_last_line_of_output_is(
            'Report bugs to https://github.com/andreafrancia/trash-cli/issues')

    def test_trash_list_last_line(self):
        from trashcli.list import ListCmd

        cmd = ListCmd(self.out, None, None, None, None)
        cmd.run('', '--help')

        self.assert_last_line_of_output_is(
            'Report bugs to https://github.com/andreafrancia/trash-cli/issues')

    def assert_last_line_of_output_is(self, expected):
        output = self.out.getvalue()
        if len(output.splitlines()) > 0:
            last_line = output.splitlines()[-1]
        else:
            last_line = ''
        assert expected == last_line, (
            'Last line of output should be:\n\n%s\n\n' % expected +
            'but the output is\n\n%s' % output)
Пример #7
0
class OutputCollector:
    def __init__(self):
        from unit_tests.myStringIO import StringIO
        self.stream = StringIO()
        self.getvalue = self.stream.getvalue

    def write(self, data):
        self.stream.write(data)

    def should_be(self, expected):
        assert_equals_with_unidiff(expected, self.output())

    def output(self):
        return self.stream.getvalue()
Пример #8
0
class describe_trash_empty_command_line__on_invalid_options():
    def setUp(self):
        self.err, self.out = StringIO(), StringIO()
        self.cmd = EmptyCmd(
                       err = self.err,
                       out = self.out,
                       environ = {},
                       list_volumes = no_volumes,
                       now = None,
                       file_reader = FileSystemReader(),
                       getuid = None,
                       file_remover = None,
                       version = None,
                       )

    def it_should_fail(self):

        self.exit_code = self.cmd.run('trash-empty', '-2')

        exit_code_for_command_line_usage = 64
        assert_equals(exit_code_for_command_line_usage, self.exit_code)

    def it_should_complain_to_the_standard_error(self):

        self.exit_code = self.cmd.run('trash-empty', '-2')

        assert_equals(self.err.getvalue(), dedent("""\
                trash-empty: invalid option -- '2'
                """))

    def test_with_a_different_option(self):

        self.cmd.run('trash-empty', '-3')

        assert_equals(self.err.getvalue(), dedent("""\
                trash-empty: invalid option -- '3'
                """))
Пример #9
0
 def test(self):
     out = StringIO()
     help_printer = PrintHelp(trashcli.list.description, out)
     help_printer.my_print_help('trash-list')
     assert out.getvalue() == (
         'Usage: trash-list [OPTIONS...]\n'
         '\n'
         'List trashed files\n'
         '\n'
         'Options:\n'
         "  --version   show program's version number and exit\n"
         '  -h, --help  show this help message and exit\n'
         '\n'
         'Report bugs to https://github.com/andreafrancia/trash-cli/issues\n'
     )
Пример #10
0
    def test_trash_empty_will_crash_on_unreadable_directory_issue_48(self):
        out = StringIO()
        err = StringIO()
        mkdirs('data/Trash/files')
        mkdirs('data/Trash/files/unreadable')
        os.chmod('data/Trash/files/unreadable', 0o300)

        assert_true(os.path.exists('data/Trash/files/unreadable'))

        empty(['trash-empty'], stdout = out, stderr = err,
                environ={'XDG_DATA_HOME':'data'})

        assert_equal("trash-empty: cannot remove data/Trash/files/unreadable\n",
                     err.getvalue())
        os.chmod('data/Trash/files/unreadable', 0o700)
        shutil.rmtree('data')
Пример #11
0
    def test_trash_empty_will_crash_on_unreadable_directory_issue_48(self):
        out = StringIO()
        err = StringIO()
        mkdirs('data/Trash/files')
        mkdirs('data/Trash/files/unreadable')
        os.chmod('data/Trash/files/unreadable', 0o300)

        assert_true(os.path.exists('data/Trash/files/unreadable'))

        empty(['trash-empty'], stdout = out, stderr = err,
                environ={'XDG_DATA_HOME':'data'})

        assert_equals("trash-empty: cannot remove data/Trash/files/unreadable\n",
                      err.getvalue())
        os.chmod('data/Trash/files/unreadable', 0o700)
        shutil.rmtree('data')
Пример #12
0
class OutputCollector:
    def __init__(self):
        from unit_tests.myStringIO import StringIO
        self.stream = StringIO()
        self.getvalue = self.stream.getvalue
    def write(self,data):
        self.stream.write(data)
    def assert_equal_to(self, expected):
        return self.should_be(expected)
    def should_be(self, expected):
        assert_equals_with_unidiff(expected, self.output())
    def should_match(self, regex):
        text = self.output()
        from nose.tools import assert_regexp_matches
        assert_regexp_matches(text, regex)
    def output(self):
        return self.stream.getvalue()
Пример #13
0
class TestTrashRm(unittest.TestCase):
    def setUp(self):
        require_empty_dir('sandbox/xdh')
        self.xdg_data_home = MyPath.make_temp_dir()
        self.stderr = StringIO()
        self.trash_rm = RmCmd(environ={'XDG_DATA_HOME': self.xdg_data_home},
                              getuid=123,
                              list_volumes=lambda: [],
                              stderr=self.stderr,
                              file_reader=FileSystemReader())

    def test_issue69(self):
        self.add_trashinfo("foo.trashinfo", a_trashinfo_without_path())

        self.trash_rm.run(['trash-rm', 'ignored'])

        assert (self.stderr.getvalue() ==
                "trash-rm: %s/Trash/info/foo.trashinfo: unable to parse 'Path'"
                '\n' % self.xdg_data_home)

    def test_integration(self):
        self.add_trashinfo("1.trashinfo",
                           a_trashinfo_with_path('to/be/deleted'))
        self.add_trashinfo("2.trashinfo", a_trashinfo_with_path('to/be/kept'))

        self.trash_rm.run(['trash-rm', 'delete*'])

        self.assert_trashinfo_has_been_deleted("1.trashinfo")

    def add_trashinfo(self, trashinfo_name, contents):
        make_file(self.trashinfo_path(trashinfo_name), contents)

    def trashinfo_path(self, trashinfo_name):
        return self.xdg_data_home / 'Trash/info' / trashinfo_name

    def assert_trashinfo_has_been_deleted(self, trashinfo_name):
        import os
        path = self.trashinfo_path(trashinfo_name)
        assert not os.path.exists(path), 'File "%s" still exists' % path

    def tearDown(self):
        self.xdg_data_home.clean_up()
Пример #14
0
class OutputCollector:
    def __init__(self):
        from unit_tests.myStringIO import StringIO
        self.stream = StringIO()
        self.getvalue = self.stream.getvalue

    def write(self, data):
        self.stream.write(data)

    def assert_equal_to(self, expected):
        return self.should_be(expected)

    def should_be(self, expected):
        assert_equals_with_unidiff(expected, self.output())

    def should_match(self, regex):
        text = self.output()
        from nose.tools import assert_regexp_matches
        assert_regexp_matches(text, regex)

    def output(self):
        return self.stream.getvalue()
Пример #15
0
 def test(self):
     out = StringIO()
     empty(['trash-empty', '-h'], stdout = out)
     assert_regexp_matches(out.getvalue(), '^Usage. trash-empty.*')
Пример #16
0
 def test(self):
     out = StringIO()
     empty(['trash-empty', '-h'], stdout=out)
     six.assertRegex(self, out.getvalue(), '^Usage. trash-empty.*')
Пример #17
0
 def test(self):
     out = StringIO()
     empty(['trash-empty', '-h'], stdout = out)
     assert_regexp_matches(out.getvalue(), '^Usage. trash-empty.*')