~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_generate_ids.py

  • Committer: Vincent Ladeuil
  • Date: 2013-10-04 09:56:23 UTC
  • mto: (6588.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6589.
  • Revision ID: v.ladeuil+lp@free.fr-20131004095623-xlan34vg0y51gdb5
Stricter checks on configuration option names

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2009, 2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for bzrlib/generate_ids.py"""
18
18
 
19
 
import re
20
 
 
21
19
from bzrlib import (
22
20
    generate_ids,
23
21
    tests,
26
24
 
27
25
class TestFileIds(tests.TestCase):
28
26
    """Test functions which generate file ids"""
29
 
    
 
27
 
 
28
    def assertGenFileId(self, regex, filename):
 
29
        """gen_file_id should create a file id matching the regex.
 
30
 
 
31
        The file id should be ascii, and should be an 8-bit string
 
32
        """
 
33
        file_id = generate_ids.gen_file_id(filename)
 
34
        self.assertContainsRe(file_id, '^'+regex+'$')
 
35
        # It should be a utf8 file_id, not a unicode one
 
36
        self.assertIsInstance(file_id, str)
 
37
        # gen_file_id should always return ascii file ids.
 
38
        file_id.decode('ascii')
 
39
 
30
40
    def test_gen_file_id(self):
31
41
        gen_file_id = generate_ids.gen_file_id
32
42
 
40
50
        self.assertStartsWith(gen_file_id('..gam.py'), 'gam.py-')
41
51
        self.assertStartsWith(gen_file_id('..Mwoo oof\t m'), 'mwoooofm-')
42
52
 
43
 
        # we remove unicode characters, and still don't end up with a 
 
53
        # we remove unicode characters, and still don't end up with a
44
54
        # hidden file id
45
55
        self.assertStartsWith(gen_file_id(u'\xe5\xb5.txt'), 'txt-')
46
 
        
 
56
 
47
57
        # Our current method of generating unique ids adds 33 characters
48
58
        # plus an serial number (log10(N) characters)
49
59
        # to the end of the filename. We now restrict the filename portion to
52
62
        # Test both case squashing and length restriction
53
63
        fid = gen_file_id('A'*50 + '.txt')
54
64
        self.assertStartsWith(fid, 'a'*20 + '-')
55
 
        self.failUnless(len(fid) < 60)
 
65
        self.assertTrue(len(fid) < 60)
56
66
 
57
67
        # restricting length happens after the other actions, so
58
68
        # we preserve as much as possible
59
69
        fid = gen_file_id('\xe5\xb5..aBcd\tefGhijKLMnop\tqrstuvwxyz')
60
70
        self.assertStartsWith(fid, 'abcdefghijklmnopqrst-')
61
 
        self.failUnless(len(fid) < 60)
 
71
        self.assertTrue(len(fid) < 60)
 
72
 
 
73
    def test_file_ids_are_ascii(self):
 
74
        tail = r'-\d{14}-[a-z0-9]{16}-\d+'
 
75
        self.assertGenFileId('foo' + tail, 'foo')
 
76
        self.assertGenFileId('foo' + tail, u'foo')
 
77
        self.assertGenFileId('bar' + tail, u'bar')
 
78
        self.assertGenFileId('br' + tail, u'b\xe5r')
62
79
 
63
80
    def test__next_id_suffix_sets_suffix(self):
64
81
        generate_ids._gen_file_id_suffix = None
93
110
class TestGenRevisionId(tests.TestCase):
94
111
    """Test generating revision ids"""
95
112
 
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))
100
 
 
101
113
    def assertGenRevisionId(self, regex, username, timestamp=None):
102
114
        """gen_revision_id should create a revision id matching the regex"""
103
115
        revision_id = generate_ids.gen_revision_id(username, timestamp)
104
 
        self.assertMatchesRe(regex, revision_id)
 
116
        self.assertContainsRe(revision_id, '^'+regex+'$')
105
117
        # It should be a utf8 revision_id, not a unicode one
106
118
        self.assertIsInstance(revision_id, str)
107
119
        # gen_revision_id should always return ascii revision ids.