1
# Copyright (C) 2005-2010 Canonical Ltd
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.
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.
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
17
"""Tests of status command.
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.
25
from cStringIO import StringIO
27
from os import mkdir, chdir, rmdir, unlink
29
from tempfile import TemporaryFile
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
46
class BranchStatus(TestCaseWithTransport):
49
super(BranchStatus, self).setUp()
50
# As TestCase.setUp clears all hooks, we install this default
51
# post_status hook handler for the test.
52
status.hooks.install_named_hook('post_status',
53
status._show_shelve_summary,
56
def assertStatus(self, expected_lines, working_tree,
57
revision=None, short=False, pending=True, verbose=False):
58
"""Run status in working_tree and look for output.
60
:param expected_lines: The lines to look for.
61
:param working_tree: The tree to run status in.
63
output_string = self.status_string(working_tree, revision, short,
65
self.assertEqual(expected_lines, output_string.splitlines(True))
67
def status_string(self, wt, revision=None, short=False, pending=True,
69
# use a real file rather than StringIO because it doesn't handle
71
tof = codecs.getwriter('utf-8')(TemporaryFile())
72
show_tree_status(wt, to_file=tof, revision=revision, short=short,
73
show_pending=pending, verbose=verbose)
75
return tof.read().decode('utf-8')
77
def test_branch_status(self):
78
"""Test basic branch status"""
79
wt = self.make_branch_and_tree('.')
81
# status with no commits or files - it must
82
# work and show no output. We do this with no
83
# commits to be sure that it's not going to fail
85
self.assertStatus([], wt)
87
self.build_tree(['hello.c', 'bye.c'])
100
# add a commit to allow showing pending merges.
101
wt.commit('create a parent to allow testing merge output')
103
wt.add_parent_tree_id('pending@pending-0-0')
108
'pending merge tips: (use -v to see all merge revisions)\n',
109
' (ghost) pending@pending-0-0\n',
117
' (ghost) pending@pending-0-0\n',
123
'P (ghost) pending@pending-0-0\n',
136
wt, short=True, pending=False)
138
def test_branch_status_revisions(self):
139
"""Tests branch status with revisions"""
140
wt = self.make_branch_and_tree('.')
142
self.build_tree(['hello.c', 'bye.c'])
145
wt.commit('Test message')
147
revs = [RevisionSpec.from_string('0')]
156
self.build_tree(['more.c'])
158
wt.commit('Another test message')
160
revs.append(RevisionSpec.from_string('1'))
169
def test_pending(self):
170
"""Pending merges display works, including Unicode"""
172
wt = self.make_branch_and_tree('branch')
174
wt.commit("Empty commit 1")
175
b_2_dir = b.bzrdir.sprout('./copy')
176
b_2 = b_2_dir.open_branch()
177
wt2 = b_2_dir.open_workingtree()
178
wt.commit(u"\N{TIBETAN DIGIT TWO} Empty commit 2")
179
wt2.merge_from_branch(wt.branch)
180
message = self.status_string(wt2, verbose=True)
181
self.assertStartsWith(message, "pending merges:\n")
182
self.assertEndsWith(message, "Empty commit 2\n")
184
# must be long to make sure we see elipsis at the end
185
wt.commit("Empty commit 3 " +
186
"blah blah blah blah " * 100)
187
wt2.merge_from_branch(wt.branch)
188
message = self.status_string(wt2, verbose=True)
189
self.assertStartsWith(message, "pending merges:\n")
190
self.assert_("Empty commit 3" in message)
191
self.assertEndsWith(message, "...\n")
193
def test_tree_status_ignores(self):
194
"""Tests branch status with ignores"""
195
wt = self.make_branch_and_tree('.')
196
self.run_bzr('ignore *~')
197
wt.commit('commit .bzrignore')
198
self.build_tree(['foo.c', 'foo.c~'])
209
def test_tree_status_specific_files(self):
210
"""Tests branch status with given specific files"""
211
wt = self.make_branch_and_tree('.')
214
self.build_tree(['directory/','directory/hello.c',
215
'bye.c','test.c','dir2/',
229
' directory/hello.c\n'
237
'? directory/hello.c\n'
242
self.assertRaises(errors.PathsDoNotExist,
244
wt, specific_files=['bye.c','test.c','absent.c'],
248
show_tree_status(wt, specific_files=['directory'], to_file=tof)
250
self.assertEquals(tof.readlines(),
252
' directory/hello.c\n'
255
show_tree_status(wt, specific_files=['directory'], to_file=tof,
258
self.assertEquals(tof.readlines(), ['? directory/hello.c\n'])
261
show_tree_status(wt, specific_files=['dir2'], to_file=tof)
263
self.assertEquals(tof.readlines(),
268
show_tree_status(wt, specific_files=['dir2'], to_file=tof, short=True)
270
self.assertEquals(tof.readlines(), ['? dir2/\n'])
273
revs = [RevisionSpec.from_string('0'), RevisionSpec.from_string('1')]
274
show_tree_status(wt, specific_files=['test.c'], to_file=tof,
275
short=True, revision=revs)
277
self.assertEquals(tof.readlines(), ['+N test.c\n'])
280
show_tree_status(wt, specific_files=['missing.c'], to_file=tof)
282
self.assertEquals(tof.readlines(),
287
show_tree_status(wt, specific_files=['missing.c'], to_file=tof,
290
self.assertEquals(tof.readlines(),
293
def test_specific_files_conflicts(self):
294
tree = self.make_branch_and_tree('.')
295
self.build_tree(['dir2/'])
297
tree.commit('added dir2')
298
tree.set_conflicts(conflicts.ConflictList(
299
[conflicts.ContentsConflict('foo')]))
301
show_tree_status(tree, specific_files=['dir2'], to_file=tof)
302
self.assertEqualDiff('', tof.getvalue())
303
tree.set_conflicts(conflicts.ConflictList(
304
[conflicts.ContentsConflict('dir2')]))
306
show_tree_status(tree, specific_files=['dir2'], to_file=tof)
307
self.assertEqualDiff('conflicts:\n Contents conflict in dir2\n',
310
tree.set_conflicts(conflicts.ConflictList(
311
[conflicts.ContentsConflict('dir2/file1')]))
313
show_tree_status(tree, specific_files=['dir2'], to_file=tof)
314
self.assertEqualDiff('conflicts:\n Contents conflict in dir2/file1\n',
317
def _prepare_nonexistent(self):
318
wt = self.make_branch_and_tree('.')
319
self.assertStatus([], wt)
320
self.build_tree(['FILE_A', 'FILE_B', 'FILE_C', 'FILE_D', 'FILE_E', ])
326
wt.commit('Create five empty files.')
327
open('FILE_B', 'w').write('Modification to file FILE_B.')
328
open('FILE_C', 'w').write('Modification to file FILE_C.')
329
unlink('FILE_E') # FILE_E will be versioned but missing
330
open('FILE_Q', 'w').write('FILE_Q is added but not committed.')
331
wt.add('FILE_Q') # FILE_Q will be added but not committed
332
open('UNVERSIONED_BUT_EXISTING', 'w')
335
def test_status_nonexistent_file(self):
336
# files that don't exist in either the basis tree or working tree
337
# should give an error
338
wt = self._prepare_nonexistent()
348
' UNVERSIONED_BUT_EXISTING\n',
356
'? UNVERSIONED_BUT_EXISTING\n',
360
# Okay, everything's looking good with the existent files.
361
# Let's see what happens when we throw in non-existent files.
363
# bzr st [--short] NONEXISTENT '
368
out, err = self.run_bzr('status NONEXISTENT', retcode=3)
369
self.assertEqual(expected, out.splitlines(True))
370
self.assertContainsRe(err,
371
r'.*ERROR: Path\(s\) do not exist: '
376
out, err = self.run_bzr('status --short NONEXISTENT', retcode=3)
377
self.assertContainsRe(err,
378
r'.*ERROR: Path\(s\) do not exist: '
381
def test_status_nonexistent_file_with_others(self):
382
# bzr st [--short] NONEXISTENT ...others..
383
wt = self._prepare_nonexistent()
393
out, err = self.run_bzr('status NONEXISTENT '
394
'FILE_A FILE_B FILE_C FILE_D FILE_E',
396
self.assertEqual(expected, out.splitlines(True))
397
self.assertContainsRe(err,
398
r'.*ERROR: Path\(s\) do not exist: '
406
out, err = self.run_bzr('status --short NONEXISTENT '
407
'FILE_A FILE_B FILE_C FILE_D FILE_E',
409
self.assertEqual(expected, out.splitlines(True))
410
self.assertContainsRe(err,
411
r'.*ERROR: Path\(s\) do not exist: '
414
def test_status_multiple_nonexistent_files(self):
415
# bzr st [--short] NONEXISTENT ... ANOTHER_NONEXISTENT ...
416
wt = self._prepare_nonexistent()
424
' ANOTHER_NONEXISTENT\n',
427
out, err = self.run_bzr('status NONEXISTENT '
428
'FILE_A FILE_B ANOTHER_NONEXISTENT '
429
'FILE_C FILE_D FILE_E', retcode=3)
430
self.assertEqual(expected, out.splitlines(True))
431
self.assertContainsRe(err,
432
r'.*ERROR: Path\(s\) do not exist: '
433
'ANOTHER_NONEXISTENT NONEXISTENT.*')
438
'X ANOTHER_NONEXISTENT\n',
441
out, err = self.run_bzr('status --short NONEXISTENT '
442
'FILE_A FILE_B ANOTHER_NONEXISTENT '
443
'FILE_C FILE_D FILE_E', retcode=3)
444
self.assertEqual(expected, out.splitlines(True))
445
self.assertContainsRe(err,
446
r'.*ERROR: Path\(s\) do not exist: '
447
'ANOTHER_NONEXISTENT NONEXISTENT.*')
449
def test_status_nonexistent_file_with_unversioned(self):
450
# bzr st [--short] NONEXISTENT A B UNVERSIONED_BUT_EXISTING C D E Q
451
wt = self._prepare_nonexistent()
461
' UNVERSIONED_BUT_EXISTING\n',
465
out, err = self.run_bzr('status NONEXISTENT '
466
'FILE_A FILE_B UNVERSIONED_BUT_EXISTING '
467
'FILE_C FILE_D FILE_E FILE_Q', retcode=3)
468
self.assertEqual(expected, out.splitlines(True))
469
self.assertContainsRe(err,
470
r'.*ERROR: Path\(s\) do not exist: '
474
'? UNVERSIONED_BUT_EXISTING\n',
480
out, err = self.run_bzr('status --short NONEXISTENT '
481
'FILE_A FILE_B UNVERSIONED_BUT_EXISTING '
482
'FILE_C FILE_D FILE_E FILE_Q', retcode=3)
483
self.assertEqual(expected, out.splitlines(True))
484
self.assertContainsRe(err,
485
r'.*ERROR: Path\(s\) do not exist: '
488
def test_status_out_of_date(self):
489
"""Simulate status of out-of-date tree after remote push"""
490
tree = self.make_branch_and_tree('.')
491
self.build_tree_contents([('a', 'foo\n')])
495
tree.commit('add test file')
496
# simulate what happens after a remote push
497
tree.set_last_revision("0")
499
# before run another commands we should unlock tree
501
out, err = self.run_bzr('status')
502
self.assertEqual("working tree is out of date, run 'bzr update'\n",
505
def test_status_on_ignored(self):
506
"""Tests branch status on an unversioned file which is considered ignored.
508
See https://bugs.launchpad.net/bzr/+bug/40103
510
tree = self.make_branch_and_tree('.')
512
self.build_tree(['test1.c', 'test1.c~', 'test2.c~'])
513
result = self.run_bzr('status')[0]
514
self.assertContainsRe(result, "unknown:\n test1.c\n")
515
short_result = self.run_bzr('status --short')[0]
516
self.assertContainsRe(short_result, "\? test1.c\n")
518
result = self.run_bzr('status test1.c')[0]
519
self.assertContainsRe(result, "unknown:\n test1.c\n")
520
short_result = self.run_bzr('status --short test1.c')[0]
521
self.assertContainsRe(short_result, "\? test1.c\n")
523
result = self.run_bzr('status test1.c~')[0]
524
self.assertContainsRe(result, "ignored:\n test1.c~\n")
525
short_result = self.run_bzr('status --short test1.c~')[0]
526
self.assertContainsRe(short_result, "I test1.c~\n")
528
result = self.run_bzr('status test1.c~ test2.c~')[0]
529
self.assertContainsRe(result, "ignored:\n test1.c~\n test2.c~\n")
530
short_result = self.run_bzr('status --short test1.c~ test2.c~')[0]
531
self.assertContainsRe(short_result, "I test1.c~\nI test2.c~\n")
533
result = self.run_bzr('status test1.c test1.c~ test2.c~')[0]
534
self.assertContainsRe(result, "unknown:\n test1.c\nignored:\n test1.c~\n test2.c~\n")
535
short_result = self.run_bzr('status --short test1.c test1.c~ test2.c~')[0]
536
self.assertContainsRe(short_result, "\? test1.c\nI test1.c~\nI test2.c~\n")
538
def test_status_write_lock(self):
539
"""Test that status works without fetching history and
542
See https://bugs.launchpad.net/bzr/+bug/149270
545
wt = self.make_branch_and_tree('branch1')
547
wt.commit('Empty commit 1')
548
wt2 = b.bzrdir.sprout('branch2').open_workingtree()
549
wt2.commit('Empty commit 2')
550
out, err = self.run_bzr('status branch1 -rbranch:branch2')
551
self.assertEqual('', out)
553
def test_status_with_shelves(self):
554
"""Ensure that _show_shelve_summary handler works.
556
wt = self.make_branch_and_tree('.')
557
self.build_tree(['hello.c'])
559
self.run_bzr(['shelve', '--all', '-m', 'foo'])
560
self.build_tree(['bye.c'])
565
'1 shelf exists. See "bzr shelve --list" for details.\n',
568
self.run_bzr(['shelve', '--all', '-m', 'bar'])
569
self.build_tree(['spam.c'])
574
'2 shelves exist. See "bzr shelve --list" for details.\n',
579
class CheckoutStatus(BranchStatus):
582
super(CheckoutStatus, self).setUp()
586
def make_branch_and_tree(self, relpath):
587
source = self.make_branch(pathjoin('..', relpath))
588
checkout = bzrdir.BzrDirMetaFormat1().initialize(relpath)
589
bzrlib.branch.BranchReferenceFormat().initialize(checkout,
590
target_branch=source)
591
return checkout.create_workingtree()
594
class TestStatus(TestCaseWithTransport):
596
def test_status_plain(self):
597
tree = self.make_branch_and_tree('.')
599
self.build_tree(['hello.txt'])
600
result = self.run_bzr("status")[0]
601
self.assertContainsRe(result, "unknown:\n hello.txt\n")
603
tree.add("hello.txt")
604
result = self.run_bzr("status")[0]
605
self.assertContainsRe(result, "added:\n hello.txt\n")
607
tree.commit(message="added")
608
result = self.run_bzr("status -r 0..1")[0]
609
self.assertContainsRe(result, "added:\n hello.txt\n")
611
result = self.run_bzr("status -c 1")[0]
612
self.assertContainsRe(result, "added:\n hello.txt\n")
614
self.build_tree(['world.txt'])
615
result = self.run_bzr("status -r 0")[0]
616
self.assertContainsRe(result, "added:\n hello.txt\n" \
617
"unknown:\n world.txt\n")
618
result2 = self.run_bzr("status -r 0..")[0]
619
self.assertEquals(result2, result)
621
def test_status_short(self):
622
tree = self.make_branch_and_tree('.')
624
self.build_tree(['hello.txt'])
625
result = self.run_bzr("status --short")[0]
626
self.assertContainsRe(result, "[?] hello.txt\n")
628
tree.add("hello.txt")
629
result = self.run_bzr("status --short")[0]
630
self.assertContainsRe(result, "[+]N hello.txt\n")
632
tree.commit(message="added")
633
result = self.run_bzr("status --short -r 0..1")[0]
634
self.assertContainsRe(result, "[+]N hello.txt\n")
636
self.build_tree(['world.txt'])
637
result = self.run_bzr("status --short -r 0")[0]
638
self.assertContainsRe(result, "[+]N hello.txt\n" \
640
result2 = self.run_bzr("status --short -r 0..")[0]
641
self.assertEquals(result2, result)
643
def test_status_versioned(self):
644
tree = self.make_branch_and_tree('.')
646
self.build_tree(['hello.txt'])
647
result = self.run_bzr("status --versioned")[0]
648
self.assertNotContainsRe(result, "unknown:\n hello.txt\n")
650
tree.add("hello.txt")
651
result = self.run_bzr("status --versioned")[0]
652
self.assertContainsRe(result, "added:\n hello.txt\n")
655
result = self.run_bzr("status --versioned -r 0..1")[0]
656
self.assertContainsRe(result, "added:\n hello.txt\n")
658
self.build_tree(['world.txt'])
659
result = self.run_bzr("status --versioned -r 0")[0]
660
self.assertContainsRe(result, "added:\n hello.txt\n")
661
self.assertNotContainsRe(result, "unknown:\n world.txt\n")
662
result2 = self.run_bzr("status --versioned -r 0..")[0]
663
self.assertEquals(result2, result)
665
def test_status_SV(self):
666
tree = self.make_branch_and_tree('.')
668
self.build_tree(['hello.txt'])
669
result = self.run_bzr("status -SV")[0]
670
self.assertNotContainsRe(result, "hello.txt")
672
tree.add("hello.txt")
673
result = self.run_bzr("status -SV")[0]
674
self.assertContainsRe(result, "[+]N hello.txt\n")
676
tree.commit(message="added")
677
result = self.run_bzr("status -SV -r 0..1")[0]
678
self.assertContainsRe(result, "[+]N hello.txt\n")
680
self.build_tree(['world.txt'])
681
result = self.run_bzr("status -SV -r 0")[0]
682
self.assertContainsRe(result, "[+]N hello.txt\n")
684
result2 = self.run_bzr("status -SV -r 0..")[0]
685
self.assertEquals(result2, result)
687
def assertStatusContains(self, pattern, short=False):
688
"""Run status, and assert it contains the given pattern"""
690
result = self.run_bzr("status --short")[0]
692
result = self.run_bzr("status")[0]
693
self.assertContainsRe(result, pattern)
695
def test_kind_change_plain(self):
696
tree = self.make_branch_and_tree('.')
697
self.build_tree(['file'])
699
tree.commit('added file')
701
self.build_tree(['file/'])
702
self.assertStatusContains('kind changed:\n file \(file => directory\)')
703
tree.rename_one('file', 'directory')
704
self.assertStatusContains('renamed:\n file/ => directory/\n' \
705
'modified:\n directory/\n')
707
self.assertStatusContains('removed:\n file\n')
709
def test_kind_change_short(self):
710
tree = self.make_branch_and_tree('.')
711
self.build_tree(['file'])
713
tree.commit('added file')
715
self.build_tree(['file/'])
716
self.assertStatusContains('K file => file/',
718
tree.rename_one('file', 'directory')
719
self.assertStatusContains('RK file => directory/',
722
self.assertStatusContains('RD file => directory',
725
def test_status_illegal_revision_specifiers(self):
726
out, err = self.run_bzr('status -r 1..23..123', retcode=3)
727
self.assertContainsRe(err, 'one or two revision specifiers')
729
def test_status_no_pending(self):
730
a_tree = self.make_branch_and_tree('a')
731
self.build_tree(['a/a'])
734
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
735
self.build_tree(['b/b'])
739
self.run_bzr('merge ../b', working_dir='a')
740
out, err = self.run_bzr('status --no-pending', working_dir='a')
741
self.assertEquals(out, "added:\n b\n")
743
def test_pending_specific_files(self):
744
"""With a specific file list, pending merges are not shown."""
745
tree = self.make_branch_and_tree('tree')
746
self.build_tree_contents([('tree/a', 'content of a\n')])
748
r1_id = tree.commit('one')
749
alt = tree.bzrdir.sprout('alt').open_workingtree()
750
self.build_tree_contents([('alt/a', 'content of a\nfrom alt\n')])
751
alt_id = alt.commit('alt')
752
tree.merge_from_branch(alt.branch)
753
output = self.make_utf8_encoded_stringio()
754
show_tree_status(tree, to_file=output)
755
self.assertContainsRe(output.getvalue(), 'pending merge')
756
out, err = self.run_bzr('status tree/a')
757
self.assertNotContainsRe(out, 'pending merge')
760
class TestStatusEncodings(TestCaseWithTransport):
762
def make_uncommitted_tree(self):
763
"""Build a branch with uncommitted unicode named changes in the cwd."""
764
working_tree = self.make_branch_and_tree(u'.')
765
filename = u'hell\u00d8'
767
self.build_tree_contents([(filename, 'contents of hello')])
768
except UnicodeEncodeError:
769
raise TestSkipped("can't build unicode working tree in "
770
"filesystem encoding %s" % sys.getfilesystemencoding())
771
working_tree.add(filename)
774
def test_stdout_ascii(self):
775
self.overrideAttr(osutils, '_cached_user_encoding', 'ascii')
776
working_tree = self.make_uncommitted_tree()
777
stdout, stderr = self.run_bzr("status")
779
self.assertEquals(stdout, """\
784
def test_stdout_latin1(self):
785
self.overrideAttr(osutils, '_cached_user_encoding', 'latin-1')
786
working_tree = self.make_uncommitted_tree()
787
stdout, stderr = self.run_bzr('status')
789
self.assertEquals(stdout, u"""\
792
""".encode('latin-1'))