/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2018-08-21 19:28:52 UTC
  • mfrom: (7078 work)
  • mto: This revision was merged to the branch mainline in revision 7101.
  • Revision ID: jelmer@jelmer.uk-20180821192852-ipy6m8q95bbst223
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
from io import (
34
34
    BytesIO,
35
35
    StringIO,
 
36
    TextIOWrapper,
36
37
    )
37
38
import itertools
38
39
import logging
1916
1917
        if not feature.available():
1917
1918
            raise UnavailableFeature(feature)
1918
1919
 
1919
 
    def _run_bzr_autosplit(self, args, retcode, encoding, stdin,
1920
 
            working_dir):
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,
1926
 
                )
1927
 
 
1928
 
    def _run_bzr_core(self, args, retcode, encoding, stdin,
 
1920
    def _run_bzr_core(self, args, encoding, stdin, stdout, stderr,
1929
1921
            working_dir):
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()
1935
1925
 
1936
1926
        self.log('run brz: %r', args)
1937
1927
 
1938
 
        if sys.version_info[0] == 2:
1939
 
            stdout = ui_testing.BytesIOWithEncoding()
1940
 
            stderr = ui_testing.BytesIOWithEncoding()
1941
 
            stdout.encoding = stderr.encoding = encoding
 
1928
        if PY3:
 
1929
            self._last_cmd_stdout = stdout
 
1930
            self._last_cmd_stderr = stderr
1942
1931
        else:
1943
 
            stdout = ui_testing.StringIOWithEncoding()
1944
 
            stderr = ui_testing.StringIOWithEncoding()
1945
 
            stdout.encoding = stderr.encoding = encoding
1946
 
 
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)
1952
 
 
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)
1955
1934
 
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,
1973
1952
                    args)
1974
1953
        finally:
1975
 
            logger.removeHandler(handler)
1976
1954
            ui.ui_factory = old_ui_factory
1977
1955
            if cwd is not None:
1978
1956
                os.chdir(cwd)
1979
1957
 
 
1958
        return result
 
1959
 
 
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.
 
1963
 
 
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:
 
1967
 
 
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.
 
1971
 
 
1972
        2- A single string, eg "add a".  This is the most convenient
 
1973
        for hardcoded commands.
 
1974
 
 
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.
 
1978
 
 
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.)
 
1982
 
 
1983
        This sends the stdout/stderr results into the test's log,
 
1984
        where it may be useful for debugging.  See also run_captured.
 
1985
 
 
1986
        :keyword stdin: A string to be used as stdin for the command.
 
1987
        :keyword retcode: The status code the command should return;
 
1988
            default 0.
 
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.
 
1992
        """
 
1993
        if isinstance(args, string_types):
 
1994
            args = shlex.split(args)
 
1995
 
 
1996
        if encoding is None:
 
1997
            encoding = osutils.get_user_encoding()
 
1998
 
 
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
 
2003
 
 
2004
            # FIXME: don't call into logging here
 
2005
            handler = trace.EncodedStreamHandler(
 
2006
                stderr, errors="replace")
 
2007
        else:
 
2008
            stdout = BytesIO()
 
2009
            stderr = BytesIO()
 
2010
            wrapped_stdout = TextIOWrapper(stdout, encoding)
 
2011
            wrapped_stderr = TextIOWrapper(stderr, encoding)
 
2012
            handler = logging.StreamHandler(wrapped_stderr)
 
2013
        handler.setLevel(logging.INFO)
 
2014
 
 
2015
        logger = logging.getLogger('')
 
2016
        logger.addHandler(handler)
 
2017
        try:
 
2018
            result = self._run_bzr_core(
 
2019
                    args, encoding=encoding, stdin=stdin, stdout=wrapped_stdout,
 
2020
                    stderr=wrapped_stderr, working_dir=working_dir,
 
2021
                    )
 
2022
        finally:
 
2023
            logger.removeHandler(handler)
 
2024
 
 
2025
        if PY3:
 
2026
            wrapped_stdout.flush()
 
2027
            wrapped_stderr.flush()
 
2028
 
1980
2029
        out = stdout.getvalue()
1981
2030
        err = stderr.getvalue()
1982
2031
        if out:
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)
 
2041
        return out, err
1990
2042
 
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.
2023
2075
        """
2024
 
        retcode, out, err = self._run_bzr_autosplit(
2025
 
            args=args,
2026
 
            retcode=retcode,
2027
 
            encoding=encoding,
2028
 
            stdin=stdin,
2029
 
            working_dir=working_dir,
2030
 
            )
 
2076
        if isinstance(args, string_types):
 
2077
            args = shlex.split(args)
 
2078
 
 
2079
        if encoding is None:
 
2080
            encoding = osutils.get_user_encoding()
 
2081
 
 
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")
 
2089
        else:
 
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)
 
2095
 
 
2096
        logger = logging.getLogger('')
 
2097
        logger.addHandler(handler)
 
2098
 
 
2099
        try:
 
2100
            result = self._run_bzr_core(args,
 
2101
                    encoding=encoding, stdin=stdin, stdout=stdout,
 
2102
                    stderr=stderr, working_dir=working_dir,
 
2103
                    )
 
2104
        finally:
 
2105
            logger.removeHandler(handler)
 
2106
 
 
2107
        out = stdout.getvalue()
 
2108
        err = stderr.getvalue()
 
2109
        if out:
 
2110
            self.log('output:\n%r', out)
 
2111
        if err:
 
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)