~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_version_info.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-20 14:51:03 UTC
  • mfrom: (0.8.23 version_info)
  • mto: This revision was merged to the branch mainline in revision 2028.
  • Revision ID: john@arbash-meinel.com-20060920145103-02725c6d6c886040
[merge] version-info plugin, and cleanup for layout in bzr

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