|
1 #!/usr/bin/env python |
|
2 |
|
3 # This Source Code Form is subject to the terms of the Mozilla Public |
|
4 # License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 # You can obtain one at http://mozilla.org/MPL/2.0/. |
|
6 |
|
7 import mozfile |
|
8 import mozprofile |
|
9 import os |
|
10 import tempfile |
|
11 import unittest |
|
12 |
|
13 here = os.path.dirname(os.path.abspath(__file__)) |
|
14 |
|
15 class TestProfilePrint(unittest.TestCase): |
|
16 |
|
17 def test_profileprint(self): |
|
18 """ |
|
19 test the summary function |
|
20 """ |
|
21 |
|
22 keys = set(['Files', 'Path', 'user.js']) |
|
23 ff_prefs = mozprofile.FirefoxProfile.preferences # shorthand |
|
24 pref_string = '\n'.join(['%s: %s' % (key, ff_prefs[key]) |
|
25 for key in sorted(ff_prefs.keys())]) |
|
26 |
|
27 tempdir = tempfile.mkdtemp() |
|
28 try: |
|
29 profile = mozprofile.FirefoxProfile(tempdir) |
|
30 parts = profile.summary(return_parts=True) |
|
31 parts = dict(parts) |
|
32 |
|
33 self.assertEqual(parts['Path'], tempdir) |
|
34 self.assertEqual(set(parts.keys()), keys) |
|
35 self.assertEqual(pref_string, parts['user.js'].strip()) |
|
36 |
|
37 except: |
|
38 raise |
|
39 finally: |
|
40 mozfile.rmtree(tempdir) |
|
41 |
|
42 def test_strcast(self): |
|
43 """ |
|
44 test casting to a string |
|
45 """ |
|
46 |
|
47 profile = mozprofile.Profile() |
|
48 self.assertEqual(str(profile), profile.summary()) |
|
49 |
|
50 def test_profile_diff(self): |
|
51 profile1 = mozprofile.Profile() |
|
52 profile2 = mozprofile.Profile(preferences=dict(foo='bar')) |
|
53 |
|
54 # diff a profile against itself; no difference |
|
55 self.assertEqual([], mozprofile.diff(profile1, profile1)) |
|
56 |
|
57 # diff two profiles |
|
58 diff = dict(mozprofile.diff(profile1, profile2)) |
|
59 self.assertEqual(diff.keys(), ['user.js']) |
|
60 lines = [line.strip() for line in diff['user.js'].splitlines()] |
|
61 self.assertTrue('+foo: bar' in lines) |
|
62 |
|
63 # diff a blank vs FirefoxProfile |
|
64 ff_profile = mozprofile.FirefoxProfile() |
|
65 diff = dict(mozprofile.diff(profile2, ff_profile)) |
|
66 self.assertEqual(diff.keys(), ['user.js']) |
|
67 lines = [line.strip() for line in diff['user.js'].splitlines()] |
|
68 self.assertTrue('-foo: bar' in lines) |
|
69 ff_pref_lines = ['+%s: %s' % (key, value) |
|
70 for key, value in mozprofile.FirefoxProfile.preferences.items()] |
|
71 self.assertTrue(set(ff_pref_lines).issubset(lines)) |
|
72 |
|
73 if __name__ == '__main__': |
|
74 unittest.main() |