annotate profilemanager/tests/test.py @ 79:145e111903d2 default tip

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