/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

  • Committer: Jelmer Vernooij
  • Date: 2010-03-12 02:14:56 UTC
  • mto: This revision was merged to the branch mainline in revision 5106.
  • Revision ID: jelmer@samba.org-20100312021456-6dk2272z7armx043
Put automatic_tag_name on Branch.

Show diffs side-by-side

added added

removed removed

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