~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/test_baz_import.py

  • Committer: Robert Collins
  • Date: 2005-09-28 05:43:19 UTC
  • mto: (147.2.6) (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: robertc@robertcollins.net-20050928054319-2c2e9e3048bbc215
find_branch -> open_containing change

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#    along with this program; if not, write to the Free Software
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
 
from bzrlib.tests import TestCaseInTempDir, TestCase
19
 
from bzrlib import repository
20
 
from bzrlib.osutils import has_symlinks
 
18
from bzrlib.selftest import TestCaseInTempDir, TestCase
 
19
from bzrlib.selftest.blackbox import has_symlinks
21
20
try:
22
21
    import pybaz
23
22
except ImportError:
24
23
    pybaz = None
25
24
import os
26
 
try:
27
 
    from bzrtools import cmd_baz_import
28
 
    from bzrtools.baz_import import (import_version, revision_id,
29
 
                                      make_archive, map_file_id)
30
 
except ImportError:
31
 
    from bzrlib.plugins.bzrtools import cmd_baz_import 
32
 
    from bzrlib.plugins.bzrtools.baz_import import (import_version, 
33
 
                                                    revision_id,
34
 
                                                    make_archive,
35
 
                                                    map_file_id)
 
25
from baz_import import import_version, revision_id, cmd_baz_import
 
26
from bzrlib.branch import find_branch
 
27
from bzrlib.errors import NoSuchRevision
36
28
import shutil
37
29
from StringIO import StringIO
38
30
import tempfile
39
31
from testresources import (TestResource, TestLoader, OptimisingTestSuite,
40
 
                               ResourcedTestCase)
41
 
    
42
 
import bzrlib
43
 
from bzrlib.errors import NoSuchRevision
44
 
try:
45
 
    from bzrtools.fai import namespace_previous
46
 
except ImportError:
47
 
    from bzrlib.plugins.bzrtools.fai import namespace_previous
48
 
from bzrlib.branch import Branch
 
32
                           ResourcedTestCase)
 
33
from fai import namespace_previous
49
34
 
50
35
def test_suite():
51
36
    if pybaz is None:
52
 
        from unittest import TestSuite
53
 
        return TestSuite()
 
37
        return OptimisingTestSuite()
54
38
    return TestLoader().loadTestsFromName(__name__)
55
39
 
56
40
 
68
52
        os.environ['HOME'] = self._homedir
69
53
 
70
54
        self._archiveroot = os.path.join(self._tmpdir, 'archive')
71
 
        self._archive = make_archive('demo@DONOTUSE', str(self._archiveroot))
 
55
        self._archive = pybaz.make_archive('demo@DONOTUSE', str(self._archiveroot))
72
56
        pybaz.set_my_id("Test User<test@example.org>")
73
57
 
74
58
        self.make_empty_import()
75
 
        self.make_utf8_log()
76
59
 
77
60
        self._empty_tag = 'demo@DONOTUSE/c--empty-tag--0'
78
 
        self._empty_tag_bzr = revision_id(self._empty_tag + '--base-0', None)
 
61
        self._empty_tag_bzr = revision_id(self._empty_tag + '--base-0')
79
62
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
80
63
            pybaz.Version(self._empty_tag))
81
64
 
82
65
        self._empty_merged_tag = 'demo@DONOTUSE/c--empty-merged-tag--0'
83
66
        self._empty_merged_tag_bzr_base = revision_id(self._empty_merged_tag 
84
 
                                                 + '--base-0', None)
 
67
                                                 + '--base-0')
85
68
        self._empty_merged_tag_bzr = revision_id(self._empty_merged_tag 
86
 
                                                 + '--patch-1', None)
 
69
                                                 + '--patch-1')
87
70
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
88
71
            pybaz.Version(self._empty_merged_tag))
89
72
        tree = pybaz.Revision(self._empty_merged_tag + '--base-0').get(
97
80
        
98
81
        # tree, two commits, includes merge of other branch
99
82
        self._empty_merged_tag_2 = 'demo@DONOTUSE/c--empty-tag-2--0'
100
 
        self._empty_merged_tag_2_bzr_base = revision_id(
101
 
            self._empty_merged_tag_2 + '--base-0', None)
102
 
        self._empty_merged_tag_2_bzr = revision_id(
103
 
            self._empty_merged_tag_2 + '--patch-1', None)
 
83
        self._empty_merged_tag_2_bzr_base = revision_id(self._empty_merged_tag_2 + '--base-0')
 
84
        self._empty_merged_tag_2_bzr = revision_id(self._empty_merged_tag_2 + '--patch-1')
104
85
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
105
86
            pybaz.Version(self._empty_merged_tag_2))
106
87
        tree = pybaz.Revision(self._empty_merged_tag_2 + '--base-0').get (
112
93
        tree.commit(msg)
113
94
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
114
95
 
115
 
        self._bad_id_tag = 'demo@DONOTUSE/c--bad-id--0'
116
 
        self._bad_id_tag_bzr_base = revision_id(self._bad_id_tag + '--base-0',
117
 
                                                None)
118
 
        self._bad_id_tag_bzr = revision_id(self._bad_id_tag + '--patch-1',
119
 
                                           None)
120
 
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
121
 
            pybaz.Version(self._bad_id_tag))
122
 
        tree = pybaz.Revision(self._bad_id_tag + '--base-0').get(
123
 
            os.path.join(self._tmpdir, 'tree'))
124
 
        from bzrlib.plugins.bzrtools.baz_import import add_file
125
 
        add_file(os.path.join(self._tmpdir,'tree/path'), 'text', 
126
 
                 'this_id/needs%escaping')
127
 
        msg = tree.log_message()
128
 
        msg["summary"] = "commit something which needs escaping."
129
 
        tree.commit(msg)
130
 
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
131
 
 
132
96
        self.make_import_symlink()
133
97
        self.make_missing_ancestor()
134
 
        self.make_inbranch_continuation()
135
98
 
136
99
    def make_import_symlink(self):
137
100
        self._import_symlink = 'demo@DONOTUSE/c--import-symlink--0'
138
 
        self._import_symlink_bzr = revision_id(
139
 
            self._import_symlink + '--base-0', None)
 
101
        self._import_symlink_bzr = revision_id(self._import_symlink + '--base-0')
140
102
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
141
103
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
142
104
                               self._import_symlink)
143
 
        link_path = os.path.join(self._tmpdir, 'tree', 'alink')
144
 
        os.symlink('missing-file-name', link_path)
 
105
        os.symlink('missing-file-name',
 
106
                   os.path.join(self._tmpdir, 'tree', 'alink'))
145
107
        tree.add_tag('alink')
146
108
        id_file = open(os.path.join(tree, '.arch-ids', 'alink.id'), 'w')
147
109
        id_file.write('symlink_tag\n')
149
111
        msg = tree.log_message()
150
112
        msg["summary"] = "Import with a symlink"
151
113
        tree.import_(msg)
152
 
        os.unlink(link_path)
153
 
        f = file(link_path, 'w')
154
 
        f.write('Not a symlink no more!')
155
 
        f.close()
156
 
        msg = tree.log_message()
157
 
        msg["summary"] = "Turn a symlink into a file"
158
 
        tree.commit(msg)
159
114
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
160
115
 
161
116
    def make_empty_import(self):
162
117
        self._import = 'demo@DONOTUSE/c--import--0'
163
118
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
164
 
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), 
165
 
                               self._import)
 
119
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), self._import)
166
120
        msg = tree.log_message()
167
121
        msg["summary"] = "I am importing now"
168
122
        tree.import_(msg)
169
123
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
170
124
 
171
 
    def make_utf8_log(self):
172
 
        self._utf8 = 'demo@DONOTUSE/c--utf8--0'
173
 
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
174
 
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), 
175
 
                               self._utf8)
176
 
        msg = tree.log_message()
177
 
        msg["summary"] = u"I am importing now\u1234".encode('utf-8')
178
 
        tree.import_(msg)
179
 
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
180
 
 
181
125
    def make_missing_ancestor(self):
182
126
        self._archivegoneroot = os.path.join(self._tmpdir, 'archivegone')
183
 
        self._archive = make_archive('demo-gone@DONOTUSE',
184
 
                                     str(self._archivegoneroot))
 
127
        self._archive = pybaz.make_archive('demo-gone@DONOTUSE',
 
128
                                           str(self._archivegoneroot))
185
129
        self._missing_import = 'demo-gone@DONOTUSE/c--import--0'
186
130
        self._missing_import_bzr = revision_id(self._missing_import 
187
 
                                                 + '--base-0', None)
 
131
                                                 + '--base-0')
188
132
        self._missing_ancestor = 'demo@DONOTUSE/c--gone--0'
189
133
        self._missing_ancestor_bzr = revision_id(self._missing_ancestor 
190
 
                                                 + '--base-0', None)
 
134
                                                 + '--base-0')
191
135
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
192
136
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), 
193
137
                               self._missing_import)
198
142
        # tag into the kept archive
199
143
        pybaz.Revision(self._missing_import + '--base-0').make_continuation(
200
144
            pybaz.Version(self._missing_ancestor))
201
 
 
202
 
        # make an import for testing history-reuse logic.
203
 
        # note the use of a namespace layout here.
204
 
        self._missing_import_imported = os.path.join(self._tmpdir, 
205
 
                                                     'archivegone-bzr')
206
 
        os.mkdir(os.path.join(self._tmpdir, 'archivegone-bzr'))
207
 
        os.mkdir(os.path.join(self._tmpdir, 'archivegone-bzr', 'c'))
208
 
        import_version(os.path.join(self._tmpdir, 'archivegone-bzr', 
209
 
                                    'c', 'import'),
210
 
                       pybaz.Version(self._missing_import), None)
211
145
        # and make it inaccessible
212
146
        pybaz.Archive('demo-gone@DONOTUSE').unregister()
213
147
 
214
 
    def make_inbranch_continuation(self):
215
 
        self._inbranch_tag = 'demo@DONOTUSE/c--inbranch-tag--0'
216
 
        self._inbranch_tag_base = self._inbranch_tag + '--base-0'
217
 
        self._inbranch_tag_base_bzr = revision_id(self._inbranch_tag_base, 
218
 
                                                  None)
219
 
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
220
 
            pybaz.Version(self._inbranch_tag))
221
 
        self._inbranch_tag_head = self._inbranch_tag + '--patch-1'
222
 
        self._inbranch_tag_head_bzr = revision_id(self._inbranch_tag_head,
223
 
                                                  None)
224
 
        pybaz.Revision(self._inbranch_tag_base).make_continuation(
225
 
            pybaz.Version(self._inbranch_tag))
226
 
 
227
148
    @classmethod
228
149
    def _makeResource(self):
229
150
        return BazTreeResource()
240
161
    def setUp(self):
241
162
        TestCaseInTempDir.setUp(self)
242
163
        ResourcedTestCase.setUpResources(self)
243
 
        os.environ['HOME'] = self._baz._homedir
 
164
        self.output = StringIO()
244
165
 
245
166
    def tearDown(self):
246
167
        ResourcedTestCase.tearDownResources(self)
247
168
        TestCaseInTempDir.tearDown(self)
248
 
 
249
 
    def test_import_utf8(self):
250
 
        import_version('output', pybaz.Version(self._baz._utf8), None)
251
 
        branch = Branch.open('output')
252
 
        plain_revid = 'Arch-1:demo@DONOTUSE%c--utf8--0--base-0'
253
 
        self.assertEqual([plain_revid], branch.revision_history())
254
 
        self.assertEqual(u'I am importing now\ufffd\ufffd\ufffd',
255
 
                         branch.repository.get_revision(plain_revid).message)
256
 
        import_version('output2', pybaz.Version(self._baz._utf8), 'utf-8')
257
 
        branch2 = Branch.open('output2')
258
 
        utf8_revid = 'Arch-1-utf-8:demo@DONOTUSE%c--utf8--0--base-0'
259
 
        self.assertEqual([utf8_revid], branch2.revision_history())
260
 
        self.assertEqual(u'I am importing now\u1234',
261
 
                         branch2.repository.get_revision(utf8_revid).message)
262
 
 
 
169
 
 
170
    def collect(self, text):
 
171
        self.output.write(text)
 
172
        self.output.write("\n")
 
173
 
263
174
    def test_import_empty(self):
264
 
        import_version('output', pybaz.Version(self._baz._import), None)
 
175
        import_version('output', pybaz.Version(self._baz._import), self.collect)
265
176
        # expected results:
266
 
        # one commit, no files, revision identifier of 
267
 
        # 'demo@DONOTUSE_c--import--0--base-0'
268
 
        branch = Branch.open('output')
269
 
        repo = branch.repository
 
177
        # one commit, no files, revision identifier of 'demo@DONOTUSE_c--import--0--base-0'
 
178
        branch = find_branch('output', find_root=False)
270
179
        self.assertEqual(branch.revision_history(),
271
180
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
272
 
        rev = repo.get_revision('Arch-1:demo@DONOTUSE%c--import--0--base-0')
 
181
        rev = branch.get_revision('Arch-1:demo@DONOTUSE%c--import--0--base-0')
273
182
        # and again.
274
183
        import_version('output2', pybaz.Version('demo@DONOTUSE/c--import--0'),
275
 
                       None)
276
 
        branch2 = Branch.open('output2')
277
 
        repo2 = branch2.repository
 
184
                       self.collect)
 
185
        branch2 = find_branch('output2', find_root=False)
278
186
        self.assertEqual(branch.revision_history(), branch2.revision_history())
279
 
        rev2 = repo2.get_revision('Arch-1:demo@DONOTUSE%c--import--0--base-0')
 
187
        rev2 = branch2.get_revision('Arch-1:demo@DONOTUSE%c--import--0--base-0')
280
188
        # they must be the same
281
189
        self.assertEqual(rev, rev2)
282
190
 
287
195
                         "Arch-1:demo@DONOTUSE%c--import--0--base-0")
288
196
 
289
197
    def test_empty_tagged(self):
290
 
        import_version('output', pybaz.Version(self._baz._empty_tag), None)
 
198
        import_version('output', pybaz.Version(self._baz._empty_tag),
 
199
                       self.collect)
291
200
        # expected results:
292
201
        # two commits, no files, revision identifiers of 
293
202
        # 'demo@DONOTUSE_c--import--0--base-0' and
294
203
        # self._baz._empty_tag_bzr
295
 
        branch = Branch.open('output')
 
204
        branch = find_branch('output', find_root=False)
296
205
        self.assertEqual(branch.revision_history(),
297
206
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
298
207
                          self._baz._empty_tag_bzr])
299
 
        rev = branch.repository.get_revision(self._baz._empty_tag_bzr)
 
208
        rev = branch.get_revision(self._baz._empty_tag_bzr)
300
209
        # and again.
301
 
        import_version('output2', pybaz.Version(self._baz._empty_tag), None)
302
 
        branch2 = Branch.open('output2')
 
210
        import_version('output2', pybaz.Version(self._baz._empty_tag),
 
211
                       self.collect)
 
212
        branch2 = find_branch('output2', find_root=False)
303
213
        self.assertEqual(branch.revision_history(), branch2.revision_history())
304
 
        rev2 = branch2.repository.get_revision(self._baz._empty_tag_bzr)
 
214
        rev2 = branch2.get_revision(self._baz._empty_tag_bzr)
305
215
        # they must be the same
306
216
        self.assertEqual(rev, rev2)
307
217
 
308
218
        # and we should get some expected values:
309
219
        self.assertEqual(rev.committer, "Test User<test@example.org>")
310
 
        self.assertEqual(rev.message, 
311
 
                         "tag of demo@DONOTUSE/c--import--0--base-0")
 
220
        self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--import--0--base-0")
312
221
        self.assertEqual(rev.revision_id, self._baz._empty_tag_bzr)
313
222
 
314
223
    def test_empty_merged_tagged(self):
315
 
        import_version('output', pybaz.Version(self._baz._empty_merged_tag), 
316
 
                       None)
 
224
        import_version('output', pybaz.Version(self._baz._empty_merged_tag),
 
225
                       self.collect)
317
226
        # expected results:
318
227
        # two commits, no files, revision identifiers of 
319
228
        # 'demo@DONOTUSE_c--import--0--base-0' and
321
230
        # self._baz._empty_merged_tag_bzr
322
231
        # and a merged revision from the latter of
323
232
        # self._baz._empty_tag_bzr
324
 
        branch = Branch.open('output')
325
 
        repo = branch.repository
 
233
        branch = find_branch('output', find_root=False)
326
234
        self.assertEqual(branch.revision_history(),
327
235
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
328
236
                          self._baz._empty_merged_tag_bzr_base,
329
237
                          self._baz._empty_merged_tag_bzr])
330
238
        # and again.
331
 
        import_version('output2', pybaz.Version(self._baz._empty_merged_tag), 
332
 
                       None)
333
 
        branch2 = Branch.open('output2')
334
 
        repo2 = branch2.repository
 
239
        import_version('output2', pybaz.Version(self._baz._empty_merged_tag),
 
240
                       self.collect)
 
241
        branch2 = find_branch('output2', find_root=False)
335
242
        # and import what we should be merged up against for checking with.
336
 
        import_version('output3', pybaz.Version(self._baz._empty_tag), None)
337
 
        branch3 = Branch.open('output3')
 
243
        import_version('output3', pybaz.Version(self._baz._empty_tag),
 
244
                       self.collect)
 
245
        branch3 = find_branch('output3', find_root=False)
338
246
        
339
247
        self.assertEqual(branch.revision_history(), branch2.revision_history())
340
 
        self.assertNotEqual(branch.revision_history(), 
341
 
                            branch3.revision_history())
 
248
        self.assertNotEqual(branch.revision_history(), branch3.revision_history())
342
249
        # check revisions in the history.
343
 
        rev = repo.get_revision(self._baz._empty_merged_tag_bzr_base)
344
 
        rev2 = repo2.get_revision(self._baz._empty_merged_tag_bzr_base)
 
250
        rev = branch.get_revision(self._baz._empty_merged_tag_bzr_base)
 
251
        rev2 = branch2.get_revision(self._baz._empty_merged_tag_bzr_base)
345
252
        # they must be the same
346
253
        self.assertEqual(rev, rev2)
347
254
        # and we should get some expected values:
348
255
        self.assertEqual(rev.committer, "Test User<test@example.org>")
349
 
        self.assertEqual(rev.message, 
350
 
                         "tag of demo@DONOTUSE/c--import--0--base-0")
 
256
        self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--import--0--base-0")
351
257
        self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_bzr_base)
352
 
        self.assertEqual(['Arch-1:demo@DONOTUSE%c--import--0--base-0'],
353
 
                         rev.parent_ids)
354
258
 
355
259
        # check next revisions in the history.
356
 
        rev = repo.get_revision(self._baz._empty_merged_tag_bzr)
357
 
        rev2 = repo2.get_revision(self._baz._empty_merged_tag_bzr)
 
260
        rev = branch.get_revision(self._baz._empty_merged_tag_bzr)
 
261
        rev2 = branch2.get_revision(self._baz._empty_merged_tag_bzr)
358
262
        # they must be the same
359
263
        self.assertEqual(rev, rev2)
360
264
        # and we should get some expected values:
361
265
        self.assertEqual(rev.committer, "Test User<test@example.org>")
362
266
        self.assertEqual(rev.message, "did a merge, yarh")
363
267
        self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_bzr)
364
 
        self.assertEqual(rev.parent_ids[0],
 
268
        self.assertEqual(rev.parents[0].revision_id,
365
269
                         self._baz._empty_merged_tag_bzr_base)
366
 
        self.assertEqual(rev.parent_ids[1], self._baz._empty_tag_bzr)
 
270
        self.assertEqual(rev.parents[1].revision_id,
 
271
                         self._baz._empty_tag_bzr)
367
272
 
368
273
        # this tree should have nothing missing from that tree.   
369
274
        # FIXME there is no code for this right now.
370
275
        # self.assertEqual(branch.missing_revisions(branch3), [])
371
276
        
372
277
    def test_merge_branch_with_merges(self):
373
 
        import_version('output', pybaz.Version(self._baz._empty_merged_tag_2), 
374
 
                       None)
 
278
        import_version('output', pybaz.Version(self._baz._empty_merged_tag_2),
 
279
                       self.collect)
375
280
        # expected results:
376
281
        # two commits, no files, revision identifiers of 
377
282
        # 'demo@DONOTUSE_c--import--0--base-0' and
379
284
        # self._baz._empty_merged_tag_2_bzr
380
285
        # and a merged revision from the latter of
381
286
        # self._baz._empty_merged_tag_bzr
382
 
        branch = Branch.open('output')
383
 
        repo = branch.repository
 
287
        branch = find_branch('output', find_root=False)
384
288
        self.assertEqual(branch.revision_history(),
385
289
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
386
290
                          self._baz._empty_merged_tag_2_bzr_base,
387
291
                          self._baz._empty_merged_tag_2_bzr])
388
292
        # and again.
389
293
        import_version('output2', pybaz.Version(self._baz._empty_merged_tag_2),
390
 
                       None)
391
 
        branch2 = Branch.open('output2')
392
 
        repo2 = branch2.repository
 
294
                       self.collect)
 
295
        branch2 = find_branch('output2', find_root=False)
393
296
        # and import what we should be merged up against for checking with.
394
297
        import_version('output3', pybaz.Version(self._baz._empty_merged_tag),
395
 
                       None)
396
 
        branch3 = Branch.open('output3')
 
298
                       self.collect)
 
299
        branch3 = find_branch('output3', find_root=False)
397
300
        
398
301
        self.assertEqual(branch.revision_history(), branch2.revision_history())
399
 
        self.assertNotEqual(branch.revision_history(), 
400
 
                            branch3.revision_history())
 
302
        self.assertNotEqual(branch.revision_history(), branch3.revision_history())
401
303
        # check revisions in the history.
402
 
        rev = repo.get_revision(self._baz._empty_merged_tag_2_bzr_base)
403
 
        rev2 = repo2.get_revision(self._baz._empty_merged_tag_2_bzr_base)
 
304
        rev = branch.get_revision(self._baz._empty_merged_tag_2_bzr_base)
 
305
        rev2 = branch2.get_revision(self._baz._empty_merged_tag_2_bzr_base)
404
306
        # they must be the same
405
307
        self.assertEqual(rev, rev2)
406
308
        # and we should get some expected values:
407
309
        self.assertEqual(rev.committer, "Test User<test@example.org>")
408
 
        self.assertEqual(rev.message, 
409
 
                         "tag of demo@DONOTUSE/c--import--0--base-0")
410
 
        self.assertEqual(rev.revision_id, 
411
 
                         self._baz._empty_merged_tag_2_bzr_base)
 
310
        self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--import--0--base-0")
 
311
        self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_2_bzr_base)
412
312
 
413
313
        # check next revisions in the history.
414
 
        rev = repo.get_revision(self._baz._empty_merged_tag_2_bzr)
415
 
        rev2 = repo2.get_revision(self._baz._empty_merged_tag_2_bzr)
 
314
        rev = branch.get_revision(self._baz._empty_merged_tag_2_bzr)
 
315
        rev2 = branch2.get_revision(self._baz._empty_merged_tag_2_bzr)
416
316
        # they must be the same
417
317
        self.assertEqual(rev, rev2)
418
318
        # and we should get some expected values:
419
319
        self.assertEqual(rev.committer, "Test User<test@example.org>")
420
320
        self.assertEqual(rev.message, "merge in a merged tree.")
421
321
        self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_2_bzr)
422
 
        self.assertEqual(rev.parent_ids[0],
 
322
        self.assertEqual(rev.parents[0].revision_id,
423
323
                         self._baz._empty_merged_tag_2_bzr_base)
424
 
        self.assertEqual(rev.parent_ids[1],
 
324
        self.assertEqual(rev.parents[1].revision_id,
425
325
                         self._baz._empty_merged_tag_bzr)
426
326
 
427
327
        # this tree should have nothing missing from that tree.   
429
329
        # self.assertEqual(branch.missing_revisions(branch3), [])
430
330
        
431
331
    def test_import_symlink(self):
432
 
        import_version('output', pybaz.Version(self._baz._import_symlink), 
433
 
                       None, max_count=1)
 
332
        import_version('output', pybaz.Version(self._baz._import_symlink),
 
333
                       self.collect)
434
334
        # expected results:
435
 
        # two commits, no files, revision identifier of 
436
 
        # 'demo@DONOTUSE_c--import--0--base-0'
437
 
        branch = Branch.open('output')
 
335
        # one commit, no files, revision identifier of 'demo@DONOTUSE_c--import--0--base-0'
 
336
        branch = find_branch('output', find_root=False)
438
337
        self.assertEqual(branch.revision_history(),
439
338
                         [self._baz._import_symlink_bzr])
440
 
        rev = branch.repository.get_revision(self._baz._import_symlink_bzr)
 
339
        rev = branch.get_revision(self._baz._import_symlink_bzr)
441
340
        # and again.
442
341
        import_version('output2', pybaz.Version(self._baz._import_symlink),
443
 
                       None, max_count=1)
444
 
        branch2 = Branch.open('output2')
 
342
                       self.collect)
 
343
        branch2 = find_branch('output2', find_root=False)
445
344
        self.assertEqual(branch.revision_history(), branch2.revision_history())
446
 
        rev2 = branch2.repository.get_revision(self._baz._import_symlink_bzr)
 
345
        rev2 = branch2.get_revision(self._baz._import_symlink_bzr)
447
346
        # they must be the same
448
347
        self.assertEqual(rev, rev2)
449
348
 
453
352
        self.assertEqual(rev.revision_id, self._baz._import_symlink_bzr)
454
353
 
455
354
        # and we want the symlink alink with target 'missing-file-name'
456
 
        inv = branch.repository.get_inventory(rev.revision_id)
 
355
        inv = branch.get_inventory(rev.inventory_id)
457
356
        self.assertEqual(inv.path2id('alink'), 'x_symlink_tag')
458
357
        entry = inv['x_symlink_tag']
459
358
        self.assertEqual(entry.kind, 'symlink')
460
359
        self.assertEqual(entry.symlink_target, 'missing-file-name')
461
360
 
462
 
        # current bzr doesn't handle type changes
463
 
        self.assertRaises(AssertionError, import_version, 'output3',
464
 
                          pybaz.Version(self._baz._import_symlink), None)
465
 
 
466
361
    def test_missing_ancestor(self):
467
362
        import_version('output', pybaz.Version(self._baz._missing_ancestor),
468
 
                       None)
 
363
                       self.collect)
469
364
        # expected results:
470
365
        # one commits, no files, revision identifiers of 
471
366
        # 'demo@DONOTUSE_c--gone--0--base-0' and
472
367
        # a merge of demo-gone@DONOTUSE%c--import--0
473
 
        branch = Branch.open('output')
 
368
        branch = find_branch('output', find_root=False)
474
369
        self.assertEqual(branch.revision_history(),
475
370
                         [self._baz._missing_ancestor_bzr])
476
 
        rev = branch.repository.get_revision(self._baz._missing_ancestor_bzr)
 
371
        rev = branch.get_revision(self._baz._missing_ancestor_bzr)
477
372
        # and again.
478
 
        import_version('output2', pybaz.Version(self._baz._missing_ancestor), 
479
 
                       None)
480
 
        branch2 = Branch.open('output2')
 
373
        import_version('output2', pybaz.Version(self._baz._missing_ancestor),
 
374
                       self.collect)
 
375
        branch2 = find_branch('output2', find_root=False)
481
376
        self.assertEqual(branch.revision_history(), branch2.revision_history())
482
 
        rev2 = branch2.repository.get_revision(self._baz._missing_ancestor_bzr)
 
377
        rev2 = branch2.get_revision(self._baz._missing_ancestor_bzr)
483
378
        # they must be the same
484
379
        self.assertEqual(rev, rev2)
485
380
 
486
381
        # and we should get some expected values:
487
382
        self.assertEqual(rev.committer, "Test User<test@example.org>")
488
 
        self.assertEqual(rev.message, 
489
 
                         "tag of demo-gone@DONOTUSE/c--import--0--base-0")
 
383
        self.assertEqual(rev.message, "tag of demo-gone@DONOTUSE/c--import--0--base-0")
490
384
        self.assertEqual(rev.revision_id, self._baz._missing_ancestor_bzr)
491
 
        self.assertEqual(rev.parent_ids[0], self._baz._missing_import_bzr)
492
 
        self.assertEqual(1, len(rev.parent_ids))
 
385
        self.assertEqual(rev.parents[0].revision_id,
 
386
                         self._baz._missing_import_bzr)
 
387
        self.assertEqual(1, len(rev.parents))
493
388
 
494
389
        # must NOT be able to get the merged evision
495
 
        self.assertRaises(NoSuchRevision, branch.repository.get_revision, 
 
390
        self.assertRaises(NoSuchRevision, branch.get_revision, 
496
391
                          self._baz._missing_import_bzr)
497
392
 
498
 
    def test_missing_ancestor_reusing_history(self):
499
 
        import_version('output', pybaz.Version(self._baz._missing_ancestor),
500
 
                       None,
501
 
                       reuse_history_from=[self._baz._missing_import_imported])
502
 
        # expected results:
503
 
        # one commits, no files, revision identifiers of 
504
 
        # 'demo-gone@DONOTUSE%c--import--0--base-0' and 
505
 
        # 'demo@DONOTUSE%c--gone--0--base-0'
506
 
        branch = Branch.open('output')
507
 
        self.assertEqual(branch.revision_history(),
508
 
                         [self._baz._missing_import_bzr,
509
 
                          self._baz._missing_ancestor_bzr])
510
 
        rev = branch.repository.get_revision(self._baz._missing_ancestor_bzr)
511
 
        # and again.
512
 
        import_version('output2', pybaz.Version(self._baz._missing_ancestor),
513
 
                       None,
514
 
                       reuse_history_from=[self._baz._missing_import_imported])
515
 
        branch2 = Branch.open('output2')
516
 
        self.assertEqual(branch.revision_history(), branch2.revision_history())
517
 
        rev2 = branch2.repository.get_revision(self._baz._missing_ancestor_bzr)
518
 
        # they must be the same
519
 
        self.assertEqual(rev, rev2)
520
 
 
521
 
        # must be able to get the missing base revision
522
 
        branch.repository.get_revision(self._baz._missing_import_bzr)
523
 
 
524
 
        # and we should get some expected values:
525
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
526
 
        self.assertEqual(rev.message, 
527
 
                         "tag of demo-gone@DONOTUSE/c--import--0--base-0")
528
 
        self.assertEqual(rev.revision_id, self._baz._missing_ancestor_bzr)
529
 
        self.assertEqual(rev.parent_ids[0], self._baz._missing_import_bzr)
530
 
        self.assertEqual(1, len(rev.parent_ids))
531
 
 
532
 
    def test_bad_file_id(self):
533
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag), None)
534
 
        # expected results:
535
 
        # three commits, one files, revision identifiers of 
536
 
        # 'demo@DONOTUSE_c--import--0--base-0' ,
537
 
        # 'demo@DONOTUSE/c--bad-id--0--base-0' ,
538
 
        # ''demo@DONOTUSE/c--bad-id--0--patch-1'
539
 
        branch = Branch.open('output')
540
 
        self.assertEqual(branch.revision_history(),
541
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
542
 
                          self._baz._bad_id_tag_bzr_base,
543
 
                          self._baz._bad_id_tag_bzr])
544
 
        rev = branch.repository.get_revision(self._baz._bad_id_tag_bzr)
545
 
        inv = branch.repository.get_inventory(self._baz._bad_id_tag_bzr)
546
 
        self.assertEqual(inv.path2id('path'), 'x_this_id%2fneeds%25escaping')
547
 
        self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
548
 
 
549
 
    def test_appending_revisions_already_present(self):
550
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag), None,
551
 
                       max_count=2)
552
 
        # expected results:
553
 
        # three commits, one files, revision identifiers of 
554
 
        # 'demo@DONOTUSE_c--import--0--base-0' ,
555
 
        # 'demo@DONOTUSE/c--bad-id--0--base-0' ,
556
 
        # ''demo@DONOTUSE/c--bad-id--0--patch-1'
557
 
        branch = Branch.open('output')
558
 
        self.assertEqual(branch.revision_history(),
559
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
560
 
                          self._baz._bad_id_tag_bzr_base])
561
 
        branch.set_revision_history(
562
 
            ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
563
 
        del branch
564
 
        branch = Branch.open('output')
565
 
        self.assertEqual(branch.revision_history(),
566
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
567
 
        del branch
568
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag), None)
569
 
        branch = Branch.open('output')
570
 
        self.assertEqual(branch.revision_history(),
571
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
572
 
                          self._baz._bad_id_tag_bzr_base,
573
 
                          self._baz._bad_id_tag_bzr])
574
 
        rev = branch.repository.get_revision(self._baz._bad_id_tag_bzr)
575
 
        inv = branch.repository.get_inventory(self._baz._bad_id_tag_bzr)
576
 
        self.assertEqual(inv.path2id('path'), 'x_this_id%2fneeds%25escaping')
577
 
        self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
578
 
 
579
 
    def test_appending_revisions_all_already_present(self):
580
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag), None)
581
 
        # expected results:
582
 
        # three commits, one files, revision identifiers of 
583
 
        # 'demo@DONOTUSE_c--import--0--base-0' ,
584
 
        # 'demo@DONOTUSE/c--bad-id--0--base-0' ,
585
 
        # ''demo@DONOTUSE/c--bad-id--0--patch-1'
586
 
        branch = Branch.open('output')
587
 
        self.assertEqual(branch.revision_history(),
588
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
589
 
                          self._baz._bad_id_tag_bzr_base,
590
 
                          self._baz._bad_id_tag_bzr])
591
 
        branch.set_revision_history(
592
 
            ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
593
 
        del branch
594
 
        branch = Branch.open('output')
595
 
        self.assertEqual(branch.revision_history(),
596
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
597
 
        del branch
598
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag), None)
599
 
        branch = Branch.open('output')
600
 
        self.assertEqual(branch.revision_history(),
601
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
602
 
                          self._baz._bad_id_tag_bzr_base,
603
 
                          self._baz._bad_id_tag_bzr])
604
 
        rev = branch.repository.get_revision(self._baz._bad_id_tag_bzr)
605
 
        inv = branch.repository.get_inventory(self._baz._bad_id_tag_bzr)
606
 
        self.assertEqual(inv.path2id('path'), 'x_this_id%2fneeds%25escaping')
607
 
        self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
608
 
 
609
 
    def test_inbranch_conversion(self):
610
 
        import_version('output', pybaz.Version(self._baz._inbranch_tag), None)
611
 
        # expected results:
612
 
        # three commits, no files, revision identifiers of 
613
 
        # 'demo@DONOTUSE_c--import--0--base-0' and
614
 
        # self._baz._inbranch_tag_base_bzr
615
 
        # self._baz._inbranch_tag_head_bzr
616
 
        # with no merges.
617
 
        branch = Branch.open('output')
618
 
        self.assertEqual(branch.revision_history(),
619
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
620
 
                          self._baz._inbranch_tag_base_bzr,
621
 
                          self._baz._inbranch_tag_head_bzr])
622
 
        # and again.
623
 
        import_version('output2', pybaz.Version(self._baz._inbranch_tag), None)
624
 
        branch2 = Branch.open('output2')
625
 
        
626
 
        self.assertEqual(branch.revision_history(), branch2.revision_history())
627
 
        # check revisions in the history.
628
 
        rev = branch.repository.get_revision(self._baz._inbranch_tag_base_bzr)
629
 
        rev2 = branch2.repository.get_revision(self._baz._inbranch_tag_base_bzr)
630
 
        # they must be the same
631
 
        self.assertEqual(rev, rev2)
632
 
        # and we should get some expected values:
633
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
634
 
        self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--import--0--base-0")
635
 
        self.assertEqual(rev.revision_id, self._baz._inbranch_tag_base_bzr)
636
 
 
637
 
        # check next revisions in the history.
638
 
        rev = branch.repository.get_revision(self._baz._inbranch_tag_head_bzr)
639
 
        rev2 = branch2.repository.get_revision(self._baz._inbranch_tag_head_bzr)
640
 
        # they must be the same
641
 
        self.assertEqual(rev, rev2)
642
 
        # and we should get some expected values:
643
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
644
 
        self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--inbranch-tag--0--base-0")
645
 
        self.assertEqual(rev.revision_id, self._baz._inbranch_tag_head_bzr)
646
 
        self.assertEqual(rev.parent_ids,
647
 
                         [self._baz._inbranch_tag_base_bzr])
648
 
 
649
 
    def test_no_commits_same_as_missing(self):
650
 
        path = 'output'
651
 
        os.mkdir(path)
652
 
        branch = bzrlib.bzrdir.BzrDir.create_branch_convenience(path)
653
 
        import_version(path, pybaz.Version(self._baz._import), None)
654
 
        # expected results:
655
 
        # one commit, revision identifier of 
656
 
        # 'demo@DONOTUSE_c--import--0--base-0'
657
 
        self.assertEqual(branch.revision_history(),
658
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
659
 
 
660
393
 
661
394
class TestNamespacePrevious(TestCase):
662
395
 
680
413
                          self.version['version-0'])
681
414
 
682
415
    def test_version1_version0(self):
683
 
        self.assertEqual(namespace_previous(self.version['versionfix-1']),
 
416
        self.assertEqual(namespace_previous(self.version['version-1']),
684
417
                         self.version['version-0'])
685
418
 
686
419
    def test_version3000_patch2999(self):
687
 
        self.assertEqual(namespace_previous(self.version['versionfix-3000']),
688
 
                         self.version['versionfix-2999'])
689
 
 
 
420
        self.assertEqual(namespace_previous(self.version['version-3000']),
 
421
                         self.version['version-2999'])
690
422
 
691
423
class TestNamespaceMapping(TestCase):
692
424
 
693
425
    def test_namespace_mapping_branch(self):
694
 
        from bzrlib.plugins.bzrtools.baz_import import map_namespace
 
426
        from baz_import import map_namespace
695
427
        branch = pybaz.Branch('foo@example.com/c--b')
696
428
        self.assertRaises(pybaz.errors.NamespaceError, map_namespace, branch)
697
429
        self.assertEqual('c/b', map_namespace(branch['0']))
698
430
        self.assertEqual('c/0.1/b', map_namespace(branch['0.1']))
699
431
 
700
432
    def test_namespace_mapping_no_branch(self):
701
 
        from bzrlib.plugins.bzrtools.baz_import import map_namespace
 
433
        from baz_import import map_namespace
702
434
        category = pybaz.Category('foo@example.com/c')
703
435
        self.assertRaises(pybaz.errors.NamespaceError, map_namespace, category)
704
436
        self.assertEqual('c/+trunk', 
707
439
                         map_namespace(pybaz.Version('%s--0.1' % category)))
708
440
 
709
441
 
710
 
class TestFileIdMapping(TestCase):
711
 
 
712
 
    def test_slash(self):
713
 
        self.assertEqual('c%2fc', map_file_id('c/c'))
714
 
        self.assertEqual('c%25c', map_file_id('c%c'))
715
 
 
716
 
 
717
442
class TestImport(TestCaseInTempDir):
718
443
 
719
444
    def setUp(self):
724
449
        os.mkdir(self._homedir)
725
450
        os.environ['HOME'] = self._homedir
726
451
        self._archiveroot = os.path.join(self._tmpdir, 'archive')
727
 
        self._archive = make_archive('demo@DONOTUSE', str(self._archiveroot))
 
452
        self._archive = pybaz.make_archive('demo@DONOTUSE', 
 
453
                                           str(self._archiveroot))
728
454
 
729
455
    def tearDown(self):
730
456
        os.environ['HOME'] = self._oldhome
734
460
    def make_import(self, namespace):
735
461
        self._import = 'demo@DONOTUSE/%s' % namespace
736
462
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
737
 
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), 
738
 
                               self._import)
 
463
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), self._import)
739
464
        msg = tree.log_message()
740
465
        msg["summary"] = "I am importing now"
741
466
        tree.import_(msg)
742
467
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
743
468
 
744
469
    def test_cmd_exists(self):
745
 
        from bzrlib.plugins.bzrtools import cmd_baz_import
 
470
        from baz_import import cmd_baz_import
746
471
 
747
472
    def test_empty_archive(self):
748
473
        command = cmd_baz_import()
749
474
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
750
475
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output')))
751
 
        walk_len = len(list(os.walk(os.path.join(self._tmpdir,'output'))))
752
 
        self.assertEqual(7, walk_len)
 
476
        self.assertEqual(1, 
 
477
                        len(list(os.walk(os.path.join(self._tmpdir,'output')))))
753
478
 
754
479
    def test_two_branches(self):
755
480
        self.make_import('c--0')
757
482
        command = cmd_baz_import()
758
483
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
759
484
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output')))
760
 
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output', 
761
 
                                       'c','+trunk')))
762
 
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output', 
763
 
                                                    'c1', '0.2','branch')))
764
 
        default_format = repository.RepositoryFormat.get_default_format()
765
 
        if getattr(default_format, 'rich_root_data', False):
766
 
            num_files = 21
767
 
        else:
768
 
            num_files = 20
769
 
        walk_len = len(list(os.walk(os.path.join(self._tmpdir,'output'))))
770
 
        self.assertEqual(num_files, walk_len)
 
485
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output', 'c','+trunk')))
 
486
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output', 'c1', '0.2','branch')))
 
487
        self.assertEqual(14, 
 
488
                        len(list(os.walk(os.path.join(self._tmpdir,'output')))))
771
489
 
772
490
    def test_run_twice(self):
773
491
        self.make_import('c--0')
774
492
        command = cmd_baz_import()
775
493
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
776
494
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
777
 
        
778
 
    def test_accepts_reuse_history(self):
779
 
        self.make_import('c--0')
780
 
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
781
 
                     'demo@DONOTUSE', '.', '.')
782
 
        
783
 
    def test_does_not_need_reuse_history(self):
784
 
        self.make_import('c--0')
785
 
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
786
 
                     'demo@DONOTUSE')
787
 
 
788
 
    def test_does_not_need_reuse_history(self):
789
 
        self.make_import('c--0')
790
 
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
791
 
                     'demo@DONOTUSE')
792
 
 
793
 
    def test_encoding_flag(self):
794
 
        self.make_import('c--0')
795
 
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
796
 
                     'demo@DONOTUSE')
797
 
        self.assertEqual(['Arch-1:demo@DONOTUSE%c--0--base-0'], 
798
 
                         Branch.open(os.path.join(self._tmpdir, 
799
 
                                     'output/c/+trunk')).revision_history())
800
 
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output2'),
801
 
                     'demo@DONOTUSE', '--encoding', 'utf-8')
802
 
        self.assertEqual(['Arch-1-utf-8:demo@DONOTUSE%c--0--base-0'], 
803
 
                         Branch.open(os.path.join(self._tmpdir, 
804
 
                                     'output2/c/+trunk')).revision_history())
805
 
        self.run_bzr('baz-import-branch', os.path.join(self._tmpdir, 'output3'),
806
 
                     'demo@DONOTUSE/c--0')
807
 
        self.assertEqual(['Arch-1:demo@DONOTUSE%c--0--base-0'], 
808
 
                         Branch.open(os.path.join(self._tmpdir, 
809
 
                                     'output3')).revision_history())
810
 
        self.run_bzr('baz-import-branch', os.path.join(self._tmpdir, 
811
 
                      'output4'), 'demo@DONOTUSE/c--0', '--encoding', 'utf-8')
812
 
        self.assertEqual(['Arch-1-utf-8:demo@DONOTUSE%c--0--base-0'], 
813
 
                         Branch.open(os.path.join(self._tmpdir, 
814
 
                                     'output4')).revision_history())