~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# Copyright (C) 2005-2012, 2016 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests of status command.

Most of these depend on the particular formatting used.
As such they really are blackbox tests even though some of the
tests are not using self.capture. If we add tests for the programmatic
interface later, they will be non blackbox tests.
"""

from cStringIO import StringIO
import codecs
from os import mkdir, chdir, rmdir, unlink
import sys
from tempfile import TemporaryFile

from bzrlib import (
    bzrdir,
    conflicts,
    errors,
    osutils,
    status,
    )
import bzrlib.branch
from bzrlib.osutils import pathjoin
from bzrlib.revisionspec import RevisionSpec
from bzrlib.status import show_tree_status
from bzrlib.tests import TestCaseWithTransport, TestSkipped
from bzrlib.workingtree import WorkingTree


class BranchStatus(TestCaseWithTransport):

    def setUp(self):
        super(BranchStatus, self).setUp()
        # As TestCase.setUp clears all hooks, we install this default
        # post_status hook handler for the test.
        status.hooks.install_named_hook('post_status',
            status._show_shelve_summary,
            'bzr status')

    def assertStatus(self, expected_lines, working_tree, specific_files=None,
        revision=None, short=False, pending=True, verbose=False):
        """Run status in working_tree and look for output.

        :param expected_lines: The lines to look for.
        :param working_tree: The tree to run status in.
        """
        output_string = self.status_string(working_tree, specific_files, revision, short,
                pending, verbose)
        self.assertEqual(expected_lines, output_string.splitlines(True))

    def status_string(self, wt, specific_files=None, revision=None,
        short=False, pending=True, verbose=False):
        # use a real file rather than StringIO because it doesn't handle
        # Unicode very well.
        tof = codecs.getwriter('utf-8')(TemporaryFile())
        show_tree_status(wt, specific_files=specific_files, to_file=tof,
                revision=revision, short=short, show_pending=pending,
                verbose=verbose)
        tof.seek(0)
        return tof.read().decode('utf-8')

    def test_branch_status(self):
        """Test basic branch status"""
        wt = self.make_branch_and_tree('.')

        # status with no commits or files - it must
        # work and show no output. We do this with no
        # commits to be sure that it's not going to fail
        # as a corner case.
        self.assertStatus([], wt)

        self.build_tree(['hello.c', 'bye.c'])
        self.assertStatus([
                'unknown:\n',
                '  bye.c\n',
                '  hello.c\n',
            ],
            wt)
        self.assertStatus([
                '?   bye.c\n',
                '?   hello.c\n',
            ],
            wt, short=True)

        # add a commit to allow showing pending merges.
        wt.commit('create a parent to allow testing merge output')

        wt.add_parent_tree_id('pending@pending-0-0')
        self.assertStatus([
                'unknown:\n',
                '  bye.c\n',
                '  hello.c\n',
                'pending merge tips: (use -v to see all merge revisions)\n',
                '  (ghost) pending@pending-0-0\n',
            ],
            wt)
        self.assertStatus([
                'unknown:\n',
                '  bye.c\n',
                '  hello.c\n',
                'pending merges:\n',
                '  (ghost) pending@pending-0-0\n',
            ],
            wt, verbose=True)
        self.assertStatus([
                '?   bye.c\n',
                '?   hello.c\n',
                'P   (ghost) pending@pending-0-0\n',
            ],
            wt, short=True)
        self.assertStatus([
                'unknown:\n',
                '  bye.c\n',
                '  hello.c\n',
            ],
            wt, pending=False)
        self.assertStatus([
                '?   bye.c\n',
                '?   hello.c\n',
            ],
            wt, short=True, pending=False)

    def test_branch_status_revisions(self):
        """Tests branch status with revisions"""
        wt = self.make_branch_and_tree('.')

        self.build_tree(['hello.c', 'bye.c'])
        wt.add('hello.c')
        wt.add('bye.c')
        wt.commit('Test message')

        revs = [RevisionSpec.from_string('0')]
        self.assertStatus([
                'added:\n',
                '  bye.c\n',
                '  hello.c\n'
            ],
            wt,
            revision=revs)

        self.build_tree(['more.c'])
        wt.add('more.c')
        wt.commit('Another test message')

        revs.append(RevisionSpec.from_string('1'))
        self.assertStatus([
                'added:\n',
                '  bye.c\n',
                '  hello.c\n',
            ],
            wt,
            revision=revs)

    def test_pending(self):
        """Pending merges display works, including Unicode"""
        mkdir("./branch")
        wt = self.make_branch_and_tree('branch')
        b = wt.branch
        wt.commit("Empty commit 1")
        b_2_dir = b.bzrdir.sprout('./copy')
        b_2 = b_2_dir.open_branch()
        wt2 = b_2_dir.open_workingtree()
        wt.commit(u"\N{TIBETAN DIGIT TWO} Empty commit 2")
        wt2.merge_from_branch(wt.branch)
        message = self.status_string(wt2, verbose=True)
        self.assertStartsWith(message, "pending merges:\n")
        self.assertEndsWith(message, "Empty commit 2\n")
        wt2.commit("merged")
        # must be long to make sure we see elipsis at the end
        wt.commit("Empty commit 3 " +
                   "blah blah blah blah " * 100)
        wt2.merge_from_branch(wt.branch)
        message = self.status_string(wt2, verbose=True)
        self.assertStartsWith(message, "pending merges:\n")
        self.assertTrue("Empty commit 3" in message)
        self.assertEndsWith(message, "...\n")

    def test_tree_status_ignores(self):
        """Tests branch status with ignores"""
        wt = self.make_branch_and_tree('.')
        self.run_bzr('ignore *~')
        wt.commit('commit .bzrignore')
        self.build_tree(['foo.c', 'foo.c~'])
        self.assertStatus([
                'unknown:\n',
                '  foo.c\n',
                ],
                wt)
        self.assertStatus([
                '?   foo.c\n',
                ],
                wt, short=True)

    def test_tree_status_specific_files(self):
        """Tests branch status with given specific files"""
        wt = self.make_branch_and_tree('.')
        b = wt.branch

        self.build_tree(['directory/','directory/hello.c',
                         'bye.c','test.c','dir2/',
                         'missing.c'])
        wt.add('directory')
        wt.add('test.c')
        wt.commit('testing')
        wt.add('missing.c')
        unlink('missing.c')

        self.assertStatus([
                'missing:\n',
                '  missing.c\n',
                'unknown:\n',
                '  bye.c\n',
                '  dir2/\n',
                '  directory/hello.c\n'
                ],
                wt)

        self.assertStatus([
                '?   bye.c\n',
                '?   dir2/\n',
                '+!  missing.c\n',
                '?   directory/hello.c\n'
                ],
                wt, short=True)

        tof = StringIO()
        self.assertRaises(errors.PathsDoNotExist,
                          show_tree_status,
                          wt, specific_files=['bye.c','test.c','absent.c'],
                          to_file=tof)

        tof = StringIO()
        show_tree_status(wt, specific_files=['directory'], to_file=tof)
        tof.seek(0)
        self.assertEqual(tof.readlines(),
                          ['unknown:\n',
                           '  directory/hello.c\n'
                           ])
        tof = StringIO()
        show_tree_status(wt, specific_files=['directory'], to_file=tof,
                         short=True)
        tof.seek(0)
        self.assertEqual(tof.readlines(), ['?   directory/hello.c\n'])

        tof = StringIO()
        show_tree_status(wt, specific_files=['dir2'], to_file=tof)
        tof.seek(0)
        self.assertEqual(tof.readlines(),
                          ['unknown:\n',
                           '  dir2/\n'
                           ])
        tof = StringIO()
        show_tree_status(wt, specific_files=['dir2'], to_file=tof, short=True)
        tof.seek(0)
        self.assertEqual(tof.readlines(), ['?   dir2/\n'])

        tof = StringIO()
        revs = [RevisionSpec.from_string('0'), RevisionSpec.from_string('1')]
        show_tree_status(wt, specific_files=['test.c'], to_file=tof,
                         short=True, revision=revs)
        tof.seek(0)
        self.assertEqual(tof.readlines(), ['+N  test.c\n'])

        tof = StringIO()
        show_tree_status(wt, specific_files=['missing.c'], to_file=tof)
        tof.seek(0)
        self.assertEqual(tof.readlines(),
                          ['missing:\n',
                           '  missing.c\n'])

        tof = StringIO()
        show_tree_status(wt, specific_files=['missing.c'], to_file=tof,
                         short=True)
        tof.seek(0)
        self.assertEqual(tof.readlines(),
                          ['+!  missing.c\n'])

    def test_specific_files_conflicts(self):
        tree = self.make_branch_and_tree('.')
        self.build_tree(['dir2/'])
        tree.add('dir2')
        tree.commit('added dir2')
        tree.set_conflicts(conflicts.ConflictList(
            [conflicts.ContentsConflict('foo')]))
        tof = StringIO()
        show_tree_status(tree, specific_files=['dir2'], to_file=tof)
        self.assertEqualDiff('', tof.getvalue())
        tree.set_conflicts(conflicts.ConflictList(
            [conflicts.ContentsConflict('dir2')]))
        tof = StringIO()
        show_tree_status(tree, specific_files=['dir2'], to_file=tof)
        self.assertEqualDiff('conflicts:\n  Contents conflict in dir2\n',
                             tof.getvalue())

        tree.set_conflicts(conflicts.ConflictList(
            [conflicts.ContentsConflict('dir2/file1')]))
        tof = StringIO()
        show_tree_status(tree, specific_files=['dir2'], to_file=tof)
        self.assertEqualDiff('conflicts:\n  Contents conflict in dir2/file1\n',
                             tof.getvalue())

    def _prepare_nonexistent(self):
        wt = self.make_branch_and_tree('.')
        self.assertStatus([], wt)
        self.build_tree(['FILE_A', 'FILE_B', 'FILE_C', 'FILE_D', 'FILE_E', ])
        wt.add('FILE_A')
        wt.add('FILE_B')
        wt.add('FILE_C')
        wt.add('FILE_D')
        wt.add('FILE_E')
        wt.commit('Create five empty files.')
        with open('FILE_B', 'w') as f: f.write('Modification to file FILE_B.')
        with open('FILE_C', 'w') as f: f.write('Modification to file FILE_C.')
        unlink('FILE_E')  # FILE_E will be versioned but missing
        with open('FILE_Q', 'w') as f: f.write('FILE_Q is added but not committed.')
        wt.add('FILE_Q')  # FILE_Q will be added but not committed
        open('UNVERSIONED_BUT_EXISTING', 'w')
        return wt

    def test_status_nonexistent_file(self):
        # files that don't exist in either the basis tree or working tree
        # should give an error
        wt = self._prepare_nonexistent()
        self.assertStatus([
            'removed:\n',
            '  FILE_E\n',
            'added:\n',
            '  FILE_Q\n',
            'modified:\n',
            '  FILE_B\n',
            '  FILE_C\n',
            'unknown:\n',
            '  UNVERSIONED_BUT_EXISTING\n',
            ],
            wt)
        self.assertStatus([
            ' M  FILE_B\n',
            ' M  FILE_C\n',
            ' D  FILE_E\n',
            '+N  FILE_Q\n',
            '?   UNVERSIONED_BUT_EXISTING\n',
            ],
            wt, short=True)

        # Okay, everything's looking good with the existent files.
        # Let's see what happens when we throw in non-existent files.

        # bzr st [--short] NONEXISTENT '
        expected = [
          'nonexistent:\n',
          '  NONEXISTENT\n',
          ]
        out, err = self.run_bzr('status NONEXISTENT', retcode=3)
        self.assertEqual(expected, out.splitlines(True))
        self.assertContainsRe(err,
                              r'.*ERROR: Path\(s\) do not exist: '
                              'NONEXISTENT.*')
        expected = [
          'X:   NONEXISTENT\n',
          ]
        out, err = self.run_bzr('status --short NONEXISTENT', retcode=3)
        self.assertContainsRe(err,
                              r'.*ERROR: Path\(s\) do not exist: '
                              'NONEXISTENT.*')

    def test_status_nonexistent_file_with_others(self):
        # bzr st [--short] NONEXISTENT ...others..
        wt = self._prepare_nonexistent()
        expected = [
          'removed:\n',
          '  FILE_E\n',
          'modified:\n',
          '  FILE_B\n',
          '  FILE_C\n',
          'nonexistent:\n',
          '  NONEXISTENT\n',
          ]
        out, err = self.run_bzr('status NONEXISTENT '
                                'FILE_A FILE_B FILE_C FILE_D FILE_E',
                                retcode=3)
        self.assertEqual(expected, out.splitlines(True))
        self.assertContainsRe(err,
                              r'.*ERROR: Path\(s\) do not exist: '
                              'NONEXISTENT.*')
        expected = [
          ' D  FILE_E\n',
          ' M  FILE_C\n',
          ' M  FILE_B\n',
          'X   NONEXISTENT\n',
          ]
        out, err = self.run_bzr('status --short NONEXISTENT '
                                'FILE_A FILE_B FILE_C FILE_D FILE_E',
                                retcode=3)
        self.assertEqual(expected, out.splitlines(True))
        self.assertContainsRe(err,
                              r'.*ERROR: Path\(s\) do not exist: '
                              'NONEXISTENT.*')

    def test_status_multiple_nonexistent_files(self):
        # bzr st [--short] NONEXISTENT ... ANOTHER_NONEXISTENT ...
        wt = self._prepare_nonexistent()
        expected = [
          'removed:\n',
          '  FILE_E\n',
          'modified:\n',
          '  FILE_B\n',
          '  FILE_C\n',
          'nonexistent:\n',
          '  ANOTHER_NONEXISTENT\n',
          '  NONEXISTENT\n',
          ]
        out, err = self.run_bzr('status NONEXISTENT '
                                'FILE_A FILE_B ANOTHER_NONEXISTENT '
                                'FILE_C FILE_D FILE_E', retcode=3)
        self.assertEqual(expected, out.splitlines(True))
        self.assertContainsRe(err,
                              r'.*ERROR: Path\(s\) do not exist: '
                              'ANOTHER_NONEXISTENT NONEXISTENT.*')
        expected = [
          ' D  FILE_E\n',
          ' M  FILE_C\n',
          ' M  FILE_B\n',
          'X   ANOTHER_NONEXISTENT\n',
          'X   NONEXISTENT\n',
          ]
        out, err = self.run_bzr('status --short NONEXISTENT '
                                'FILE_A FILE_B ANOTHER_NONEXISTENT '
                                'FILE_C FILE_D FILE_E', retcode=3)
        self.assertEqual(expected, out.splitlines(True))
        self.assertContainsRe(err,
                              r'.*ERROR: Path\(s\) do not exist: '
                              'ANOTHER_NONEXISTENT NONEXISTENT.*')

    def test_status_nonexistent_file_with_unversioned(self):
        # bzr st [--short] NONEXISTENT A B UNVERSIONED_BUT_EXISTING C D E Q
        wt = self._prepare_nonexistent()
        expected = [
          'removed:\n',
          '  FILE_E\n',
          'added:\n',
          '  FILE_Q\n',
          'modified:\n',
          '  FILE_B\n',
          '  FILE_C\n',
          'unknown:\n',
          '  UNVERSIONED_BUT_EXISTING\n',
          'nonexistent:\n',
          '  NONEXISTENT\n',
          ]
        out, err = self.run_bzr('status NONEXISTENT '
                                'FILE_A FILE_B UNVERSIONED_BUT_EXISTING '
                                'FILE_C FILE_D FILE_E FILE_Q', retcode=3)
        self.assertEqual(expected, out.splitlines(True))
        self.assertContainsRe(err,
                              r'.*ERROR: Path\(s\) do not exist: '
                              'NONEXISTENT.*')
        expected = [
          '+N  FILE_Q\n',
          '?   UNVERSIONED_BUT_EXISTING\n',
          ' D  FILE_E\n',
          ' M  FILE_C\n',
          ' M  FILE_B\n',
          'X   NONEXISTENT\n',
          ]
        expected.sort()
        out, err = self.run_bzr('status --short NONEXISTENT '
                                'FILE_A FILE_B UNVERSIONED_BUT_EXISTING '
                                'FILE_C FILE_D FILE_E FILE_Q', retcode=3)
        actual = out.splitlines(True)
        actual.sort()
        self.assertEqual(expected, actual)
        self.assertContainsRe(err,
                              r'.*ERROR: Path\(s\) do not exist: '
                              'NONEXISTENT.*')

    def test_status_out_of_date(self):
        """Simulate status of out-of-date tree after remote push"""
        tree = self.make_branch_and_tree('.')
        self.build_tree_contents([('a', 'foo\n')])
        tree.lock_write()
        try:
            tree.add(['a'])
            tree.commit('add test file')
            # simulate what happens after a remote push
            tree.set_last_revision("0")
        finally:
            # before run another commands we should unlock tree
            tree.unlock()
        out, err = self.run_bzr('status')
        self.assertEqual("working tree is out of date, run 'bzr update'\n",
                         err)

    def test_status_on_ignored(self):
        """Tests branch status on an unversioned file which is considered ignored.

        See https://bugs.launchpad.net/bzr/+bug/40103
        """
        tree = self.make_branch_and_tree('.')

        self.build_tree(['test1.c', 'test1.c~', 'test2.c~'])
        result = self.run_bzr('status')[0]
        self.assertContainsRe(result, "unknown:\n  test1.c\n")
        short_result = self.run_bzr('status --short')[0]
        self.assertContainsRe(short_result, "\?   test1.c\n")

        result = self.run_bzr('status test1.c')[0]
        self.assertContainsRe(result, "unknown:\n  test1.c\n")
        short_result = self.run_bzr('status --short test1.c')[0]
        self.assertContainsRe(short_result, "\?   test1.c\n")

        result = self.run_bzr('status test1.c~')[0]
        self.assertContainsRe(result, "ignored:\n  test1.c~\n")
        short_result = self.run_bzr('status --short test1.c~')[0]
        self.assertContainsRe(short_result, "I   test1.c~\n")

        result = self.run_bzr('status test1.c~ test2.c~')[0]
        self.assertContainsRe(result, "ignored:\n  test1.c~\n  test2.c~\n")
        short_result = self.run_bzr('status --short test1.c~ test2.c~')[0]
        self.assertContainsRe(short_result, "I   test1.c~\nI   test2.c~\n")

        result = self.run_bzr('status test1.c test1.c~ test2.c~')[0]
        self.assertContainsRe(result, "unknown:\n  test1.c\nignored:\n  test1.c~\n  test2.c~\n")
        short_result = self.run_bzr('status --short test1.c test1.c~ test2.c~')[0]
        self.assertContainsRe(short_result, "\?   test1.c\nI   test1.c~\nI   test2.c~\n")

    def test_status_write_lock(self):
        """Test that status works without fetching history and
        having a write lock.

        See https://bugs.launchpad.net/bzr/+bug/149270
        """
        mkdir('branch1')
        wt = self.make_branch_and_tree('branch1')
        b = wt.branch
        wt.commit('Empty commit 1')
        wt2 = b.bzrdir.sprout('branch2').open_workingtree()
        wt2.commit('Empty commit 2')
        out, err = self.run_bzr('status branch1 -rbranch:branch2')
        self.assertEqual('', out)

    def test_status_with_shelves(self):
        """Ensure that _show_shelve_summary handler works.
        """
        wt = self.make_branch_and_tree('.')
        self.build_tree(['hello.c'])
        wt.add('hello.c')
        self.run_bzr(['shelve', '--all', '-m', 'foo'])
        self.build_tree(['bye.c'])
        wt.add('bye.c')
        self.assertStatus([
                'added:\n',
                '  bye.c\n',
                '1 shelf exists. See "bzr shelve --list" for details.\n',
            ],
            wt)
        self.run_bzr(['shelve', '--all', '-m', 'bar'])
        self.build_tree(['eggs.c', 'spam.c'])
        wt.add('eggs.c')
        wt.add('spam.c')
        self.assertStatus([
                'added:\n',
                '  eggs.c\n',
                '  spam.c\n',
                '2 shelves exist. See "bzr shelve --list" for details.\n',
            ],
            wt)
        self.assertStatus([
                'added:\n',
                '  spam.c\n',
            ],
            wt,
            specific_files=['spam.c'])


class CheckoutStatus(BranchStatus):

    def setUp(self):
        super(CheckoutStatus, self).setUp()
        mkdir('codir')
        chdir('codir')

    def make_branch_and_tree(self, relpath):
        source = self.make_branch(pathjoin('..', relpath))
        checkout = bzrdir.BzrDirMetaFormat1().initialize(relpath)
        checkout.set_branch_reference(source)
        return checkout.create_workingtree()


class TestStatus(TestCaseWithTransport):

    def test_status_plain(self):
        tree = self.make_branch_and_tree('.')

        self.build_tree(['hello.txt'])
        result = self.run_bzr("status")[0]
        self.assertContainsRe(result, "unknown:\n  hello.txt\n")

        tree.add("hello.txt")
        result = self.run_bzr("status")[0]
        self.assertContainsRe(result, "added:\n  hello.txt\n")

        tree.commit(message="added")
        result = self.run_bzr("status -r 0..1")[0]
        self.assertContainsRe(result, "added:\n  hello.txt\n")

        result = self.run_bzr("status -c 1")[0]
        self.assertContainsRe(result, "added:\n  hello.txt\n")

        self.build_tree(['world.txt'])
        result = self.run_bzr("status -r 0")[0]
        self.assertContainsRe(result, "added:\n  hello.txt\n" \
                                      "unknown:\n  world.txt\n")
        result2 = self.run_bzr("status -r 0..")[0]
        self.assertEqual(result2, result)

    def test_status_short(self):
        tree = self.make_branch_and_tree('.')

        self.build_tree(['hello.txt'])
        result = self.run_bzr("status --short")[0]
        self.assertContainsRe(result, "[?]   hello.txt\n")

        tree.add("hello.txt")
        result = self.run_bzr("status --short")[0]
        self.assertContainsRe(result, "[+]N  hello.txt\n")

        tree.commit(message="added")
        result = self.run_bzr("status --short -r 0..1")[0]
        self.assertContainsRe(result, "[+]N  hello.txt\n")

        self.build_tree(['world.txt'])
        result = self.run_bzr("status -S -r 0")[0]
        self.assertContainsRe(result, "[+]N  hello.txt\n" \
                                      "[?]   world.txt\n")
        result2 = self.run_bzr("status -S -r 0..")[0]
        self.assertEqual(result2, result)

    def test_status_versioned(self):
        tree = self.make_branch_and_tree('.')

        self.build_tree(['hello.txt'])
        result = self.run_bzr("status --versioned")[0]
        self.assertNotContainsRe(result, "unknown:\n  hello.txt\n")

        tree.add("hello.txt")
        result = self.run_bzr("status --versioned")[0]
        self.assertContainsRe(result, "added:\n  hello.txt\n")

        tree.commit("added")
        result = self.run_bzr("status --versioned -r 0..1")[0]
        self.assertContainsRe(result, "added:\n  hello.txt\n")

        self.build_tree(['world.txt'])
        result = self.run_bzr("status --versioned -r 0")[0]
        self.assertContainsRe(result, "added:\n  hello.txt\n")
        self.assertNotContainsRe(result, "unknown:\n  world.txt\n")
        result2 = self.run_bzr("status --versioned -r 0..")[0]
        self.assertEqual(result2, result)

    def test_status_SV(self):
        tree = self.make_branch_and_tree('.')

        self.build_tree(['hello.txt'])
        result = self.run_bzr("status -SV")[0]
        self.assertNotContainsRe(result, "hello.txt")

        tree.add("hello.txt")
        result = self.run_bzr("status -SV")[0]
        self.assertContainsRe(result, "[+]N  hello.txt\n")

        tree.commit(message="added")
        result = self.run_bzr("status -SV -r 0..1")[0]
        self.assertContainsRe(result, "[+]N  hello.txt\n")

        self.build_tree(['world.txt'])
        result = self.run_bzr("status -SV -r 0")[0]
        self.assertContainsRe(result, "[+]N  hello.txt\n")

        result2 = self.run_bzr("status -SV -r 0..")[0]
        self.assertEqual(result2, result)

    def assertStatusContains(self, pattern, short=False):
        """Run status, and assert it contains the given pattern"""
        if short:
            result = self.run_bzr("status --short")[0]
        else:
            result = self.run_bzr("status")[0]
        self.assertContainsRe(result, pattern)

    def test_kind_change_plain(self):
        tree = self.make_branch_and_tree('.')
        self.build_tree(['file'])
        tree.add('file')
        tree.commit('added file')
        unlink('file')
        self.build_tree(['file/'])
        self.assertStatusContains('kind changed:\n  file \(file => directory\)')
        tree.rename_one('file', 'directory')
        self.assertStatusContains('renamed:\n  file/ => directory/\n' \
                                  'modified:\n  directory/\n')
        rmdir('directory')
        self.assertStatusContains('removed:\n  file\n')

    def test_kind_change_short(self):
        tree = self.make_branch_and_tree('.')
        self.build_tree(['file'])
        tree.add('file')
        tree.commit('added file')
        unlink('file')
        self.build_tree(['file/'])
        self.assertStatusContains('K  file => file/',
                                   short=True)
        tree.rename_one('file', 'directory')
        self.assertStatusContains('RK  file => directory/',
                                   short=True)
        rmdir('directory')
        self.assertStatusContains('RD  file => directory',
                                   short=True)

    def test_status_illegal_revision_specifiers(self):
        out, err = self.run_bzr('status -r 1..23..123', retcode=3)
        self.assertContainsRe(err, 'one or two revision specifiers')

    def test_status_no_pending(self):
        a_tree = self.make_branch_and_tree('a')
        self.build_tree(['a/a'])
        a_tree.add('a')
        a_tree.commit('a')
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
        self.build_tree(['b/b'])
        b_tree.add('b')
        b_tree.commit('b')

        self.run_bzr('merge ../b', working_dir='a')
        out, err = self.run_bzr('status --no-pending', working_dir='a')
        self.assertEqual(out, "added:\n  b\n")

    def test_pending_specific_files(self):
        """With a specific file list, pending merges are not shown."""
        tree = self.make_branch_and_tree('tree')
        self.build_tree_contents([('tree/a', 'content of a\n')])
        tree.add('a')
        r1_id = tree.commit('one')
        alt = tree.bzrdir.sprout('alt').open_workingtree()
        self.build_tree_contents([('alt/a', 'content of a\nfrom alt\n')])
        alt_id = alt.commit('alt')
        tree.merge_from_branch(alt.branch)
        output = self.make_utf8_encoded_stringio()
        show_tree_status(tree, to_file=output)
        self.assertContainsRe(output.getvalue(), 'pending merge')
        out, err = self.run_bzr('status tree/a')
        self.assertNotContainsRe(out, 'pending merge')


class TestStatusEncodings(TestCaseWithTransport):

    def make_uncommitted_tree(self):
        """Build a branch with uncommitted unicode named changes in the cwd."""
        working_tree = self.make_branch_and_tree(u'.')
        filename = u'hell\u00d8'
        try:
            self.build_tree_contents([(filename, 'contents of hello')])
        except UnicodeEncodeError:
            raise TestSkipped("can't build unicode working tree in "
                "filesystem encoding %s" % sys.getfilesystemencoding())
        working_tree.add(filename)
        return working_tree

    def test_stdout_ascii(self):
        self.overrideAttr(osutils, '_cached_user_encoding', 'ascii')
        working_tree = self.make_uncommitted_tree()
        stdout, stderr = self.run_bzr("status")

        self.assertEqual(stdout, """\
added:
  hell?
""")

    def test_stdout_latin1(self):
        self.overrideAttr(osutils, '_cached_user_encoding', 'latin-1')
        working_tree = self.make_uncommitted_tree()
        stdout, stderr = self.run_bzr('status')

        self.assertEqual(stdout, u"""\
added:
  hell\u00d8
""".encode('latin-1'))