~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_version_info.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-09-21 22:59:25 UTC
  • mfrom: (2022.1.7 version-info-55794)
  • Revision ID: pqm@pqm.ubuntu.com-20060921225925-ab09f1d0cb2c838c
(jam) Add command 'bzr version-info'

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 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
"""Tests for version_info"""
 
18
 
 
19
from cStringIO import StringIO
 
20
import imp
 
21
import os
 
22
import sys
 
23
 
 
24
from bzrlib.tests import TestCaseWithTransport
 
25
from bzrlib.rio import read_stanzas
 
26
 
 
27
from bzrlib.version_info_formats.format_rio import RioVersionInfoBuilder
 
28
from bzrlib.version_info_formats.format_python import PythonVersionInfoBuilder
 
29
 
 
30
 
 
31
class TestVersionInfo(TestCaseWithTransport):
 
32
 
 
33
    def create_branch(self):
 
34
        wt = self.make_branch_and_tree('branch')
 
35
 
 
36
        self.build_tree(['branch/a'])
 
37
        wt.add('a')
 
38
        wt.commit('a', rev_id='r1')
 
39
 
 
40
        self.build_tree(['branch/b'])
 
41
        wt.add('b')
 
42
        wt.commit('b', rev_id='r2')
 
43
 
 
44
        self.build_tree_contents([('branch/a', 'new contents\n')])
 
45
        wt.commit('a2', rev_id='r3')
 
46
 
 
47
        return wt
 
48
 
 
49
    def test_rio_version_text(self):
 
50
        wt = self.create_branch()
 
51
 
 
52
        def regen(**kwargs):
 
53
            sio = StringIO()
 
54
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
 
55
                                            **kwargs)
 
56
            builder.generate(sio)
 
57
            val = sio.getvalue()
 
58
            return val
 
59
 
 
60
        val = regen()
 
61
        self.assertContainsRe(val, 'build-date:')
 
62
        self.assertContainsRe(val, 'date:')
 
63
        self.assertContainsRe(val, 'revno: 3')
 
64
        self.assertContainsRe(val, 'revision-id: r3')
 
65
 
 
66
        val = regen(check_for_clean=True)
 
67
        self.assertContainsRe(val, 'clean: True')
 
68
 
 
69
        self.build_tree(['branch/c'])
 
70
        val = regen(check_for_clean=True)
 
71
        self.assertContainsRe(val, 'clean: False')
 
72
        os.remove('branch/c')
 
73
 
 
74
        val = regen(include_revision_history=True)
 
75
        self.assertContainsRe(val, 'id: r1')
 
76
        self.assertContainsRe(val, 'message: a')
 
77
        self.assertContainsRe(val, 'id: r2')
 
78
        self.assertContainsRe(val, 'message: b')
 
79
        self.assertContainsRe(val, 'id: r3')
 
80
        self.assertContainsRe(val, 'message: a2')
 
81
 
 
82
    def test_rio_version(self):
 
83
        wt = self.create_branch()
 
84
 
 
85
        def regen(**kwargs):
 
86
            sio = StringIO()
 
87
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
 
88
                                            **kwargs)
 
89
            builder.generate(sio)
 
90
            sio.seek(0)
 
91
            stanzas = list(read_stanzas(sio))
 
92
            self.assertEqual(1, len(stanzas))
 
93
            return stanzas[0]
 
94
 
 
95
        def get_one_stanza(stanza, key):
 
96
            new_stanzas = list(read_stanzas(
 
97
                                StringIO(stanza[key].encode('utf8'))))
 
98
            self.assertEqual(1, len(new_stanzas))
 
99
            return new_stanzas[0]
 
100
 
 
101
        stanza = regen()
 
102
        self.failUnless('date' in stanza)
 
103
        self.failUnless('build-date' in stanza)
 
104
        self.assertEqual(['3'], stanza.get_all('revno'))
 
105
        self.assertEqual(['r3'], stanza.get_all('revision-id'))
 
106
 
 
107
        stanza = regen(check_for_clean=True)
 
108
        self.assertEqual(['True'], stanza.get_all('clean'))
 
109
 
 
110
        self.build_tree(['branch/c'])
 
111
        stanza = regen(check_for_clean=True, include_file_revisions=True)
 
112
        self.assertEqual(['False'], stanza.get_all('clean'))
 
113
 
 
114
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
 
115
        self.assertEqual(['a', 'b', 'c'], file_rev_stanza.get_all('path'))
 
116
        self.assertEqual(['r3', 'r2', 'unversioned'],
 
117
            file_rev_stanza.get_all('revision'))
 
118
        os.remove('branch/c')
 
119
 
 
120
        stanza = regen(include_revision_history=True)
 
121
        revision_stanza = get_one_stanza(stanza, 'revisions')
 
122
        self.assertEqual(['r1', 'r2', 'r3'], revision_stanza.get_all('id'))
 
123
        self.assertEqual(['a', 'b', 'a2'], revision_stanza.get_all('message'))
 
124
        self.assertEqual(3, len(revision_stanza.get_all('date')))
 
125
 
 
126
        # a was modified, so it should show up modified again
 
127
        self.build_tree(['branch/a', 'branch/c'])
 
128
        wt.add('c')
 
129
        wt.rename_one('b', 'd')
 
130
        stanza = regen(check_for_clean=True, include_file_revisions=True)
 
131
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
 
132
        self.assertEqual(['a', 'b', 'c', 'd'], file_rev_stanza.get_all('path'))
 
133
        self.assertEqual(['modified', 'renamed to d', 'new', 'renamed from b'],
 
134
                         file_rev_stanza.get_all('revision'))
 
135
 
 
136
        wt.commit('modified', rev_id='r4')
 
137
        wt.remove(['c', 'd'])
 
138
        os.remove('branch/d')
 
139
        stanza = regen(check_for_clean=True, include_file_revisions=True)
 
140
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
 
141
        self.assertEqual(['a', 'c', 'd'], file_rev_stanza.get_all('path'))
 
142
        self.assertEqual(['r4', 'unversioned', 'removed'],
 
143
                         file_rev_stanza.get_all('revision'))
 
144
 
 
145
    def test_python_version(self):
 
146
        wt = self.create_branch()
 
147
 
 
148
        def regen(**kwargs):
 
149
            """Create a test module, import and return it"""
 
150
            outf = open('test_version_information.py', 'wb')
 
151
            try:
 
152
                builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt,
 
153
                                                   **kwargs)
 
154
                builder.generate(outf)
 
155
            finally:
 
156
                outf.close()
 
157
            module_info = imp.find_module('test_version_information',
 
158
                                          [os.getcwdu()])
 
159
            tvi = imp.load_module('tvi', *module_info)
 
160
            # Make sure the module isn't cached
 
161
            sys.modules.pop('tvi', None)
 
162
            sys.modules.pop('test_version_information', None)
 
163
            # Delete the compiled versions, because we are generating
 
164
            # a new file fast enough that python doesn't detect it
 
165
            # needs to recompile, and using sleep() just makes the
 
166
            # test slow
 
167
            if os.path.exists('test_version_information.pyc'):
 
168
                os.remove('test_version_information.pyc')
 
169
            if os.path.exists('test_version_information.pyo'):
 
170
                os.remove('test_version_information.pyo')
 
171
            return tvi
 
172
 
 
173
        tvi = regen()
 
174
        self.assertEqual(3, tvi.version_info['revno'])
 
175
        self.assertEqual('r3', tvi.version_info['revision_id'])
 
176
        self.failUnless(tvi.version_info.has_key('date'))
 
177
        self.assertEqual(None, tvi.version_info['clean'])
 
178
 
 
179
        tvi = regen(check_for_clean=True)
 
180
        self.assertEqual(True, tvi.version_info['clean'])
 
181
 
 
182
        self.build_tree(['branch/c'])
 
183
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
184
        self.assertEqual(False, tvi.version_info['clean'])
 
185
        self.assertEqual(['a', 'b', 'c'], sorted(tvi.file_revisions.keys()))
 
186
        self.assertEqual('r3', tvi.file_revisions['a'])
 
187
        self.assertEqual('r2', tvi.file_revisions['b'])
 
188
        self.assertEqual('unversioned', tvi.file_revisions['c'])
 
189
        os.remove('branch/c')
 
190
 
 
191
        tvi = regen(include_revision_history=True)
 
192
 
 
193
        rev_info = [(rev, message) for rev, message, timestamp, timezone
 
194
                                   in tvi.revisions]
 
195
        self.assertEqual([('r1', 'a'), ('r2', 'b'), ('r3', 'a2')], rev_info)
 
196
 
 
197
        # a was modified, so it should show up modified again
 
198
        self.build_tree(['branch/a', 'branch/c'])
 
199
        wt.add('c')
 
200
        wt.rename_one('b', 'd')
 
201
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
202
        self.assertEqual(['a', 'b', 'c', 'd'], sorted(tvi.file_revisions.keys()))
 
203
        self.assertEqual('modified', tvi.file_revisions['a'])
 
204
        self.assertEqual('renamed to d', tvi.file_revisions['b'])
 
205
        self.assertEqual('new', tvi.file_revisions['c'])
 
206
        self.assertEqual('renamed from b', tvi.file_revisions['d'])
 
207
 
 
208
        wt.commit('modified', rev_id='r4')
 
209
        wt.remove(['c', 'd'])
 
210
        os.remove('branch/d')
 
211
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
212
        self.assertEqual(['a', 'c', 'd'], sorted(tvi.file_revisions.keys()))
 
213
        self.assertEqual('r4', tvi.file_revisions['a'])
 
214
        self.assertEqual('unversioned', tvi.file_revisions['c'])
 
215
        self.assertEqual('removed', tvi.file_revisions['d'])
 
216
 
 
217