Rev 2532: Refactoring run_bzr code into more of a common base. in http://sourcefrog.net/bzr/cleanup-runbzr
Martin Pool
mbp at sourcefrog.net
Tue Jun 26 09:46:24 BST 2007
At http://sourcefrog.net/bzr/cleanup-runbzr
------------------------------------------------------------
revno: 2532
revision-id: mbp at sourcefrog.net-20070626084623-5l1ej6s026urooi1
parent: mbp at sourcefrog.net-20070626080236-14ihgomtffc9tezj
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: cleanup-runbzr
timestamp: Tue 2007-06-26 18:46:23 +1000
message:
Refactoring run_bzr code into more of a common base.
modified:
bzrlib/tests/__init__.py selftest.py-20050531073622-8d0e3c8845c97a64
bzrlib/tests/test_merge_core.py test_merge_core.py-20050824132511-eb99b23a0eec641b
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py 2007-06-26 08:02:36 +0000
+++ b/bzrlib/tests/__init__.py 2007-06-26 08:46:23 +0000
@@ -1237,6 +1237,12 @@
:param stdin: A string to be used as stdin for the command.
:param working_dir: Change to this directory before running
"""
+ return self._run_bzr_autosplit(argv, retcode=retcode,
+ encoding=encoding, stdin=stdin, working_dir=working_dir,
+ )
+
+ def _run_bzr_autosplit(self, args, retcode, encoding, stdin,
+ working_dir):
if encoding is None:
encoding = bzrlib.user_encoding
stdout = StringIOWrapper()
@@ -1244,13 +1250,13 @@
stdout.encoding = encoding
stderr.encoding = encoding
- if isinstance(argv, basestring):
- argv = shlex.split(argv)
- elif isinstance(argv, tuple):
- if len(argv) == 1 and isinstance(argv[0], basestring):
- argv = shlex.split(argv[0])
+ if isinstance(args, basestring):
+ args = shlex.split(args)
+ elif isinstance(args, tuple):
+ if len(args) == 1 and isinstance(args[0], basestring):
+ args = shlex.split(args[0])
- self.log('run bzr: %r', argv)
+ self.log('run bzr: %r', args)
# FIXME: don't call into logging here
handler = logging.StreamHandler(stderr)
handler.setLevel(logging.INFO)
@@ -1269,9 +1275,9 @@
debug.debug_flags.clear()
try:
result = self.apply_redirected(ui.ui_factory.stdin,
- stdout, stderr,
- bzrlib.commands.run_bzr_catch_errors,
- argv)
+ stdout, stderr,
+ bzrlib.commands.run_bzr_catch_errors,
+ args)
finally:
debug.debug_flags.update(saved_debug_flags)
finally:
@@ -1294,13 +1300,30 @@
def run_bzr(self, *args, **kwargs):
"""Invoke bzr, as if it were run from the command line.
+ The argument list should not include the bzr program name - the
+ first argument is normally the bzr command. Arguments may be
+ passed in three ways:
+
+ 1- A list of strings, eg ["commit", "a"]. This is recommended
+ when the command contains whitespace or metacharacters, or
+ is built up at run time.
+
+ 2- A single string, eg "add a". This is the most convenient
+ for hardcoded commands.
+
+ 3- Several varargs parameters, eg run_bzr("add", "a").
+ This is deprecated.
+
This should be the main method for tests that want to exercise the
overall behavior of the bzr application (rather than a unit test
or a functional test of the library.)
:param stdin: A string to be used as stdin for the command.
- :param retcode: The status code the command should return
+ :param retcode: The status code the command should return;
+ default 0.
:param working_dir: The directory to run the command in
+ :param error_regexes: A list of expected error messages. If
+ specified they must be seen in the error output of the command.
"""
retcode = kwargs.pop('retcode', 0)
encoding = kwargs.pop('encoding', None)
@@ -1308,14 +1331,18 @@
working_dir = kwargs.pop('working_dir', None)
error_regexes = kwargs.pop('error_regexes', [])
- if len(args) != 1:
+ if len(args) == 1:
+ if isinstance(args[0], (list, basestring)):
+ args = args[0]
+ else:
warnings.warn("passing varargs to run_bzr is deprecated "
"from bzr 0.18 onwards; please pass a list or "
"string instead")
- out, err = self.run_bzr_captured(retcode=retcode,
+ out, err = self._run_bzr_autosplit(args=args,
+ retcode=retcode,
encoding=encoding, stdin=stdin, working_dir=working_dir,
- argv=args)
+ )
for regex in error_regexes:
self.assertContainsRe(err, regex)
=== modified file 'bzrlib/tests/test_merge_core.py'
--- a/bzrlib/tests/test_merge_core.py 2007-03-25 12:46:04 +0000
+++ b/bzrlib/tests/test_merge_core.py 2007-06-26 08:46:23 +0000
@@ -522,7 +522,7 @@
file('a/file', 'wb').write('contents\n')
wta.add('file')
wta.commit('a_revision', allow_pointless=False)
- self.run_bzr('branch', 'a', 'b')
+ self.run_bzr(['branch', 'a', 'b'])
os.remove('a/file')
wta.commit('removed file', allow_pointless=False)
file('b/file', 'wb').write('changed contents\n')
@@ -537,7 +537,7 @@
file('a/file', 'wb').write('contents\n')
a_wt.add('file')
a_wt.commit('r0')
- self.run_bzr('branch', 'a', 'b')
+ self.run_bzr(['branch', 'a', 'b'])
b_wt = WorkingTree.open('b')
os.chmod('b/file', 0755)
os.remove('a/file')
@@ -555,7 +555,7 @@
a_wt.add('un', 'un-id')
a_wt.add('deux', 'deux-id')
a_wt.commit('r0', rev_id='r0')
- self.run_bzr('branch', 'a', 'b')
+ self.run_bzr(['branch', 'a', 'b'])
b_wt = WorkingTree.open('b')
b_wt.rename_one('un','tmp')
b_wt.rename_one('deux','un')
@@ -573,7 +573,7 @@
file('a/file', 'wb').write('THIS')
a_wt.add('file')
a_wt.commit('r0')
- self.run_bzr('branch', 'a', 'b')
+ self.run_bzr(['branch', 'a', 'b'])
b_wt = WorkingTree.open('b')
os.remove('b/file')
b_wt.commit('r1')
@@ -602,7 +602,7 @@
file('a/foo', 'wb').write('A/FOO')
a_wt.add('foo')
a_wt.commit('added foo')
- self.run_bzr('branch', 'a', 'b')
+ self.run_bzr(['branch', 'a', 'b'])
b_wt = WorkingTree.open('b')
b_wt.rename_one('foo', 'bar')
file('b/foo', 'wb').write('B/FOO')
@@ -630,7 +630,7 @@
file('a/foo', 'wb').write('A/FOO')
a_wt.add('foo')
a_wt.commit('added foo')
- self.run_bzr('branch', 'a', 'b')
+ self.run_bzr('branch a b')
b_wt = WorkingTree.open('b')
os.mkdir('b/bar')
b_wt.add('bar')
@@ -659,7 +659,7 @@
a_wt.add('foo')
a_wt.add('foo/bar')
a_wt.commit('added foo/bar')
- self.run_bzr('branch', 'a', 'b')
+ self.run_bzr(['branch', 'a', 'b'])
b_wt = WorkingTree.open('b')
b_wt.rename_one('foo/bar', 'bar')
os.rmdir('b/foo')
@@ -688,7 +688,7 @@
a_wt.add('foo')
a_wt.add('bar')
a_wt.commit('added foo and bar')
- self.run_bzr('branch', 'a', 'b')
+ self.run_bzr(['branch', 'a', 'b'])
b_wt = WorkingTree.open('b')
os.unlink('b/foo')
b_wt.remove('foo')
More information about the bazaar-commits
mailing list