1
# Copyright (C) 2005, 2006 Canonical Ltd
1
# Copyright (C) 2005 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
"""Tests of status command.
19
20
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
24
from cStringIO import StringIO
26
from tempfile import TemporaryFile
27
from os import mkdir, chdir, rmdir, unlink
29
from tempfile import TemporaryFile
38
from bzrlib.osutils import pathjoin
29
from bzrlib.tests import TestCaseInTempDir
39
30
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
45
class BranchStatus(TestCaseWithTransport):
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.
51
:param expected_lines: The lines to look for.
52
:param working_tree: The tree to run status in.
54
output_string = self.status_string(working_tree, revision, short,
56
self.assertEqual(expected_lines, output_string.splitlines(True))
58
def status_string(self, wt, revision=None, short=False, pending=True,
31
from bzrlib.merge import merge
32
from bzrlib.status import show_status
33
from bzrlib.branch import Branch
34
from bzrlib.clone import copy_branch
36
class BranchStatus(TestCaseInTempDir):
38
def test_branch_status(self):
39
"""Test basic branch status"""
40
from bzrlib.status import show_status
41
from bzrlib.branch import Branch
43
b = Branch.initialize(u'.')
47
show_status(b, to_file=tof)
48
self.assertEquals(tof.getvalue(), "")
51
self.build_tree(['hello.c', 'bye.c'])
52
b.working_tree().add_pending_merge('pending@pending-0-0')
53
show_status(b, to_file=tof)
55
self.assertEquals(tof.readlines(),
60
' pending@pending-0-0\n'
63
def test_branch_status_revisions(self):
64
"""Tests branch status with revisions"""
66
b = Branch.initialize(u'.')
69
self.build_tree(['hello.c', 'bye.c'])
70
b.working_tree().add('hello.c')
71
b.working_tree().add('bye.c')
72
b.working_tree().commit('Test message')
76
revs.append(RevisionSpec(0))
78
show_status(b, to_file=tof, revision=revs)
81
self.assertEquals(tof.readlines(),
86
self.build_tree(['more.c'])
87
b.working_tree().add('more.c')
88
b.working_tree().commit('Another test message')
91
revs.append(RevisionSpec(1))
93
show_status(b, to_file=tof, revision=revs)
96
self.assertEquals(tof.readlines(),
101
def status_string(self, branch):
60
102
# use a real file rather than StringIO because it doesn't handle
61
103
# Unicode very well.
62
104
tof = codecs.getwriter('utf-8')(TemporaryFile())
63
show_tree_status(wt, to_file=tof, revision=revision, short=short,
64
show_pending=pending, verbose=verbose)
105
show_status(branch, to_file=tof)
66
107
return tof.read().decode('utf-8')
68
def test_branch_status(self):
69
"""Test basic branch status"""
70
wt = self.make_branch_and_tree('.')
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
76
self.assertStatus([], wt)
78
self.build_tree(['hello.c', 'bye.c'])
91
# add a commit to allow showing pending merges.
92
wt.commit('create a parent to allow testing merge output')
94
wt.add_parent_tree_id('pending@pending-0-0')
99
'pending merge tips: (use -v to see all merge revisions)\n',
100
' (ghost) pending@pending-0-0\n',
108
' (ghost) pending@pending-0-0\n',
114
'P (ghost) pending@pending-0-0\n',
127
wt, short=True, pending=False)
129
def test_branch_status_revisions(self):
130
"""Tests branch status with revisions"""
131
wt = self.make_branch_and_tree('.')
133
self.build_tree(['hello.c', 'bye.c'])
136
wt.commit('Test message')
138
revs = [RevisionSpec.from_string('0')]
147
self.build_tree(['more.c'])
149
wt.commit('Another test message')
151
revs.append(RevisionSpec.from_string('1'))
160
109
def test_pending(self):
161
110
"""Pending merges display works, including Unicode"""
162
111
mkdir("./branch")
163
wt = self.make_branch_and_tree('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")
112
b = Branch.initialize('./branch')
113
b.working_tree().commit("Empty commit 1")
114
b_2 = copy_branch(b, './copy')
115
b.working_tree().commit(u"\N{TIBETAN DIGIT TWO} Empty commit 2")
116
merge(["./branch", -1], [None, None], this_dir = './copy')
117
message = self.status_string(b_2)
118
self.assert_(message.startswith("pending merges:\n"))
119
self.assert_(message.endswith("Empty commit 2\n"))
120
b_2.working_tree().commit("merged")
175
121
# 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")
122
b.working_tree().commit("Empty commit 3 " +
123
"blah blah blah blah " * 10)
124
merge(["./branch", -1], [None, None], this_dir = './copy')
125
message = self.status_string(b_2)
126
self.assert_(message.startswith("pending merges:\n"))
181
127
self.assert_("Empty commit 3" in message)
182
self.assertEndsWith(message, "...\n")
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~'])
200
def test_tree_status_specific_files(self):
128
self.assert_(message.endswith("...\n"))
130
def test_branch_status_specific_files(self):
201
131
"""Tests branch status with given specific files"""
202
wt = self.make_branch_and_tree('.')
132
from cStringIO import StringIO
133
from bzrlib.status import show_status
134
from bzrlib.branch import Branch
136
b = Branch.initialize(u'.')
205
138
self.build_tree(['directory/','directory/hello.c', 'bye.c','test.c','dir2/'])
214
' directory/hello.c\n'
221
'? directory/hello.c\n'
226
self.assertRaises(errors.PathsDoNotExist,
228
wt, specific_files=['bye.c','test.c','absent.c'],
232
show_tree_status(wt, specific_files=['directory'], to_file=tof)
234
self.assertEquals(tof.readlines(),
236
' directory/hello.c\n'
239
show_tree_status(wt, specific_files=['directory'], to_file=tof,
242
self.assertEquals(tof.readlines(), ['? directory/hello.c\n'])
245
show_tree_status(wt, specific_files=['dir2'], to_file=tof)
247
self.assertEquals(tof.readlines(),
252
show_tree_status(wt, specific_files=['dir2'], to_file=tof, short=True)
254
self.assertEquals(tof.readlines(), ['? dir2/\n'])
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)
261
self.assertEquals(tof.readlines(), ['+N test.c\n'])
263
def test_specific_files_conflicts(self):
264
tree = self.make_branch_and_tree('.')
265
self.build_tree(['dir2/'])
267
tree.commit('added dir2')
268
tree.set_conflicts(conflicts.ConflictList(
269
[conflicts.ContentsConflict('foo')]))
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')]))
276
show_tree_status(tree, specific_files=['dir2'], to_file=tof)
277
self.assertEqualDiff('conflicts:\n Contents conflict in dir2\n',
280
tree.set_conflicts(conflicts.ConflictList(
281
[conflicts.ContentsConflict('dir2/file1')]))
283
show_tree_status(tree, specific_files=['dir2'], to_file=tof)
284
self.assertEqualDiff('conflicts:\n Contents conflict in dir2/file1\n',
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', ])
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')
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()
318
' UNVERSIONED_BUT_EXISTING\n',
326
'? UNVERSIONED_BUT_EXISTING\n',
330
# Okay, everything's looking good with the existent files.
331
# Let's see what happens when we throw in non-existent files.
333
# bzr st [--short] NONEXISTENT '
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: '
346
out, err = self.run_bzr('status --short NONEXISTENT', retcode=3)
347
self.assertContainsRe(err,
348
r'.*ERROR: Path\(s\) do not exist: '
351
def test_status_nonexistent_file_with_others(self):
352
# bzr st [--short] NONEXISTENT ...others..
353
wt = self._prepare_nonexistent()
363
out, err = self.run_bzr('status NONEXISTENT '
364
'FILE_A FILE_B FILE_C FILE_D FILE_E',
366
self.assertEqual(expected, out.splitlines(True))
367
self.assertContainsRe(err,
368
r'.*ERROR: Path\(s\) do not exist: '
376
out, err = self.run_bzr('status --short NONEXISTENT '
377
'FILE_A FILE_B FILE_C FILE_D FILE_E',
379
self.assertEqual(expected, out.splitlines(True))
380
self.assertContainsRe(err,
381
r'.*ERROR: Path\(s\) do not exist: '
384
def test_status_multiple_nonexistent_files(self):
385
# bzr st [--short] NONEXISTENT ... ANOTHER_NONEXISTENT ...
386
wt = self._prepare_nonexistent()
394
' ANOTHER_NONEXISTENT\n',
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.*')
408
'X ANOTHER_NONEXISTENT\n',
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.*')
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()
431
' UNVERSIONED_BUT_EXISTING\n',
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: '
444
'? UNVERSIONED_BUT_EXISTING\n',
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: '
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')])
465
tree.commit('add test file')
466
# simulate what happens after a remote push
467
tree.set_last_revision("0")
469
# before run another commands we should unlock tree
471
out, err = self.run_bzr('status')
472
self.assertEqual("working tree is out of date, run 'bzr update'\n",
475
def test_status_write_lock(self):
476
"""Test that status works without fetching history and
479
See https://bugs.launchpad.net/bzr/+bug/149270
482
wt = self.make_branch_and_tree('branch1')
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)
491
class CheckoutStatus(BranchStatus):
494
super(CheckoutStatus, self).setUp()
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, source)
502
return checkout.create_workingtree()
505
class TestStatus(TestCaseWithTransport):
507
def test_status_plain(self):
508
tree = self.make_branch_and_tree('.')
510
self.build_tree(['hello.txt'])
511
result = self.run_bzr("status")[0]
512
self.assertContainsRe(result, "unknown:\n hello.txt\n")
514
tree.add("hello.txt")
515
result = self.run_bzr("status")[0]
516
self.assertContainsRe(result, "added:\n hello.txt\n")
518
tree.commit(message="added")
519
result = self.run_bzr("status -r 0..1")[0]
520
self.assertContainsRe(result, "added:\n hello.txt\n")
522
result = self.run_bzr("status -c 1")[0]
523
self.assertContainsRe(result, "added:\n hello.txt\n")
525
self.build_tree(['world.txt'])
526
result = self.run_bzr("status -r 0")[0]
527
self.assertContainsRe(result, "added:\n hello.txt\n" \
528
"unknown:\n world.txt\n")
529
result2 = self.run_bzr("status -r 0..")[0]
530
self.assertEquals(result2, result)
532
def test_status_short(self):
533
tree = self.make_branch_and_tree('.')
535
self.build_tree(['hello.txt'])
536
result = self.run_bzr("status --short")[0]
537
self.assertContainsRe(result, "[?] hello.txt\n")
539
tree.add("hello.txt")
540
result = self.run_bzr("status --short")[0]
541
self.assertContainsRe(result, "[+]N hello.txt\n")
543
tree.commit(message="added")
544
result = self.run_bzr("status --short -r 0..1")[0]
545
self.assertContainsRe(result, "[+]N hello.txt\n")
547
self.build_tree(['world.txt'])
548
result = self.run_bzr("status --short -r 0")[0]
549
self.assertContainsRe(result, "[+]N hello.txt\n" \
551
result2 = self.run_bzr("status --short -r 0..")[0]
552
self.assertEquals(result2, result)
554
def test_status_versioned(self):
555
tree = self.make_branch_and_tree('.')
557
self.build_tree(['hello.txt'])
558
result = self.run_bzr("status --versioned")[0]
559
self.assertNotContainsRe(result, "unknown:\n hello.txt\n")
561
tree.add("hello.txt")
562
result = self.run_bzr("status --versioned")[0]
563
self.assertContainsRe(result, "added:\n hello.txt\n")
566
result = self.run_bzr("status --versioned -r 0..1")[0]
567
self.assertContainsRe(result, "added:\n hello.txt\n")
569
self.build_tree(['world.txt'])
570
result = self.run_bzr("status --versioned -r 0")[0]
571
self.assertContainsRe(result, "added:\n hello.txt\n")
572
self.assertNotContainsRe(result, "unknown:\n world.txt\n")
573
result2 = self.run_bzr("status --versioned -r 0..")[0]
574
self.assertEquals(result2, result)
576
def test_status_SV(self):
577
tree = self.make_branch_and_tree('.')
579
self.build_tree(['hello.txt'])
580
result = self.run_bzr("status -SV")[0]
581
self.assertNotContainsRe(result, "hello.txt")
583
tree.add("hello.txt")
584
result = self.run_bzr("status -SV")[0]
585
self.assertContainsRe(result, "[+]N hello.txt\n")
587
tree.commit(message="added")
588
result = self.run_bzr("status -SV -r 0..1")[0]
589
self.assertContainsRe(result, "[+]N hello.txt\n")
591
self.build_tree(['world.txt'])
592
result = self.run_bzr("status -SV -r 0")[0]
593
self.assertContainsRe(result, "[+]N hello.txt\n")
595
result2 = self.run_bzr("status -SV -r 0..")[0]
596
self.assertEquals(result2, result)
598
def assertStatusContains(self, pattern):
599
"""Run status, and assert it contains the given pattern"""
600
result = self.run_bzr("status --short")[0]
601
self.assertContainsRe(result, pattern)
603
def test_kind_change_short(self):
604
tree = self.make_branch_and_tree('.')
605
self.build_tree(['file'])
607
tree.commit('added file')
609
self.build_tree(['file/'])
610
self.assertStatusContains('K file => file/')
611
tree.rename_one('file', 'directory')
612
self.assertStatusContains('RK file => directory/')
614
self.assertStatusContains('RD file => directory')
616
def test_status_illegal_revision_specifiers(self):
617
out, err = self.run_bzr('status -r 1..23..123', retcode=3)
618
self.assertContainsRe(err, 'one or two revision specifiers')
620
def test_status_no_pending(self):
621
a_tree = self.make_branch_and_tree('a')
622
self.build_tree(['a/a'])
625
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
626
self.build_tree(['b/b'])
630
self.run_bzr('merge ../b', working_dir='a')
631
out, err = self.run_bzr('status --no-pending', working_dir='a')
632
self.assertEquals(out, "added:\n b\n")
634
def test_pending_specific_files(self):
635
"""With a specific file list, pending merges are not shown."""
636
tree = self.make_branch_and_tree('tree')
637
self.build_tree_contents([('tree/a', 'content of a\n')])
639
r1_id = tree.commit('one')
640
alt = tree.bzrdir.sprout('alt').open_workingtree()
641
self.build_tree_contents([('alt/a', 'content of a\nfrom alt\n')])
642
alt_id = alt.commit('alt')
643
tree.merge_from_branch(alt.branch)
644
output = self.make_utf8_encoded_stringio()
645
show_tree_status(tree, to_file=output)
646
self.assertContainsRe(output.getvalue(), 'pending merge')
647
out, err = self.run_bzr('status tree/a')
648
self.assertNotContainsRe(out, 'pending merge')
651
class TestStatusEncodings(TestCaseWithTransport):
654
TestCaseWithTransport.setUp(self)
655
self.user_encoding = osutils._cached_user_encoding
656
self.stdout = sys.stdout
659
osutils._cached_user_encoding = self.user_encoding
660
sys.stdout = self.stdout
661
TestCaseWithTransport.tearDown(self)
663
def make_uncommitted_tree(self):
664
"""Build a branch with uncommitted unicode named changes in the cwd."""
665
working_tree = self.make_branch_and_tree(u'.')
666
filename = u'hell\u00d8'
668
self.build_tree_contents([(filename, 'contents of hello')])
669
except UnicodeEncodeError:
670
raise TestSkipped("can't build unicode working tree in "
671
"filesystem encoding %s" % sys.getfilesystemencoding())
672
working_tree.add(filename)
675
def test_stdout_ascii(self):
676
sys.stdout = StringIO()
677
osutils._cached_user_encoding = 'ascii'
678
working_tree = self.make_uncommitted_tree()
679
stdout, stderr = self.run_bzr("status")
681
self.assertEquals(stdout, """\
686
def test_stdout_latin1(self):
687
sys.stdout = StringIO()
688
osutils._cached_user_encoding = 'latin-1'
689
working_tree = self.make_uncommitted_tree()
690
stdout, stderr = self.run_bzr('status')
692
self.assertEquals(stdout, u"""\
695
""".encode('latin-1'))
139
b.working_tree().add('directory')
140
b.working_tree().add('test.c')
141
b.working_tree().commit('testing')
144
show_status(b, to_file=tof)
146
self.assertEquals(tof.readlines(),
150
' directory/hello.c\n'
154
show_status(b, specific_files=['bye.c','test.c','absent.c'], to_file=tof)
156
self.assertEquals(tof.readlines(),
162
show_status(b, specific_files=['directory'], to_file=tof)
164
self.assertEquals(tof.readlines(),
166
' directory/hello.c\n'
169
show_status(b, specific_files=['dir2'], to_file=tof)
171
self.assertEquals(tof.readlines(),