~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

  • Committer: Martin Pool
  • Date: 2005-06-22 07:18:01 UTC
  • Revision ID: mbp@sourcefrog.net-20050622071801-916bbae5bd93cf0a
- new mkdir command

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
it's normally invoked.
27
27
"""
28
28
 
29
 
import sys
30
 
 
31
 
from bzrlib.selftest import InTempDir, BzrTestBase
32
 
 
33
 
 
34
 
 
35
 
class ExternalBase(InTempDir):
36
 
    def runbzr(self, args, retcode=0,backtick=False):
37
 
        try:
38
 
            import shutil
39
 
            from subprocess import call
40
 
        except ImportError, e:
41
 
            _need_subprocess()
42
 
            raise
43
 
 
44
 
        if isinstance(args, basestring):
45
 
            args = args.split()
46
 
 
47
 
        if backtick:
48
 
            return self.backtick(['python', self.BZRPATH,] + args,
49
 
                           retcode=retcode)
50
 
        else:
51
 
            return self.runcmd(['python', self.BZRPATH,] + args,
52
 
                           retcode=retcode)
53
 
 
54
 
 
55
 
 
56
 
class MvCommand(BzrTestBase):
57
 
    def runbzr(self):
58
 
        """Test two modes of operation for mv"""
59
 
        b = Branch('.', init=True)
60
 
        self.build_tree(['a', 'c', 'subdir/'])
61
 
        self.run_bzr('mv', 'a', 'b')
62
 
        self.run_bzr('mv', 'b', 'subdir')
63
 
        self.run_bzr('mv', 'subdir/b', 'a')
64
 
        self.run_bzr('mv', 'a', 'b', 'subdir')
65
 
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
66
 
 
67
 
 
68
 
 
69
 
class TestVersion(BzrTestBase):
70
 
    """Check output from version command and master option is reasonable"""
 
29
# this code was previously in testbzr
 
30
 
 
31
from unittest import TestCase
 
32
from bzrlib.selftest import TestBase, InTempDir
 
33
 
 
34
class TestVersion(TestBase):
71
35
    def runTest(self):
72
36
        # output is intentionally passed through to stdout so that we
73
37
        # can see the version being tested
74
 
        from cStringIO import StringIO
75
 
        save_out = sys.stdout
76
 
        try:
77
 
            sys.stdout = tmp_out = StringIO()
78
 
            
79
 
            self.run_bzr('version')
80
 
        finally:
81
 
            sys.stdout = save_out
82
 
 
83
 
        output = tmp_out.getvalue()
84
 
        self.log('bzr version output:')
85
 
        self.log(output)
86
 
        
87
 
        self.assert_(output.startswith('bzr (bazaar-ng) '))
88
 
        self.assertNotEqual(output.index('Canonical'), -1)
89
 
 
90
 
        # make sure --version is consistent
91
 
        try:
92
 
            sys.stdout = tmp_out = StringIO()
93
 
            
94
 
            self.run_bzr('--version')
95
 
        finally:
96
 
            sys.stdout = save_out
97
 
 
98
 
        self.log('bzr --version output:')
99
 
        self.log(tmp_out.getvalue())
100
 
 
101
 
        self.assertEquals(output, tmp_out.getvalue())
102
 
 
103
 
 
104
 
        
105
 
 
106
 
 
107
 
class HelpCommands(ExternalBase):
 
38
        self.runcmd(['bzr', 'version'])
 
39
 
 
40
 
 
41
 
 
42
class HelpCommands(TestBase):
108
43
    def runTest(self):
109
 
        self.runbzr('--help')
110
 
        self.runbzr('help')
111
 
        self.runbzr('help commands')
112
 
        self.runbzr('help help')
113
 
        self.runbzr('commit -h')
114
 
 
115
 
 
116
 
class InitBranch(ExternalBase):
 
44
        self.runcmd('bzr --help')
 
45
        self.runcmd('bzr help')
 
46
        self.runcmd('bzr help commands')
 
47
        self.runcmd('bzr help help')
 
48
        self.runcmd('bzr commit -h')
 
49
 
 
50
 
 
51
class InitBranch(InTempDir):
117
52
    def runTest(self):
118
53
        import os
119
 
        self.runbzr(['init'])
120
 
 
121
 
 
122
 
class UserIdentity(ExternalBase):
 
54
        self.runcmd(['bzr', 'init'])
 
55
 
 
56
 
 
57
 
 
58
class UserIdentity(InTempDir):
123
59
    def runTest(self):
124
60
        # this should always identify something, if only "john@localhost"
125
 
        self.runbzr("whoami")
126
 
        self.runbzr("whoami --email")
127
 
 
128
 
        self.assertEquals(self.runbzr("whoami --email",
129
 
                                      backtick=True).count('@'), 1)
130
 
        
131
 
class UserIdentityBranch(ExternalBase):
132
 
    def runTest(self):
133
 
        # tests branch specific user identity
134
 
        self.runbzr('init')
135
 
        f = file('.bzr/email', 'wt')
136
 
        f.write('Branch Identity <branch@identi.ty>')
137
 
        f.close()
138
 
        whoami = self.runbzr("whoami",backtick=True)
139
 
        whoami_email = self.runbzr("whoami --email",backtick=True)
140
 
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
141
 
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
142
 
 
143
 
 
144
 
class InvalidCommands(ExternalBase):
145
 
    def runTest(self):
146
 
        self.runbzr("pants", retcode=1)
147
 
        self.runbzr("--pants off", retcode=1)
148
 
        self.runbzr("diff --message foo", retcode=1)
149
 
 
150
 
 
151
 
 
152
 
class EmptyCommit(ExternalBase):
153
 
    def runTest(self):
154
 
        self.runbzr("init")
155
 
        self.build_tree(['hello.txt'])
156
 
        self.runbzr("commit -m empty", retcode=1)
157
 
        self.runbzr("add hello.txt")
158
 
        self.runbzr("commit -m added")
159
 
 
160
 
 
161
 
 
162
 
class IgnorePatterns(ExternalBase):
163
 
    def runTest(self):
164
 
        from bzrlib.branch import Branch
165
 
        
166
 
        b = Branch('.', init=True)
167
 
        self.assertEquals(list(b.unknowns()), [])
168
 
 
169
 
        file('foo.tmp', 'wt').write('tmp files are ignored')
170
 
        self.assertEquals(list(b.unknowns()), [])
171
 
        assert self.backtick('bzr unknowns') == ''
172
 
 
173
 
        file('foo.c', 'wt').write('int main() {}')
174
 
        self.assertEquals(list(b.unknowns()), ['foo.c'])
175
 
        assert self.backtick('bzr unknowns') == 'foo.c\n'
176
 
 
177
 
        self.runbzr(['add', 'foo.c'])
178
 
        assert self.backtick('bzr unknowns') == ''
179
 
 
180
 
        # 'ignore' works when creating the .bzignore file
181
 
        file('foo.blah', 'wt').write('blah')
182
 
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
183
 
        self.runbzr('ignore *.blah')
184
 
        self.assertEquals(list(b.unknowns()), [])
185
 
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
186
 
 
187
 
        # 'ignore' works when then .bzrignore file already exists
188
 
        file('garh', 'wt').write('garh')
189
 
        self.assertEquals(list(b.unknowns()), ['garh'])
190
 
        assert self.backtick('bzr unknowns') == 'garh\n'
191
 
        self.runbzr('ignore garh')
192
 
        self.assertEquals(list(b.unknowns()), [])
193
 
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
194
 
        
195
 
 
196
 
 
197
 
 
198
 
class OldTests(ExternalBase):
 
61
        self.runcmd("bzr whoami")
 
62
        self.runcmd("bzr whoami --email")
 
63
        self.assertEquals(self.backtick("bzr whoami --email").count('@'),
 
64
                          1)
 
65
 
 
66
 
 
67
class InvalidCommands(InTempDir):
 
68
    def runTest(self):
 
69
        self.runcmd("bzr pants", retcode=1)
 
70
        self.runcmd("bzr --pants off", retcode=1)
 
71
        self.runcmd("bzr diff --message foo", retcode=1)
 
72
 
 
73
 
 
74
 
 
75
class OldTests(InTempDir):
199
76
    # old tests moved from ./testbzr
200
77
    def runTest(self):
201
78
        from os import chdir, mkdir
202
79
        from os.path import exists
203
80
        import os
204
81
 
205
 
        runbzr = self.runbzr
 
82
        runcmd = self.runcmd
206
83
        backtick = self.backtick
207
84
        progress = self.log
208
85
 
209
86
        progress("basic branch creation")
210
 
        mkdir('branch1')
 
87
        runcmd(['mkdir', 'branch1'])
211
88
        chdir('branch1')
212
 
        runbzr('init')
 
89
        runcmd('bzr init')
213
90
 
214
91
        self.assertEquals(backtick('bzr root').rstrip(),
215
92
                          os.path.join(self.test_dir, 'branch1'))
221
98
        f.close()
222
99
 
223
100
        out = backtick("bzr unknowns")
224
 
        self.assertEquals(out, 'test.txt\n')
 
101
        assert out == 'test.txt\n'
225
102
 
226
103
        out = backtick("bzr status")
227
104
        assert out == 'unknown:\n  test.txt\n'
256
133
                       "  test.txt\n")
257
134
 
258
135
        progress("command help")
259
 
        runbzr("help st")
260
 
        runbzr("help")
261
 
        runbzr("help commands")
262
 
        runbzr("help slartibartfast", 1)
 
136
        runcmd("bzr help st")
 
137
        runcmd("bzr help")
 
138
        runcmd("bzr help commands")
 
139
        runcmd("bzr help slartibartfast", 1)
263
140
 
264
141
        out = backtick("bzr help ci")
265
142
        out.index('aliases: ')
266
143
 
267
144
        progress("can't rename unversioned file")
268
 
        runbzr("rename test.txt new-test.txt", 1)
 
145
        runcmd("bzr rename test.txt new-test.txt", 1)
269
146
 
270
147
        progress("adding a file")
271
148
 
272
 
        runbzr("add test.txt")
 
149
        runcmd("bzr add test.txt")
273
150
        assert backtick("bzr unknowns") == ''
274
151
        assert backtick("bzr status --all") == ("added:\n"
275
152
                                                "  test.txt\n")
276
153
 
277
154
        progress("rename newly-added file")
278
 
        runbzr("rename test.txt hello.txt")
 
155
        runcmd("bzr rename test.txt hello.txt")
279
156
        assert os.path.exists("hello.txt")
280
157
        assert not os.path.exists("test.txt")
281
158
 
282
159
        assert backtick("bzr revno") == '0\n'
283
160
 
284
161
        progress("add first revision")
285
 
        runbzr(['commit', '-m', 'add first revision'])
 
162
        runcmd(["bzr", "commit", "-m", 'add first revision'])
286
163
 
287
164
        progress("more complex renames")
288
165
        os.mkdir("sub1")
289
 
        runbzr("rename hello.txt sub1", 1)
290
 
        runbzr("rename hello.txt sub1/hello.txt", 1)
291
 
        runbzr("move hello.txt sub1", 1)
 
166
        runcmd("bzr rename hello.txt sub1", 1)
 
167
        runcmd("bzr rename hello.txt sub1/hello.txt", 1)
 
168
        runcmd("bzr move hello.txt sub1", 1)
292
169
 
293
 
        runbzr("add sub1")
294
 
        runbzr("rename sub1 sub2")
295
 
        runbzr("move hello.txt sub2")
 
170
        runcmd("bzr add sub1")
 
171
        runcmd("bzr rename sub1 sub2")
 
172
        runcmd("bzr move hello.txt sub2")
296
173
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
297
174
 
298
175
        assert exists("sub2")
300
177
        assert not exists("sub1")
301
178
        assert not exists("hello.txt")
302
179
 
303
 
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
 
180
        runcmd(['bzr', 'commit', '-m', 'commit with some things moved to subdirs'])
304
181
 
305
182
        mkdir("sub1")
306
 
        runbzr('add sub1')
307
 
        runbzr('move sub2/hello.txt sub1')
 
183
        runcmd('bzr add sub1')
 
184
        runcmd('bzr move sub2/hello.txt sub1')
308
185
        assert not exists('sub2/hello.txt')
309
186
        assert exists('sub1/hello.txt')
310
 
        runbzr('move sub2 sub1')
 
187
        runcmd('bzr move sub2 sub1')
311
188
        assert not exists('sub2')
312
189
        assert exists('sub1/sub2')
313
190
 
314
 
        runbzr(['commit', '-m', 'rename nested subdirectories'])
 
191
        runcmd(['bzr', 'commit', '-m', 'rename nested subdirectories'])
315
192
 
316
193
        chdir('sub1/sub2')
317
194
        self.assertEquals(backtick('bzr root')[:-1],
318
195
                          os.path.join(self.test_dir, 'branch1'))
319
 
        runbzr('move ../hello.txt .')
 
196
        runcmd('bzr move ../hello.txt .')
320
197
        assert exists('./hello.txt')
321
198
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
322
199
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
323
 
        runbzr(['commit', '-m', 'move to parent directory'])
 
200
        runcmd(['bzr', 'commit', '-m', 'move to parent directory'])
324
201
        chdir('..')
325
202
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
326
203
 
327
 
        runbzr('move sub2/hello.txt .')
 
204
        runcmd('bzr move sub2/hello.txt .')
328
205
        assert exists('hello.txt')
329
206
 
330
207
        f = file('hello.txt', 'wt')
335
212
        f.write('this is my new commit\n')
336
213
        f.close()
337
214
 
338
 
        runbzr('commit -F msg.tmp')
 
215
        runcmd('bzr commit -F msg.tmp')
339
216
 
340
217
        assert backtick('bzr revno') == '5\n'
341
 
        runbzr('export -r 5 export-5.tmp')
342
 
        runbzr('export export.tmp')
343
 
 
344
 
        runbzr('log')
345
 
        runbzr('log -v')
346
 
        runbzr('log -v --forward')
347
 
        runbzr('log -m', retcode=1)
348
 
        log_out = backtick('bzr log -m commit')
349
 
        assert "this is my new commit" in log_out
350
 
        assert "rename nested" not in log_out
351
 
        assert 'revision-id' not in log_out
352
 
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
218
        runcmd('bzr export -r 5 export-5.tmp')
 
219
        runcmd('bzr export export.tmp')
 
220
 
 
221
        runcmd('bzr log')
 
222
        runcmd('bzr log -v')
 
223
 
353
224
 
354
225
 
355
226
        progress("file with spaces in name")
356
227
        mkdir('sub directory')
357
228
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
358
 
        runbzr('add .')
359
 
        runbzr('diff')
360
 
        runbzr('commit -m add-spaces')
361
 
        runbzr('check')
362
 
 
363
 
        runbzr('log')
364
 
        runbzr('log --forward')
365
 
 
366
 
        runbzr('info')
367
 
 
368
 
 
369
 
 
370
 
 
371
 
 
372
 
 
373
 
class RevertCommand(ExternalBase):
374
 
    def runTest(self):
375
 
        self.runbzr('init')
376
 
 
377
 
        file('hello', 'wt').write('foo')
378
 
        self.runbzr('add hello')
379
 
        self.runbzr('commit -m setup hello')
380
 
 
381
 
        file('goodbye', 'wt').write('baz')
382
 
        self.runbzr('add goodbye')
383
 
        self.runbzr('commit -m setup goodbye')
 
229
        runcmd('bzr add .')
 
230
        runcmd('bzr diff')
 
231
        runcmd('bzr commit -m add-spaces')
 
232
        runcmd('bzr check')
 
233
 
 
234
        runcmd('bzr log')
 
235
        runcmd('bzr log --forward')
 
236
 
 
237
        runcmd('bzr info')
 
238
 
 
239
 
 
240
 
 
241
 
 
242
 
 
243
 
 
244
        chdir('..')
 
245
        chdir('..')
 
246
        progress('branch')
 
247
        # Can't create a branch if it already exists
 
248
        runcmd('bzr branch branch1', retcode=1)
 
249
        # Can't create a branch if its parent doesn't exist
 
250
        runcmd('bzr branch /unlikely/to/exist', retcode=1)
 
251
        runcmd('bzr branch branch1 branch2')
 
252
 
 
253
        progress("pull")
 
254
        chdir('branch1')
 
255
        runcmd('bzr pull', retcode=1)
 
256
        runcmd('bzr pull ../branch2')
 
257
        chdir('.bzr')
 
258
        runcmd('bzr pull')
 
259
        runcmd('bzr commit -m empty')
 
260
        runcmd('bzr pull')
 
261
        chdir('../../branch2')
 
262
        runcmd('bzr pull')
 
263
        runcmd('bzr commit -m empty')
 
264
        chdir('../branch1')
 
265
        runcmd('bzr commit -m empty')
 
266
        runcmd('bzr pull', retcode=1)
 
267
        chdir ('..')
 
268
 
 
269
        progress('status after remove')
 
270
        mkdir('status-after-remove')
 
271
        # see mail from William Dodé, 2005-05-25
 
272
        # $ bzr init; touch a; bzr add a; bzr commit -m "add a"
 
273
        #     * looking for changes...
 
274
        #     added a
 
275
        #     * commited r1
 
276
        #     $ bzr remove a
 
277
        #     $ bzr status
 
278
        #     bzr: local variable 'kind' referenced before assignment
 
279
        #     at /vrac/python/bazaar-ng/bzrlib/diff.py:286 in compare_trees()
 
280
        #     see ~/.bzr.log for debug information
 
281
        chdir('status-after-remove')
 
282
        runcmd('bzr init')
 
283
        file('a', 'w').write('foo')
 
284
        runcmd('bzr add a')
 
285
        runcmd(['bzr', 'commit', '-m', 'add a'])
 
286
        runcmd('bzr remove a')
 
287
        runcmd('bzr status')
 
288
 
 
289
        chdir('..')
 
290
 
 
291
        progress('ignore patterns')
 
292
        mkdir('ignorebranch')
 
293
        chdir('ignorebranch')
 
294
        runcmd('bzr init')
 
295
        assert backtick('bzr unknowns') == ''
 
296
 
 
297
        file('foo.tmp', 'wt').write('tmp files are ignored')
 
298
        assert backtick('bzr unknowns') == ''
 
299
 
 
300
        file('foo.c', 'wt').write('int main() {}')
 
301
        assert backtick('bzr unknowns') == 'foo.c\n'
 
302
        runcmd('bzr add foo.c')
 
303
        assert backtick('bzr unknowns') == ''
 
304
 
 
305
        # 'ignore' works when creating the .bzignore file
 
306
        file('foo.blah', 'wt').write('blah')
 
307
        assert backtick('bzr unknowns') == 'foo.blah\n'
 
308
        runcmd('bzr ignore *.blah')
 
309
        assert backtick('bzr unknowns') == ''
 
310
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
311
 
 
312
        # 'ignore' works when then .bzrignore file already exists
 
313
        file('garh', 'wt').write('garh')
 
314
        assert backtick('bzr unknowns') == 'garh\n'
 
315
        runcmd('bzr ignore garh')
 
316
        assert backtick('bzr unknowns') == ''
 
317
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
318
 
 
319
        chdir('..')
 
320
 
 
321
 
 
322
 
 
323
 
 
324
        progress("recursive and non-recursive add")
 
325
        mkdir('no-recurse')
 
326
        chdir('no-recurse')
 
327
        runcmd('bzr init')
 
328
        mkdir('foo')
 
329
        fp = os.path.join('foo', 'test.txt')
 
330
        f = file(fp, 'w')
 
331
        f.write('hello!\n')
 
332
        f.close()
 
333
        runcmd('bzr add --no-recurse foo')
 
334
        runcmd('bzr file-id foo')
 
335
        runcmd('bzr file-id ' + fp, 1)      # not versioned yet
 
336
        runcmd('bzr commit -m add-dir-only')
 
337
 
 
338
        runcmd('bzr file-id ' + fp, 1)      # still not versioned 
 
339
 
 
340
        runcmd('bzr add foo')
 
341
        runcmd('bzr file-id ' + fp)
 
342
        runcmd('bzr commit -m add-sub-file')
 
343
 
 
344
        chdir('..')
 
345
 
 
346
 
 
347
 
 
348
    
384
349
        
385
 
        file('hello', 'wt').write('bar')
386
 
        file('goodbye', 'wt').write('qux')
387
 
        self.runbzr('revert hello')
388
 
        self.check_file_contents('hello', 'foo')
389
 
        self.check_file_contents('goodbye', 'qux')
390
 
        self.runbzr('revert')
391
 
        self.check_file_contents('goodbye', 'baz')
392
 
 
 
350
 
 
351
 
 
352
# lists all tests from this module in the best order to run them.  we
 
353
# do it this way rather than just discovering them all because it
 
354
# allows us to test more basic functions first where failures will be
 
355
# easiest to understand.
 
356
 
 
357
def suite():
 
358
    from unittest import TestSuite
 
359
    s = TestSuite()
 
360
    s.addTests([TestVersion(),
 
361
                InitBranch(),
 
362
                HelpCommands(),
 
363
                UserIdentity(),
 
364
                InvalidCommands(),
 
365
                OldTests()])
 
366
    return s