/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
2
#
3
# This program is free software; you can redistribute it and/or modify
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
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.
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
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
"""UI tests for the test framework."""
18
1963.1.1 by John Arbash Meinel
run_bzr_subprocess() can take an env_changes parameter
19
import os
1910.17.2 by Andrew Bennetts
Add start_bzr_subprocess and stop_bzr_subprocess to allow test code to continue
20
import signal
1692.3.3 by Robert Collins
Get run_bzr in tests to always assign a new, clean ui factory.
21
import sys
22
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
23
import bzrlib
1963.1.2 by John Arbash Meinel
Cleanups suggested by Martin, add test that env_changes can remove an env variable
24
from bzrlib import (
25
    osutils,
26
    )
1551.2.47 by abentley
Fixed test_selftest's use of sftp
27
from bzrlib.errors import ParamikoNotPresent
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
28
from bzrlib.tests import (
29
                          TestCase,
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
30
                          TestCaseWithMemoryTransport,
1819.1.3 by Carl Friedrich Bolz
(lifeless, cfbolz): Add recording of benchmark results to the benchmark history
31
                          TestCaseWithTransport,
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
32
                          TestSkipped,
33
                          )
1687.1.2 by Robert Collins
Add stdin parameter to run_bzr and run_bzr_captured.
34
from bzrlib.tests.blackbox import ExternalBase
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
35
36
37
class TestOptions(TestCase):
38
39
    current_test = None
40
41
    def test_transport_set_to_sftp(self):
42
        # test the --transport option has taken effect from within the
43
        # test_transport test
1551.2.47 by abentley
Fixed test_selftest's use of sftp
44
        try:
45
            import bzrlib.transport.sftp
46
        except ParamikoNotPresent:
47
            raise TestSkipped("Paramiko not present")
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
48
        if TestOptions.current_test != "test_transport_set_to_sftp":
49
            return
50
        self.assertEqual(bzrlib.transport.sftp.SFTPAbsoluteServer,
51
                         bzrlib.tests.default_transport)
52
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
53
    def test_transport_set_to_memory(self):
54
        # test the --transport option has taken effect from within the
55
        # test_transport test
56
        import bzrlib.transport.memory
57
        if TestOptions.current_test != "test_transport_set_to_memory":
58
            return
59
        self.assertEqual(bzrlib.transport.memory.MemoryServer,
60
                         bzrlib.tests.default_transport)
61
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
62
    def test_transport(self):
63
        # test that --transport=sftp works
1551.2.47 by abentley
Fixed test_selftest's use of sftp
64
        try:
65
            import bzrlib.transport.sftp
66
        except ParamikoNotPresent:
67
            raise TestSkipped("Paramiko not present")
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
68
        old_transport = bzrlib.tests.default_transport
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
69
        old_root = TestCaseWithMemoryTransport.TEST_ROOT
70
        TestCaseWithMemoryTransport.TEST_ROOT = None
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
71
        try:
72
            TestOptions.current_test = "test_transport_set_to_sftp"
73
            stdout = self.capture('selftest --transport=sftp test_transport_set_to_sftp')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
74
            
75
            self.assertContainsRe(stdout, 'Ran 1 test')
76
            self.assertEqual(old_transport, bzrlib.tests.default_transport)
77
78
            TestOptions.current_test = "test_transport_set_to_memory"
79
            stdout = self.capture('selftest --transport=memory test_transport_set_to_memory')
1534.4.25 by Robert Collins
Add a --transport parameter to the test suite to set the default transport to be used in the test suite.
80
            self.assertContainsRe(stdout, 'Ran 1 test')
81
            self.assertEqual(old_transport, bzrlib.tests.default_transport)
82
        finally:
83
            bzrlib.tests.default_transport = old_transport
84
            TestOptions.current_test = None
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
85
            TestCaseWithMemoryTransport.TEST_ROOT = old_root
1687.1.2 by Robert Collins
Add stdin parameter to run_bzr and run_bzr_captured.
86
87
88
class TestRunBzr(ExternalBase):
89
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
90
    def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
91
                         working_dir=None):
2027.5.3 by John Arbash Meinel
Add docstring to why run_bzr_captured is overridden
92
        """Override run_bzr_captured to test how it is invoked by run_bzr.
93
94
        We test how run_bzr_captured actually invokes bzr in another location.
95
        Here we only need to test that it is run_bzr passes the right
96
        parameters to run_bzr_captured.
97
        """
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
98
        self.argv = argv
99
        self.retcode = retcode
100
        self.encoding = encoding
1687.1.2 by Robert Collins
Add stdin parameter to run_bzr and run_bzr_captured.
101
        self.stdin = stdin
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
102
        self.working_dir = working_dir
103
104
    def test_args(self):
105
        """Test that run_bzr passes args correctly to run_bzr_captured"""
106
        self.run_bzr('arg1', 'arg2', 'arg3', retcode=1)
107
        self.assertEqual(('arg1', 'arg2', 'arg3'), self.argv)
108
109
    def test_encoding(self):
110
        """Test that run_bzr passes encoding to run_bzr_captured"""
111
        self.run_bzr('foo', 'bar')
112
        self.assertEqual(None, self.encoding)
113
        self.assertEqual(('foo', 'bar'), self.argv)
114
115
        self.run_bzr('foo', 'bar', encoding='baz')
116
        self.assertEqual('baz', self.encoding)
117
        self.assertEqual(('foo', 'bar'), self.argv)
118
119
    def test_retcode(self):
120
        """Test that run_bzr passes retcode to run_bzr_captured"""
121
        # Default is retcode == 0
122
        self.run_bzr('foo', 'bar')
123
        self.assertEqual(0, self.retcode)
124
        self.assertEqual(('foo', 'bar'), self.argv)
125
126
        self.run_bzr('foo', 'bar', retcode=1)
127
        self.assertEqual(1, self.retcode)
128
        self.assertEqual(('foo', 'bar'), self.argv)
129
130
        self.run_bzr('foo', 'bar', retcode=None)
131
        self.assertEqual(None, self.retcode)
132
        self.assertEqual(('foo', 'bar'), self.argv)
133
134
        self.run_bzr('foo', 'bar', retcode=3)
135
        self.assertEqual(3, self.retcode)
136
        self.assertEqual(('foo', 'bar'), self.argv)
1687.1.2 by Robert Collins
Add stdin parameter to run_bzr and run_bzr_captured.
137
138
    def test_stdin(self):
139
        # test that the stdin keyword to run_bzr is passed through to
1687.1.15 by Robert Collins
Review comments.
140
        # run_bzr_captured as-is. We do this by overriding
141
        # run_bzr_captured in this class, and then calling run_bzr,
142
        # which is a convenience function for run_bzr_captured, so 
143
        # should invoke it.
1687.1.2 by Robert Collins
Add stdin parameter to run_bzr and run_bzr_captured.
144
        self.run_bzr('foo', 'bar', stdin='gam')
145
        self.assertEqual('gam', self.stdin)
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
146
        self.assertEqual(('foo', 'bar'), self.argv)
147
1687.1.2 by Robert Collins
Add stdin parameter to run_bzr and run_bzr_captured.
148
        self.run_bzr('foo', 'bar', stdin='zippy')
149
        self.assertEqual('zippy', self.stdin)
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
150
        self.assertEqual(('foo', 'bar'), self.argv)
151
152
    def test_working_dir(self):
153
        """Test that run_bzr passes working_dir to run_bzr_captured"""
154
        self.run_bzr('foo', 'bar')
155
        self.assertEqual(None, self.working_dir)
156
        self.assertEqual(('foo', 'bar'), self.argv)
157
158
        self.run_bzr('foo', 'bar', working_dir='baz')
159
        self.assertEqual('baz', self.working_dir)
160
        self.assertEqual(('foo', 'bar'), self.argv)
1687.1.2 by Robert Collins
Add stdin parameter to run_bzr and run_bzr_captured.
161
162
1819.1.3 by Carl Friedrich Bolz
(lifeless, cfbolz): Add recording of benchmark results to the benchmark history
163
class TestBenchmarkTests(TestCaseWithTransport):
164
165
    def test_benchmark_runs_benchmark_tests(self):
166
        """bzr selftest --benchmark should not run the default test suite."""
167
        # We test this by passing a regression test name to --benchmark, which
168
        # should result in 0 rests run.
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
169
        old_root = TestCaseWithMemoryTransport.TEST_ROOT
1819.1.3 by Carl Friedrich Bolz
(lifeless, cfbolz): Add recording of benchmark results to the benchmark history
170
        try:
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
171
            TestCaseWithMemoryTransport.TEST_ROOT = None
1819.1.3 by Carl Friedrich Bolz
(lifeless, cfbolz): Add recording of benchmark results to the benchmark history
172
            out, err = self.run_bzr('selftest', '--benchmark', 'workingtree_implementations')
173
        finally:
1986.2.3 by Robert Collins
New test base class TestCaseWithMemoryTransport offers memory-only
174
            TestCaseWithMemoryTransport.TEST_ROOT = old_root
1819.1.3 by Carl Friedrich Bolz
(lifeless, cfbolz): Add recording of benchmark results to the benchmark history
175
        self.assertContainsRe(out, 'Ran 0 tests.*\n\nOK')
176
        self.assertEqual(
177
            'running tests...\ntests passed\n',
178
            err)
179
        benchfile = open(".perf_history", "rt")
180
        try:
181
            lines = benchfile.readlines()
182
        finally:
183
            benchfile.close()
184
        self.assertEqual(1, len(lines))
185
        self.assertContainsRe(lines[0], "--date [0-9.]+")
186
187
1687.1.2 by Robert Collins
Add stdin parameter to run_bzr and run_bzr_captured.
188
class TestRunBzrCaptured(ExternalBase):
189
190
    def apply_redirected(self, stdin=None, stdout=None, stderr=None,
191
                         a_callable=None, *args, **kwargs):
192
        self.stdin = stdin
1687.1.11 by Robert Collins
Teach TestCase.run_bzr_captured about the ui factories.
193
        self.factory_stdin = getattr(bzrlib.ui.ui_factory, "stdin", None)
1692.3.3 by Robert Collins
Get run_bzr in tests to always assign a new, clean ui factory.
194
        self.factory = bzrlib.ui.ui_factory
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
195
        self.working_dir = osutils.getcwd()
1692.3.3 by Robert Collins
Get run_bzr in tests to always assign a new, clean ui factory.
196
        stdout.write('foo\n')
197
        stderr.write('bar\n')
1687.1.2 by Robert Collins
Add stdin parameter to run_bzr and run_bzr_captured.
198
        return 0
199
200
    def test_stdin(self):
201
        # test that the stdin keyword to run_bzr_captured is passed through to
1687.1.15 by Robert Collins
Review comments.
202
        # apply_redirected as a StringIO. We do this by overriding
203
        # apply_redirected in this class, and then calling run_bzr_captured,
204
        # which calls apply_redirected. 
1687.1.2 by Robert Collins
Add stdin parameter to run_bzr and run_bzr_captured.
205
        self.run_bzr_captured(['foo', 'bar'], stdin='gam')
206
        self.assertEqual('gam', self.stdin.read())
1687.1.11 by Robert Collins
Teach TestCase.run_bzr_captured about the ui factories.
207
        self.assertTrue(self.stdin is self.factory_stdin)
1687.1.2 by Robert Collins
Add stdin parameter to run_bzr and run_bzr_captured.
208
        self.run_bzr_captured(['foo', 'bar'], stdin='zippy')
209
        self.assertEqual('zippy', self.stdin.read())
1687.1.11 by Robert Collins
Teach TestCase.run_bzr_captured about the ui factories.
210
        self.assertTrue(self.stdin is self.factory_stdin)
1692.3.3 by Robert Collins
Get run_bzr in tests to always assign a new, clean ui factory.
211
212
    def test_ui_factory(self):
213
        # each invocation of self.run_bzr_captured should get its own UI
214
        # factory, which is an instance of TestUIFactory, with stdout and
215
        # stderr attached to the stdout and stderr of the invoked
216
        # run_bzr_captured
217
        current_factory = bzrlib.ui.ui_factory
218
        self.run_bzr_captured(['foo'])
219
        self.failIf(current_factory is self.factory)
220
        self.assertNotEqual(sys.stdout, self.factory.stdout)
221
        self.assertNotEqual(sys.stderr, self.factory.stderr)
222
        self.assertEqual('foo\n', self.factory.stdout.getvalue())
223
        self.assertEqual('bar\n', self.factory.stderr.getvalue())
224
        self.assertIsInstance(self.factory, bzrlib.tests.blackbox.TestUIFactory)
1871.1.1 by Robert Collins
Relocate bzrlib selftest external output tests to bzrlib/tests/blackbox/test_selftest.py.
225
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
226
    def test_working_dir(self):
227
        self.build_tree(['one/', 'two/'])
228
        cwd = osutils.getcwd()
229
230
        # Default is to work in the current directory
231
        self.run_bzr_captured(['foo', 'bar'])
232
        self.assertEqual(cwd, self.working_dir)
233
234
        self.run_bzr_captured(['foo', 'bar'], working_dir=None)
235
        self.assertEqual(cwd, self.working_dir)
236
2027.5.2 by John Arbash Meinel
add tests that the working directory is preserved, cleanup run_bzr_subprocess
237
        # The function should be run in the alternative directory
238
        # but afterwards the current working dir shouldn't be changed
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
239
        self.run_bzr_captured(['foo', 'bar'], working_dir='one')
240
        self.assertNotEqual(cwd, self.working_dir)
241
        self.assertEndsWith(self.working_dir, 'one')
2027.5.2 by John Arbash Meinel
add tests that the working directory is preserved, cleanup run_bzr_subprocess
242
        self.assertEqual(cwd, osutils.getcwd())
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
243
244
        self.run_bzr_captured(['foo', 'bar'], working_dir='two')
245
        self.assertNotEqual(cwd, self.working_dir)
246
        self.assertEndsWith(self.working_dir, 'two')
2027.5.2 by John Arbash Meinel
add tests that the working directory is preserved, cleanup run_bzr_subprocess
247
        self.assertEqual(cwd, osutils.getcwd())
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
248
249
250
class TestRunBzrSubprocess(TestCaseWithTransport):
251
1871.1.1 by Robert Collins
Relocate bzrlib selftest external output tests to bzrlib/tests/blackbox/test_selftest.py.
252
    def test_run_bzr_subprocess(self):
253
        """The run_bzr_helper_external comand behaves nicely."""
254
        result = self.run_bzr_subprocess('--version')
255
        result = self.run_bzr_subprocess('--version', retcode=None)
256
        self.assertContainsRe(result[0], 'is free software')
257
        self.assertRaises(AssertionError, self.run_bzr_subprocess, 
258
                          '--versionn')
259
        result = self.run_bzr_subprocess('--versionn', retcode=3)
260
        result = self.run_bzr_subprocess('--versionn', retcode=None)
261
        self.assertContainsRe(result[1], 'unknown command')
1857.1.20 by Aaron Bentley
Strip out all the EnumOption stuff
262
        err = self.run_bzr_subprocess('merge', '--merge-type', 'magic merge', 
263
                                      retcode=3)[1]
264
        self.assertContainsRe(err, 'No known merge type magic merge')
1871.1.1 by Robert Collins
Relocate bzrlib selftest external output tests to bzrlib/tests/blackbox/test_selftest.py.
265
1963.1.1 by John Arbash Meinel
run_bzr_subprocess() can take an env_changes parameter
266
    def test_run_bzr_subprocess_env(self):
267
        """run_bzr_subprocess can set environment variables in the child only.
268
269
        These changes should not change the running process, only the child.
270
        """
271
        # The test suite should unset this variable
272
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
273
        out, err = self.run_bzr_subprocess('whoami', env_changes={
274
                                            'BZR_EMAIL':'Joe Foo <joe@foo.com>'
1963.1.11 by John Arbash Meinel
Add a universal_newlines flag to run_bzr_subprocess, so we can be line-ending independent for tests
275
                                          }, universal_newlines=True)
1963.1.1 by John Arbash Meinel
run_bzr_subprocess() can take an env_changes parameter
276
        self.assertEqual('', err)
277
        self.assertEqual('Joe Foo <joe@foo.com>\n', out)
278
        # And it should not be modified
279
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
280
281
        # Do it again with a different address, just to make sure
282
        # it is actually changing
283
        out, err = self.run_bzr_subprocess('whoami', env_changes={
284
                                            'BZR_EMAIL':'Barry <bar@foo.com>'
1963.1.11 by John Arbash Meinel
Add a universal_newlines flag to run_bzr_subprocess, so we can be line-ending independent for tests
285
                                          }, universal_newlines=True)
1963.1.1 by John Arbash Meinel
run_bzr_subprocess() can take an env_changes parameter
286
        self.assertEqual('', err)
287
        self.assertEqual('Barry <bar@foo.com>\n', out)
288
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
289
1963.1.2 by John Arbash Meinel
Cleanups suggested by Martin, add test that env_changes can remove an env variable
290
    def test_run_bzr_subprocess_env_del(self):
291
        """run_bzr_subprocess can remove environment variables too."""
292
        # Create a random email, so we are sure this won't collide
293
        rand_bzr_email = 'John Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
294
        rand_email = 'Jane Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
295
        os.environ['BZR_EMAIL'] = rand_bzr_email
296
        os.environ['EMAIL'] = rand_email
297
        try:
298
            # By default, the child will inherit the current env setting
1963.1.11 by John Arbash Meinel
Add a universal_newlines flag to run_bzr_subprocess, so we can be line-ending independent for tests
299
            out, err = self.run_bzr_subprocess('whoami', universal_newlines=True)
1963.1.2 by John Arbash Meinel
Cleanups suggested by Martin, add test that env_changes can remove an env variable
300
            self.assertEqual('', err)
301
            self.assertEqual(rand_bzr_email + '\n', out)
302
303
            # Now that BZR_EMAIL is not set, it should fall back to EMAIL
304
            out, err = self.run_bzr_subprocess('whoami',
1963.1.11 by John Arbash Meinel
Add a universal_newlines flag to run_bzr_subprocess, so we can be line-ending independent for tests
305
                                               env_changes={'BZR_EMAIL':None},
306
                                               universal_newlines=True)
1963.1.2 by John Arbash Meinel
Cleanups suggested by Martin, add test that env_changes can remove an env variable
307
            self.assertEqual('', err)
308
            self.assertEqual(rand_email + '\n', out)
309
310
            # This switches back to the default email guessing logic
311
            # Which shouldn't match either of the above addresses
312
            out, err = self.run_bzr_subprocess('whoami',
1963.1.11 by John Arbash Meinel
Add a universal_newlines flag to run_bzr_subprocess, so we can be line-ending independent for tests
313
                           env_changes={'BZR_EMAIL':None, 'EMAIL':None},
314
                           universal_newlines=True)
1963.1.2 by John Arbash Meinel
Cleanups suggested by Martin, add test that env_changes can remove an env variable
315
316
            self.assertEqual('', err)
317
            self.assertNotEqual(rand_bzr_email + '\n', out)
318
            self.assertNotEqual(rand_email + '\n', out)
319
        finally:
320
            # TestCase cleans up BZR_EMAIL, and EMAIL at startup
321
            del os.environ['BZR_EMAIL']
322
            del os.environ['EMAIL']
1871.1.1 by Robert Collins
Relocate bzrlib selftest external output tests to bzrlib/tests/blackbox/test_selftest.py.
323
1963.1.4 by John Arbash Meinel
env_changes={} should be safe to remove variables that aren't there
324
    def test_run_bzr_subprocess_env_del_missing(self):
325
        """run_bzr_subprocess won't fail if deleting a nonexistant env var"""
326
        self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
327
        out, err = self.run_bzr_subprocess('rocks',
1963.1.11 by John Arbash Meinel
Add a universal_newlines flag to run_bzr_subprocess, so we can be line-ending independent for tests
328
                        env_changes={'NON_EXISTANT_ENV_VAR':None},
329
                        universal_newlines=True)
330
        self.assertEqual('it sure does!\n', out)
331
        self.assertEqual('', err)
1963.1.4 by John Arbash Meinel
env_changes={} should be safe to remove variables that aren't there
332
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
333
    def test_run_bzr_subprocess_working_dir(self):
2027.5.2 by John Arbash Meinel
add tests that the working directory is preserved, cleanup run_bzr_subprocess
334
        """Test that we can specify the working dir for the child"""
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
335
        cwd = osutils.getcwd()
336
337
        self.make_branch_and_tree('.')
338
        self.make_branch_and_tree('one')
339
        self.make_branch_and_tree('two')
340
2027.5.2 by John Arbash Meinel
add tests that the working directory is preserved, cleanup run_bzr_subprocess
341
        def get_root(**kwargs):
342
            """Spawn a process to get the 'root' of the tree.
343
344
            You can pass in arbitrary new arguments. This just makes
345
            sure that the returned path doesn't have trailing whitespace.
346
            """
347
            return self.run_bzr_subprocess('root', **kwargs)[0].rstrip()
348
349
        self.assertEqual(cwd, get_root())
350
        self.assertEqual(cwd, get_root(working_dir=None))
351
        # Has our path changed?
352
        self.assertEqual(cwd, osutils.getcwd())
353
354
        dir1 = get_root(working_dir='one')
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
355
        self.assertEndsWith(dir1, 'one')
2027.5.2 by John Arbash Meinel
add tests that the working directory is preserved, cleanup run_bzr_subprocess
356
        self.assertEqual(cwd, osutils.getcwd())
357
358
        dir2 = get_root(working_dir='two')
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
359
        self.assertEndsWith(dir2, 'two')
2027.5.2 by John Arbash Meinel
add tests that the working directory is preserved, cleanup run_bzr_subprocess
360
        self.assertEqual(cwd, osutils.getcwd())
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
361
362
2067.2.2 by John Arbash Meinel
Review comments from Robert
363
class _DontSpawnProcess(Exception):
364
    """A simple exception which just allows us to skip unnecessary steps"""
365
366
367
class TestRunBzrSubprocessCommands(TestCaseWithTransport):
368
369
    def _popen(self, *args, **kwargs):
370
        """Record the command that is run, so that we can ensure it is correct"""
371
        self._popen_args = args
372
        self._popen_kwargs = kwargs
373
        raise _DontSpawnProcess()
374
375
    def test_run_bzr_subprocess_no_plugins(self):
376
        self.assertRaises(_DontSpawnProcess, self.run_bzr_subprocess)
377
        command = self._popen_args[0]
378
        self.assertEqual(sys.executable, command[0])
379
        self.assertEqual(self.get_bzr_path(), command[1])
380
        self.assertEqual(['--no-plugins'], command[2:])
381
382
    def test_allow_plugins(self):
2067.2.4 by John Arbash Meinel
fixup one test
383
        self.assertRaises(_DontSpawnProcess,
384
                          self.run_bzr_subprocess, allow_plugins=True)
2067.2.2 by John Arbash Meinel
Review comments from Robert
385
        command = self._popen_args[0]
386
        self.assertEqual([], command[2:])
387
388
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
389
class TestBzrSubprocess(TestCaseWithTransport):
390
1910.17.2 by Andrew Bennetts
Add start_bzr_subprocess and stop_bzr_subprocess to allow test code to continue
391
    def test_start_and_stop_bzr_subprocess(self):
392
        """We can start and perform other test actions while that process is
393
        still alive.
394
        """
1910.17.8 by Andrew Bennetts
Refactor run_bzr_subprocess to use start_bzr_subprocess and finish_bzr_subprocess.
395
        process = self.start_bzr_subprocess(['--version'])
1910.17.2 by Andrew Bennetts
Add start_bzr_subprocess and stop_bzr_subprocess to allow test code to continue
396
        result = self.finish_bzr_subprocess(process)
397
        self.assertContainsRe(result[0], 'is free software')
398
        self.assertEqual('', result[1])
399
400
    def test_start_and_stop_bzr_subprocess_with_error(self):
401
        """finish_bzr_subprocess allows specification of the desired exit code.
402
        """
1910.17.8 by Andrew Bennetts
Refactor run_bzr_subprocess to use start_bzr_subprocess and finish_bzr_subprocess.
403
        process = self.start_bzr_subprocess(['--versionn'])
1910.17.2 by Andrew Bennetts
Add start_bzr_subprocess and stop_bzr_subprocess to allow test code to continue
404
        result = self.finish_bzr_subprocess(process, retcode=3)
405
        self.assertEqual('', result[0])
406
        self.assertContainsRe(result[1], 'unknown command')
407
408
    def test_start_and_stop_bzr_subprocess_ignoring_retcode(self):
409
        """finish_bzr_subprocess allows the exit code to be ignored."""
1910.17.8 by Andrew Bennetts
Refactor run_bzr_subprocess to use start_bzr_subprocess and finish_bzr_subprocess.
410
        process = self.start_bzr_subprocess(['--versionn'])
1910.17.2 by Andrew Bennetts
Add start_bzr_subprocess and stop_bzr_subprocess to allow test code to continue
411
        result = self.finish_bzr_subprocess(process, retcode=None)
412
        self.assertEqual('', result[0])
413
        self.assertContainsRe(result[1], 'unknown command')
414
415
    def test_start_and_stop_bzr_subprocess_with_unexpected_retcode(self):
416
        """finish_bzr_subprocess raises self.failureException if the retcode is
417
        not the expected one.
418
        """
1910.17.8 by Andrew Bennetts
Refactor run_bzr_subprocess to use start_bzr_subprocess and finish_bzr_subprocess.
419
        process = self.start_bzr_subprocess(['--versionn'])
1910.17.2 by Andrew Bennetts
Add start_bzr_subprocess and stop_bzr_subprocess to allow test code to continue
420
        self.assertRaises(self.failureException, self.finish_bzr_subprocess,
421
                          process, retcode=0)
422
        
423
    def test_start_and_stop_bzr_subprocess_send_signal(self):
424
        """finish_bzr_subprocess raises self.failureException if the retcode is
425
        not the expected one.
426
        """
1910.17.9 by Andrew Bennetts
Add skip_if_plan_to_signal flag to start_bzr_subprocess.
427
        process = self.start_bzr_subprocess(['wait-until-signalled'],
428
                                            skip_if_plan_to_signal=True)
1910.17.2 by Andrew Bennetts
Add start_bzr_subprocess and stop_bzr_subprocess to allow test code to continue
429
        self.assertEqual('running\n', process.stdout.readline())
430
        result = self.finish_bzr_subprocess(process, send_signal=signal.SIGINT,
431
                                            retcode=3)
432
        self.assertEqual('', result[0])
433
        self.assertEqual('bzr: interrupted\n', result[1])
2027.5.1 by John Arbash Meinel
Add working_dir=XX to run_bzr_* functions, and clean up tests
434
435
    def test_start_and_stop_working_dir(self):
436
        cwd = osutils.getcwd()
437
438
        self.make_branch_and_tree('one')
439
440
        process = self.start_bzr_subprocess(['root'], working_dir='one')
441
        result = self.finish_bzr_subprocess(process)
442
        self.assertEndsWith(result[0], 'one\n')
443
        self.assertEqual('', result[1])
1910.17.2 by Andrew Bennetts
Add start_bzr_subprocess and stop_bzr_subprocess to allow test code to continue
444
        
1963.1.4 by John Arbash Meinel
env_changes={} should be safe to remove variables that aren't there
445
1871.1.1 by Robert Collins
Relocate bzrlib selftest external output tests to bzrlib/tests/blackbox/test_selftest.py.
446
class TestRunBzrError(ExternalBase):
447
448
    def test_run_bzr_error(self):
449
        out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
450
        self.assertEqual(out, 'it sure does!\n')
451
452
        out, err = self.run_bzr_error(["'foobarbaz' is not a versioned file"],
453
                                      'file-id', 'foobarbaz')