def get_output_ex(args, expect_error=False): ofiles = {} def recovery_open_(filename, mode=None): if 'test_data' in filename and mode is None: return open(filename) else: if mode is 'w': ofile = ContextualStringIO() ofiles[filename] = ofile return ofile with mock.patch('garecovery.recoverycli.open', side_effect=recovery_open_): with mock.patch('sys.stdout', io.StringIO()) as output: result = main([ sys.argv[0], ] + args) if expect_error: assert result != 0, output.getvalue() else: assert result == 0, output.getvalue() # Convert StringIOs in ofiles to string content for convenience ofiles = {filename: ofiles[filename].getvalue() for filename in ofiles} return output.getvalue(), ofiles
def get_argparse_error(args): """When argparse raises an error it writes to stderr and does a sys.exit""" with mock.patch('sys.stderr', io.StringIO()) as output: try: result = main([ sys.argv[0], ] + args) raise Exception("Expected a fail") except: pass return output.getvalue()