~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

  • Committer: Robert Collins
  • Date: 2005-10-08 08:45:42 UTC
  • Revision ID: robertc@robertcollins.net-20051008084542-48ea5a99756f970e
add rm alias to remove

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
"""Black-box tests for bzr.
20
20
 
21
21
These check that it behaves properly when it's invoked through the regular
22
 
command-line interface.
23
 
 
24
 
This always reinvokes bzr through a new Python interpreter, which is a
25
 
bit inefficient but arguably tests in a way more representative of how
26
 
it's normally invoked.
 
22
command-line interface. This doesn't actually run a new interpreter but 
 
23
rather starts again from the run_bzr function.
27
24
"""
28
25
 
29
 
# this code was previously in testbzr
30
 
 
31
 
from unittest import TestCase
32
 
from bzrlib.selftest import TestBase, InTempDir
33
 
 
34
 
 
35
 
 
36
 
class ExternalBase(InTempDir):
37
 
    def runbzr(self, args, retcode=0):
38
 
        try:
39
 
            import shutil
40
 
            from subprocess import call
41
 
        except ImportError, e:
42
 
            _need_subprocess()
43
 
            raise
44
 
 
 
26
 
 
27
from cStringIO import StringIO
 
28
import os
 
29
import shutil
 
30
import sys
 
31
import os
 
32
 
 
33
from bzrlib.branch import Branch
 
34
from bzrlib.errors import BzrCommandError
 
35
from bzrlib.osutils import has_symlinks
 
36
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
 
37
from bzrlib.selftest.HTTPTestUtil import TestCaseWithWebserver
 
38
 
 
39
 
 
40
class ExternalBase(TestCaseInTempDir):
 
41
 
 
42
    def runbzr(self, args, retcode=0, backtick=False):
45
43
        if isinstance(args, basestring):
46
44
            args = args.split()
47
 
            
48
 
        return self.runcmd(['python', self.BZRPATH,] + args,
49
 
                           retcode=retcode)
50
 
 
51
 
 
52
 
 
53
 
class TestVersion(ExternalBase):
54
 
    def runTest(self):
55
 
        # output is intentionally passed through to stdout so that we
56
 
        # can see the version being tested
57
 
        self.runbzr(['version'])
58
 
 
59
 
 
60
 
 
61
 
class HelpCommands(ExternalBase):
62
 
    def runTest(self):
 
45
 
 
46
        if backtick:
 
47
            return self.run_bzr_captured(args, retcode=retcode)[0]
 
48
        else:
 
49
            return self.run_bzr_captured(args, retcode=retcode)
 
50
 
 
51
 
 
52
class TestCommands(ExternalBase):
 
53
 
 
54
    def test_help_commands(self):
63
55
        self.runbzr('--help')
64
56
        self.runbzr('help')
65
57
        self.runbzr('help commands')
66
58
        self.runbzr('help help')
67
59
        self.runbzr('commit -h')
68
60
 
69
 
 
70
 
class InitBranch(ExternalBase):
71
 
    def runTest(self):
72
 
        import os
 
61
    def test_init_branch(self):
73
62
        self.runbzr(['init'])
74
63
 
75
 
 
76
 
 
77
 
class UserIdentity(ExternalBase):
78
 
    def runTest(self):
 
64
    def test_whoami(self):
79
65
        # this should always identify something, if only "john@localhost"
80
66
        self.runbzr("whoami")
81
67
        self.runbzr("whoami --email")
82
 
        self.assertEquals(self.backtick("bzr whoami --email").count('@'),
83
 
                          1)
84
 
 
85
 
 
86
 
class InvalidCommands(ExternalBase):
87
 
    def runTest(self):
 
68
 
 
69
        self.assertEquals(self.runbzr("whoami --email",
 
70
                                      backtick=True).count('@'), 1)
 
71
        
 
72
    def test_whoami_branch(self):
 
73
        """branch specific user identity works."""
 
74
        self.runbzr('init')
 
75
        f = file('.bzr/email', 'wt')
 
76
        f.write('Branch Identity <branch@identi.ty>')
 
77
        f.close()
 
78
        bzr_email = os.environ.get('BZREMAIL')
 
79
        if bzr_email is not None:
 
80
            del os.environ['BZREMAIL']
 
81
        whoami = self.runbzr("whoami",backtick=True)
 
82
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
83
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
 
84
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
 
85
        # Verify that the environment variable overrides the value 
 
86
        # in the file
 
87
        os.environ['BZREMAIL'] = 'Different ID <other@environ.ment>'
 
88
        whoami = self.runbzr("whoami",backtick=True)
 
89
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
90
        self.assertTrue(whoami.startswith('Different ID <other@environ.ment>'))
 
91
        self.assertTrue(whoami_email.startswith('other@environ.ment'))
 
92
        if bzr_email is not None:
 
93
            os.environ['BZREMAIL'] = bzr_email
 
94
 
 
95
    def test_invalid_commands(self):
88
96
        self.runbzr("pants", retcode=1)
89
97
        self.runbzr("--pants off", retcode=1)
90
98
        self.runbzr("diff --message foo", retcode=1)
91
99
 
92
 
 
93
 
 
94
 
class EmptyCommit(ExternalBase):
95
 
    def runTest(self):
 
100
    def test_empty_commit(self):
96
101
        self.runbzr("init")
97
102
        self.build_tree(['hello.txt'])
98
103
        self.runbzr("commit -m empty", retcode=1)
99
104
        self.runbzr("add hello.txt")
100
105
        self.runbzr("commit -m added")
101
106
 
102
 
 
103
 
 
104
 
class IgnorePatterns(ExternalBase):
105
 
    def runTest(self):
 
107
    def test_empty_commit_message(self):
 
108
        self.runbzr("init")
 
109
        file('foo.c', 'wt').write('int main() {}')
 
110
        self.runbzr(['add', 'foo.c'])
 
111
        self.runbzr(["commit", "-m", ""] , retcode=1) 
 
112
 
 
113
    def test_ignore_patterns(self):
106
114
        from bzrlib.branch import Branch
107
115
        
108
 
        b = Branch('.', init=True)
 
116
        b = Branch.initialize('.')
109
117
        self.assertEquals(list(b.unknowns()), [])
110
118
 
111
119
        file('foo.tmp', 'wt').write('tmp files are ignored')
112
120
        self.assertEquals(list(b.unknowns()), [])
113
 
        assert self.backtick('bzr unknowns') == ''
 
121
        assert self.capture('unknowns') == ''
114
122
 
115
123
        file('foo.c', 'wt').write('int main() {}')
116
124
        self.assertEquals(list(b.unknowns()), ['foo.c'])
117
 
        assert self.backtick('bzr unknowns') == 'foo.c\n'
 
125
        assert self.capture('unknowns') == 'foo.c\n'
118
126
 
119
127
        self.runbzr(['add', 'foo.c'])
120
 
        assert self.backtick('bzr unknowns') == ''
 
128
        assert self.capture('unknowns') == ''
121
129
 
122
130
        # 'ignore' works when creating the .bzignore file
123
131
        file('foo.blah', 'wt').write('blah')
124
132
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
125
133
        self.runbzr('ignore *.blah')
126
134
        self.assertEquals(list(b.unknowns()), [])
127
 
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
135
        assert file('.bzrignore', 'rU').read() == '*.blah\n'
128
136
 
129
137
        # 'ignore' works when then .bzrignore file already exists
130
138
        file('garh', 'wt').write('garh')
131
139
        self.assertEquals(list(b.unknowns()), ['garh'])
132
 
        assert self.backtick('bzr unknowns') == 'garh\n'
 
140
        assert self.capture('unknowns') == 'garh\n'
133
141
        self.runbzr('ignore garh')
134
142
        self.assertEquals(list(b.unknowns()), [])
135
 
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
136
 
        
137
 
 
 
143
        assert file('.bzrignore', 'rU').read() == '*.blah\ngarh\n'
 
144
 
 
145
    def test_revert(self):
 
146
        self.runbzr('init')
 
147
 
 
148
        file('hello', 'wt').write('foo')
 
149
        self.runbzr('add hello')
 
150
        self.runbzr('commit -m setup hello')
 
151
 
 
152
        file('goodbye', 'wt').write('baz')
 
153
        self.runbzr('add goodbye')
 
154
        self.runbzr('commit -m setup goodbye')
 
155
 
 
156
        file('hello', 'wt').write('bar')
 
157
        file('goodbye', 'wt').write('qux')
 
158
        self.runbzr('revert hello')
 
159
        self.check_file_contents('hello', 'foo')
 
160
        self.check_file_contents('goodbye', 'qux')
 
161
        self.runbzr('revert')
 
162
        self.check_file_contents('goodbye', 'baz')
 
163
 
 
164
        os.mkdir('revertdir')
 
165
        self.runbzr('add revertdir')
 
166
        self.runbzr('commit -m f')
 
167
        os.rmdir('revertdir')
 
168
        self.runbzr('revert')
 
169
 
 
170
        os.symlink('/unlikely/to/exist', 'symlink')
 
171
        self.runbzr('add symlink')
 
172
        self.runbzr('commit -m f')
 
173
        os.unlink('symlink')
 
174
        self.runbzr('revert')
 
175
        
 
176
        file('hello', 'wt').write('xyz')
 
177
        self.runbzr('commit -m xyz hello')
 
178
        self.runbzr('revert -r 1 hello')
 
179
        self.check_file_contents('hello', 'foo')
 
180
        self.runbzr('revert hello')
 
181
        self.check_file_contents('hello', 'xyz')
 
182
        os.chdir('revertdir')
 
183
        self.runbzr('revert')
 
184
        os.chdir('..')
 
185
 
 
186
 
 
187
    def test_mv_modes(self):
 
188
        """Test two modes of operation for mv"""
 
189
        from bzrlib.branch import Branch
 
190
        b = Branch.initialize('.')
 
191
        self.build_tree(['a', 'c', 'subdir/'])
 
192
        self.run_bzr_captured(['add', self.test_dir])
 
193
        self.run_bzr_captured(['mv', 'a', 'b'])
 
194
        self.run_bzr_captured(['mv', 'b', 'subdir'])
 
195
        self.run_bzr_captured(['mv', 'subdir/b', 'a'])
 
196
        self.run_bzr_captured(['mv', 'a', 'c', 'subdir'])
 
197
        self.run_bzr_captured(['mv', 'subdir/a', 'subdir/newa'])
 
198
 
 
199
    def test_main_version(self):
 
200
        """Check output from version command and master option is reasonable"""
 
201
        # output is intentionally passed through to stdout so that we
 
202
        # can see the version being tested
 
203
        output = self.runbzr('version', backtick=1)
 
204
        self.log('bzr version output:')
 
205
        self.log(output)
 
206
        self.assert_(output.startswith('bzr (bazaar-ng) '))
 
207
        self.assertNotEqual(output.index('Canonical'), -1)
 
208
        # make sure --version is consistent
 
209
        tmp_output = self.runbzr('--version', backtick=1)
 
210
        self.log('bzr --version output:')
 
211
        self.log(tmp_output)
 
212
        self.assertEquals(output, tmp_output)
 
213
 
 
214
    def example_branch(test):
 
215
        test.runbzr('init')
 
216
        file('hello', 'wt').write('foo')
 
217
        test.runbzr('add hello')
 
218
        test.runbzr('commit -m setup hello')
 
219
        file('goodbye', 'wt').write('baz')
 
220
        test.runbzr('add goodbye')
 
221
        test.runbzr('commit -m setup goodbye')
 
222
 
 
223
    def test_export(self):
 
224
        os.mkdir('branch')
 
225
        os.chdir('branch')
 
226
        self.example_branch()
 
227
        self.runbzr('export ../latest')
 
228
        self.assertEqual(file('../latest/goodbye', 'rt').read(), 'baz')
 
229
        self.runbzr('export ../first -r 1')
 
230
        assert not os.path.exists('../first/goodbye')
 
231
        self.assertEqual(file('../first/hello', 'rt').read(), 'foo')
 
232
        self.runbzr('export ../first.gz -r 1')
 
233
        self.assertEqual(file('../first.gz/hello', 'rt').read(), 'foo')
 
234
        self.runbzr('export ../first.bz2 -r 1')
 
235
        self.assertEqual(file('../first.bz2/hello', 'rt').read(), 'foo')
 
236
        self.runbzr('export ../first.tar -r 1')
 
237
        assert os.path.isfile('../first.tar')
 
238
        from tarfile import TarFile
 
239
        tf = TarFile('../first.tar')
 
240
        assert 'first/hello' in tf.getnames(), tf.getnames()
 
241
        self.assertEqual(tf.extractfile('first/hello').read(), 'foo')
 
242
        self.runbzr('export ../first.tar.gz -r 1')
 
243
        assert os.path.isfile('../first.tar.gz')
 
244
        self.runbzr('export ../first.tbz2 -r 1')
 
245
        assert os.path.isfile('../first.tbz2')
 
246
        self.runbzr('export ../first.tar.bz2 -r 1')
 
247
        assert os.path.isfile('../first.tar.bz2')
 
248
        self.runbzr('export ../first.tar.tbz2 -r 1')
 
249
        assert os.path.isfile('../first.tar.tbz2')
 
250
        from bz2 import BZ2File
 
251
        tf = TarFile('../first.tar.tbz2', 
 
252
                     fileobj=BZ2File('../first.tar.tbz2', 'r'))
 
253
        assert 'first.tar/hello' in tf.getnames(), tf.getnames()
 
254
        self.assertEqual(tf.extractfile('first.tar/hello').read(), 'foo')
 
255
        self.runbzr('export ../first2.tar -r 1 --root pizza')
 
256
        tf = TarFile('../first2.tar')
 
257
        assert 'pizza/hello' in tf.getnames(), tf.getnames()
 
258
 
 
259
    def test_diff(self):
 
260
        self.example_branch()
 
261
        file('hello', 'wt').write('hello world!')
 
262
        self.runbzr('commit -m fixing hello')
 
263
        output = self.runbzr('diff -r 2..3', backtick=1)
 
264
        self.assert_('\n+hello world!' in output)
 
265
        output = self.runbzr('diff -r last:3..last:1', backtick=1)
 
266
        self.assert_('\n+baz' in output)
 
267
 
 
268
    def test_branch(self):
 
269
        """Branch from one branch to another."""
 
270
        os.mkdir('a')
 
271
        os.chdir('a')
 
272
        self.example_branch()
 
273
        os.chdir('..')
 
274
        self.runbzr('branch a b')
 
275
        self.runbzr('branch a c -r 1')
 
276
        os.chdir('b')
 
277
        self.runbzr('commit -m foo --unchanged')
 
278
        os.chdir('..')
 
279
        # naughty - abstraction violations RBC 20050928  
 
280
        print "test_branch used to delete the stores, how is this meant to work ?"
 
281
        #shutil.rmtree('a/.bzr/revision-store')
 
282
        #shutil.rmtree('a/.bzr/inventory-store', ignore_errors=True)
 
283
        #shutil.rmtree('a/.bzr/text-store', ignore_errors=True)
 
284
        self.runbzr('branch a d --basis b')
 
285
 
 
286
    def test_merge(self):
 
287
        from bzrlib.branch import Branch
 
288
        
 
289
        os.mkdir('a')
 
290
        os.chdir('a')
 
291
        self.example_branch()
 
292
        os.chdir('..')
 
293
        self.runbzr('branch a b')
 
294
        os.chdir('b')
 
295
        file('goodbye', 'wt').write('quux')
 
296
        self.runbzr(['commit',  '-m',  "more u's are always good"])
 
297
 
 
298
        os.chdir('../a')
 
299
        file('hello', 'wt').write('quuux')
 
300
        # We can't merge when there are in-tree changes
 
301
        self.runbzr('merge ../b', retcode=1)
 
302
        self.runbzr(['commit', '-m', "Like an epidemic of u's"])
 
303
        self.runbzr('merge ../b')
 
304
        self.check_file_contents('goodbye', 'quux')
 
305
        # Merging a branch pulls its revision into the tree
 
306
        a = Branch.open('.')
 
307
        b = Branch.open('../b')
 
308
        a.get_revision_xml(b.last_revision())
 
309
        self.log('pending merges: %s', a.pending_merges())
 
310
        #        assert a.pending_merges() == [b.last_revision()], "Assertion %s %s" \
 
311
        #        % (a.pending_merges(), b.last_patch())
 
312
 
 
313
    def test_merge_with_missing_file(self):
 
314
        """Merge handles missing file conflicts"""
 
315
        os.mkdir('a')
 
316
        os.chdir('a')
 
317
        os.mkdir('sub')
 
318
        print >> file('sub/a.txt', 'wb'), "hello"
 
319
        print >> file('b.txt', 'wb'), "hello"
 
320
        print >> file('sub/c.txt', 'wb'), "hello"
 
321
        self.runbzr('init')
 
322
        self.runbzr('add')
 
323
        self.runbzr(('commit', '-m', 'added a'))
 
324
        self.runbzr('branch . ../b')
 
325
        print >> file('sub/a.txt', 'ab'), "there"
 
326
        print >> file('b.txt', 'ab'), "there"
 
327
        print >> file('sub/c.txt', 'ab'), "there"
 
328
        self.runbzr(('commit', '-m', 'Added there'))
 
329
        os.unlink('sub/a.txt')
 
330
        os.unlink('sub/c.txt')
 
331
        os.rmdir('sub')
 
332
        os.unlink('b.txt')
 
333
        self.runbzr(('commit', '-m', 'Removed a.txt'))
 
334
        os.chdir('../b')
 
335
        print >> file('sub/a.txt', 'ab'), "something"
 
336
        print >> file('b.txt', 'ab'), "something"
 
337
        print >> file('sub/c.txt', 'ab'), "something"
 
338
        self.runbzr(('commit', '-m', 'Modified a.txt'))
 
339
        self.runbzr('merge ../a/')
 
340
        assert os.path.exists('sub/a.txt.THIS')
 
341
        assert os.path.exists('sub/a.txt.BASE')
 
342
        os.chdir('../a')
 
343
        self.runbzr('merge ../b/')
 
344
        assert os.path.exists('sub/a.txt.OTHER')
 
345
        assert os.path.exists('sub/a.txt.BASE')
 
346
 
 
347
    def test_pull(self):
 
348
        """Pull changes from one branch to another."""
 
349
        os.mkdir('a')
 
350
        os.chdir('a')
 
351
 
 
352
        self.example_branch()
 
353
        self.runbzr('pull', retcode=1)
 
354
        self.runbzr('missing', retcode=1)
 
355
        self.runbzr('missing .')
 
356
        self.runbzr('missing')
 
357
        self.runbzr('pull')
 
358
        self.runbzr('pull /', retcode=1)
 
359
        self.runbzr('pull')
 
360
 
 
361
        os.chdir('..')
 
362
        self.runbzr('branch a b')
 
363
        os.chdir('b')
 
364
        self.runbzr('pull')
 
365
        os.mkdir('subdir')
 
366
        self.runbzr('add subdir')
 
367
        self.runbzr('commit -m blah --unchanged')
 
368
        os.chdir('../a')
 
369
        a = Branch.open('.')
 
370
        b = Branch.open('../b')
 
371
        assert a.revision_history() == b.revision_history()[:-1]
 
372
        self.runbzr('pull ../b')
 
373
        assert a.revision_history() == b.revision_history()
 
374
        self.runbzr('commit -m blah2 --unchanged')
 
375
        os.chdir('../b')
 
376
        self.runbzr('commit -m blah3 --unchanged')
 
377
        self.runbzr('pull ../a', retcode=1)
 
378
        print "DECIDE IF PULL CAN CONVERGE, blackbox.py"
 
379
        return
 
380
        os.chdir('../a')
 
381
        self.runbzr('merge ../b')
 
382
        self.runbzr('commit -m blah4 --unchanged')
 
383
        os.chdir('../b/subdir')
 
384
        self.runbzr('pull ../../a')
 
385
        assert a.revision_history()[-1] == b.revision_history()[-1]
 
386
        self.runbzr('commit -m blah5 --unchanged')
 
387
        self.runbzr('commit -m blah6 --unchanged')
 
388
        os.chdir('..')
 
389
        self.runbzr('pull ../a')
 
390
        os.chdir('../a')
 
391
        self.runbzr('commit -m blah7 --unchanged')
 
392
        self.runbzr('merge ../b')
 
393
        self.runbzr('commit -m blah8 --unchanged')
 
394
        self.runbzr('pull ../b')
 
395
        self.runbzr('pull ../b')
 
396
 
 
397
    def test_locations(self):
 
398
        """Using and remembering different locations"""
 
399
        os.mkdir('a')
 
400
        os.chdir('a')
 
401
        self.runbzr('init')
 
402
        self.runbzr('commit -m unchanged --unchanged')
 
403
        self.runbzr('pull', retcode=1)
 
404
        self.runbzr('merge', retcode=1)
 
405
        self.runbzr('branch . ../b')
 
406
        os.chdir('../b')
 
407
        self.runbzr('pull')
 
408
        self.runbzr('branch . ../c')
 
409
        self.runbzr('pull ../c')
 
410
        self.runbzr('merge')
 
411
        os.chdir('../a')
 
412
        self.runbzr('pull ../b')
 
413
        self.runbzr('pull')
 
414
        self.runbzr('pull ../c')
 
415
        self.runbzr('branch ../c ../d')
 
416
        shutil.rmtree('../c')
 
417
        self.runbzr('pull')
 
418
        os.chdir('../b')
 
419
        self.runbzr('pull')
 
420
        os.chdir('../d')
 
421
        self.runbzr('pull', retcode=1)
 
422
        self.runbzr('pull ../a --remember')
 
423
        self.runbzr('pull')
 
424
        
 
425
    def test_add_reports(self):
 
426
        """add command prints the names of added files."""
 
427
        b = Branch.initialize('.')
 
428
        self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
 
429
        out = self.run_bzr_captured(['add'], retcode = 0)[0]
 
430
        # the ordering is not defined at the moment
 
431
        results = sorted(out.rstrip('\n').split('\n'))
 
432
        self.assertEquals(['added dir',
 
433
                           'added dir'+os.sep+'sub.txt',
 
434
                           'added top.txt',],
 
435
                          results)
 
436
 
 
437
    def test_unknown_command(self):
 
438
        """Handling of unknown command."""
 
439
        out, err = self.run_bzr_captured(['fluffy-badger'],
 
440
                                         retcode=1)
 
441
        self.assertEquals(out, '')
 
442
        err.index('unknown command')
 
443
 
 
444
    def test_conflicts(self):
 
445
        """Handling of merge conflicts"""
 
446
        os.mkdir('base')
 
447
        os.chdir('base')
 
448
        file('hello', 'wb').write("hi world")
 
449
        file('answer', 'wb').write("42")
 
450
        self.runbzr('init')
 
451
        self.runbzr('add')
 
452
        self.runbzr('commit -m base')
 
453
        self.runbzr('branch . ../other')
 
454
        self.runbzr('branch . ../this')
 
455
        os.chdir('../other')
 
456
        file('hello', 'wb').write("Hello.")
 
457
        file('answer', 'wb').write("Is anyone there?")
 
458
        self.runbzr('commit -m other')
 
459
        os.chdir('../this')
 
460
        file('hello', 'wb').write("Hello, world")
 
461
        self.runbzr('mv answer question')
 
462
        file('question', 'wb').write("What do you get when you multiply six"
 
463
                                   "times nine?")
 
464
        self.runbzr('commit -m this')
 
465
        self.runbzr('merge ../other')
 
466
        result = self.runbzr('conflicts', backtick=1)
 
467
        self.assertEquals(result, "hello\nquestion\n")
 
468
        result = self.runbzr('status', backtick=1)
 
469
        assert "conflicts:\n  hello\n  question\n" in result, result
 
470
        self.runbzr('resolve hello')
 
471
        result = self.runbzr('conflicts', backtick=1)
 
472
        self.assertEquals(result, "question\n")
 
473
        self.runbzr('commit -m conflicts', retcode=1)
 
474
        self.runbzr('resolve --all')
 
475
        result = self.runbzr('conflicts', backtick=1)
 
476
        self.runbzr('commit -m conflicts')
 
477
        self.assertEquals(result, "")
 
478
 
 
479
def listdir_sorted(dir):
 
480
    L = os.listdir(dir)
 
481
    L.sort()
 
482
    return L
138
483
 
139
484
 
140
485
class OldTests(ExternalBase):
141
 
    # old tests moved from ./testbzr
142
 
    def runTest(self):
 
486
    """old tests moved from ./testbzr."""
 
487
 
 
488
    def test_bzr(self):
143
489
        from os import chdir, mkdir
144
490
        from os.path import exists
145
 
        import os
146
491
 
147
492
        runbzr = self.runbzr
148
 
        backtick = self.backtick
 
493
        capture = self.capture
149
494
        progress = self.log
150
495
 
151
496
        progress("basic branch creation")
153
498
        chdir('branch1')
154
499
        runbzr('init')
155
500
 
156
 
        self.assertEquals(backtick('bzr root').rstrip(),
 
501
        self.assertEquals(capture('root').rstrip(),
157
502
                          os.path.join(self.test_dir, 'branch1'))
158
503
 
159
504
        progress("status of new file")
162
507
        f.write('hello world!\n')
163
508
        f.close()
164
509
 
165
 
        out = backtick("bzr unknowns")
166
 
        self.assertEquals(out, 'test.txt\n')
 
510
        self.assertEquals(capture('unknowns'), 'test.txt\n')
167
511
 
168
 
        out = backtick("bzr status")
 
512
        out = capture("status")
169
513
        assert out == 'unknown:\n  test.txt\n'
170
514
 
171
 
        out = backtick("bzr status --all")
 
515
        out = capture("status --all")
172
516
        assert out == "unknown:\n  test.txt\n"
173
517
 
174
 
        out = backtick("bzr status test.txt --all")
 
518
        out = capture("status test.txt --all")
175
519
        assert out == "unknown:\n  test.txt\n"
176
520
 
177
521
        f = file('test2.txt', 'wt')
178
522
        f.write('goodbye cruel world...\n')
179
523
        f.close()
180
524
 
181
 
        out = backtick("bzr status test.txt")
 
525
        out = capture("status test.txt")
182
526
        assert out == "unknown:\n  test.txt\n"
183
527
 
184
 
        out = backtick("bzr status")
 
528
        out = capture("status")
185
529
        assert out == ("unknown:\n"
186
530
                       "  test.txt\n"
187
531
                       "  test2.txt\n")
189
533
        os.unlink('test2.txt')
190
534
 
191
535
        progress("command aliases")
192
 
        out = backtick("bzr st --all")
 
536
        out = capture("st --all")
193
537
        assert out == ("unknown:\n"
194
538
                       "  test.txt\n")
195
539
 
196
 
        out = backtick("bzr stat")
 
540
        out = capture("stat")
197
541
        assert out == ("unknown:\n"
198
542
                       "  test.txt\n")
199
543
 
203
547
        runbzr("help commands")
204
548
        runbzr("help slartibartfast", 1)
205
549
 
206
 
        out = backtick("bzr help ci")
 
550
        out = capture("help ci")
207
551
        out.index('aliases: ')
208
552
 
209
553
        progress("can't rename unversioned file")
212
556
        progress("adding a file")
213
557
 
214
558
        runbzr("add test.txt")
215
 
        assert backtick("bzr unknowns") == ''
216
 
        assert backtick("bzr status --all") == ("added:\n"
 
559
        assert capture("unknowns") == ''
 
560
        assert capture("status --all") == ("added:\n"
217
561
                                                "  test.txt\n")
218
562
 
219
563
        progress("rename newly-added file")
221
565
        assert os.path.exists("hello.txt")
222
566
        assert not os.path.exists("test.txt")
223
567
 
224
 
        assert backtick("bzr revno") == '0\n'
 
568
        assert capture("revno") == '0\n'
225
569
 
226
570
        progress("add first revision")
227
571
        runbzr(['commit', '-m', 'add first revision'])
235
579
        runbzr("add sub1")
236
580
        runbzr("rename sub1 sub2")
237
581
        runbzr("move hello.txt sub2")
238
 
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
582
        assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
239
583
 
240
584
        assert exists("sub2")
241
585
        assert exists("sub2/hello.txt")
256
600
        runbzr(['commit', '-m', 'rename nested subdirectories'])
257
601
 
258
602
        chdir('sub1/sub2')
259
 
        self.assertEquals(backtick('bzr root')[:-1],
 
603
        self.assertEquals(capture('root')[:-1],
260
604
                          os.path.join(self.test_dir, 'branch1'))
261
605
        runbzr('move ../hello.txt .')
262
606
        assert exists('./hello.txt')
263
 
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
264
 
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
607
        self.assertEquals(capture('relpath hello.txt'),
 
608
                          os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
 
609
        assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
265
610
        runbzr(['commit', '-m', 'move to parent directory'])
266
611
        chdir('..')
267
 
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
612
        assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
268
613
 
269
614
        runbzr('move sub2/hello.txt .')
270
615
        assert exists('hello.txt')
279
624
 
280
625
        runbzr('commit -F msg.tmp')
281
626
 
282
 
        assert backtick('bzr revno') == '5\n'
 
627
        assert capture('revno') == '5\n'
283
628
        runbzr('export -r 5 export-5.tmp')
284
629
        runbzr('export export.tmp')
285
630
 
287
632
        runbzr('log -v')
288
633
        runbzr('log -v --forward')
289
634
        runbzr('log -m', retcode=1)
290
 
        log_out = backtick('bzr log -m commit')
 
635
        log_out = capture('log -m commit')
291
636
        assert "this is my new commit" in log_out
292
637
        assert "rename nested" not in log_out
293
638
        assert 'revision-id' not in log_out
294
 
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
639
        assert 'revision-id' in capture('log --show-ids -m commit')
295
640
 
296
641
 
297
642
        progress("file with spaces in name")
307
652
 
308
653
        runbzr('info')
309
654
 
310
 
 
311
 
 
312
 
 
313
 
 
314
 
 
315
 
        chdir('..')
316
 
        chdir('..')
317
 
        progress('branch')
318
 
        assert os.path.exists('branch1')
319
 
        assert not os.path.exists('branch2')
320
 
        # Can't create a branch if it already exists
321
 
        runbzr('branch branch1', retcode=1)
322
 
        # Can't create a branch if its parent doesn't exist
323
 
        runbzr('branch /unlikely/to/exist', retcode=1)
324
 
        runbzr('branch branch1 branch2')
325
 
        assert exists('branch2')
326
 
        assert exists('branch2/sub1')
327
 
        assert exists('branch2/sub1/hello.txt')
328
 
        
329
 
        runbzr('branch --revision 0 branch1 branch3')
330
 
        assert not exists('branch3/sub1/hello.txt')
331
 
        runbzr('branch --revision 0..3 branch1 branch4', retcode=1)
332
 
 
333
 
        progress("pull")
334
 
        chdir('branch1')
335
 
        runbzr('pull', retcode=1)
336
 
        runbzr('pull ../branch2')
337
 
        chdir('.bzr')
338
 
        runbzr('pull')
339
 
        runbzr('commit --unchanged -m empty')
340
 
        runbzr('pull')
341
 
        chdir('../../branch2')
342
 
        runbzr('pull')
343
 
        runbzr('commit --unchanged -m empty')
344
 
        chdir('../branch1')
345
 
        runbzr('commit --unchanged -m empty')
346
 
        runbzr('pull', retcode=1)
347
 
        chdir ('..')
348
 
 
349
 
        progress('status after remove')
350
 
        mkdir('status-after-remove')
351
 
        # see mail from William Dodé, 2005-05-25
352
 
        # $ bzr init; touch a; bzr add a; bzr commit -m "add a"
353
 
        #     * looking for changes...
354
 
        #     added a
355
 
        #     * commited r1
356
 
        #     $ bzr remove a
357
 
        #     $ bzr status
358
 
        #     bzr: local variable 'kind' referenced before assignment
359
 
        #     at /vrac/python/bazaar-ng/bzrlib/diff.py:286 in compare_trees()
360
 
        #     see ~/.bzr.log for debug information
361
 
        chdir('status-after-remove')
362
 
        runbzr('init')
363
 
        file('a', 'w').write('foo')
364
 
        runbzr('add a')
365
 
        runbzr(['commit', '-m', 'add a'])
366
 
        runbzr('remove a')
367
 
        runbzr('status')
368
 
 
369
 
        chdir('..')
370
 
 
371
 
 
372
 
 
373
 
        progress("recursive and non-recursive add")
374
 
        mkdir('no-recurse')
375
 
        chdir('no-recurse')
376
 
        runbzr('init')
377
 
        mkdir('foo')
378
 
        fp = os.path.join('foo', 'test.txt')
379
 
        f = file(fp, 'w')
380
 
        f.write('hello!\n')
381
 
        f.close()
382
 
        runbzr('add --no-recurse foo')
383
 
        runbzr('file-id foo')
384
 
        runbzr('file-id ' + fp, 1)      # not versioned yet
385
 
        runbzr('commit -m add-dir-only')
386
 
 
387
 
        self.runbzr('file-id ' + fp, 1)      # still not versioned 
388
 
 
389
 
        self.runbzr('add foo')
390
 
        self.runbzr('file-id ' + fp)
391
 
        self.runbzr('commit -m add-sub-file')
392
 
 
393
 
        chdir('..')
394
 
 
395
 
 
396
 
 
397
 
class RevertCommand(ExternalBase):
398
 
    def runTest(self):
399
 
        self.runbzr('init')
400
 
 
401
 
        file('hello', 'wt').write('foo')
402
 
        self.runbzr('add hello')
403
 
        self.runbzr('commit -m setup hello')
404
 
        
405
 
        file('hello', 'wt').write('bar')
406
 
        self.runbzr('revert hello')
407
 
        self.check_file_contents('hello', 'foo')
408
 
 
 
655
        if has_symlinks():
 
656
            progress("symlinks")
 
657
            mkdir('symlinks')
 
658
            chdir('symlinks')
 
659
            runbzr('init')
 
660
            os.symlink("NOWHERE1", "link1")
 
661
            runbzr('add link1')
 
662
            assert self.capture('unknowns') == ''
 
663
            runbzr(['commit', '-m', '1: added symlink link1'])
 
664
    
 
665
            mkdir('d1')
 
666
            runbzr('add d1')
 
667
            assert self.capture('unknowns') == ''
 
668
            os.symlink("NOWHERE2", "d1/link2")
 
669
            assert self.capture('unknowns') == 'd1/link2\n'
 
670
            # is d1/link2 found when adding d1
 
671
            runbzr('add d1')
 
672
            assert self.capture('unknowns') == ''
 
673
            os.symlink("NOWHERE3", "d1/link3")
 
674
            assert self.capture('unknowns') == 'd1/link3\n'
 
675
            runbzr(['commit', '-m', '2: added dir, symlink'])
 
676
    
 
677
            runbzr('rename d1 d2')
 
678
            runbzr('move d2/link2 .')
 
679
            runbzr('move link1 d2')
 
680
            assert os.readlink("./link2") == "NOWHERE2"
 
681
            assert os.readlink("d2/link1") == "NOWHERE1"
 
682
            runbzr('add d2/link3')
 
683
            runbzr('diff')
 
684
            runbzr(['commit', '-m', '3: rename of dir, move symlinks, add link3'])
 
685
    
 
686
            os.unlink("link2")
 
687
            os.symlink("TARGET 2", "link2")
 
688
            os.unlink("d2/link1")
 
689
            os.symlink("TARGET 1", "d2/link1")
 
690
            runbzr('diff')
 
691
            assert self.capture("relpath d2/link1") == "d2/link1\n"
 
692
            runbzr(['commit', '-m', '4: retarget of two links'])
 
693
    
 
694
            runbzr('remove d2/link1')
 
695
            assert self.capture('unknowns') == 'd2/link1\n'
 
696
            runbzr(['commit', '-m', '5: remove d2/link1'])
 
697
            # try with the rm alias
 
698
            runbzr('add d2/link1')
 
699
            runbzr(['commit', '-m', '6: add d2/link1'])
 
700
            runbzr('rm d2/link1')
 
701
            assert self.capture('unknowns') == 'd2/link1\n'
 
702
            runbzr(['commit', '-m', '7: remove d2/link1'])
 
703
    
 
704
            os.mkdir("d1")
 
705
            runbzr('add d1')
 
706
            runbzr('rename d2/link3 d1/link3new')
 
707
            assert self.capture('unknowns') == 'd2/link1\n'
 
708
            runbzr(['commit', '-m', '8: remove d2/link1, move/rename link3'])
 
709
            
 
710
            runbzr(['check'])
 
711
            
 
712
            runbzr(['export', '-r', '1', 'exp1.tmp'])
 
713
            chdir("exp1.tmp")
 
714
            assert listdir_sorted(".") == [ "link1" ]
 
715
            assert os.readlink("link1") == "NOWHERE1"
 
716
            chdir("..")
 
717
            
 
718
            runbzr(['export', '-r', '2', 'exp2.tmp'])
 
719
            chdir("exp2.tmp")
 
720
            assert listdir_sorted(".") == [ "d1", "link1" ]
 
721
            chdir("..")
 
722
            
 
723
            runbzr(['export', '-r', '3', 'exp3.tmp'])
 
724
            chdir("exp3.tmp")
 
725
            assert listdir_sorted(".") == [ "d2", "link2" ]
 
726
            assert listdir_sorted("d2") == [ "link1", "link3" ]
 
727
            assert os.readlink("d2/link1") == "NOWHERE1"
 
728
            assert os.readlink("link2")    == "NOWHERE2"
 
729
            chdir("..")
 
730
            
 
731
            runbzr(['export', '-r', '4', 'exp4.tmp'])
 
732
            chdir("exp4.tmp")
 
733
            assert listdir_sorted(".") == [ "d2", "link2" ]
 
734
            assert os.readlink("d2/link1") == "TARGET 1"
 
735
            assert os.readlink("link2")    == "TARGET 2"
 
736
            assert listdir_sorted("d2") == [ "link1", "link3" ]
 
737
            chdir("..")
 
738
            
 
739
            runbzr(['export', '-r', '5', 'exp5.tmp'])
 
740
            chdir("exp5.tmp")
 
741
            assert listdir_sorted(".") == [ "d2", "link2" ]
 
742
            assert os.path.islink("link2")
 
743
            assert listdir_sorted("d2")== [ "link3" ]
 
744
            chdir("..")
 
745
            
 
746
            runbzr(['export', '-r', '8', 'exp6.tmp'])
 
747
            chdir("exp6.tmp")
 
748
            self.assertEqual(listdir_sorted("."), [ "d1", "d2", "link2"])
 
749
            assert listdir_sorted("d1") == [ "link3new" ]
 
750
            assert listdir_sorted("d2") == []
 
751
            assert os.readlink("d1/link3new") == "NOWHERE3"
 
752
            chdir("..")
 
753
        else:
 
754
            progress("skipping symlink tests")
 
755
 
 
756
 
 
757
class HttpTests(TestCaseWithWebserver):
 
758
    """Test bzr ui commands against remote branches."""
 
759
 
 
760
    def test_branch(self):
 
761
        os.mkdir('from')
 
762
        branch = Branch.initialize('from')
 
763
        branch.commit('empty commit for nonsense', allow_pointless=True)
 
764
        url = self.get_remote_url('from')
 
765
        self.run_bzr('branch', url, 'to')
 
766
        branch = Branch.open('to')
 
767
        self.assertEqual(1, len(branch.revision_history()))