~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_status.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-13 00:26:41 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101013002641-9tlh9k89mlj1666m
Keep docs-plain working.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests of status command.
 
18
 
 
19
Most of these depend on the particular formatting used.
 
20
As such they really are blackbox tests even though some of the
 
21
tests are not using self.capture. If we add tests for the programmatic
 
22
interface later, they will be non blackbox tests.
 
23
"""
 
24
 
 
25
from cStringIO import StringIO
 
26
import codecs
 
27
from os import mkdir, chdir, rmdir, unlink
 
28
import sys
 
29
from tempfile import TemporaryFile
 
30
 
 
31
from bzrlib import (
 
32
    bzrdir,
 
33
    conflicts,
 
34
    errors,
 
35
    osutils,
 
36
    status,
 
37
    )
 
38
import bzrlib.branch
 
39
from bzrlib.osutils import pathjoin
 
40
from bzrlib.revisionspec import RevisionSpec
 
41
from bzrlib.status import show_tree_status
 
42
from bzrlib.tests import TestCaseWithTransport, TestSkipped
 
43
from bzrlib.workingtree import WorkingTree
 
44
 
 
45
 
 
46
class BranchStatus(TestCaseWithTransport):
 
47
 
 
48
    def assertStatus(self, expected_lines, working_tree,
 
49
        revision=None, short=False, pending=True, verbose=False):
 
50
        """Run status in working_tree and look for output.
 
51
 
 
52
        :param expected_lines: The lines to look for.
 
53
        :param working_tree: The tree to run status in.
 
54
        """
 
55
        output_string = self.status_string(working_tree, revision, short,
 
56
                pending, verbose)
 
57
        self.assertEqual(expected_lines, output_string.splitlines(True))
 
58
 
 
59
    def status_string(self, wt, revision=None, short=False, pending=True,
 
60
        verbose=False):
 
61
        # use a real file rather than StringIO because it doesn't handle
 
62
        # Unicode very well.
 
63
        tof = codecs.getwriter('utf-8')(TemporaryFile())
 
64
        show_tree_status(wt, to_file=tof, revision=revision, short=short,
 
65
                show_pending=pending, verbose=verbose)
 
66
        tof.seek(0)
 
67
        return tof.read().decode('utf-8')
 
68
 
 
69
    def test_branch_status(self):
 
70
        """Test basic branch status"""
 
71
        wt = self.make_branch_and_tree('.')
 
72
 
 
73
        # status with no commits or files - it must
 
74
        # work and show no output. We do this with no
 
75
        # commits to be sure that it's not going to fail
 
76
        # as a corner case.
 
77
        self.assertStatus([], wt)
 
78
 
 
79
        self.build_tree(['hello.c', 'bye.c'])
 
80
        self.assertStatus([
 
81
                'unknown:\n',
 
82
                '  bye.c\n',
 
83
                '  hello.c\n',
 
84
            ],
 
85
            wt)
 
86
        self.assertStatus([
 
87
                '?   bye.c\n',
 
88
                '?   hello.c\n',
 
89
            ],
 
90
            wt, short=True)
 
91
 
 
92
        # add a commit to allow showing pending merges.
 
93
        wt.commit('create a parent to allow testing merge output')
 
94
 
 
95
        wt.add_parent_tree_id('pending@pending-0-0')
 
96
        self.assertStatus([
 
97
                'unknown:\n',
 
98
                '  bye.c\n',
 
99
                '  hello.c\n',
 
100
                'pending merge tips: (use -v to see all merge revisions)\n',
 
101
                '  (ghost) pending@pending-0-0\n',
 
102
            ],
 
103
            wt)
 
104
        self.assertStatus([
 
105
                'unknown:\n',
 
106
                '  bye.c\n',
 
107
                '  hello.c\n',
 
108
                'pending merges:\n',
 
109
                '  (ghost) pending@pending-0-0\n',
 
110
            ],
 
111
            wt, verbose=True)
 
112
        self.assertStatus([
 
113
                '?   bye.c\n',
 
114
                '?   hello.c\n',
 
115
                'P   (ghost) pending@pending-0-0\n',
 
116
            ],
 
117
            wt, short=True)
 
118
        self.assertStatus([
 
119
                'unknown:\n',
 
120
                '  bye.c\n',
 
121
                '  hello.c\n',
 
122
            ],
 
123
            wt, pending=False)
 
124
        self.assertStatus([
 
125
                '?   bye.c\n',
 
126
                '?   hello.c\n',
 
127
            ],
 
128
            wt, short=True, pending=False)
 
129
 
 
130
    def test_branch_status_revisions(self):
 
131
        """Tests branch status with revisions"""
 
132
        wt = self.make_branch_and_tree('.')
 
133
 
 
134
        self.build_tree(['hello.c', 'bye.c'])
 
135
        wt.add('hello.c')
 
136
        wt.add('bye.c')
 
137
        wt.commit('Test message')
 
138
 
 
139
        revs = [RevisionSpec.from_string('0')]
 
140
        self.assertStatus([
 
141
                'added:\n',
 
142
                '  bye.c\n',
 
143
                '  hello.c\n'
 
144
            ],
 
145
            wt,
 
146
            revision=revs)
 
147
 
 
148
        self.build_tree(['more.c'])
 
149
        wt.add('more.c')
 
150
        wt.commit('Another test message')
 
151
 
 
152
        revs.append(RevisionSpec.from_string('1'))
 
153
        self.assertStatus([
 
154
                'added:\n',
 
155
                '  bye.c\n',
 
156
                '  hello.c\n',
 
157
            ],
 
158
            wt,
 
159
            revision=revs)
 
160
 
 
161
    def test_pending(self):
 
162
        """Pending merges display works, including Unicode"""
 
163
        mkdir("./branch")
 
164
        wt = self.make_branch_and_tree('branch')
 
165
        b = wt.branch
 
166
        wt.commit("Empty commit 1")
 
167
        b_2_dir = b.bzrdir.sprout('./copy')
 
168
        b_2 = b_2_dir.open_branch()
 
169
        wt2 = b_2_dir.open_workingtree()
 
170
        wt.commit(u"\N{TIBETAN DIGIT TWO} Empty commit 2")
 
171
        wt2.merge_from_branch(wt.branch)
 
172
        message = self.status_string(wt2, verbose=True)
 
173
        self.assertStartsWith(message, "pending merges:\n")
 
174
        self.assertEndsWith(message, "Empty commit 2\n")
 
175
        wt2.commit("merged")
 
176
        # must be long to make sure we see elipsis at the end
 
177
        wt.commit("Empty commit 3 " +
 
178
                   "blah blah blah blah " * 100)
 
179
        wt2.merge_from_branch(wt.branch)
 
180
        message = self.status_string(wt2, verbose=True)
 
181
        self.assertStartsWith(message, "pending merges:\n")
 
182
        self.assert_("Empty commit 3" in message)
 
183
        self.assertEndsWith(message, "...\n")
 
184
 
 
185
    def test_tree_status_ignores(self):
 
186
        """Tests branch status with ignores"""
 
187
        wt = self.make_branch_and_tree('.')
 
188
        self.run_bzr('ignore *~')
 
189
        wt.commit('commit .bzrignore')
 
190
        self.build_tree(['foo.c', 'foo.c~'])
 
191
        self.assertStatus([
 
192
                'unknown:\n',
 
193
                '  foo.c\n',
 
194
                ],
 
195
                wt)
 
196
        self.assertStatus([
 
197
                '?   foo.c\n',
 
198
                ],
 
199
                wt, short=True)
 
200
 
 
201
    def test_tree_status_specific_files(self):
 
202
        """Tests branch status with given specific files"""
 
203
        wt = self.make_branch_and_tree('.')
 
204
        b = wt.branch
 
205
 
 
206
        self.build_tree(['directory/','directory/hello.c', 'bye.c','test.c','dir2/'])
 
207
        wt.add('directory')
 
208
        wt.add('test.c')
 
209
        wt.commit('testing')
 
210
 
 
211
        self.assertStatus([
 
212
                'unknown:\n',
 
213
                '  bye.c\n',
 
214
                '  dir2/\n',
 
215
                '  directory/hello.c\n'
 
216
                ],
 
217
                wt)
 
218
 
 
219
        self.assertStatus([
 
220
                '?   bye.c\n',
 
221
                '?   dir2/\n',
 
222
                '?   directory/hello.c\n'
 
223
                ],
 
224
                wt, short=True)
 
225
 
 
226
        tof = StringIO()
 
227
        self.assertRaises(errors.PathsDoNotExist,
 
228
                          show_tree_status,
 
229
                          wt, specific_files=['bye.c','test.c','absent.c'],
 
230
                          to_file=tof)
 
231
 
 
232
        tof = StringIO()
 
233
        show_tree_status(wt, specific_files=['directory'], to_file=tof)
 
234
        tof.seek(0)
 
235
        self.assertEquals(tof.readlines(),
 
236
                          ['unknown:\n',
 
237
                           '  directory/hello.c\n'
 
238
                           ])
 
239
        tof = StringIO()
 
240
        show_tree_status(wt, specific_files=['directory'], to_file=tof,
 
241
                         short=True)
 
242
        tof.seek(0)
 
243
        self.assertEquals(tof.readlines(), ['?   directory/hello.c\n'])
 
244
 
 
245
        tof = StringIO()
 
246
        show_tree_status(wt, specific_files=['dir2'], to_file=tof)
 
247
        tof.seek(0)
 
248
        self.assertEquals(tof.readlines(),
 
249
                          ['unknown:\n',
 
250
                           '  dir2/\n'
 
251
                           ])
 
252
        tof = StringIO()
 
253
        show_tree_status(wt, specific_files=['dir2'], to_file=tof, short=True)
 
254
        tof.seek(0)
 
255
        self.assertEquals(tof.readlines(), ['?   dir2/\n'])
 
256
 
 
257
        tof = StringIO()
 
258
        revs = [RevisionSpec.from_string('0'), RevisionSpec.from_string('1')]
 
259
        show_tree_status(wt, specific_files=['test.c'], to_file=tof,
 
260
                         short=True, revision=revs)
 
261
        tof.seek(0)
 
262
        self.assertEquals(tof.readlines(), ['+N  test.c\n'])
 
263
 
 
264
    def test_specific_files_conflicts(self):
 
265
        tree = self.make_branch_and_tree('.')
 
266
        self.build_tree(['dir2/'])
 
267
        tree.add('dir2')
 
268
        tree.commit('added dir2')
 
269
        tree.set_conflicts(conflicts.ConflictList(
 
270
            [conflicts.ContentsConflict('foo')]))
 
271
        tof = StringIO()
 
272
        show_tree_status(tree, specific_files=['dir2'], to_file=tof)
 
273
        self.assertEqualDiff('', tof.getvalue())
 
274
        tree.set_conflicts(conflicts.ConflictList(
 
275
            [conflicts.ContentsConflict('dir2')]))
 
276
        tof = StringIO()
 
277
        show_tree_status(tree, specific_files=['dir2'], to_file=tof)
 
278
        self.assertEqualDiff('conflicts:\n  Contents conflict in dir2\n',
 
279
                             tof.getvalue())
 
280
 
 
281
        tree.set_conflicts(conflicts.ConflictList(
 
282
            [conflicts.ContentsConflict('dir2/file1')]))
 
283
        tof = StringIO()
 
284
        show_tree_status(tree, specific_files=['dir2'], to_file=tof)
 
285
        self.assertEqualDiff('conflicts:\n  Contents conflict in dir2/file1\n',
 
286
                             tof.getvalue())
 
287
 
 
288
    def _prepare_nonexistent(self):
 
289
        wt = self.make_branch_and_tree('.')
 
290
        self.assertStatus([], wt)
 
291
        self.build_tree(['FILE_A', 'FILE_B', 'FILE_C', 'FILE_D', 'FILE_E', ])
 
292
        wt.add('FILE_A')
 
293
        wt.add('FILE_B')
 
294
        wt.add('FILE_C')
 
295
        wt.add('FILE_D')
 
296
        wt.add('FILE_E')
 
297
        wt.commit('Create five empty files.')
 
298
        open('FILE_B', 'w').write('Modification to file FILE_B.')
 
299
        open('FILE_C', 'w').write('Modification to file FILE_C.')
 
300
        unlink('FILE_E')  # FILE_E will be versioned but missing
 
301
        open('FILE_Q', 'w').write('FILE_Q is added but not committed.')
 
302
        wt.add('FILE_Q')  # FILE_Q will be added but not committed
 
303
        open('UNVERSIONED_BUT_EXISTING', 'w')
 
304
        return wt
 
305
 
 
306
    def test_status_nonexistent_file(self):
 
307
        # files that don't exist in either the basis tree or working tree
 
308
        # should give an error
 
309
        wt = self._prepare_nonexistent()
 
310
        self.assertStatus([
 
311
            'removed:\n',
 
312
            '  FILE_E\n',
 
313
            'added:\n',
 
314
            '  FILE_Q\n',
 
315
            'modified:\n',
 
316
            '  FILE_B\n',
 
317
            '  FILE_C\n',
 
318
            'unknown:\n',
 
319
            '  UNVERSIONED_BUT_EXISTING\n',
 
320
            ],
 
321
            wt)
 
322
        self.assertStatus([
 
323
            ' M  FILE_B\n',
 
324
            ' M  FILE_C\n',
 
325
            ' D  FILE_E\n',
 
326
            '+N  FILE_Q\n',
 
327
            '?   UNVERSIONED_BUT_EXISTING\n',
 
328
            ],
 
329
            wt, short=True)
 
330
 
 
331
        # Okay, everything's looking good with the existent files.
 
332
        # Let's see what happens when we throw in non-existent files.
 
333
 
 
334
        # bzr st [--short] NONEXISTENT '
 
335
        expected = [
 
336
          'nonexistent:\n',
 
337
          '  NONEXISTENT\n',
 
338
          ]
 
339
        out, err = self.run_bzr('status NONEXISTENT', retcode=3)
 
340
        self.assertEqual(expected, out.splitlines(True))
 
341
        self.assertContainsRe(err,
 
342
                              r'.*ERROR: Path\(s\) do not exist: '
 
343
                              'NONEXISTENT.*')
 
344
        expected = [
 
345
          'X:   NONEXISTENT\n',
 
346
          ]
 
347
        out, err = self.run_bzr('status --short NONEXISTENT', retcode=3)
 
348
        self.assertContainsRe(err,
 
349
                              r'.*ERROR: Path\(s\) do not exist: '
 
350
                              'NONEXISTENT.*')
 
351
 
 
352
    def test_status_nonexistent_file_with_others(self):
 
353
        # bzr st [--short] NONEXISTENT ...others..
 
354
        wt = self._prepare_nonexistent()
 
355
        expected = [
 
356
          'removed:\n',
 
357
          '  FILE_E\n',
 
358
          'modified:\n',
 
359
          '  FILE_B\n',
 
360
          '  FILE_C\n',
 
361
          'nonexistent:\n',
 
362
          '  NONEXISTENT\n',
 
363
          ]
 
364
        out, err = self.run_bzr('status NONEXISTENT '
 
365
                                'FILE_A FILE_B FILE_C FILE_D FILE_E',
 
366
                                retcode=3)
 
367
        self.assertEqual(expected, out.splitlines(True))
 
368
        self.assertContainsRe(err,
 
369
                              r'.*ERROR: Path\(s\) do not exist: '
 
370
                              'NONEXISTENT.*')
 
371
        expected = [
 
372
          ' D  FILE_E\n',
 
373
          ' M  FILE_C\n',
 
374
          ' M  FILE_B\n',
 
375
          'X   NONEXISTENT\n',
 
376
          ]
 
377
        out, err = self.run_bzr('status --short NONEXISTENT '
 
378
                                'FILE_A FILE_B FILE_C FILE_D FILE_E',
 
379
                                retcode=3)
 
380
        self.assertEqual(expected, out.splitlines(True))
 
381
        self.assertContainsRe(err,
 
382
                              r'.*ERROR: Path\(s\) do not exist: '
 
383
                              'NONEXISTENT.*')
 
384
 
 
385
    def test_status_multiple_nonexistent_files(self):
 
386
        # bzr st [--short] NONEXISTENT ... ANOTHER_NONEXISTENT ...
 
387
        wt = self._prepare_nonexistent()
 
388
        expected = [
 
389
          'removed:\n',
 
390
          '  FILE_E\n',
 
391
          'modified:\n',
 
392
          '  FILE_B\n',
 
393
          '  FILE_C\n',
 
394
          'nonexistent:\n',
 
395
          '  ANOTHER_NONEXISTENT\n',
 
396
          '  NONEXISTENT\n',
 
397
          ]
 
398
        out, err = self.run_bzr('status NONEXISTENT '
 
399
                                'FILE_A FILE_B ANOTHER_NONEXISTENT '
 
400
                                'FILE_C FILE_D FILE_E', retcode=3)
 
401
        self.assertEqual(expected, out.splitlines(True))
 
402
        self.assertContainsRe(err,
 
403
                              r'.*ERROR: Path\(s\) do not exist: '
 
404
                              'ANOTHER_NONEXISTENT NONEXISTENT.*')
 
405
        expected = [
 
406
          ' D  FILE_E\n',
 
407
          ' M  FILE_C\n',
 
408
          ' M  FILE_B\n',
 
409
          'X   ANOTHER_NONEXISTENT\n',
 
410
          'X   NONEXISTENT\n',
 
411
          ]
 
412
        out, err = self.run_bzr('status --short NONEXISTENT '
 
413
                                'FILE_A FILE_B ANOTHER_NONEXISTENT '
 
414
                                'FILE_C FILE_D FILE_E', retcode=3)
 
415
        self.assertEqual(expected, out.splitlines(True))
 
416
        self.assertContainsRe(err,
 
417
                              r'.*ERROR: Path\(s\) do not exist: '
 
418
                              'ANOTHER_NONEXISTENT NONEXISTENT.*')
 
419
 
 
420
    def test_status_nonexistent_file_with_unversioned(self):
 
421
        # bzr st [--short] NONEXISTENT A B UNVERSIONED_BUT_EXISTING C D E Q
 
422
        wt = self._prepare_nonexistent()
 
423
        expected = [
 
424
          'removed:\n',
 
425
          '  FILE_E\n',
 
426
          'added:\n',
 
427
          '  FILE_Q\n',
 
428
          'modified:\n',
 
429
          '  FILE_B\n',
 
430
          '  FILE_C\n',
 
431
          'unknown:\n',
 
432
          '  UNVERSIONED_BUT_EXISTING\n',
 
433
          'nonexistent:\n',
 
434
          '  NONEXISTENT\n',
 
435
          ]
 
436
        out, err = self.run_bzr('status NONEXISTENT '
 
437
                                'FILE_A FILE_B UNVERSIONED_BUT_EXISTING '
 
438
                                'FILE_C FILE_D FILE_E FILE_Q', retcode=3)
 
439
        self.assertEqual(expected, out.splitlines(True))
 
440
        self.assertContainsRe(err,
 
441
                              r'.*ERROR: Path\(s\) do not exist: '
 
442
                              'NONEXISTENT.*')
 
443
        expected = [
 
444
          '+N  FILE_Q\n',
 
445
          '?   UNVERSIONED_BUT_EXISTING\n',
 
446
          ' D  FILE_E\n',
 
447
          ' M  FILE_C\n',
 
448
          ' M  FILE_B\n',
 
449
          'X   NONEXISTENT\n',
 
450
          ]
 
451
        out, err = self.run_bzr('status --short NONEXISTENT '
 
452
                                'FILE_A FILE_B UNVERSIONED_BUT_EXISTING '
 
453
                                'FILE_C FILE_D FILE_E FILE_Q', retcode=3)
 
454
        self.assertEqual(expected, out.splitlines(True))
 
455
        self.assertContainsRe(err,
 
456
                              r'.*ERROR: Path\(s\) do not exist: '
 
457
                              'NONEXISTENT.*')
 
458
 
 
459
    def test_status_out_of_date(self):
 
460
        """Simulate status of out-of-date tree after remote push"""
 
461
        tree = self.make_branch_and_tree('.')
 
462
        self.build_tree_contents([('a', 'foo\n')])
 
463
        tree.lock_write()
 
464
        try:
 
465
            tree.add(['a'])
 
466
            tree.commit('add test file')
 
467
            # simulate what happens after a remote push
 
468
            tree.set_last_revision("0")
 
469
        finally:
 
470
            # before run another commands we should unlock tree
 
471
            tree.unlock()
 
472
        out, err = self.run_bzr('status')
 
473
        self.assertEqual("working tree is out of date, run 'bzr update'\n",
 
474
                         err)
 
475
 
 
476
    def test_status_on_ignored(self):
 
477
        """Tests branch status on an unversioned file which is considered ignored.
 
478
 
 
479
        See https://bugs.launchpad.net/bzr/+bug/40103
 
480
        """
 
481
        tree = self.make_branch_and_tree('.')
 
482
 
 
483
        self.build_tree(['test1.c', 'test1.c~', 'test2.c~'])
 
484
        result = self.run_bzr('status')[0]
 
485
        self.assertContainsRe(result, "unknown:\n  test1.c\n")
 
486
        short_result = self.run_bzr('status --short')[0]
 
487
        self.assertContainsRe(short_result, "\?   test1.c\n")
 
488
 
 
489
        result = self.run_bzr('status test1.c')[0]
 
490
        self.assertContainsRe(result, "unknown:\n  test1.c\n")
 
491
        short_result = self.run_bzr('status --short test1.c')[0]
 
492
        self.assertContainsRe(short_result, "\?   test1.c\n")
 
493
 
 
494
        result = self.run_bzr('status test1.c~')[0]
 
495
        self.assertContainsRe(result, "ignored:\n  test1.c~\n")
 
496
        short_result = self.run_bzr('status --short test1.c~')[0]
 
497
        self.assertContainsRe(short_result, "I   test1.c~\n")
 
498
 
 
499
        result = self.run_bzr('status test1.c~ test2.c~')[0]
 
500
        self.assertContainsRe(result, "ignored:\n  test1.c~\n  test2.c~\n")
 
501
        short_result = self.run_bzr('status --short test1.c~ test2.c~')[0]
 
502
        self.assertContainsRe(short_result, "I   test1.c~\nI   test2.c~\n")
 
503
 
 
504
        result = self.run_bzr('status test1.c test1.c~ test2.c~')[0]
 
505
        self.assertContainsRe(result, "unknown:\n  test1.c\nignored:\n  test1.c~\n  test2.c~\n")
 
506
        short_result = self.run_bzr('status --short test1.c test1.c~ test2.c~')[0]
 
507
        self.assertContainsRe(short_result, "\?   test1.c\nI   test1.c~\nI   test2.c~\n")
 
508
 
 
509
    def test_status_write_lock(self):
 
510
        """Test that status works without fetching history and
 
511
        having a write lock.
 
512
 
 
513
        See https://bugs.launchpad.net/bzr/+bug/149270
 
514
        """
 
515
        mkdir('branch1')
 
516
        wt = self.make_branch_and_tree('branch1')
 
517
        b = wt.branch
 
518
        wt.commit('Empty commit 1')
 
519
        wt2 = b.bzrdir.sprout('branch2').open_workingtree()
 
520
        wt2.commit('Empty commit 2')
 
521
        out, err = self.run_bzr('status branch1 -rbranch:branch2')
 
522
        self.assertEqual('', out)
 
523
 
 
524
    def test_status_with_shelves(self):
 
525
        """Ensure that _show_shelve_summary handler works.
 
526
        """
 
527
        wt = self.make_branch_and_tree('.')
 
528
        self.build_tree(['hello.c'])
 
529
        wt.add('hello.c')
 
530
        self.run_bzr(['shelve', '--all', '-m', 'foo'])
 
531
        self.build_tree(['bye.c'])
 
532
        wt.add('bye.c')
 
533
        # As TestCase.setUp clears all hooks, we install this default
 
534
        # post_status hook handler for the test.
 
535
        status.hooks.install_named_hook('post_status',
 
536
            status._show_shelve_summary,
 
537
            'bzr status')
 
538
        self.assertStatus([
 
539
                'added:\n',
 
540
                '  bye.c\n',
 
541
                '1 shelves exist. See "bzr shelve --list" for details.\n',
 
542
            ],
 
543
            wt)
 
544
 
 
545
 
 
546
class CheckoutStatus(BranchStatus):
 
547
 
 
548
    def setUp(self):
 
549
        super(CheckoutStatus, self).setUp()
 
550
        mkdir('codir')
 
551
        chdir('codir')
 
552
 
 
553
    def make_branch_and_tree(self, relpath):
 
554
        source = self.make_branch(pathjoin('..', relpath))
 
555
        checkout = bzrdir.BzrDirMetaFormat1().initialize(relpath)
 
556
        bzrlib.branch.BranchReferenceFormat().initialize(checkout,
 
557
            target_branch=source)
 
558
        return checkout.create_workingtree()
 
559
 
 
560
 
 
561
class TestStatus(TestCaseWithTransport):
 
562
 
 
563
    def test_status_plain(self):
 
564
        tree = self.make_branch_and_tree('.')
 
565
 
 
566
        self.build_tree(['hello.txt'])
 
567
        result = self.run_bzr("status")[0]
 
568
        self.assertContainsRe(result, "unknown:\n  hello.txt\n")
 
569
 
 
570
        tree.add("hello.txt")
 
571
        result = self.run_bzr("status")[0]
 
572
        self.assertContainsRe(result, "added:\n  hello.txt\n")
 
573
 
 
574
        tree.commit(message="added")
 
575
        result = self.run_bzr("status -r 0..1")[0]
 
576
        self.assertContainsRe(result, "added:\n  hello.txt\n")
 
577
 
 
578
        result = self.run_bzr("status -c 1")[0]
 
579
        self.assertContainsRe(result, "added:\n  hello.txt\n")
 
580
 
 
581
        self.build_tree(['world.txt'])
 
582
        result = self.run_bzr("status -r 0")[0]
 
583
        self.assertContainsRe(result, "added:\n  hello.txt\n" \
 
584
                                      "unknown:\n  world.txt\n")
 
585
        result2 = self.run_bzr("status -r 0..")[0]
 
586
        self.assertEquals(result2, result)
 
587
 
 
588
    def test_status_short(self):
 
589
        tree = self.make_branch_and_tree('.')
 
590
 
 
591
        self.build_tree(['hello.txt'])
 
592
        result = self.run_bzr("status --short")[0]
 
593
        self.assertContainsRe(result, "[?]   hello.txt\n")
 
594
 
 
595
        tree.add("hello.txt")
 
596
        result = self.run_bzr("status --short")[0]
 
597
        self.assertContainsRe(result, "[+]N  hello.txt\n")
 
598
 
 
599
        tree.commit(message="added")
 
600
        result = self.run_bzr("status --short -r 0..1")[0]
 
601
        self.assertContainsRe(result, "[+]N  hello.txt\n")
 
602
 
 
603
        self.build_tree(['world.txt'])
 
604
        result = self.run_bzr("status --short -r 0")[0]
 
605
        self.assertContainsRe(result, "[+]N  hello.txt\n" \
 
606
                                      "[?]   world.txt\n")
 
607
        result2 = self.run_bzr("status --short -r 0..")[0]
 
608
        self.assertEquals(result2, result)
 
609
 
 
610
    def test_status_versioned(self):
 
611
        tree = self.make_branch_and_tree('.')
 
612
 
 
613
        self.build_tree(['hello.txt'])
 
614
        result = self.run_bzr("status --versioned")[0]
 
615
        self.assertNotContainsRe(result, "unknown:\n  hello.txt\n")
 
616
 
 
617
        tree.add("hello.txt")
 
618
        result = self.run_bzr("status --versioned")[0]
 
619
        self.assertContainsRe(result, "added:\n  hello.txt\n")
 
620
 
 
621
        tree.commit("added")
 
622
        result = self.run_bzr("status --versioned -r 0..1")[0]
 
623
        self.assertContainsRe(result, "added:\n  hello.txt\n")
 
624
 
 
625
        self.build_tree(['world.txt'])
 
626
        result = self.run_bzr("status --versioned -r 0")[0]
 
627
        self.assertContainsRe(result, "added:\n  hello.txt\n")
 
628
        self.assertNotContainsRe(result, "unknown:\n  world.txt\n")
 
629
        result2 = self.run_bzr("status --versioned -r 0..")[0]
 
630
        self.assertEquals(result2, result)
 
631
 
 
632
    def test_status_SV(self):
 
633
        tree = self.make_branch_and_tree('.')
 
634
 
 
635
        self.build_tree(['hello.txt'])
 
636
        result = self.run_bzr("status -SV")[0]
 
637
        self.assertNotContainsRe(result, "hello.txt")
 
638
 
 
639
        tree.add("hello.txt")
 
640
        result = self.run_bzr("status -SV")[0]
 
641
        self.assertContainsRe(result, "[+]N  hello.txt\n")
 
642
 
 
643
        tree.commit(message="added")
 
644
        result = self.run_bzr("status -SV -r 0..1")[0]
 
645
        self.assertContainsRe(result, "[+]N  hello.txt\n")
 
646
 
 
647
        self.build_tree(['world.txt'])
 
648
        result = self.run_bzr("status -SV -r 0")[0]
 
649
        self.assertContainsRe(result, "[+]N  hello.txt\n")
 
650
 
 
651
        result2 = self.run_bzr("status -SV -r 0..")[0]
 
652
        self.assertEquals(result2, result)
 
653
 
 
654
    def assertStatusContains(self, pattern, short=False):
 
655
        """Run status, and assert it contains the given pattern"""
 
656
        if short:
 
657
            result = self.run_bzr("status --short")[0]
 
658
        else:
 
659
            result = self.run_bzr("status")[0]
 
660
        self.assertContainsRe(result, pattern)
 
661
 
 
662
    def test_kind_change_plain(self):
 
663
        tree = self.make_branch_and_tree('.')
 
664
        self.build_tree(['file'])
 
665
        tree.add('file')
 
666
        tree.commit('added file')
 
667
        unlink('file')
 
668
        self.build_tree(['file/'])
 
669
        self.assertStatusContains('kind changed:\n  file \(file => directory\)')
 
670
        tree.rename_one('file', 'directory')
 
671
        self.assertStatusContains('renamed:\n  file/ => directory/\n' \
 
672
                                  'modified:\n  directory/\n')
 
673
        rmdir('directory')
 
674
        self.assertStatusContains('removed:\n  file\n')
 
675
 
 
676
    def test_kind_change_short(self):
 
677
        tree = self.make_branch_and_tree('.')
 
678
        self.build_tree(['file'])
 
679
        tree.add('file')
 
680
        tree.commit('added file')
 
681
        unlink('file')
 
682
        self.build_tree(['file/'])
 
683
        self.assertStatusContains('K  file => file/',
 
684
                                   short=True)
 
685
        tree.rename_one('file', 'directory')
 
686
        self.assertStatusContains('RK  file => directory/',
 
687
                                   short=True)
 
688
        rmdir('directory')
 
689
        self.assertStatusContains('RD  file => directory',
 
690
                                   short=True)
 
691
 
 
692
    def test_status_illegal_revision_specifiers(self):
 
693
        out, err = self.run_bzr('status -r 1..23..123', retcode=3)
 
694
        self.assertContainsRe(err, 'one or two revision specifiers')
 
695
 
 
696
    def test_status_no_pending(self):
 
697
        a_tree = self.make_branch_and_tree('a')
 
698
        self.build_tree(['a/a'])
 
699
        a_tree.add('a')
 
700
        a_tree.commit('a')
 
701
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
 
702
        self.build_tree(['b/b'])
 
703
        b_tree.add('b')
 
704
        b_tree.commit('b')
 
705
 
 
706
        self.run_bzr('merge ../b', working_dir='a')
 
707
        out, err = self.run_bzr('status --no-pending', working_dir='a')
 
708
        self.assertEquals(out, "added:\n  b\n")
 
709
 
 
710
    def test_pending_specific_files(self):
 
711
        """With a specific file list, pending merges are not shown."""
 
712
        tree = self.make_branch_and_tree('tree')
 
713
        self.build_tree_contents([('tree/a', 'content of a\n')])
 
714
        tree.add('a')
 
715
        r1_id = tree.commit('one')
 
716
        alt = tree.bzrdir.sprout('alt').open_workingtree()
 
717
        self.build_tree_contents([('alt/a', 'content of a\nfrom alt\n')])
 
718
        alt_id = alt.commit('alt')
 
719
        tree.merge_from_branch(alt.branch)
 
720
        output = self.make_utf8_encoded_stringio()
 
721
        show_tree_status(tree, to_file=output)
 
722
        self.assertContainsRe(output.getvalue(), 'pending merge')
 
723
        out, err = self.run_bzr('status tree/a')
 
724
        self.assertNotContainsRe(out, 'pending merge')
 
725
 
 
726
 
 
727
class TestStatusEncodings(TestCaseWithTransport):
 
728
 
 
729
    def make_uncommitted_tree(self):
 
730
        """Build a branch with uncommitted unicode named changes in the cwd."""
 
731
        working_tree = self.make_branch_and_tree(u'.')
 
732
        filename = u'hell\u00d8'
 
733
        try:
 
734
            self.build_tree_contents([(filename, 'contents of hello')])
 
735
        except UnicodeEncodeError:
 
736
            raise TestSkipped("can't build unicode working tree in "
 
737
                "filesystem encoding %s" % sys.getfilesystemencoding())
 
738
        working_tree.add(filename)
 
739
        return working_tree
 
740
 
 
741
    def test_stdout_ascii(self):
 
742
        self.overrideAttr(osutils, '_cached_user_encoding', 'ascii')
 
743
        working_tree = self.make_uncommitted_tree()
 
744
        stdout, stderr = self.run_bzr("status")
 
745
 
 
746
        self.assertEquals(stdout, """\
 
747
added:
 
748
  hell?
 
749
""")
 
750
 
 
751
    def test_stdout_latin1(self):
 
752
        self.overrideAttr(osutils, '_cached_user_encoding', 'latin-1')
 
753
        working_tree = self.make_uncommitted_tree()
 
754
        stdout, stderr = self.run_bzr('status')
 
755
 
 
756
        self.assertEquals(stdout, u"""\
 
757
added:
 
758
  hell\u00d8
 
759
""".encode('latin-1'))
 
760