~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-09-25 19:40:52 UTC
  • mfrom: (2027.5.5 run_bzr_workingdir)
  • Revision ID: pqm@pqm.ubuntu.com-20060925194052-a685edd457d83f8e
(jam) add working_dir= parameter to run_bzr* functions

Show diffs side-by-side

added added

removed removed

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