michael@0: #!/usr/bin/env python michael@0: michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: # You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import mozfile michael@0: import mozprofile michael@0: import os michael@0: import tempfile michael@0: import unittest michael@0: michael@0: here = os.path.dirname(os.path.abspath(__file__)) michael@0: michael@0: class TestProfilePrint(unittest.TestCase): michael@0: michael@0: def test_profileprint(self): michael@0: """ michael@0: test the summary function michael@0: """ michael@0: michael@0: keys = set(['Files', 'Path', 'user.js']) michael@0: ff_prefs = mozprofile.FirefoxProfile.preferences # shorthand michael@0: pref_string = '\n'.join(['%s: %s' % (key, ff_prefs[key]) michael@0: for key in sorted(ff_prefs.keys())]) michael@0: michael@0: tempdir = tempfile.mkdtemp() michael@0: try: michael@0: profile = mozprofile.FirefoxProfile(tempdir) michael@0: parts = profile.summary(return_parts=True) michael@0: parts = dict(parts) michael@0: michael@0: self.assertEqual(parts['Path'], tempdir) michael@0: self.assertEqual(set(parts.keys()), keys) michael@0: self.assertEqual(pref_string, parts['user.js'].strip()) michael@0: michael@0: except: michael@0: raise michael@0: finally: michael@0: mozfile.rmtree(tempdir) michael@0: michael@0: def test_strcast(self): michael@0: """ michael@0: test casting to a string michael@0: """ michael@0: michael@0: profile = mozprofile.Profile() michael@0: self.assertEqual(str(profile), profile.summary()) michael@0: michael@0: def test_profile_diff(self): michael@0: profile1 = mozprofile.Profile() michael@0: profile2 = mozprofile.Profile(preferences=dict(foo='bar')) michael@0: michael@0: # diff a profile against itself; no difference michael@0: self.assertEqual([], mozprofile.diff(profile1, profile1)) michael@0: michael@0: # diff two profiles michael@0: diff = dict(mozprofile.diff(profile1, profile2)) michael@0: self.assertEqual(diff.keys(), ['user.js']) michael@0: lines = [line.strip() for line in diff['user.js'].splitlines()] michael@0: self.assertTrue('+foo: bar' in lines) michael@0: michael@0: # diff a blank vs FirefoxProfile michael@0: ff_profile = mozprofile.FirefoxProfile() michael@0: diff = dict(mozprofile.diff(profile2, ff_profile)) michael@0: self.assertEqual(diff.keys(), ['user.js']) michael@0: lines = [line.strip() for line in diff['user.js'].splitlines()] michael@0: self.assertTrue('-foo: bar' in lines) michael@0: ff_pref_lines = ['+%s: %s' % (key, value) michael@0: for key, value in mozprofile.FirefoxProfile.preferences.items()] michael@0: self.assertTrue(set(ff_pref_lines).issubset(lines)) michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()