1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for bzrlib/generate_ids.py"""
27
class TestFileIds(tests.TestCase):
28
"""Test functions which generate file ids"""
30
def test_gen_file_id(self):
31
gen_file_id = generate_ids.gen_file_id
33
# We try to use the filename if possible
34
self.assertStartsWith(gen_file_id('bar'), 'bar-')
36
# but we squash capitalization, and remove non word characters
37
self.assertStartsWith(gen_file_id('Mwoo oof\t m'), 'mwoooofm-')
39
# We also remove leading '.' characters to prevent hidden file-ids
40
self.assertStartsWith(gen_file_id('..gam.py'), 'gam.py-')
41
self.assertStartsWith(gen_file_id('..Mwoo oof\t m'), 'mwoooofm-')
43
# we remove unicode characters, and still don't end up with a
45
self.assertStartsWith(gen_file_id(u'\xe5\xb5.txt'), 'txt-')
47
# Our current method of generating unique ids adds 33 characters
48
# plus an serial number (log10(N) characters)
49
# to the end of the filename. We now restrict the filename portion to
50
# be <= 20 characters, so the maximum length should now be approx < 60
52
# Test both case squashing and length restriction
53
fid = gen_file_id('A'*50 + '.txt')
54
self.assertStartsWith(fid, 'a'*20 + '-')
55
self.failUnless(len(fid) < 60)
57
# restricting length happens after the other actions, so
58
# we preserve as much as possible
59
fid = gen_file_id('\xe5\xb5..aBcd\tefGhijKLMnop\tqrstuvwxyz')
60
self.assertStartsWith(fid, 'abcdefghijklmnopqrst-')
61
self.failUnless(len(fid) < 60)
63
def test__next_id_suffix_sets_suffix(self):
64
generate_ids._gen_file_id_suffix = None
65
generate_ids._next_id_suffix()
66
self.assertNotEqual(None, generate_ids._gen_file_id_suffix)
68
def test__next_id_suffix_increments(self):
69
generate_ids._gen_file_id_suffix = "foo-"
70
generate_ids._gen_file_id_serial = 1
72
self.assertEqual("foo-2", generate_ids._next_id_suffix())
73
self.assertEqual("foo-3", generate_ids._next_id_suffix())
74
self.assertEqual("foo-4", generate_ids._next_id_suffix())
75
self.assertEqual("foo-5", generate_ids._next_id_suffix())
76
self.assertEqual("foo-6", generate_ids._next_id_suffix())
77
self.assertEqual("foo-7", generate_ids._next_id_suffix())
78
self.assertEqual("foo-8", generate_ids._next_id_suffix())
79
self.assertEqual("foo-9", generate_ids._next_id_suffix())
80
self.assertEqual("foo-10", generate_ids._next_id_suffix())
82
# Reset so that all future ids generated in the test suite
83
# don't end in 'foo-XXX'
84
generate_ids._gen_file_id_suffix = None
85
generate_ids._gen_file_id_serial = 0
87
def test_gen_root_id(self):
88
# Mostly just make sure gen_root_id() exists
89
root_id = generate_ids.gen_root_id()
90
self.assertStartsWith(root_id, 'tree_root-')
93
class TestGenRevisionId(tests.TestCase):
94
"""Test generating revision ids"""
96
def assertMatchesRe(self, regex, text):
97
"""Make sure text is matched by the regex given"""
98
if re.match(regex, text) is None:
99
self.fail('Pattern %s did not match text %s' % (regex, text))
101
def assertGenRevisionId(self, regex, username, timestamp=None):
102
"""gen_revision_id should create a revision id matching the regex"""
103
revision_id = generate_ids.gen_revision_id(username, timestamp)
104
self.assertMatchesRe(regex, revision_id)
106
def test_timestamp(self):
107
"""passing a timestamp should cause it to be used"""
108
self.assertGenRevisionId(r'user@host-\d{14}-[a-z0-9]{16}', 'user@host')
109
self.assertGenRevisionId('user@host-20061102205056-[a-z0-9]{16}',
110
'user@host', 1162500656.688)
111
self.assertGenRevisionId(r'user@host-20061102205024-[a-z0-9]{16}',
112
'user@host', 1162500624.000)
114
def test_gen_revision_id_email(self):
115
"""gen_revision_id uses email address if present"""
116
regex = r'user\+joe_bar@foo-bar\.com-\d{14}-[a-z0-9]{16}'
117
self.assertGenRevisionId(regex,'user+joe_bar@foo-bar.com')
118
self.assertGenRevisionId(regex, '<user+joe_bar@foo-bar.com>')
119
self.assertGenRevisionId(regex, 'Joe Bar <user+joe_bar@foo-bar.com>')
120
self.assertGenRevisionId(regex, 'Joe Bar <user+Joe_Bar@Foo-Bar.com>')
121
self.assertGenRevisionId(regex, u'Joe B\xe5r <user+Joe_Bar@Foo-Bar.com>')
123
def test_gen_revision_id_user(self):
124
"""If there is no email, fall back to the whole username"""
125
tail = r'-\d{14}-[a-z0-9]{16}'
126
self.assertGenRevisionId('joe_bar' + tail,'Joe Bar')
127
self.assertGenRevisionId('joebar' + tail, 'joebar')
128
self.assertGenRevisionId('joe_br' + tail, u'Joe B\xe5r')
129
self.assertGenRevisionId(r'joe_br_user\+joe_bar_foo-bar.com' + tail,
130
u'Joe B\xe5r <user+Joe_Bar_Foo-Bar.com>')