def restore_output(self): assert isinstance(self.stdout, _TeedStream) out, err = (self.stdout.restore(), self.stderr.restore()) out = python_2_3_compat.bytes_to_str(out) err = python_2_3_compat.bytes_to_str(err) self.logger.handlers = self._orig_logging_handlers self._untap_output() return out, err
def write(self, msg, *args, **kwargs): if self.capturing: self._capture_file.write( python_2_3_compat.str_to_bytes(msg), *args, **kwargs) if not self.diverting: self.original_stream.write( python_2_3_compat.bytes_to_str(msg), *args, **kwargs)
def getvalue(self): assert self.capturing self.flush() os.lseek(self._capture_file.fileno(), 0, 0) # on mac python stream object does not account for captured bytes # added when another host object captures the output and turns on # passthrough return python_2_3_compat.bytes_to_str(os.read( self._capture_file.fileno(), os.path.getsize(self._capture_file.name)))
def check(self, cmd=None, stdin=None, env=None, aenv=None, files=None, prog=None, cwd=None, host=None, ret=None, out=None, rout=None, err=None, rerr=None, exp_files=None, files_to_ignore=None, universal_newlines=True): # Too many arguments pylint: disable=R0913 prog = prog or self.prog or [] host = host or self.make_host() argv = shlex.split(cmd) if isinstance(cmd, str) else cmd or [] tmpdir = None orig_wd = host.getcwd() try: tmpdir = host.mkdtemp() host.chdir(tmpdir) if files: self._write_files(host, files) if cwd: host.chdir(cwd) if aenv: env = host.env.copy() env.update(aenv) if self.child.debugger: # pragma: no cover host.print_('') host.print_('cd %s' % tmpdir, stream=host.stdout.stream) host.print_(' '.join(prog + argv), stream=host.stdout.stream) host.print_('') import pdb dbg = pdb.Pdb(stdout=host.stdout.stream) dbg.set_trace() result = self.call(host, prog + argv, stdin=stdin, env=env) actual_ret, actual_out, actual_err = result actual_out = python_2_3_compat.bytes_to_str(actual_out) actual_err = python_2_3_compat.bytes_to_str(actual_err) actual_files = self._read_files(host, tmpdir) finally: host.chdir(orig_wd) if tmpdir: host.rmtree(tmpdir) if universal_newlines: actual_out = convert_newlines(actual_out) actual_err = convert_newlines(actual_err) if ret is not None: self.assertEqual(ret, actual_ret) if out is not None: self.assertMultiLineEqual(out, actual_out) if rout is not None: self.assertRegexpMatches(actual_out, rout) if err is not None: self.assertMultiLineEqual(err, actual_err) if rerr is not None: self.assertRegexpMatches(actual_err, rerr) if exp_files: self.assert_files(exp_files, actual_files, files_to_ignore) return actual_ret, actual_out, actual_err, actual_files