~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/test_baz_import.py

  • Committer: Aaron Bentley
  • Date: 2007-06-11 05:08:34 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20070611050834-wcbta2pfitcuopku
fix long-line detection

Show diffs side-by-side

added added

removed removed

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