/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.15 by John Arbash Meinel
Merge bzr.dev 5597 to resolve NEWS, aka bzr-2.3.txt
1
# Copyright (C) 2006, 2007, 2009, 2011 Canonical Ltd
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
2
#
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.
7
#
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.
12
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
16
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
17
"""Tests for breezy/generate_ids.py"""
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
18
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
19
from .. import (
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
20
    generate_ids,
21
    tests,
22
    )
23
24
25
class TestFileIds(tests.TestCase):
26
    """Test functions which generate file ids"""
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
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
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
40
    def test_gen_file_id(self):
41
        gen_file_id = generate_ids.gen_file_id
42
43
        # We try to use the filename if possible
6973.12.3 by Jelmer Vernooij
Fixes.
44
        self.assertStartsWith(gen_file_id(b'bar'), b'bar-')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
45
46
        # but we squash capitalization, and remove non word characters
6973.12.3 by Jelmer Vernooij
Fixes.
47
        self.assertStartsWith(gen_file_id(b'Mwoo oof\t m'), b'mwoooofm-')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
48
49
        # We also remove leading '.' characters to prevent hidden file-ids
6973.12.3 by Jelmer Vernooij
Fixes.
50
        self.assertStartsWith(gen_file_id(b'..gam.py'), b'gam.py-')
51
        self.assertStartsWith(gen_file_id(b'..Mwoo oof\t m'), b'mwoooofm-')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
52
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
53
        # we remove unicode characters, and still don't end up with a
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
54
        # hidden file id
6973.12.3 by Jelmer Vernooij
Fixes.
55
        self.assertStartsWith(gen_file_id(u'\xe5\xb5.txt'), b'txt-')
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
56
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
57
        # Our current method of generating unique ids adds 33 characters
58
        # plus an serial number (log10(N) characters)
59
        # to the end of the filename. We now restrict the filename portion to
60
        # be <= 20 characters, so the maximum length should now be approx < 60
61
62
        # Test both case squashing and length restriction
63
        fid = gen_file_id('A'*50 + '.txt')
64
        self.assertStartsWith(fid, 'a'*20 + '-')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
65
        self.assertTrue(len(fid) < 60)
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
66
67
        # restricting length happens after the other actions, so
68
        # we preserve as much as possible
69
        fid = gen_file_id('\xe5\xb5..aBcd\tefGhijKLMnop\tqrstuvwxyz')
70
        self.assertStartsWith(fid, 'abcdefghijklmnopqrst-')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
71
        self.assertTrue(len(fid) < 60)
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
72
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
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')
79
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
80
    def test__next_id_suffix_sets_suffix(self):
81
        generate_ids._gen_file_id_suffix = None
82
        generate_ids._next_id_suffix()
83
        self.assertNotEqual(None, generate_ids._gen_file_id_suffix)
84
85
    def test__next_id_suffix_increments(self):
6973.12.1 by Jelmer Vernooij
Fix more tests.
86
        generate_ids._gen_file_id_suffix = b"foo-"
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
87
        generate_ids._gen_file_id_serial = 1
88
        try:
6973.12.1 by Jelmer Vernooij
Fix more tests.
89
            self.assertEqual(b"foo-2", generate_ids._next_id_suffix())
90
            self.assertEqual(b"foo-3", generate_ids._next_id_suffix())
91
            self.assertEqual(b"foo-4", generate_ids._next_id_suffix())
92
            self.assertEqual(b"foo-5", generate_ids._next_id_suffix())
93
            self.assertEqual(b"foo-6", generate_ids._next_id_suffix())
94
            self.assertEqual(b"foo-7", generate_ids._next_id_suffix())
95
            self.assertEqual(b"foo-8", generate_ids._next_id_suffix())
96
            self.assertEqual(b"foo-9", generate_ids._next_id_suffix())
97
            self.assertEqual(b"foo-10", generate_ids._next_id_suffix())
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
98
        finally:
99
            # Reset so that all future ids generated in the test suite
100
            # don't end in 'foo-XXX'
101
            generate_ids._gen_file_id_suffix = None
102
            generate_ids._gen_file_id_serial = 0
103
104
    def test_gen_root_id(self):
105
        # Mostly just make sure gen_root_id() exists
106
        root_id = generate_ids.gen_root_id()
6973.9.1 by Jelmer Vernooij
More test fixes.
107
        self.assertStartsWith(root_id, b'tree_root-')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
108
109
110
class TestGenRevisionId(tests.TestCase):
111
    """Test generating revision ids"""
112
113
    def assertGenRevisionId(self, regex, username, timestamp=None):
114
        """gen_revision_id should create a revision id matching the regex"""
115
        revision_id = generate_ids.gen_revision_id(username, timestamp)
6973.12.1 by Jelmer Vernooij
Fix more tests.
116
        self.assertContainsRe(revision_id, b'^'+regex+b'$')
2249.5.14 by John Arbash Meinel
Add some tests that generate_ids.get_revision_id() generates ascii revision ids
117
        # It should be a utf8 revision_id, not a unicode one
6973.12.1 by Jelmer Vernooij
Fix more tests.
118
        self.assertIsInstance(revision_id, bytes)
2249.5.14 by John Arbash Meinel
Add some tests that generate_ids.get_revision_id() generates ascii revision ids
119
        # gen_revision_id should always return ascii revision ids.
120
        revision_id.decode('ascii')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
121
122
    def test_timestamp(self):
123
        """passing a timestamp should cause it to be used"""
6973.12.1 by Jelmer Vernooij
Fix more tests.
124
        self.assertGenRevisionId(br'user@host-\d{14}-[a-z0-9]{16}', 'user@host')
125
        self.assertGenRevisionId(b'user@host-20061102205056-[a-z0-9]{16}',
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
126
                                 'user@host', 1162500656.688)
6973.12.1 by Jelmer Vernooij
Fix more tests.
127
        self.assertGenRevisionId(br'user@host-20061102205024-[a-z0-9]{16}',
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
128
                                 'user@host', 1162500624.000)
129
130
    def test_gen_revision_id_email(self):
131
        """gen_revision_id uses email address if present"""
6973.12.1 by Jelmer Vernooij
Fix more tests.
132
        regex = br'user\+joe_bar@foo-bar\.com-\d{14}-[a-z0-9]{16}'
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
133
        self.assertGenRevisionId(regex, 'user+joe_bar@foo-bar.com')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
134
        self.assertGenRevisionId(regex, '<user+joe_bar@foo-bar.com>')
135
        self.assertGenRevisionId(regex, 'Joe Bar <user+joe_bar@foo-bar.com>')
136
        self.assertGenRevisionId(regex, 'Joe Bar <user+Joe_Bar@Foo-Bar.com>')
137
        self.assertGenRevisionId(regex, u'Joe B\xe5r <user+Joe_Bar@Foo-Bar.com>')
138
139
    def test_gen_revision_id_user(self):
140
        """If there is no email, fall back to the whole username"""
6973.12.1 by Jelmer Vernooij
Fix more tests.
141
        tail = br'-\d{14}-[a-z0-9]{16}'
142
        self.assertGenRevisionId(b'joe_bar' + tail, 'Joe Bar')
143
        self.assertGenRevisionId(b'joebar' + tail, 'joebar')
144
        self.assertGenRevisionId(b'joe_br' + tail, u'Joe B\xe5r')
145
        self.assertGenRevisionId(br'joe_br_user\+joe_bar_foo-bar.com' + tail,
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
146
                                 u'Joe B\xe5r <user+Joe_Bar_Foo-Bar.com>')
2249.5.14 by John Arbash Meinel
Add some tests that generate_ids.get_revision_id() generates ascii revision ids
147
148
    def test_revision_ids_are_ascii(self):
149
        """gen_revision_id should always return an ascii revision id."""
6973.12.1 by Jelmer Vernooij
Fix more tests.
150
        tail = br'-\d{14}-[a-z0-9]{16}'
151
        self.assertGenRevisionId(b'joe_bar' + tail, 'Joe Bar')
152
        self.assertGenRevisionId(b'joe_bar' + tail, u'Joe Bar')
153
        self.assertGenRevisionId(b'joe@foo' + tail, u'Joe Bar <joe@foo>')
2249.5.14 by John Arbash Meinel
Add some tests that generate_ids.get_revision_id() generates ascii revision ids
154
        # We cheat a little with this one, because email-addresses shouldn't
155
        # contain non-ascii characters, but generate_ids should strip them
156
        # anyway.
6973.12.1 by Jelmer Vernooij
Fix more tests.
157
        self.assertGenRevisionId(b'joe@f' + tail, u'Joe Bar <joe@f\xb6>')