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