/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 (
7265.5.1 by Jelmer Vernooij
Move generate_ids to breezy.bzr.
20
    tests,
21
    )
22
from ..bzr import (
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
23
    generate_ids,
24
    )
25
26
27
class TestFileIds(tests.TestCase):
28
    """Test functions which generate file ids"""
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
29
30
    def assertGenFileId(self, regex, filename):
31
        """gen_file_id should create a file id matching the regex.
32
33
        The file id should be ascii, and should be an 8-bit string
34
        """
35
        file_id = generate_ids.gen_file_id(filename)
7143.15.2 by Jelmer Vernooij
Run autopep8.
36
        self.assertContainsRe(file_id, b'^' + regex + b'$')
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
37
        # It should be a utf8 file_id, not a unicode one
7058.4.19 by Jelmer Vernooij
Fix generate_ids tests.
38
        self.assertIsInstance(file_id, bytes)
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
39
        # gen_file_id should always return ascii file ids.
40
        file_id.decode('ascii')
41
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
42
    def test_gen_file_id(self):
43
        gen_file_id = generate_ids.gen_file_id
44
45
        # We try to use the filename if possible
7058.4.19 by Jelmer Vernooij
Fix generate_ids tests.
46
        self.assertStartsWith(gen_file_id('bar'), b'bar-')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
47
48
        # but we squash capitalization, and remove non word characters
7058.4.19 by Jelmer Vernooij
Fix generate_ids tests.
49
        self.assertStartsWith(gen_file_id('Mwoo oof\t m'), b'mwoooofm-')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
50
51
        # We also remove leading '.' characters to prevent hidden file-ids
7058.4.19 by Jelmer Vernooij
Fix generate_ids tests.
52
        self.assertStartsWith(gen_file_id('..gam.py'), b'gam.py-')
53
        self.assertStartsWith(gen_file_id('..Mwoo oof\t m'), b'mwoooofm-')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
54
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
55
        # 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.
56
        # hidden file id
6973.12.3 by Jelmer Vernooij
Fixes.
57
        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
58
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
59
        # Our current method of generating unique ids adds 33 characters
60
        # plus an serial number (log10(N) characters)
61
        # to the end of the filename. We now restrict the filename portion to
62
        # be <= 20 characters, so the maximum length should now be approx < 60
63
64
        # Test both case squashing and length restriction
7143.15.2 by Jelmer Vernooij
Run autopep8.
65
        fid = gen_file_id('A' * 50 + '.txt')
66
        self.assertStartsWith(fid, b'a' * 20 + b'-')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
67
        self.assertTrue(len(fid) < 60)
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
68
69
        # restricting length happens after the other actions, so
70
        # we preserve as much as possible
71
        fid = gen_file_id('\xe5\xb5..aBcd\tefGhijKLMnop\tqrstuvwxyz')
7058.4.19 by Jelmer Vernooij
Fix generate_ids tests.
72
        self.assertStartsWith(fid, b'abcdefghijklmnopqrst-')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
73
        self.assertTrue(len(fid) < 60)
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
74
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
75
    def test_file_ids_are_ascii(self):
7045.5.4 by Jelmer Vernooij
Fix a few more tests.
76
        tail = br'-\d{14}-[a-z0-9]{16}-\d+'
7058.4.19 by Jelmer Vernooij
Fix generate_ids tests.
77
        self.assertGenFileId(b'foo' + tail, 'foo')
7045.5.4 by Jelmer Vernooij
Fix a few more tests.
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')
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
81
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
82
    def test__next_id_suffix_sets_suffix(self):
83
        generate_ids._gen_file_id_suffix = None
84
        generate_ids._next_id_suffix()
85
        self.assertNotEqual(None, generate_ids._gen_file_id_suffix)
86
87
    def test__next_id_suffix_increments(self):
6973.12.1 by Jelmer Vernooij
Fix more tests.
88
        generate_ids._gen_file_id_suffix = b"foo-"
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
89
        generate_ids._gen_file_id_serial = 1
90
        try:
6973.12.1 by Jelmer Vernooij
Fix more tests.
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())
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
100
        finally:
101
            # Reset so that all future ids generated in the test suite
102
            # don't end in 'foo-XXX'
103
            generate_ids._gen_file_id_suffix = None
104
            generate_ids._gen_file_id_serial = 0
105
106
    def test_gen_root_id(self):
107
        # Mostly just make sure gen_root_id() exists
108
        root_id = generate_ids.gen_root_id()
6973.9.1 by Jelmer Vernooij
More test fixes.
109
        self.assertStartsWith(root_id, b'tree_root-')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
110
111
112
class TestGenRevisionId(tests.TestCase):
113
    """Test generating revision ids"""
114
115
    def assertGenRevisionId(self, regex, username, timestamp=None):
116
        """gen_revision_id should create a revision id matching the regex"""
117
        revision_id = generate_ids.gen_revision_id(username, timestamp)
7143.15.2 by Jelmer Vernooij
Run autopep8.
118
        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
119
        # It should be a utf8 revision_id, not a unicode one
6973.12.1 by Jelmer Vernooij
Fix more tests.
120
        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
121
        # gen_revision_id should always return ascii revision ids.
122
        revision_id.decode('ascii')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
123
124
    def test_timestamp(self):
125
        """passing a timestamp should cause it to be used"""
7143.15.2 by Jelmer Vernooij
Run autopep8.
126
        self.assertGenRevisionId(
127
            br'user@host-\d{14}-[a-z0-9]{16}', 'user@host')
6973.12.1 by Jelmer Vernooij
Fix more tests.
128
        self.assertGenRevisionId(b'user@host-20061102205056-[a-z0-9]{16}',
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
129
                                 'user@host', 1162500656.688)
6973.12.1 by Jelmer Vernooij
Fix more tests.
130
        self.assertGenRevisionId(br'user@host-20061102205024-[a-z0-9]{16}',
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
131
                                 'user@host', 1162500624.000)
132
133
    def test_gen_revision_id_email(self):
134
        """gen_revision_id uses email address if present"""
6973.12.1 by Jelmer Vernooij
Fix more tests.
135
        regex = br'user\+joe_bar@foo-bar\.com-\d{14}-[a-z0-9]{16}'
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
136
        self.assertGenRevisionId(regex, 'user+joe_bar@foo-bar.com')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
137
        self.assertGenRevisionId(regex, '<user+joe_bar@foo-bar.com>')
138
        self.assertGenRevisionId(regex, 'Joe Bar <user+joe_bar@foo-bar.com>')
139
        self.assertGenRevisionId(regex, 'Joe Bar <user+Joe_Bar@Foo-Bar.com>')
7143.15.2 by Jelmer Vernooij
Run autopep8.
140
        self.assertGenRevisionId(
141
            regex, u'Joe B\xe5r <user+Joe_Bar@Foo-Bar.com>')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
142
143
    def test_gen_revision_id_user(self):
144
        """If there is no email, fall back to the whole username"""
6973.12.1 by Jelmer Vernooij
Fix more tests.
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,
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
150
                                 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
151
152
    def test_revision_ids_are_ascii(self):
153
        """gen_revision_id should always return an ascii revision id."""
6973.12.1 by Jelmer Vernooij
Fix more tests.
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>')
2249.5.14 by John Arbash Meinel
Add some tests that generate_ids.get_revision_id() generates ascii revision ids
158
        # We cheat a little with this one, because email-addresses shouldn't
159
        # contain non-ascii characters, but generate_ids should strip them
160
        # anyway.
6973.12.1 by Jelmer Vernooij
Fix more tests.
161
        self.assertGenRevisionId(b'joe@f' + tail, u'Joe Bar <joe@f\xb6>')