def run_and_throw_if_fail(self, args, quiet=False, decode_output=True, **kwargs): # Cache the child's output locally so it can be used for error reports. child_out_file = StringIO.StringIO() tee_stdout = sys.stdout if quiet: dev_null = open(os.devnull, "w") # FIXME: Does this need an encoding? tee_stdout = dev_null child_stdout = Tee(child_out_file, tee_stdout) exit_code = self._run_command_with_teed_output(args, child_stdout, **kwargs) if quiet: dev_null.close() child_output = child_out_file.getvalue() child_out_file.close() if decode_output: child_output = child_output.decode(self._child_process_encoding()) if exit_code: raise ScriptError(script_args=args, exit_code=exit_code, output=child_output) return child_output
def test_simple_tee(self): file1, file2 = StringIO.StringIO(), StringIO.StringIO() tee = Tee(file1, file2) tee.write("foo bar\n") tee.write("baz\n") self.assertEqual(file1.getvalue(), "foo bar\nbaz\n") self.assertEqual(file2.getvalue(), file1.getvalue())