~chicharreros/magicicada-client/1.0

« back to all changes in this revision

Viewing changes to ubuntuone/platform/tests/os_helper/test_linux.py

  • Committer: Magicicada Bot
  • Author(s): Facundo Batista
  • Date: 2018-03-22 18:27:26 UTC
  • mfrom: (1440.1.2 new-trash)
  • Revision ID: magicicada_bot-20180322182726-s9zxdqdnrqnuyav9
[r=nataliabidart] Start using a new way to send to trash, going away from Gnome.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
import logging
30
30
import os
31
31
 
32
 
try:
33
 
    from gi.repository import Gio as gio
34
 
    GIO_NOT_SUPPORTED = gio.IOErrorEnum.NOT_SUPPORTED
35
 
except ImportError:
36
 
    import gio
37
 
    GIO_NOT_SUPPORTED = gio.ERROR_NOT_SUPPORTED
38
 
 
39
32
from twisted.internet import defer
40
33
from ubuntuone.devtools.handlers import MementoHandler
41
34
 
42
35
from ubuntuone.platform.tests.os_helper import test_os_helper
 
36
from ubuntuone.platform.os_helper import linux
43
37
from ubuntuone.platform import (
44
38
    move_to_trash,
45
39
    open_file,
47
41
)
48
42
 
49
43
 
50
 
class FakeGIOFile(object):
51
 
    """Fake File for gio."""
52
 
 
53
 
    _bad_trash_call = None
54
 
 
55
 
    def __init__(self, path):
56
 
        pass
57
 
 
58
 
    @classmethod
59
 
    def new_for_path(klass, path):
60
 
        """Fake new_for_path for GI."""
61
 
        return klass(path)
62
 
 
63
 
    def trash(self, *args):
64
 
        """Fake trash call."""
65
 
        return self._bad_trash_call
66
 
 
67
 
 
68
44
class OSWrapperTests(test_os_helper.OSWrapperTests):
69
45
    """Tests for os wrapper functions."""
70
46
 
90
66
        self.assertNotEqual(os.stat(link).st_ino, stat_path(link).st_ino)
91
67
        self.assertEqual(os.lstat(link).st_ino, stat_path(link).st_ino)
92
68
 
93
 
    def test_movetotrash_file_bad(self):
94
 
        """Something bad happen when moving to trash, removed anyway."""
95
 
        FakeGIOFile._bad_trash_call = False   # error
96
 
        self.patch(gio, "File", FakeGIOFile)
97
 
        path = os.path.join(self.basedir, 'foo')
98
 
        open_file(path, 'w').close()
99
 
        move_to_trash(path)
100
 
        self.assertFalse(os.path.exists(path))
101
 
        self.assertTrue(self.handler.check_warning("Problems moving to trash!",
102
 
                                                   "Removing anyway", "foo"))
103
 
 
104
 
    def test_movetotrash_dir_bad(self):
105
 
        """Something bad happen when moving to trash, removed anyway."""
106
 
        FakeGIOFile._bad_trash_call = False   # error
107
 
        self.patch(gio, "File", FakeGIOFile)
108
 
        path = os.path.join(self.basedir, 'foo')
109
 
        os.mkdir(path)
110
 
        open_file(os.path.join(path, 'file inside directory'), 'w').close()
111
 
        move_to_trash(path)
112
 
        self.assertFalse(os.path.exists(path))
113
 
        self.assertTrue(self.handler.check_warning("Problems moving to trash!",
114
 
                                                   "Removing anyway", "foo"))
115
 
 
116
 
    def test_movetotrash_file_systemnotcapable(self):
117
 
        """The system is not capable of moving into trash."""
118
 
        FakeGIOFile._bad_trash_call = GIO_NOT_SUPPORTED
119
 
        self.patch(gio, "File", FakeGIOFile)
120
 
        path = os.path.join(self.basedir, 'foo')
121
 
        open_file(path, 'w').close()
122
 
        move_to_trash(path)
123
 
        self.assertFalse(os.path.exists(path))
124
 
        self.assertTrue(self.handler.check_warning("Problems moving to trash!",
125
 
                                                   "Removing anyway", "foo",
126
 
                                                   "ERROR_NOT_SUPPORTED"))
127
 
 
128
 
    def test_movetotrash_dir_systemnotcapable(self):
129
 
        """The system is not capable of moving into trash."""
130
 
        FakeGIOFile._bad_trash_call = GIO_NOT_SUPPORTED
131
 
        self.patch(gio, "File", FakeGIOFile)
132
 
        path = os.path.join(self.basedir, 'foo')
133
 
        os.mkdir(path)
134
 
        open_file(os.path.join(path, 'file inside directory'), 'w').close()
135
 
        move_to_trash(path)
136
 
        self.assertFalse(os.path.exists(path))
137
 
        self.assertTrue(self.handler.check_warning("Problems moving to trash!",
138
 
                                                   "Removing anyway", "foo",
139
 
                                                   "ERROR_NOT_SUPPORTED"))
 
69
    def test_movetotrash_bad(self):
 
70
        """Something bad happen when moving to trash, removed anyway.
 
71
 
 
72
        Simulating this as giving a non-existant path to the function, which
 
73
        will make it fail with OSError, which is the exception the send2trash
 
74
        library raises on any problem.
 
75
        """
 
76
        called = []
 
77
        self.patch(linux, '_remove_path', lambda p: called.append(p))
 
78
 
 
79
        path = os.path.join(self.basedir, 'non-existant')
 
80
        move_to_trash(path)
 
81
        self.assertEqual(called[0], path)
 
82
        self.assertTrue(self.handler.check_warning(
 
83
            "Problems moving to trash!", "Removing anyway", path))
 
84
 
 
85
    def test_remove_path_file(self):
 
86
        path = os.path.join(self.basedir, 'foo')
 
87
        open_file(path, 'w').close()
 
88
        linux._remove_path(path)
 
89
        self.assertFalse(os.path.exists(path))
 
90
 
 
91
    def test_remove_path_dir(self):
 
92
        path = os.path.join(self.basedir, 'foo')
 
93
        os.mkdir(path)
 
94
        open_file(os.path.join(path, 'file inside directory'), 'w').close()
 
95
        linux._remove_path(path)
 
96
        self.assertFalse(os.path.exists(path))