Beispiel #1
0
 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))
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()
Beispiel #3
0
    def test_help_output(self):
        err, out = StringIO(), StringIO()
        cmd = EmptyCmd(
            err=err,
            out=out,
            environ={},
            list_volumes=no_volumes,
            now=None,
            file_reader=FileSystemReader(),
            getuid=None,
            file_remover=None,
            version=None,
        )
        cmd.run('trash-empty', '--help')
        assert_equal(
            out.getvalue(),
            dedent("""\
            Usage: trash-empty [days]

            Purge trashed files.

            Options:
              --version   show program's version number and exit
              -h, --help  show this help message and exit

            Report bugs to https://github.com/andreafrancia/trash-cli/issues
            """))
Beispiel #4
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()
Beispiel #5
0
 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())
Beispiel #6
0
 def setUp(self):
     require_empty_dir('topdir')
     self.empty=EmptyCmd(
             out          = StringIO(),
             err          = StringIO(),
             environ      = {},
             list_volumes = lambda: ['topdir'],
             now          = None,
             file_reader  = FileSystemReader(),
             getuid       = lambda: 123,
             file_remover = FileRemover(),
             version      = None,
     )
Beispiel #7
0
 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 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))
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()
Beispiel #10
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'
     )
Beispiel #11
0
 def setUp(self):
     self.temp_dir = MyPath.make_temp_dir()
     self.top_dir = self.temp_dir / 'topdir'
     require_empty_dir(self.top_dir)
     self.empty = EmptyCmd(
         out=StringIO(),
         err=StringIO(),
         environ={},
         list_volumes=lambda: [self.top_dir],
         now=None,
         file_reader=FileSystemReader(),
         getuid=lambda: 123,
         file_remover=FileRemover(),
         version=None,
     )
Beispiel #12
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')
    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')
Beispiel #14
0
 def test_it_print_version(self):
     err, out = StringIO(), StringIO()
     cmd = EmptyCmd(err = err,
                    out = out,
                    environ = {},
                    list_volumes = no_volumes,
                    now = None,
                    file_reader = FileSystemReader(),
                    getuid = None,
                    file_remover = None,
                    version = '1.2.3',
                    )
     cmd.run('trash-empty', '--version')
     assert_equal(out.getvalue(), dedent("""\
         trash-empty 1.2.3
         """))
Beispiel #15
0
 def setUp(self):
     require_empty_dir('XDG_DATA_HOME')
     self.xdg_data_home   = 'XDG_DATA_HOME'
     self.environ = {'XDG_DATA_HOME':'XDG_DATA_HOME'}
     self.now = MagicMock(side_effect=RuntimeError)
     self.empty_cmd=EmptyCmd(
         out = StringIO(),
         err = StringIO(),
         environ = self.environ,
         list_volumes = no_volumes,
         now = self.now,
         file_reader = FileSystemReader(),
         getuid = None,
         file_remover = FileRemover(),
         version = None,
     )
Beispiel #16
0
    def test_without_arguments(self):
        from trashcli.rm import RmCmd
        cmd = RmCmd(None, None, None, None, None)
        cmd.stderr = StringIO()
        cmd.run([None])

        assert ('Usage:\n    trash-rm PATTERN\n\nPlease specify PATTERN\n' ==
                     cmd.stderr.getvalue())
Beispiel #17
0
 def setUp(self):
     require_empty_dir('XDG_DATA_HOME')
     self.info_dir_path   = 'XDG_DATA_HOME/Trash/info'
     self.files_dir_path  = 'XDG_DATA_HOME/Trash/files'
     self.environ = {'XDG_DATA_HOME':'XDG_DATA_HOME'}
     now = MagicMock(side_effect=RuntimeError)
     self.empty_cmd = EmptyCmd(
         out = StringIO(),
         err = StringIO(),
         environ = self.environ,
         list_volumes = no_volumes,
         now = now,
         file_reader = FileSystemReader(),
         getuid = None,
         file_remover = FileRemover(),
         version = None,
     )
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()
    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')
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))
Beispiel #21
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()
Beispiel #22
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'
                """))
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)
 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,
                    )
Beispiel #25
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()
Beispiel #26
0
    def test_without_pattern_argument(self):
        from trashcli.rm import RmCmd
        cmd = RmCmd(None, None, None, None, None)
        cmd.stderr = StringIO()
        cmd.file_reader = Mock([])
        cmd.file_reader.exists = Mock([], return_value=None)
        cmd.file_reader.entries_if_dir_exists = Mock([], return_value=[])
        cmd.environ = {}
        cmd.getuid = lambda: '111'
        cmd.list_volumes = lambda: ['/vol1']

        cmd.run([None, None])

        assert_equals('', cmd.stderr.getvalue())
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'
                """))
    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')
Beispiel #29
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()
Beispiel #30
0
 def __init__(self, XDG_DATA_HOME):
     self.XDG_DATA_HOME = XDG_DATA_HOME
     self.out = StringIO()
     self.err = StringIO()
 def __init__(self):
     from unit_tests.myStringIO import StringIO
     self.stream = StringIO()
     self.getvalue = self.stream.getvalue
 def setUp(self):
     self.out = StringIO()
 def __init__(self):
     from unit_tests.myStringIO import StringIO
     self.stream = StringIO()
     self.getvalue = self.stream.getvalue
Beispiel #34
0
 def test(self):
     out = StringIO()
     empty(['trash-empty', '-h'], stdout=out)
     six.assertRegex(self, out.getvalue(), '^Usage. trash-empty.*')
 def setUp(self):
     self.out = StringIO()
 def test(self):
     out = StringIO()
     empty(['trash-empty', '-h'], stdout = out)
     assert_regexp_matches(out.getvalue(), '^Usage. trash-empty.*')
Beispiel #37
0
 def test(self):
     out = StringIO()
     empty(['trash-empty', '-h'], stdout = out)
     assert_regexp_matches(out.getvalue(), '^Usage. trash-empty.*')