~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2006-06-05 17:19:03 UTC
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: mbp@sourcefrog.net-20060605171903-f3a801c35a7db12d
Small Transport fixups

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 by 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
"""Tests of status command.
 
19
 
 
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.
 
24
"""
 
25
 
 
26
 
 
27
from cStringIO import StringIO
 
28
from os import mkdir, chdir
 
29
import sys
 
30
from tempfile import TemporaryFile
 
31
import codecs
 
32
 
 
33
import bzrlib.branch
 
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
 
42
 
 
43
 
 
44
class BranchStatus(TestCaseWithTransport):
 
45
    
 
46
    def test_branch_status(self): 
 
47
        """Test basic branch status"""
 
48
        wt = self.make_branch_and_tree('.')
 
49
        b = wt.branch
 
50
 
 
51
        # status with nothing
 
52
        tof = StringIO()
 
53
        show_tree_status(wt, to_file=tof)
 
54
        self.assertEquals(tof.getvalue(), "")
 
55
 
 
56
        tof = StringIO()
 
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)
 
60
        tof.seek(0)
 
61
        self.assertEquals(tof.readlines(),
 
62
                          ['unknown:\n',
 
63
                           '  bye.c\n',
 
64
                           '  hello.c\n',
 
65
                           'pending merges:\n',
 
66
                           '  pending@pending-0-0\n'
 
67
                           ])
 
68
 
 
69
    def test_branch_status_revisions(self):
 
70
        """Tests branch status with revisions"""
 
71
        wt = self.make_branch_and_tree('.')
 
72
        b = wt.branch
 
73
 
 
74
        tof = StringIO()
 
75
        self.build_tree(['hello.c', 'bye.c'])
 
76
        wt.add('hello.c')
 
77
        wt.add('bye.c')
 
78
        wt.commit('Test message')
 
79
 
 
80
        tof = StringIO()
 
81
        revs =[]
 
82
        revs.append(RevisionSpec(0))
 
83
        
 
84
        show_tree_status(wt, to_file=tof, revision=revs)
 
85
        
 
86
        tof.seek(0)
 
87
        self.assertEquals(tof.readlines(),
 
88
                          ['added:\n',
 
89
                           '  bye.c\n',
 
90
                           '  hello.c\n'])
 
91
 
 
92
        self.build_tree(['more.c'])
 
93
        wt.add('more.c')
 
94
        wt.commit('Another test message')
 
95
        
 
96
        tof = StringIO()
 
97
        revs.append(RevisionSpec(1))
 
98
        
 
99
        show_tree_status(wt, to_file=tof, revision=revs)
 
100
        
 
101
        tof.seek(0)
 
102
        self.assertEquals(tof.readlines(),
 
103
                          ['added:\n',
 
104
                           '  bye.c\n',
 
105
                           '  hello.c\n'])
 
106
 
 
107
    def status_string(self, wt):
 
108
        # use a real file rather than StringIO because it doesn't handle
 
109
        # Unicode very well.
 
110
        tof = codecs.getwriter('utf-8')(TemporaryFile())
 
111
        show_tree_status(wt, to_file=tof)
 
112
        tof.seek(0)
 
113
        return tof.read().decode('utf-8')
 
114
 
 
115
    def test_pending(self):
 
116
        """Pending merges display works, including Unicode"""
 
117
        mkdir("./branch")
 
118
        wt = self.make_branch_and_tree('branch')
 
119
        b = wt.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")) 
 
129
        wt2.commit("merged")
 
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")) 
 
138
 
 
139
    def test_branch_status_specific_files(self): 
 
140
        """Tests branch status with given specific files"""
 
141
        wt = self.make_branch_and_tree('.')
 
142
        b = wt.branch
 
143
 
 
144
        self.build_tree(['directory/','directory/hello.c', 'bye.c','test.c','dir2/'])
 
145
        wt.add('directory')
 
146
        wt.add('test.c')
 
147
        wt.commit('testing')
 
148
        
 
149
        tof = StringIO()
 
150
        show_tree_status(wt, to_file=tof)
 
151
        tof.seek(0)
 
152
        self.assertEquals(tof.readlines(),
 
153
                          ['unknown:\n',
 
154
                           '  bye.c\n',
 
155
                           '  dir2\n',
 
156
                           '  directory/hello.c\n'
 
157
                           ])
 
158
 
 
159
        self.assertRaises(errors.PathsDoNotExist,
 
160
                          show_tree_status,
 
161
                          wt, specific_files=['bye.c','test.c','absent.c'], 
 
162
                          to_file=tof)
 
163
        
 
164
        tof = StringIO()
 
165
        show_tree_status(wt, specific_files=['directory'], to_file=tof)
 
166
        tof.seek(0)
 
167
        self.assertEquals(tof.readlines(),
 
168
                          ['unknown:\n',
 
169
                           '  directory/hello.c\n'
 
170
                           ])
 
171
        tof = StringIO()
 
172
        show_tree_status(wt, specific_files=['dir2'], to_file=tof)
 
173
        tof.seek(0)
 
174
        self.assertEquals(tof.readlines(),
 
175
                          ['unknown:\n',
 
176
                           '  dir2\n'
 
177
                           ])
 
178
 
 
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')
 
185
 
 
186
 
 
187
class CheckoutStatus(BranchStatus):
 
188
 
 
189
    def setUp(self):
 
190
        super(CheckoutStatus, self).setUp()
 
191
        mkdir('codir')
 
192
        chdir('codir')
 
193
        
 
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()
 
199
 
 
200
 
 
201
class TestStatus(TestCaseWithTransport):
 
202
 
 
203
    def test_status(self):
 
204
        self.run_bzr("init")
 
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)
 
218
 
 
219
        result2 = self.run_bzr("status", "-r", "0..")[0]
 
220
        self.assertEquals(result2, result)
 
221
 
 
222
 
 
223
class TestStatusEncodings(TestCaseWithTransport):
 
224
    
 
225
    def setUp(self):
 
226
        TestCaseWithTransport.setUp(self)
 
227
        self.user_encoding = bzrlib.user_encoding
 
228
        self.stdout = sys.stdout
 
229
 
 
230
    def tearDown(self):
 
231
        bzrlib.user_encoding = self.user_encoding
 
232
        sys.stdout = self.stdout
 
233
        TestCaseWithTransport.tearDown(self)
 
234
 
 
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'
 
239
        try:
 
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)
 
245
        return working_tree
 
246
 
 
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")
 
252
 
 
253
        self.assertEquals(stdout, """\
 
254
added:
 
255
  hell?
 
256
""")
 
257
 
 
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')
 
263
 
 
264
        self.assertEquals(stdout, u"""\
 
265
added:
 
266
  hell\u00d8
 
267
""".encode('latin-1'))
 
268