/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
1
# Copyright (C) 2005 Canonical Ltd
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""\
18
Tests for version_info
19
"""
20
21
import os
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
22
import sys
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
23
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
24
from StringIO import StringIO
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
25
from bzrlib.tests import TestCase, TestCaseInTempDir
26
from bzrlib.branch import Branch
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
27
from bzrlib.rio import read_stanzas
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
28
29
# TODO: jam 20051228 When part of bzrlib, this should become
30
#       from bzrlib.generate_version_info import foo
31
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
32
from generate_version_info import (is_clean,
33
        generate_rio_version, generate_python_version)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
34
from errors import UncleanError
35
36
37
class TestIsClean(TestCaseInTempDir):
38
39
    # TODO: jam 20051228 Test that a branch without a working tree
40
    #       is clean. This would be something like an SFTP test
41
42
    def test_is_clean(self):
43
        b = Branch.initialize('.')
44
        wt = b.working_tree()
45
46
        def not_clean(b):
0.8.3 by John Arbash Meinel
Playing around with some formats
47
            clean, message = is_clean(b)
48
            if clean:
49
                self.fail('Tree should not be clean')
50
51
        def check_clean(b):
52
            clean, message = is_clean(b)
53
            if not clean:
54
                self.fail(message)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
55
56
        # Nothing happened yet
0.8.3 by John Arbash Meinel
Playing around with some formats
57
        check_clean(b)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
58
59
        # Unknown file
60
        open('a', 'wb').write('a file\n')
61
        not_clean(b)
62
        
63
        # Newly added file
64
        wt.add('a')
65
        not_clean(b)
66
67
        # We committed, things are clean
68
        wt.commit('added a')
0.8.3 by John Arbash Meinel
Playing around with some formats
69
        check_clean(b)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
70
71
        # Unknown
72
        open('b', 'wb').write('b file\n')
73
        not_clean(b)
74
75
        wt.add('b')
76
        not_clean(b)
77
78
        wt.commit('added b')
0.8.3 by John Arbash Meinel
Playing around with some formats
79
        check_clean(b)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
80
81
        open('a', 'wb').write('different\n')
82
        not_clean(b)
83
84
        wt.commit('mod a')
0.8.3 by John Arbash Meinel
Playing around with some formats
85
        check_clean(b)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
86
87
        os.remove('a')
88
        not_clean(b)
89
90
        wt.commit('del a')
0.8.3 by John Arbash Meinel
Playing around with some formats
91
        check_clean(b)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
92
93
        wt.rename_one('b', 'a')
94
        not_clean(b)
95
96
        wt.commit('rename b => a')
0.8.3 by John Arbash Meinel
Playing around with some formats
97
        check_clean(b)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
98
99
100
class TestVersionInfo(TestCaseInTempDir):
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
101
102
    def create_branch(self):
103
        os.mkdir('branch')
104
        b = Branch.initialize(u'branch')
105
        wt = b.working_tree()
106
        
107
        open('branch/a', 'wb').write('a file\n')
108
        wt.add('a')
109
        wt.commit('a', rev_id='r1')
110
111
        open('branch/b', 'wb').write('b file\n')
112
        wt.add('b')
113
        wt.commit('b', rev_id='r2')
114
115
        open('branch/a', 'wb').write('new contents\n')
116
        wt.commit('a2', rev_id='r3')
117
118
        return b, wt
119
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
120
    def test_rio_version_text(self):
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
121
        b, wt = self.create_branch()
122
123
        def regen(**kwargs):
124
            sio = StringIO()
125
            generate_rio_version(b, to_file=sio, **kwargs)
126
            val = sio.getvalue()
127
            return val
128
129
        val = regen()
130
        self.assertContainsRe(val, 'date:')
131
        self.assertContainsRe(val, 'revno: 3')
132
        self.assertContainsRe(val, 'revision_id: r3')
133
134
        val = regen(check_for_clean=True)
135
        self.assertContainsRe(val, 'clean: True')
136
137
        open('branch/c', 'wb').write('now unclean\n')
138
        val = regen(check_for_clean=True)
139
        self.assertContainsRe(val, 'clean: False')
140
        os.remove('branch/c')
141
142
        val = regen(include_revision_history=True)
0.8.6 by John Arbash Meinel
Updated the blackbox tests.
143
        self.assertContainsRe(val, 'id: r1')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
144
        self.assertContainsRe(val, 'message: a')
0.8.6 by John Arbash Meinel
Updated the blackbox tests.
145
        self.assertContainsRe(val, 'id: r2')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
146
        self.assertContainsRe(val, 'message: b')
0.8.6 by John Arbash Meinel
Updated the blackbox tests.
147
        self.assertContainsRe(val, 'id: r3')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
148
        self.assertContainsRe(val, 'message: a2')
149
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
150
    def test_rio_version(self):
151
        b, wt = self.create_branch()
152
153
        def regen(**kwargs):
154
            sio = StringIO()
155
            generate_rio_version(b, to_file=sio, **kwargs)
156
            sio.seek(0)
157
            stanzas = list(read_stanzas(sio))
158
            self.assertEqual(1, len(stanzas))
159
            return stanzas[0]
160
161
        stanza = regen()
162
        self.failUnless('date' in stanza)
163
        self.assertEqual(['3'], stanza.get_all('revno'))
164
        self.assertEqual(['r3'], stanza.get_all('revision_id'))
165
166
        stanza = regen(check_for_clean=True)
167
        self.assertEqual(['True'], stanza.get_all('clean'))
168
169
        open('branch/c', 'wb').write('now unclean\n')
170
        stanza = regen(check_for_clean=True)
171
        self.assertEqual(['False'], stanza.get_all('clean'))
172
        os.remove('branch/c')
173
174
        stanza = regen(include_revision_history=True)
175
        txt = stanza['revisions']
176
        revision_stanza = list(read_stanzas(StringIO(txt)))
177
        self.assertEqual(1, len(revision_stanza))
178
        revision_stanza = revision_stanza[0]
179
        self.assertEqual(['r1', 'r2', 'r3'], revision_stanza.get_all('id'))
180
        self.assertEqual(['a', 'b', 'a2'], revision_stanza.get_all('message'))
181
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
182
    def test_python_version(self):
183
        b, wt = self.create_branch()
184
185
        def regen(**kwargs):
186
            outf = open('test_version_information.py', 'wb')
187
            generate_python_version(b, to_file=outf, **kwargs)
188
            outf.close()
189
            try:
190
                sys.path.append(os.getcwdu())
191
                import test_version_information as tvi
192
                reload(tvi)
193
            finally:
194
                sys.path.pop()
195
            # Make sure the module isn't cached
196
            sys.modules.pop('tvi', None)
197
            sys.modules.pop('test_version_information', None)
198
            # Delete the compiled versions, because we are generating
199
            # a new file fast enough that python doesn't detect it
200
            # needs to recompile, and using sleep() just makes the
201
            # test slow
202
            if os.path.exists('test_version_information.pyc'):
203
                os.remove('test_version_information.pyc')
204
            if os.path.exists('test_version_information.pyo'):
205
                os.remove('test_version_information.pyo')
206
            return tvi
207
208
        tvi = regen()
209
        self.assertEquals(3, tvi.version_info['revno'])
210
        self.assertEquals('r3', tvi.version_info['revision_id'])
211
        self.failUnless(tvi.version_info.has_key('date'))
212
        self.assertEquals(None, tvi.version_info['clean'])
213
214
        tvi = regen(check_for_clean=True)
215
        self.assertEquals(True, tvi.version_info['clean'])
216
217
        open('branch/c', 'wb').write('now unclean\n')
218
        tvi = regen(check_for_clean=True)
219
        self.assertEquals(False, tvi.version_info['clean'])
220
        os.remove('branch/c')
221
222
        tvi = regen(include_revision_history=True)
223
        self.assertEqual([('r1', 'a'), ('r2', 'b'), ('r3', 'a2')],
224
                         tvi.version_info['revisions'])
225
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
226