~nataliabidart/software-center/winged-migration

« back to all changes in this revision

Viewing changes to tests/test_utils.py

  • Committer: Natalia B. Bidart
  • Date: 2012-05-30 18:39:55 UTC
  • mto: This revision was merged to the branch mainline in revision 3030.
  • Revision ID: natalia.bidart@canonical.com-20120530183955-zbnjczayktmmg5tv
- Initial test cleanup:
  - Renamed test/ dir to tests/.
  - Isolated test code into tests/ directory, including moving all the
    get_test_window_foo from the gtk3 production modules to
    tests/gtk3/windows.py module.
  - Removed all the calls to Gtk.main() in gtk3 tests since that blocks the
    execution of the suite.
  - Pep8 and pyflakes fixes in the files already modified in this branch.
  - Minor bug fix in the softwarecenter/log.py module when trying to create a
    log dir with proper perms.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
1
import datetime
4
2
import glob
5
3
import multiprocessing
11
9
import time
12
10
import unittest
13
11
 
 
12
from tempfile import mkdtemp, NamedTemporaryFile
 
13
 
14
14
from mock import patch
15
15
 
16
 
from testutils import setup_test_env
 
16
from tests.utils import (
 
17
    do_events,
 
18
    setup_test_env,
 
19
    UTILS_DIR,
 
20
)
17
21
setup_test_env()
18
 
from softwarecenter.utils import (decode_xml_char_reference,
19
 
                                  release_filename_in_lists_from_deb_line,
20
 
                                  get_http_proxy_string_from_libproxy,
21
 
                                  )
22
 
from softwarecenter.testutils import do_events
 
22
 
 
23
from softwarecenter.expunge import ExpungeCache
 
24
from softwarecenter.utils import (
 
25
    capitalize_first_word,
 
26
    clear_token_from_ubuntu_sso_sync,
 
27
    decode_xml_char_reference,
 
28
    ensure_file_writable_and_delete_if_not,
 
29
    get_http_proxy_string_from_libproxy,
 
30
    get_http_proxy_string_from_gsettings,
 
31
    get_nice_date_string,
 
32
    get_title_from_html,
 
33
    get_uuid,
 
34
    is_no_display_desktop_file,
 
35
    make_string_from_list,
 
36
    release_filename_in_lists_from_deb_line,
 
37
    safe_makedirs,
 
38
    split_icon_ext,
 
39
)
 
40
 
 
41
EXPUNGE_SCRIPT = os.path.join(UTILS_DIR, 'expunge-cache.py')
23
42
 
24
43
 
25
44
class TestSCUtils(unittest.TestCase):
29
48
        xml = "What’s New"
30
49
        python = u"What\u2019s New"
31
50
        self.assertEqual(decode_xml_char_reference(xml), python)
32
 
        # fails currently 
 
51
        # fails currently
33
52
        #self.assertEqual(encode_for_xml(python), xml)
34
53
 
35
54
    def test_lists_filename(self):
38
57
                         "security.ubuntu.com_ubuntu_dists_maverick-security_Release")
39
58
 
40
59
    def test_get_http_proxy_from_gsettings(self):
41
 
        from softwarecenter.utils import get_http_proxy_string_from_gsettings
42
60
        # FIXME: do something more meaningful here once I figured out
43
61
        #        how to create a private fake gsettings
44
62
        proxy = get_http_proxy_string_from_gsettings()
65
83
        self.assertEqual(proxy, "http://user:pass@localhost:3128/")
66
84
 
67
85
    def test_get_title_from_html(self):
68
 
        from softwarecenter.utils import get_title_from_html
69
86
        html = """
70
87
<html>
71
88
<head>
94
111
                         "")
95
112
 
96
113
    def test_no_display_desktop_file(self):
97
 
        from softwarecenter.utils import is_no_display_desktop_file
98
114
        d = "/usr/share/app-install/desktop/wine1.4:wine.desktop"
99
115
        self.assertTrue(is_no_display_desktop_file(d))
100
116
        d = "/usr/share/app-install/desktop/software-center:ubuntu-software-center.desktop"
101
117
        self.assertFalse(is_no_display_desktop_file(d))
102
118
 
103
119
    def test_split_icon_ext(self):
104
 
        from softwarecenter.utils import split_icon_ext
105
 
        for unchanged in ["foo.bar.baz", "foo.bar", "foo", 
 
120
        for unchanged in ["foo.bar.baz", "foo.bar", "foo",
106
121
                          "foo.pngx", "foo.png.xxx"]:
107
122
            self.assertEqual(split_icon_ext(unchanged), unchanged)
108
123
        for changed in ["foo.png", "foo.tiff", "foo.jpg", "foo.jpeg"]:
109
 
            self.assertEqual(split_icon_ext(changed), 
 
124
            self.assertEqual(split_icon_ext(changed),
110
125
                            os.path.splitext(changed)[0])
111
126
 
112
127
    def test_get_nice_date_string(self):
113
 
        from softwarecenter.utils import get_nice_date_string
114
128
 
115
129
        now = datetime.datetime.utcnow()
116
130
 
128
142
 
129
143
        over_a_week = now + datetime.timedelta(days=-7)
130
144
        self.assertEqual(get_nice_date_string(over_a_week), over_a_week.isoformat().split('T')[0])
131
 
        
 
145
 
132
146
    def test_get_uuid(self):
133
 
        from softwarecenter.utils import get_uuid
134
147
        uuid = get_uuid()
135
148
        self.assertTrue(uuid and len(uuid) > 0)
136
149
 
137
150
    def test_clear_credentials(self):
138
 
        from softwarecenter.utils import clear_token_from_ubuntu_sso_sync
139
151
        clear_token_from_ubuntu_sso_sync("fo")
140
152
        do_events()
141
153
 
142
154
    def test_make_string_from_list(self):
143
 
        from softwarecenter.utils import make_string_from_list
144
155
        base = "There was a problem posting this review to %s (omg!)"
145
156
        # test the various forms
146
157
        l = ["twister"]
164
175
            "There was a problem posting this review to twister, factbook, identi.catz and baz (omg!)")
165
176
 
166
177
    def test_capitalize_first_word(self):
167
 
        from softwarecenter.utils import capitalize_first_word
168
178
        test_synopsis = "feature-rich console based todo list manager"
169
179
        capitalized = capitalize_first_word(test_synopsis)
170
180
        self.assertTrue(
175
185
            capitalized == "MPlayer's Movie Encoder")
176
186
        # ensure it does not crash for empty strings, LP: #1002271
177
187
        self.assertEqual(capitalize_first_word(""), "")
178
 
            
 
188
 
179
189
    def test_ensure_file_writable_and_delete_if_not(self):
180
 
        from softwarecenter.utils import ensure_file_writable_and_delete_if_not
181
 
        from tempfile import NamedTemporaryFile
182
190
        # first test that a non-writable file (0400) is deleted
183
191
        test_file_not_writeable = NamedTemporaryFile()
184
192
        os.chmod(test_file_not_writeable.name, stat.S_IRUSR)
193
201
        self.assertTrue(os.path.exists(test_file_writeable.name))
194
202
 
195
203
    def test_safe_makedirs(self):
196
 
        from softwarecenter.utils import safe_makedirs
197
 
        from tempfile import mkdtemp
198
204
        tmp = mkdtemp()
199
205
        # create base dir
200
206
        target = os.path.join(tmp, "foo", "bar")
215
221
        # cleanup
216
222
        shutil.rmtree(tmp)
217
223
 
 
224
 
218
225
class TestExpungeCache(unittest.TestCase):
219
226
 
220
227
    def test_expunge_cache(self):
227
234
            open(fullpath, "w").write(content)
228
235
            # set to 1970+1s time to ensure the cleaner finds it
229
236
            os.utime(fullpath, (1,1))
230
 
        res = subprocess.call(["../utils/expunge-cache.py", dirname])
 
237
        res = subprocess.call([EXPUNGE_SCRIPT, dirname])
231
238
        # no arguments
232
239
        self.assertEqual(res, 1)
233
240
        # by status
234
 
        res = subprocess.call(["../utils/expunge-cache.py",
 
241
        res = subprocess.call([EXPUNGE_SCRIPT,
235
242
                               "--debug",
236
243
                               "--by-unsuccessful-http-states",
237
244
                               dirname])
239
246
        self.assertTrue(os.path.exists(os.path.join(dirname, "foo-200")))
240
247
        self.assertTrue(os.path.exists(os.path.join(dirname, "foo-random")))
241
248
 
242
 
        # by time 
243
 
        res = subprocess.call(["../utils/expunge-cache.py",
 
249
        # by time
 
250
        res = subprocess.call([EXPUNGE_SCRIPT,
244
251
                               "--debug",
245
252
                               "--by-days", "1",
246
253
                               dirname])
254
261
            time.sleep(0.5)
255
262
            target = os.path.join(d, "marker.%s." % os.getpid())
256
263
            open(target, "w")
257
 
        from softwarecenter.expunge import ExpungeCache
258
264
        tmpdir = tempfile.mkdtemp()
259
 
        # create two ExpungeCache processes 
 
265
        # create two ExpungeCache processes
260
266
        e1 = ExpungeCache([tmpdir], by_days=0, by_unsuccessful_http_states=True)
261
267
        e1._cleanup_dir = set_marker
262
268
        e2 = ExpungeCache([tmpdir], by_days=0, by_unsuccessful_http_states=True)
272
278
        self.assertEqual(len(glob.glob(os.path.join(tmpdir, "marker.*"))), 1)
273
279
 
274
280
 
275
 
 
276
281
if __name__ == "__main__":
277
 
    import logging
278
 
    logging.basicConfig(level=logging.DEBUG)
279
282
    unittest.main()