~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

  • Committer: Martin Pool
  • Date: 2005-08-05 21:06:19 UTC
  • Revision ID: mbp@sourcefrog.net-20050805210619-87c2a27cdb61fef2
- merge aaron's merge improvements up to 
  aaron.bentley@utoronto.ca-20050805025423-c0434f52aa596d3e

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
it's normally invoked.
27
27
"""
28
28
 
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):
 
29
import sys
 
30
 
 
31
from bzrlib.selftest import TestBase, InTempDir, BzrTestBase
 
32
 
 
33
 
 
34
 
 
35
class ExternalBase(InTempDir):
 
36
    def runbzr(self, args, retcode=0):
 
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
        return self.runcmd(['python', self.BZRPATH,] + args,
 
48
                           retcode=retcode)
 
49
 
 
50
 
 
51
 
 
52
class MvCommand(BzrTestBase):
 
53
    def runbzr(self):
 
54
        """Test two modes of operation for mv"""
 
55
        b = Branch('.', init=True)
 
56
        self.build_tree(['a', 'c', 'subdir/'])
 
57
        self.run_bzr('mv', 'a', 'b')
 
58
        self.run_bzr('mv', 'b', 'subdir')
 
59
        self.run_bzr('mv', 'subdir/b', 'a')
 
60
        self.run_bzr('mv', 'a', 'b', 'subdir')
 
61
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
62
 
 
63
 
 
64
 
 
65
class TestVersion(BzrTestBase):
 
66
    """Check output from version command and master option is reasonable"""
35
67
    def runTest(self):
36
68
        # output is intentionally passed through to stdout so that we
37
69
        # can see the version being tested
38
 
        self.runcmd(['bzr', 'version'])
39
 
 
40
 
 
41
 
 
42
 
class HelpCommands(TestBase):
 
70
        from cStringIO import StringIO
 
71
        save_out = sys.stdout
 
72
        try:
 
73
            sys.stdout = tmp_out = StringIO()
 
74
            
 
75
            self.run_bzr('version')
 
76
        finally:
 
77
            sys.stdout = save_out
 
78
 
 
79
        output = tmp_out.getvalue()
 
80
        self.log('bzr version output:')
 
81
        self.log(output)
 
82
        
 
83
        self.assert_(output.startswith('bzr (bazaar-ng) '))
 
84
        self.assertNotEqual(output.index('Canonical'), -1)
 
85
 
 
86
        # make sure --version is consistent
 
87
        try:
 
88
            sys.stdout = tmp_out = StringIO()
 
89
            
 
90
            self.run_bzr('--version')
 
91
        finally:
 
92
            sys.stdout = save_out
 
93
 
 
94
        self.log('bzr --version output:')
 
95
        self.log(tmp_out.getvalue())
 
96
 
 
97
        self.assertEquals(output, tmp_out.getvalue())
 
98
 
 
99
 
 
100
        
 
101
 
 
102
 
 
103
class HelpCommands(ExternalBase):
43
104
    def runTest(self):
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):
 
105
        self.runbzr('--help')
 
106
        self.runbzr('help')
 
107
        self.runbzr('help commands')
 
108
        self.runbzr('help help')
 
109
        self.runbzr('commit -h')
 
110
 
 
111
 
 
112
class InitBranch(ExternalBase):
52
113
    def runTest(self):
53
114
        import os
54
 
        self.runcmd(['bzr', 'init'])
55
 
 
56
 
 
57
 
 
58
 
class UserIdentity(InTempDir):
 
115
        self.runbzr(['init'])
 
116
 
 
117
 
 
118
 
 
119
class UserIdentity(ExternalBase):
59
120
    def runTest(self):
60
121
        # this should always identify something, if only "john@localhost"
61
 
        self.runcmd("bzr whoami")
62
 
        self.runcmd("bzr whoami --email")
 
122
        self.runbzr("whoami")
 
123
        self.runbzr("whoami --email")
63
124
        self.assertEquals(self.backtick("bzr whoami --email").count('@'),
64
125
                          1)
65
126
 
66
127
 
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):
 
128
class InvalidCommands(ExternalBase):
 
129
    def runTest(self):
 
130
        self.runbzr("pants", retcode=1)
 
131
        self.runbzr("--pants off", retcode=1)
 
132
        self.runbzr("diff --message foo", retcode=1)
 
133
 
 
134
 
 
135
 
 
136
class EmptyCommit(ExternalBase):
 
137
    def runTest(self):
 
138
        self.runbzr("init")
 
139
        self.build_tree(['hello.txt'])
 
140
        self.runbzr("commit -m empty", retcode=1)
 
141
        self.runbzr("add hello.txt")
 
142
        self.runbzr("commit -m added")
 
143
 
 
144
 
 
145
 
 
146
class IgnorePatterns(ExternalBase):
 
147
    def runTest(self):
 
148
        from bzrlib.branch import Branch
 
149
        
 
150
        b = Branch('.', init=True)
 
151
        self.assertEquals(list(b.unknowns()), [])
 
152
 
 
153
        file('foo.tmp', 'wt').write('tmp files are ignored')
 
154
        self.assertEquals(list(b.unknowns()), [])
 
155
        assert self.backtick('bzr unknowns') == ''
 
156
 
 
157
        file('foo.c', 'wt').write('int main() {}')
 
158
        self.assertEquals(list(b.unknowns()), ['foo.c'])
 
159
        assert self.backtick('bzr unknowns') == 'foo.c\n'
 
160
 
 
161
        self.runbzr(['add', 'foo.c'])
 
162
        assert self.backtick('bzr unknowns') == ''
 
163
 
 
164
        # 'ignore' works when creating the .bzignore file
 
165
        file('foo.blah', 'wt').write('blah')
 
166
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
 
167
        self.runbzr('ignore *.blah')
 
168
        self.assertEquals(list(b.unknowns()), [])
 
169
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
170
 
 
171
        # 'ignore' works when then .bzrignore file already exists
 
172
        file('garh', 'wt').write('garh')
 
173
        self.assertEquals(list(b.unknowns()), ['garh'])
 
174
        assert self.backtick('bzr unknowns') == 'garh\n'
 
175
        self.runbzr('ignore garh')
 
176
        self.assertEquals(list(b.unknowns()), [])
 
177
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
178
        
 
179
 
 
180
 
 
181
 
 
182
class OldTests(ExternalBase):
76
183
    # old tests moved from ./testbzr
77
184
    def runTest(self):
78
185
        from os import chdir, mkdir
79
186
        from os.path import exists
80
187
        import os
81
188
 
82
 
        runcmd = self.runcmd
 
189
        runbzr = self.runbzr
83
190
        backtick = self.backtick
84
191
        progress = self.log
85
192
 
86
193
        progress("basic branch creation")
87
 
        runcmd(['mkdir', 'branch1'])
 
194
        mkdir('branch1')
88
195
        chdir('branch1')
89
 
        runcmd('bzr init')
 
196
        runbzr('init')
90
197
 
91
198
        self.assertEquals(backtick('bzr root').rstrip(),
92
199
                          os.path.join(self.test_dir, 'branch1'))
98
205
        f.close()
99
206
 
100
207
        out = backtick("bzr unknowns")
101
 
        assert out == 'test.txt\n'
 
208
        self.assertEquals(out, 'test.txt\n')
102
209
 
103
210
        out = backtick("bzr status")
104
211
        assert out == 'unknown:\n  test.txt\n'
133
240
                       "  test.txt\n")
134
241
 
135
242
        progress("command help")
136
 
        runcmd("bzr help st")
137
 
        runcmd("bzr help")
138
 
        runcmd("bzr help commands")
139
 
        runcmd("bzr help slartibartfast", 1)
 
243
        runbzr("help st")
 
244
        runbzr("help")
 
245
        runbzr("help commands")
 
246
        runbzr("help slartibartfast", 1)
140
247
 
141
248
        out = backtick("bzr help ci")
142
249
        out.index('aliases: ')
143
250
 
144
251
        progress("can't rename unversioned file")
145
 
        runcmd("bzr rename test.txt new-test.txt", 1)
 
252
        runbzr("rename test.txt new-test.txt", 1)
146
253
 
147
254
        progress("adding a file")
148
255
 
149
 
        runcmd("bzr add test.txt")
 
256
        runbzr("add test.txt")
150
257
        assert backtick("bzr unknowns") == ''
151
258
        assert backtick("bzr status --all") == ("added:\n"
152
259
                                                "  test.txt\n")
153
260
 
154
261
        progress("rename newly-added file")
155
 
        runcmd("bzr rename test.txt hello.txt")
 
262
        runbzr("rename test.txt hello.txt")
156
263
        assert os.path.exists("hello.txt")
157
264
        assert not os.path.exists("test.txt")
158
265
 
159
266
        assert backtick("bzr revno") == '0\n'
160
267
 
161
268
        progress("add first revision")
162
 
        runcmd(["bzr", "commit", "-m", 'add first revision'])
 
269
        runbzr(['commit', '-m', 'add first revision'])
163
270
 
164
271
        progress("more complex renames")
165
272
        os.mkdir("sub1")
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)
 
273
        runbzr("rename hello.txt sub1", 1)
 
274
        runbzr("rename hello.txt sub1/hello.txt", 1)
 
275
        runbzr("move hello.txt sub1", 1)
169
276
 
170
 
        runcmd("bzr add sub1")
171
 
        runcmd("bzr rename sub1 sub2")
172
 
        runcmd("bzr move hello.txt sub2")
 
277
        runbzr("add sub1")
 
278
        runbzr("rename sub1 sub2")
 
279
        runbzr("move hello.txt sub2")
173
280
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
174
281
 
175
282
        assert exists("sub2")
177
284
        assert not exists("sub1")
178
285
        assert not exists("hello.txt")
179
286
 
180
 
        runcmd(['bzr', 'commit', '-m', 'commit with some things moved to subdirs'])
 
287
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
181
288
 
182
289
        mkdir("sub1")
183
 
        runcmd('bzr add sub1')
184
 
        runcmd('bzr move sub2/hello.txt sub1')
 
290
        runbzr('add sub1')
 
291
        runbzr('move sub2/hello.txt sub1')
185
292
        assert not exists('sub2/hello.txt')
186
293
        assert exists('sub1/hello.txt')
187
 
        runcmd('bzr move sub2 sub1')
 
294
        runbzr('move sub2 sub1')
188
295
        assert not exists('sub2')
189
296
        assert exists('sub1/sub2')
190
297
 
191
 
        runcmd(['bzr', 'commit', '-m', 'rename nested subdirectories'])
 
298
        runbzr(['commit', '-m', 'rename nested subdirectories'])
192
299
 
193
300
        chdir('sub1/sub2')
194
301
        self.assertEquals(backtick('bzr root')[:-1],
195
302
                          os.path.join(self.test_dir, 'branch1'))
196
 
        runcmd('bzr move ../hello.txt .')
 
303
        runbzr('move ../hello.txt .')
197
304
        assert exists('./hello.txt')
198
305
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
199
306
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
200
 
        runcmd(['bzr', 'commit', '-m', 'move to parent directory'])
 
307
        runbzr(['commit', '-m', 'move to parent directory'])
201
308
        chdir('..')
202
309
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
203
310
 
204
 
        runcmd('bzr move sub2/hello.txt .')
 
311
        runbzr('move sub2/hello.txt .')
205
312
        assert exists('hello.txt')
206
313
 
207
314
        f = file('hello.txt', 'wt')
212
319
        f.write('this is my new commit\n')
213
320
        f.close()
214
321
 
215
 
        runcmd('bzr commit -F msg.tmp')
 
322
        runbzr('commit -F msg.tmp')
216
323
 
217
324
        assert backtick('bzr revno') == '5\n'
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
 
 
 
325
        runbzr('export -r 5 export-5.tmp')
 
326
        runbzr('export export.tmp')
 
327
 
 
328
        runbzr('log')
 
329
        runbzr('log -v')
 
330
        runbzr('log -v --forward')
 
331
        runbzr('log -m', retcode=1)
 
332
        log_out = backtick('bzr log -m commit')
 
333
        assert "this is my new commit" in log_out
 
334
        assert "rename nested" not in log_out
 
335
        assert 'revision-id' not in log_out
 
336
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
224
337
 
225
338
 
226
339
        progress("file with spaces in name")
227
340
        mkdir('sub directory')
228
341
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
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
 
    
 
342
        runbzr('add .')
 
343
        runbzr('diff')
 
344
        runbzr('commit -m add-spaces')
 
345
        runbzr('check')
 
346
 
 
347
        runbzr('log')
 
348
        runbzr('log --forward')
 
349
 
 
350
        runbzr('info')
 
351
 
 
352
 
 
353
 
 
354
 
 
355
 
 
356
 
 
357
class RevertCommand(ExternalBase):
 
358
    def runTest(self):
 
359
        self.runbzr('init')
 
360
 
 
361
        file('hello', 'wt').write('foo')
 
362
        self.runbzr('add hello')
 
363
        self.runbzr('commit -m setup hello')
349
364
        
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
 
365
        file('hello', 'wt').write('bar')
 
366
        self.runbzr('revert hello')
 
367
        self.check_file_contents('hello', 'foo')
 
368