1#!/usr/bin/env python 2# Copyright 2019 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""Runs a python script under an isolate 7 8This script attempts to emulate the contract of gtest-style tests 9invoked via recipes. 10 11If optional argument --isolated-script-test-output=[FILENAME] is passed 12to the script, json is written to that file in the format detailed in 13//docs/testing/json-test-results-format.md. 14 15This script is intended to be the base command invoked by the isolate, 16followed by a subsequent Python script.""" 17 18import argparse 19import json 20import os 21import sys 22 23# Add src/testing/ into sys.path for importing xvfb and common. 24sys.path.append( 25 os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) 26import xvfb 27from scripts import common 28 29# pylint: disable=super-with-arguments 30 31 32def main(): 33 parser = argparse.ArgumentParser() 34 parser.add_argument('--isolated-script-test-output', type=str) 35 args, _ = parser.parse_known_args() 36 37 if sys.platform == 'win32': 38 exe = os.path.join('.', 'flatbuffers_unittests.exe') 39 else: 40 exe = os.path.join('.', 'flatbuffers_unittests') 41 42 env = os.environ.copy() 43 failures = [] 44 with common.temporary_file() as tempfile_path: 45 rc = xvfb.run_executable([exe], env, stdoutfile=tempfile_path) 46 47 # The flatbuffer tests do not really conform to anything parsable, except 48 # that they will succeed with "ALL TESTS PASSED". We cannot test for 49 # equality because some tests operate on invalid input and produce error 50 # messages (e.g. "Field id in struct ProtoMessage has a non positive number 51 # value" in a test that verifies behavior if a proto message does contain 52 # a non positive number). 53 with open(tempfile_path) as f: 54 output = f.read() 55 if "ALL TESTS PASSED\n" not in output: 56 failures = [output] 57 58 if args.isolated_script_test_output: 59 with open(args.isolated_script_test_output, 'w') as fp: 60 json.dump({'valid': True,'failures': failures}, fp) 61 62 return rc 63 64 65def main_compile_targets(args): 66 json.dump(['flatbuffers_unittests'], args.output) 67 68 69if __name__ == '__main__': 70 # Conform minimally to the protocol defined by ScriptTest. 71 if 'compile_targets' in sys.argv: 72 funcs = { 73 'run': None, 74 'compile_targets': main_compile_targets, 75 } 76 sys.exit(common.run_script(sys.argv[1:], funcs)) 77 sys.exit(main()) 78