1
# Copyright (C) 2005, 2006 by 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Tests of status command.
20
Most of these depend on the particular formatting used.
21
As such they really are blackbox tests even though some of the
22
tests are not using self.capture. If we add tests for the programmatic
23
interface later, they will be non blackbox tests.
27
from cStringIO import StringIO
28
from os import mkdir, chdir
30
from tempfile import TemporaryFile
34
from bzrlib.builtins import merge
35
import bzrlib.bzrdir as bzrdir
36
import bzrlib.errors as errors
37
from bzrlib.osutils import pathjoin
38
from bzrlib.revisionspec import RevisionSpec
39
from bzrlib.status import show_tree_status
40
from bzrlib.tests import TestCaseWithTransport
41
from bzrlib.workingtree import WorkingTree
44
class BranchStatus(TestCaseWithTransport):
46
def test_branch_status(self):
47
"""Test basic branch status"""
48
wt = self.make_branch_and_tree('.')
53
show_tree_status(wt, to_file=tof)
54
self.assertEquals(tof.getvalue(), "")
57
self.build_tree(['hello.c', 'bye.c'])
58
wt.add_pending_merge('pending@pending-0-0')
59
show_tree_status(wt, to_file=tof)
61
self.assertEquals(tof.readlines(),
66
' pending@pending-0-0\n'
69
def test_branch_status_revisions(self):
70
"""Tests branch status with revisions"""
71
wt = self.make_branch_and_tree('.')
75
self.build_tree(['hello.c', 'bye.c'])
78
wt.commit('Test message')
82
revs.append(RevisionSpec(0))
84
show_tree_status(wt, to_file=tof, revision=revs)
87
self.assertEquals(tof.readlines(),
92
self.build_tree(['more.c'])
94
wt.commit('Another test message')
97
revs.append(RevisionSpec(1))
99
show_tree_status(wt, to_file=tof, revision=revs)
102
self.assertEquals(tof.readlines(),
107
def status_string(self, wt):
108
# use a real file rather than StringIO because it doesn't handle
110
tof = codecs.getwriter('utf-8')(TemporaryFile())
111
show_tree_status(wt, to_file=tof)
113
return tof.read().decode('utf-8')
115
def test_pending(self):
116
"""Pending merges display works, including Unicode"""
118
wt = self.make_branch_and_tree('branch')
120
wt.commit("Empty commit 1")
121
b_2_dir = b.bzrdir.sprout('./copy')
122
b_2 = b_2_dir.open_branch()
123
wt2 = b_2_dir.open_workingtree()
124
wt.commit(u"\N{TIBETAN DIGIT TWO} Empty commit 2")
125
merge(["./branch", -1], [None, None], this_dir = './copy')
126
message = self.status_string(wt2)
127
self.assert_(message.startswith("pending merges:\n"))
128
self.assert_(message.endswith("Empty commit 2\n"))
130
# must be long to make sure we see elipsis at the end
131
wt.commit("Empty commit 3 " +
132
"blah blah blah blah " * 10)
133
merge(["./branch", -1], [None, None], this_dir = './copy')
134
message = self.status_string(wt2)
135
self.assert_(message.startswith("pending merges:\n"))
136
self.assert_("Empty commit 3" in message)
137
self.assert_(message.endswith("...\n"))
139
def test_branch_status_specific_files(self):
140
"""Tests branch status with given specific files"""
141
wt = self.make_branch_and_tree('.')
144
self.build_tree(['directory/','directory/hello.c', 'bye.c','test.c','dir2/'])
150
show_tree_status(wt, to_file=tof)
152
self.assertEquals(tof.readlines(),
156
' directory/hello.c\n'
159
self.assertRaises(errors.PathsDoNotExist,
161
wt, specific_files=['bye.c','test.c','absent.c'],
165
show_tree_status(wt, specific_files=['directory'], to_file=tof)
167
self.assertEquals(tof.readlines(),
169
' directory/hello.c\n'
172
show_tree_status(wt, specific_files=['dir2'], to_file=tof)
174
self.assertEquals(tof.readlines(),
179
def test_status_nonexistent_file(self):
180
# files that don't exist in either the basis tree or working tree
181
# should give an error
182
wt = self.make_branch_and_tree('.')
183
out, err = self.run_bzr('status', 'does-not-exist', retcode=3)
184
self.assertContainsRe(err, r'do not exist.*does-not-exist')
187
class CheckoutStatus(BranchStatus):
190
super(CheckoutStatus, self).setUp()
194
def make_branch_and_tree(self, relpath):
195
source = self.make_branch(pathjoin('..', relpath))
196
checkout = bzrdir.BzrDirMetaFormat1().initialize(relpath)
197
bzrlib.branch.BranchReferenceFormat().initialize(checkout, source)
198
return checkout.create_workingtree()
201
class TestStatus(TestCaseWithTransport):
203
def test_status(self):
205
self.build_tree(['hello.txt'])
206
result = self.run_bzr("status")[0]
207
self.assert_("unknown:\n hello.txt\n" in result, result)
208
self.run_bzr("add", "hello.txt")
209
result = self.run_bzr("status")[0]
210
self.assert_("added:\n hello.txt\n" in result, result)
211
self.run_bzr("commit", "-m", "added")
212
result = self.run_bzr("status", "-r", "0..1")[0]
213
self.assert_("added:\n hello.txt\n" in result, result)
214
self.build_tree(['world.txt'])
215
result = self.run_bzr("status", "-r", "0")[0]
216
self.assert_("added:\n hello.txt\n" \
217
"unknown:\n world.txt\n" in result, result)
219
result2 = self.run_bzr("status", "-r", "0..")[0]
220
self.assertEquals(result2, result)
223
class TestStatusEncodings(TestCaseWithTransport):
226
TestCaseWithTransport.setUp(self)
227
self.user_encoding = bzrlib.user_encoding
228
self.stdout = sys.stdout
231
bzrlib.user_encoding = self.user_encoding
232
sys.stdout = self.stdout
233
TestCaseWithTransport.tearDown(self)
235
def make_uncommitted_tree(self):
236
"""Build a branch with uncommitted unicode named changes in the cwd."""
237
working_tree = self.make_branch_and_tree(u'.')
238
filename = u'hell\u00d8'
240
self.build_tree_contents([(filename, 'contents of hello')])
241
except UnicodeEncodeError:
242
raise TestSkipped("can't build unicode working tree in "
243
"filesystem encoding %s" % sys.getfilesystemencoding())
244
working_tree.add(filename)
247
def test_stdout_ascii(self):
248
sys.stdout = StringIO()
249
bzrlib.user_encoding = 'ascii'
250
working_tree = self.make_uncommitted_tree()
251
stdout, stderr = self.run_bzr("status")
253
self.assertEquals(stdout, """\
258
def test_stdout_latin1(self):
259
sys.stdout = StringIO()
260
bzrlib.user_encoding = 'latin-1'
261
working_tree = self.make_uncommitted_tree()
262
stdout, stderr = self.run_bzr('status')
264
self.assertEquals(stdout, u"""\
267
""".encode('latin-1'))