/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 bzrlib/tests/__init__.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
703
703
 
704
704
    def _cleanEnvironment(self):
705
705
        new_env = {
 
706
            'BZR_HOME': None, # Don't inherit BZR_HOME to all the tests.
706
707
            'HOME': os.getcwd(),
707
708
            'APPDATA': os.getcwd(),
708
709
            'BZR_EMAIL': None,
788
789
        """Shortcut that splits cmd into words, runs, and returns stdout"""
789
790
        return self.run_bzr_captured(cmd.split(), retcode=retcode)[0]
790
791
 
791
 
    def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None):
 
792
    def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
 
793
                         working_dir=None):
792
794
        """Invoke bzr and return (stdout, stderr).
793
795
 
794
796
        Useful for code that wants to check the contents of the
809
811
        :param retcode: expected return code, or None for don't-care.
810
812
        :param encoding: encoding for sys.stdout and sys.stderr
811
813
        :param stdin: A string to be used as stdin for the command.
 
814
        :param working_dir: Change to this directory before running
812
815
        """
813
816
        if encoding is None:
814
817
            encoding = bzrlib.user_encoding
830
833
            stdout=stdout,
831
834
            stderr=stderr)
832
835
        bzrlib.ui.ui_factory.stdin = stdin
 
836
 
 
837
        cwd = None
 
838
        if working_dir is not None:
 
839
            cwd = osutils.getcwd()
 
840
            os.chdir(working_dir)
 
841
 
833
842
        try:
834
843
            result = self.apply_redirected(stdin, stdout, stderr,
835
844
                                           bzrlib.commands.run_bzr_catch_errors,
837
846
        finally:
838
847
            logger.removeHandler(handler)
839
848
            bzrlib.ui.ui_factory = old_ui_factory
 
849
            if cwd is not None:
 
850
                os.chdir(cwd)
840
851
 
841
852
        out = stdout.getvalue()
842
853
        err = stderr.getvalue()
863
874
        retcode = kwargs.pop('retcode', 0)
864
875
        encoding = kwargs.pop('encoding', None)
865
876
        stdin = kwargs.pop('stdin', None)
866
 
        return self.run_bzr_captured(args, retcode=retcode, encoding=encoding, stdin=stdin)
 
877
        working_dir = kwargs.pop('working_dir', None)
 
878
        return self.run_bzr_captured(args, retcode=retcode, encoding=encoding,
 
879
                                     stdin=stdin, working_dir=working_dir)
867
880
 
868
881
    def run_bzr_decode(self, *args, **kwargs):
869
882
        if 'encoding' in kwargs:
919
932
        :param universal_newlines: Convert CRLF => LF
920
933
        """
921
934
        env_changes = kwargs.get('env_changes', {})
922
 
        process = self.start_bzr_subprocess(args, env_changes=env_changes)
 
935
        working_dir = kwargs.get('working_dir', None)
 
936
        process = self.start_bzr_subprocess(args, env_changes=env_changes,
 
937
                                            working_dir=working_dir)
923
938
        # We distinguish between retcode=None and retcode not passed.
924
939
        supplied_retcode = kwargs.get('retcode', 0)
925
940
        return self.finish_bzr_subprocess(process, retcode=supplied_retcode,
927
942
            process_args=args)
928
943
 
929
944
    def start_bzr_subprocess(self, process_args, env_changes=None,
930
 
                             skip_if_plan_to_signal=False):
 
945
                             skip_if_plan_to_signal=False,
 
946
                             working_dir=None):
931
947
        """Start bzr in a subprocess for testing.
932
948
 
933
949
        This starts a new Python interpreter and runs bzr in there.
965
981
 
966
982
        bzr_path = self.get_bzr_path()
967
983
 
 
984
        cwd = None
 
985
        if working_dir is not None:
 
986
            cwd = osutils.getcwd()
 
987
            os.chdir(working_dir)
 
988
 
968
989
        try:
969
990
            # win32 subprocess doesn't support preexec_fn
970
991
            # so we will avoid using it on all platforms, just to
974
995
                             stdin=PIPE, stdout=PIPE, stderr=PIPE)
975
996
        finally:
976
997
            restore_environment()
 
998
            if cwd is not None:
 
999
                os.chdir(cwd)
 
1000
 
977
1001
        return process
978
1002
 
979
1003
    def get_bzr_path(self):