/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_generate_ids.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2009, 2011 Canonical Ltd
 
1
# Copyright (C) 2006 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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Tests for breezy/generate_ids.py"""
18
 
 
19
 
from .. import (
 
17
"""Tests for bzrlib/generate_ids.py"""
 
18
 
 
19
import re
 
20
 
 
21
from bzrlib import (
 
22
    generate_ids,
20
23
    tests,
21
24
    )
22
 
from ..bzr import (
23
 
    generate_ids,
24
 
    )
25
25
 
26
26
 
27
27
class TestFileIds(tests.TestCase):
33
33
        The file id should be ascii, and should be an 8-bit string
34
34
        """
35
35
        file_id = generate_ids.gen_file_id(filename)
36
 
        self.assertContainsRe(file_id, b'^' + regex + b'$')
 
36
        self.assertContainsRe(file_id, '^'+regex+'$')
37
37
        # It should be a utf8 file_id, not a unicode one
38
 
        self.assertIsInstance(file_id, bytes)
 
38
        self.assertIsInstance(file_id, str)
39
39
        # gen_file_id should always return ascii file ids.
40
40
        file_id.decode('ascii')
41
41
 
43
43
        gen_file_id = generate_ids.gen_file_id
44
44
 
45
45
        # We try to use the filename if possible
46
 
        self.assertStartsWith(gen_file_id('bar'), b'bar-')
 
46
        self.assertStartsWith(gen_file_id('bar'), 'bar-')
47
47
 
48
48
        # but we squash capitalization, and remove non word characters
49
 
        self.assertStartsWith(gen_file_id('Mwoo oof\t m'), b'mwoooofm-')
 
49
        self.assertStartsWith(gen_file_id('Mwoo oof\t m'), 'mwoooofm-')
50
50
 
51
51
        # We also remove leading '.' characters to prevent hidden file-ids
52
 
        self.assertStartsWith(gen_file_id('..gam.py'), b'gam.py-')
53
 
        self.assertStartsWith(gen_file_id('..Mwoo oof\t m'), b'mwoooofm-')
 
52
        self.assertStartsWith(gen_file_id('..gam.py'), 'gam.py-')
 
53
        self.assertStartsWith(gen_file_id('..Mwoo oof\t m'), 'mwoooofm-')
54
54
 
55
55
        # we remove unicode characters, and still don't end up with a
56
56
        # hidden file id
57
 
        self.assertStartsWith(gen_file_id(u'\xe5\xb5.txt'), b'txt-')
 
57
        self.assertStartsWith(gen_file_id(u'\xe5\xb5.txt'), 'txt-')
58
58
 
59
59
        # Our current method of generating unique ids adds 33 characters
60
60
        # plus an serial number (log10(N) characters)
62
62
        # be <= 20 characters, so the maximum length should now be approx < 60
63
63
 
64
64
        # Test both case squashing and length restriction
65
 
        fid = gen_file_id('A' * 50 + '.txt')
66
 
        self.assertStartsWith(fid, b'a' * 20 + b'-')
67
 
        self.assertTrue(len(fid) < 60)
 
65
        fid = gen_file_id('A'*50 + '.txt')
 
66
        self.assertStartsWith(fid, 'a'*20 + '-')
 
67
        self.failUnless(len(fid) < 60)
68
68
 
69
69
        # restricting length happens after the other actions, so
70
70
        # we preserve as much as possible
71
71
        fid = gen_file_id('\xe5\xb5..aBcd\tefGhijKLMnop\tqrstuvwxyz')
72
 
        self.assertStartsWith(fid, b'abcdefghijklmnopqrst-')
73
 
        self.assertTrue(len(fid) < 60)
 
72
        self.assertStartsWith(fid, 'abcdefghijklmnopqrst-')
 
73
        self.failUnless(len(fid) < 60)
74
74
 
75
75
    def test_file_ids_are_ascii(self):
76
 
        tail = br'-\d{14}-[a-z0-9]{16}-\d+'
77
 
        self.assertGenFileId(b'foo' + tail, 'foo')
78
 
        self.assertGenFileId(b'foo' + tail, u'foo')
79
 
        self.assertGenFileId(b'bar' + tail, u'bar')
80
 
        self.assertGenFileId(b'br' + tail, u'b\xe5r')
 
76
        tail = r'-\d{14}-[a-z0-9]{16}-\d+'
 
77
        self.assertGenFileId('foo' + tail, 'foo')
 
78
        self.assertGenFileId('foo' + tail, u'foo')
 
79
        self.assertGenFileId('bar' + tail, u'bar')
 
80
        self.assertGenFileId('br' + tail, u'b\xe5r')
81
81
 
82
82
    def test__next_id_suffix_sets_suffix(self):
83
83
        generate_ids._gen_file_id_suffix = None
85
85
        self.assertNotEqual(None, generate_ids._gen_file_id_suffix)
86
86
 
87
87
    def test__next_id_suffix_increments(self):
88
 
        generate_ids._gen_file_id_suffix = b"foo-"
 
88
        generate_ids._gen_file_id_suffix = "foo-"
89
89
        generate_ids._gen_file_id_serial = 1
90
90
        try:
91
 
            self.assertEqual(b"foo-2", generate_ids._next_id_suffix())
92
 
            self.assertEqual(b"foo-3", generate_ids._next_id_suffix())
93
 
            self.assertEqual(b"foo-4", generate_ids._next_id_suffix())
94
 
            self.assertEqual(b"foo-5", generate_ids._next_id_suffix())
95
 
            self.assertEqual(b"foo-6", generate_ids._next_id_suffix())
96
 
            self.assertEqual(b"foo-7", generate_ids._next_id_suffix())
97
 
            self.assertEqual(b"foo-8", generate_ids._next_id_suffix())
98
 
            self.assertEqual(b"foo-9", generate_ids._next_id_suffix())
99
 
            self.assertEqual(b"foo-10", generate_ids._next_id_suffix())
 
91
            self.assertEqual("foo-2", generate_ids._next_id_suffix())
 
92
            self.assertEqual("foo-3", generate_ids._next_id_suffix())
 
93
            self.assertEqual("foo-4", generate_ids._next_id_suffix())
 
94
            self.assertEqual("foo-5", generate_ids._next_id_suffix())
 
95
            self.assertEqual("foo-6", generate_ids._next_id_suffix())
 
96
            self.assertEqual("foo-7", generate_ids._next_id_suffix())
 
97
            self.assertEqual("foo-8", generate_ids._next_id_suffix())
 
98
            self.assertEqual("foo-9", generate_ids._next_id_suffix())
 
99
            self.assertEqual("foo-10", generate_ids._next_id_suffix())
100
100
        finally:
101
101
            # Reset so that all future ids generated in the test suite
102
102
            # don't end in 'foo-XXX'
106
106
    def test_gen_root_id(self):
107
107
        # Mostly just make sure gen_root_id() exists
108
108
        root_id = generate_ids.gen_root_id()
109
 
        self.assertStartsWith(root_id, b'tree_root-')
 
109
        self.assertStartsWith(root_id, 'tree_root-')
110
110
 
111
111
 
112
112
class TestGenRevisionId(tests.TestCase):
115
115
    def assertGenRevisionId(self, regex, username, timestamp=None):
116
116
        """gen_revision_id should create a revision id matching the regex"""
117
117
        revision_id = generate_ids.gen_revision_id(username, timestamp)
118
 
        self.assertContainsRe(revision_id, b'^' + regex + b'$')
 
118
        self.assertContainsRe(revision_id, '^'+regex+'$')
119
119
        # It should be a utf8 revision_id, not a unicode one
120
 
        self.assertIsInstance(revision_id, bytes)
 
120
        self.assertIsInstance(revision_id, str)
121
121
        # gen_revision_id should always return ascii revision ids.
122
122
        revision_id.decode('ascii')
123
123
 
124
124
    def test_timestamp(self):
125
125
        """passing a timestamp should cause it to be used"""
126
 
        self.assertGenRevisionId(
127
 
            br'user@host-\d{14}-[a-z0-9]{16}', 'user@host')
128
 
        self.assertGenRevisionId(b'user@host-20061102205056-[a-z0-9]{16}',
 
126
        self.assertGenRevisionId(r'user@host-\d{14}-[a-z0-9]{16}', 'user@host')
 
127
        self.assertGenRevisionId('user@host-20061102205056-[a-z0-9]{16}',
129
128
                                 'user@host', 1162500656.688)
130
 
        self.assertGenRevisionId(br'user@host-20061102205024-[a-z0-9]{16}',
 
129
        self.assertGenRevisionId(r'user@host-20061102205024-[a-z0-9]{16}',
131
130
                                 'user@host', 1162500624.000)
132
131
 
133
132
    def test_gen_revision_id_email(self):
134
133
        """gen_revision_id uses email address if present"""
135
 
        regex = br'user\+joe_bar@foo-bar\.com-\d{14}-[a-z0-9]{16}'
136
 
        self.assertGenRevisionId(regex, 'user+joe_bar@foo-bar.com')
 
134
        regex = r'user\+joe_bar@foo-bar\.com-\d{14}-[a-z0-9]{16}'
 
135
        self.assertGenRevisionId(regex,'user+joe_bar@foo-bar.com')
137
136
        self.assertGenRevisionId(regex, '<user+joe_bar@foo-bar.com>')
138
137
        self.assertGenRevisionId(regex, 'Joe Bar <user+joe_bar@foo-bar.com>')
139
138
        self.assertGenRevisionId(regex, 'Joe Bar <user+Joe_Bar@Foo-Bar.com>')
140
 
        self.assertGenRevisionId(
141
 
            regex, u'Joe B\xe5r <user+Joe_Bar@Foo-Bar.com>')
 
139
        self.assertGenRevisionId(regex, u'Joe B\xe5r <user+Joe_Bar@Foo-Bar.com>')
142
140
 
143
141
    def test_gen_revision_id_user(self):
144
142
        """If there is no email, fall back to the whole username"""
145
 
        tail = br'-\d{14}-[a-z0-9]{16}'
146
 
        self.assertGenRevisionId(b'joe_bar' + tail, 'Joe Bar')
147
 
        self.assertGenRevisionId(b'joebar' + tail, 'joebar')
148
 
        self.assertGenRevisionId(b'joe_br' + tail, u'Joe B\xe5r')
149
 
        self.assertGenRevisionId(br'joe_br_user\+joe_bar_foo-bar.com' + tail,
 
143
        tail = r'-\d{14}-[a-z0-9]{16}'
 
144
        self.assertGenRevisionId('joe_bar' + tail, 'Joe Bar')
 
145
        self.assertGenRevisionId('joebar' + tail, 'joebar')
 
146
        self.assertGenRevisionId('joe_br' + tail, u'Joe B\xe5r')
 
147
        self.assertGenRevisionId(r'joe_br_user\+joe_bar_foo-bar.com' + tail,
150
148
                                 u'Joe B\xe5r <user+Joe_Bar_Foo-Bar.com>')
151
149
 
152
150
    def test_revision_ids_are_ascii(self):
153
151
        """gen_revision_id should always return an ascii revision id."""
154
 
        tail = br'-\d{14}-[a-z0-9]{16}'
155
 
        self.assertGenRevisionId(b'joe_bar' + tail, 'Joe Bar')
156
 
        self.assertGenRevisionId(b'joe_bar' + tail, u'Joe Bar')
157
 
        self.assertGenRevisionId(b'joe@foo' + tail, u'Joe Bar <joe@foo>')
 
152
        tail = r'-\d{14}-[a-z0-9]{16}'
 
153
        self.assertGenRevisionId('joe_bar' + tail, 'Joe Bar')
 
154
        self.assertGenRevisionId('joe_bar' + tail, u'Joe Bar')
 
155
        self.assertGenRevisionId('joe@foo' + tail, u'Joe Bar <joe@foo>')
158
156
        # We cheat a little with this one, because email-addresses shouldn't
159
157
        # contain non-ascii characters, but generate_ids should strip them
160
158
        # anyway.
161
 
        self.assertGenRevisionId(b'joe@f' + tail, u'Joe Bar <joe@f\xb6>')
 
159
        self.assertGenRevisionId('joe@f' + tail, u'Joe Bar <joe@f\xb6>')