ProfileManager
view profilemanager/tests/test.py @ 79:145e111903d2
add MPL license
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Mon May 10 13:11:38 2010 -0700 (2 years ago) |
| parents | e76a122d8622 |
| children |
line source
1 #!/usr/bin/env python
3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 #
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
10 #
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
14 # License.
15 #
16 # The Original Code is mozilla.org code.
17 #
18 # The Initial Developer of the Original Code is
19 # Mozilla.org.
20 # Portions created by the Initial Developer are Copyright (C) 2010
21 # the Initial Developer. All Rights Reserved.
22 #
23 # Contributor(s):
24 # Jeff Hammel <jhammel@mozilla.com> (Original author)
25 #
26 # Alternatively, the contents of this file may be used under the terms of
27 # either of the GNU General Public License Version 2 or later (the "GPL"),
28 # or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 # in which case the provisions of the GPL or the LGPL are applicable instead
30 # of those above. If you wish to allow use of your version of this file only
31 # under the terms of either the GPL or the LGPL, and not to allow others to
32 # use your version of this file under the terms of the MPL, indicate your
33 # decision by deleting the provisions above and replace them with the notice
34 # and other provisions required by the GPL or the LGPL. If you do not delete
35 # the provisions above, a recipient may use your version of this file under
36 # the terms of any one of the MPL, the GPL or the LGPL.
37 #
38 # ***** END LICENSE BLOCK *****
40 import os
41 import time
42 from profilemanager import ProfileManager
44 # Get the path to the test profiles.ini file:
45 from pkg_resources import resource_filename
46 profiles_dir = os.path.join('tests', 'profiles')
47 path = os.path.join(profiles_dir, 'profiles.ini')
48 profiles = resource_filename('profilemanager', path)
49 profiles_dir = resource_filename('profilemanager', profiles_dir)
50 assert os.path.exists(profiles), '%s does not exist' % profiles
51 assert os.path.exists(profiles_dir), '%s does not exist' % profiles_dir
53 # Instatiate a ProfileManager:
54 manager = ProfileManager(profiles)
56 # Remove any profiles that didn't get cleaned up:
57 profiles_dict = manager.profiles_dict()
58 for profile in profiles_dict:
59 manager.remove(profile)
60 assert os.listdir(profiles_dir) == ['profiles.ini'], 'profiles_dir isnt empty except profiles.ini'
61 assert file(profiles).read().strip() == '[General]', 'profiles.ini isnt nearly empty'
63 # Create a new profile:
64 profiledir = manager.new('testprofile')
65 assert profiledir.endswith('.testprofile'), 'profiledir should end with testprofile, instead it is %s' % profiledir
66 assert manager.list() == ['testprofile']
68 # Clone the profile:
69 manager.clone('testprofile', 'cloneprofile')
70 assert sorted(manager.list()) == ['cloneprofile', 'testprofile']
71 manager.remove('cloneprofile')
73 # Backup the profile
74 manager.backup('testprofile')
75 backups = manager.backups('testprofile')
76 assert len(backups) == 1, "Expected one backup, instead got %d" % len(backups)
77 assert backups[0][0].startswith('testprofile')
78 manager.restore('testprofile')
79 manager.restore('testprofile', delete=True)
80 backups = manager.backups('testprofile')
81 assert not backups
82 # TODO: test the backup with a populated profile
84 # Test dated backups
85 manager.backup('testprofile')
86 old_backups = manager.backups('testprofile')
87 assert len(old_backups) == 1, "Expected one backup, instead got %d" % len(old_backups)
88 old_time = int(time.time())
89 time.sleep(3)
90 manager.backup('testprofile')
91 new_backups = manager.backups('testprofile')
92 assert len(new_backups) == 2, "Expected two backups, instead got %d" % len(new_backups)
93 restore_time = old_time + 1
94 restored = manager.restore('testprofile', date=restore_time)
95 assert restored == old_backups[0], "Tried to restore from %d [%s], but got [%s]" % (restore_time, old_backups[0], restored)
96 # TODO: test with alternate forms of date
98 # Restore the newest profile
99 restored = manager.restore('testprofile')
100 assert restored != old_backups[0], "Tried to restore the newest but got the old one [%s]" % old_backups[0]
101 manager.restore('testprofile', delete=True)
102 manager.restore('testprofile', delete=True)
103 assert not manager.backups()
104 assert manager.list() == ['testprofile']
106 # Cleanup:
107 manager.remove('testprofile')
108 assert manager.list() == []
109 f = file(profiles, 'w')
110 print >> f, '[General]'
