1916
1917
if not feature.available():
1917
1918
raise UnavailableFeature(feature)
1919
def _run_bzr_autosplit(self, args, retcode, encoding, stdin,
1921
"""Run bazaar command line, splitting up a string command line."""
1922
if isinstance(args, string_types):
1923
args = shlex.split(args)
1924
return self._run_bzr_core(args, retcode=retcode,
1925
encoding=encoding, stdin=stdin, working_dir=working_dir,
1928
def _run_bzr_core(self, args, retcode, encoding, stdin,
1920
def _run_bzr_core(self, args, encoding, stdin, stdout, stderr,
1930
1922
# Clear chk_map page cache, because the contents are likely to mask
1931
1923
# locking errors.
1932
1924
chk_map.clear_cache()
1933
if encoding is None:
1934
encoding = osutils.get_user_encoding()
1936
1926
self.log('run brz: %r', args)
1938
if sys.version_info[0] == 2:
1939
stdout = ui_testing.BytesIOWithEncoding()
1940
stderr = ui_testing.BytesIOWithEncoding()
1941
stdout.encoding = stderr.encoding = encoding
1929
self._last_cmd_stdout = stdout
1930
self._last_cmd_stderr = stderr
1943
stdout = ui_testing.StringIOWithEncoding()
1944
stderr = ui_testing.StringIOWithEncoding()
1945
stdout.encoding = stderr.encoding = encoding
1947
# FIXME: don't call into logging here
1948
handler = trace.EncodedStreamHandler(
1949
stderr, errors="replace", level=logging.INFO)
1950
logger = logging.getLogger('')
1951
logger.addHandler(handler)
1953
self._last_cmd_stdout = codecs.getwriter(encoding)(stdout)
1954
self._last_cmd_stderr = codecs.getwriter(encoding)(stderr)
1932
self._last_cmd_stdout = codecs.getwriter(encoding)(stdout)
1933
self._last_cmd_stderr = codecs.getwriter(encoding)(stderr)
1956
1935
old_ui_factory = ui.ui_factory
1957
1936
ui.ui_factory = ui_testing.TestUIFactory(
1972
1951
_mod_commands.run_bzr_catch_user_errors,
1975
logger.removeHandler(handler)
1976
1954
ui.ui_factory = old_ui_factory
1977
1955
if cwd is not None:
1960
def run_bzr_raw(self, args, retcode=0, stdin=None, encoding=None,
1961
working_dir=None, error_regexes=[]):
1962
"""Invoke brz, as if it were run from the command line.
1964
The argument list should not include the brz program name - the
1965
first argument is normally the brz command. Arguments may be
1966
passed in three ways:
1968
1- A list of strings, eg ["commit", "a"]. This is recommended
1969
when the command contains whitespace or metacharacters, or
1970
is built up at run time.
1972
2- A single string, eg "add a". This is the most convenient
1973
for hardcoded commands.
1975
This runs brz through the interface that catches and reports
1976
errors, and with logging set to something approximating the
1977
default, so that error reporting can be checked.
1979
This should be the main method for tests that want to exercise the
1980
overall behavior of the brz application (rather than a unit test
1981
or a functional test of the library.)
1983
This sends the stdout/stderr results into the test's log,
1984
where it may be useful for debugging. See also run_captured.
1986
:keyword stdin: A string to be used as stdin for the command.
1987
:keyword retcode: The status code the command should return;
1989
:keyword working_dir: The directory to run the command in
1990
:keyword error_regexes: A list of expected error messages. If
1991
specified they must be seen in the error output of the command.
1993
if isinstance(args, string_types):
1994
args = shlex.split(args)
1996
if encoding is None:
1997
encoding = osutils.get_user_encoding()
1999
if sys.version_info[0] == 2:
2000
wrapped_stdout = stdout = ui_testing.BytesIOWithEncoding()
2001
wrapped_stderr = stderr = ui_testing.BytesIOWithEncoding()
2002
stdout.encoding = stderr.encoding = encoding
2004
# FIXME: don't call into logging here
2005
handler = trace.EncodedStreamHandler(
2006
stderr, errors="replace")
2010
wrapped_stdout = TextIOWrapper(stdout, encoding)
2011
wrapped_stderr = TextIOWrapper(stderr, encoding)
2012
handler = logging.StreamHandler(wrapped_stderr)
2013
handler.setLevel(logging.INFO)
2015
logger = logging.getLogger('')
2016
logger.addHandler(handler)
2018
result = self._run_bzr_core(
2019
args, encoding=encoding, stdin=stdin, stdout=wrapped_stdout,
2020
stderr=wrapped_stderr, working_dir=working_dir,
2023
logger.removeHandler(handler)
2026
wrapped_stdout.flush()
2027
wrapped_stderr.flush()
1980
2029
out = stdout.getvalue()
1981
2030
err = stderr.getvalue()
1986
2035
if retcode is not None:
1987
2036
self.assertEqual(retcode, result,
1988
2037
message='Unexpected return code')
1989
return result, out, err
2038
self.assertIsInstance(error_regexes, (list, tuple))
2039
for regex in error_regexes:
2040
self.assertContainsRe(err, regex)
1991
2043
def run_bzr(self, args, retcode=0, stdin=None, encoding=None,
1992
2044
working_dir=None, error_regexes=[]):
2021
2073
:keyword error_regexes: A list of expected error messages. If
2022
2074
specified they must be seen in the error output of the command.
2024
retcode, out, err = self._run_bzr_autosplit(
2029
working_dir=working_dir,
2076
if isinstance(args, string_types):
2077
args = shlex.split(args)
2079
if encoding is None:
2080
encoding = osutils.get_user_encoding()
2082
if sys.version_info[0] == 2:
2083
stdout = ui_testing.BytesIOWithEncoding()
2084
stderr = ui_testing.BytesIOWithEncoding()
2085
stdout.encoding = stderr.encoding = encoding
2086
# FIXME: don't call into logging here
2087
handler = trace.EncodedStreamHandler(
2088
stderr, errors="replace")
2090
stdout = ui_testing.StringIOWithEncoding()
2091
stderr = ui_testing.StringIOWithEncoding()
2092
stdout.encoding = stderr.encoding = encoding
2093
handler = logging.StreamHandler(stream=stderr)
2094
handler.setLevel(logging.INFO)
2096
logger = logging.getLogger('')
2097
logger.addHandler(handler)
2100
result = self._run_bzr_core(args,
2101
encoding=encoding, stdin=stdin, stdout=stdout,
2102
stderr=stderr, working_dir=working_dir,
2105
logger.removeHandler(handler)
2107
out = stdout.getvalue()
2108
err = stderr.getvalue()
2110
self.log('output:\n%r', out)
2112
self.log('errors:\n%r', err)
2113
if retcode is not None:
2114
self.assertEqual(retcode, result,
2115
message='Unexpected return code')
2031
2116
self.assertIsInstance(error_regexes, (list, tuple))
2032
2117
for regex in error_regexes:
2033
2118
self.assertContainsRe(err, regex)