1*890232f2SAndroid Build Coastguard Worker# Copyright 2022 Google Inc. All rights reserved. 2*890232f2SAndroid Build Coastguard Worker# 3*890232f2SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*890232f2SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*890232f2SAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*890232f2SAndroid Build Coastguard Worker# 7*890232f2SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*890232f2SAndroid Build Coastguard Worker# 9*890232f2SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*890232f2SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*890232f2SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*890232f2SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*890232f2SAndroid Build Coastguard Worker# limitations under the License. 14*890232f2SAndroid Build Coastguard Worker 15*890232f2SAndroid Build Coastguard Workerimport argparse 16*890232f2SAndroid Build Coastguard Workerimport platform 17*890232f2SAndroid Build Coastguard Workerimport subprocess 18*890232f2SAndroid Build Coastguard Workerfrom pathlib import Path 19*890232f2SAndroid Build Coastguard Worker 20*890232f2SAndroid Build Coastguard Workerparser = argparse.ArgumentParser() 21*890232f2SAndroid Build Coastguard Workerparser.add_argument( 22*890232f2SAndroid Build Coastguard Worker "--flatc", help="path of the Flat C compiler relative to the root directory" 23*890232f2SAndroid Build Coastguard Worker) 24*890232f2SAndroid Build Coastguard Worker 25*890232f2SAndroid Build Coastguard Workerargs = parser.parse_args() 26*890232f2SAndroid Build Coastguard Worker 27*890232f2SAndroid Build Coastguard Worker# Get the path where this script is located so we can invoke the script from 28*890232f2SAndroid Build Coastguard Worker# any directory and have the paths work correctly. 29*890232f2SAndroid Build Coastguard Workerscript_path = Path(__file__).parent.resolve() 30*890232f2SAndroid Build Coastguard Worker 31*890232f2SAndroid Build Coastguard Worker# Get the root path as an absolute path, so all derived paths are absolute. 32*890232f2SAndroid Build Coastguard Workerroot_path = script_path.parent.parent.absolute() 33*890232f2SAndroid Build Coastguard Worker 34*890232f2SAndroid Build Coastguard Worker# Get the location of the flatc executable, reading from the first command line 35*890232f2SAndroid Build Coastguard Worker# argument or defaulting to default names. 36*890232f2SAndroid Build Coastguard Workerflatc_exe = Path( 37*890232f2SAndroid Build Coastguard Worker ("flatc" if not platform.system() == "Windows" else "flatc.exe") 38*890232f2SAndroid Build Coastguard Worker if not args.flatc 39*890232f2SAndroid Build Coastguard Worker else args.flatc 40*890232f2SAndroid Build Coastguard Worker) 41*890232f2SAndroid Build Coastguard Worker 42*890232f2SAndroid Build Coastguard Worker# Find and assert flatc compiler is present. 43*890232f2SAndroid Build Coastguard Workerif root_path in flatc_exe.parents: 44*890232f2SAndroid Build Coastguard Worker flatc_exe = flatc_exe.relative_to(root_path) 45*890232f2SAndroid Build Coastguard Workerflatc_path = Path(root_path, flatc_exe) 46*890232f2SAndroid Build Coastguard Workerassert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path) 47*890232f2SAndroid Build Coastguard Worker 48*890232f2SAndroid Build Coastguard Worker# Execute the flatc compiler with the specified parameters 49*890232f2SAndroid Build Coastguard Workerdef flatc(options, cwd=script_path): 50*890232f2SAndroid Build Coastguard Worker cmd = [str(flatc_path)] + options 51*890232f2SAndroid Build Coastguard Worker subprocess.check_call(cmd, cwd=str(cwd)) 52*890232f2SAndroid Build Coastguard Worker 53*890232f2SAndroid Build Coastguard Worker 54*890232f2SAndroid Build Coastguard Workerdef make_absolute(filename, path=script_path): 55*890232f2SAndroid Build Coastguard Worker return str(Path(path, filename).absolute()) 56*890232f2SAndroid Build Coastguard Worker 57*890232f2SAndroid Build Coastguard Worker 58*890232f2SAndroid Build Coastguard Workerdef assert_file_exists(filename, path=script_path): 59*890232f2SAndroid Build Coastguard Worker file = Path(path, filename) 60*890232f2SAndroid Build Coastguard Worker assert file.exists(), "could not find file: " + filename 61*890232f2SAndroid Build Coastguard Worker return file 62*890232f2SAndroid Build Coastguard Worker 63*890232f2SAndroid Build Coastguard Worker 64*890232f2SAndroid Build Coastguard Workerdef assert_file_doesnt_exists(filename, path=script_path): 65*890232f2SAndroid Build Coastguard Worker file = Path(path, filename) 66*890232f2SAndroid Build Coastguard Worker assert not file.exists(), "file exists but shouldn't: " + filename 67*890232f2SAndroid Build Coastguard Worker return file 68*890232f2SAndroid Build Coastguard Worker 69*890232f2SAndroid Build Coastguard Worker 70*890232f2SAndroid Build Coastguard Workerdef assert_file_contains(file, needles): 71*890232f2SAndroid Build Coastguard Worker with open(file) as file: 72*890232f2SAndroid Build Coastguard Worker contents = file.read() 73*890232f2SAndroid Build Coastguard Worker for needle in [needles] if isinstance(needles, str) else needles: 74*890232f2SAndroid Build Coastguard Worker assert needle in contents, ( 75*890232f2SAndroid Build Coastguard Worker "coudn't find '" + needle + "' in file: " + str(file) 76*890232f2SAndroid Build Coastguard Worker ) 77*890232f2SAndroid Build Coastguard Worker return file 78*890232f2SAndroid Build Coastguard Worker 79*890232f2SAndroid Build Coastguard Worker 80*890232f2SAndroid Build Coastguard Workerdef assert_file_doesnt_contains(file, needles): 81*890232f2SAndroid Build Coastguard Worker with open(file) as file: 82*890232f2SAndroid Build Coastguard Worker contents = file.read() 83*890232f2SAndroid Build Coastguard Worker for needle in [needles] if isinstance(needles, str) else needles: 84*890232f2SAndroid Build Coastguard Worker assert needle not in contents, ( 85*890232f2SAndroid Build Coastguard Worker "Found unexpected '" + needle + "' in file: " + str(file) 86*890232f2SAndroid Build Coastguard Worker ) 87*890232f2SAndroid Build Coastguard Worker return file 88*890232f2SAndroid Build Coastguard Worker 89*890232f2SAndroid Build Coastguard Worker 90*890232f2SAndroid Build Coastguard Workerdef assert_file_and_contents( 91*890232f2SAndroid Build Coastguard Worker file, needle, doesnt_contain=None, path=script_path, unlink=True 92*890232f2SAndroid Build Coastguard Worker): 93*890232f2SAndroid Build Coastguard Worker assert_file_contains(assert_file_exists(file, path), needle) 94*890232f2SAndroid Build Coastguard Worker if doesnt_contain: 95*890232f2SAndroid Build Coastguard Worker assert_file_doesnt_contains(assert_file_exists(file, path), doesnt_contain) 96*890232f2SAndroid Build Coastguard Worker if unlink: 97*890232f2SAndroid Build Coastguard Worker Path(path, file).unlink() 98*890232f2SAndroid Build Coastguard Worker 99*890232f2SAndroid Build Coastguard Worker 100*890232f2SAndroid Build Coastguard Workerdef run_all(*modules): 101*890232f2SAndroid Build Coastguard Worker failing = 0 102*890232f2SAndroid Build Coastguard Worker passing = 0 103*890232f2SAndroid Build Coastguard Worker for module in modules: 104*890232f2SAndroid Build Coastguard Worker methods = [ 105*890232f2SAndroid Build Coastguard Worker func 106*890232f2SAndroid Build Coastguard Worker for func in dir(module) 107*890232f2SAndroid Build Coastguard Worker if callable(getattr(module, func)) and not func.startswith("__") 108*890232f2SAndroid Build Coastguard Worker ] 109*890232f2SAndroid Build Coastguard Worker module_failing = 0 110*890232f2SAndroid Build Coastguard Worker module_passing = 0 111*890232f2SAndroid Build Coastguard Worker for method in methods: 112*890232f2SAndroid Build Coastguard Worker try: 113*890232f2SAndroid Build Coastguard Worker print("{0}.{1}".format(module.__name__, method)) 114*890232f2SAndroid Build Coastguard Worker getattr(module, method)(module) 115*890232f2SAndroid Build Coastguard Worker print(" [PASSED]") 116*890232f2SAndroid Build Coastguard Worker module_passing = module_passing + 1 117*890232f2SAndroid Build Coastguard Worker except Exception as e: 118*890232f2SAndroid Build Coastguard Worker print(" [FAILED]: " + str(e)) 119*890232f2SAndroid Build Coastguard Worker module_failing = module_failing + 1 120*890232f2SAndroid Build Coastguard Worker print( 121*890232f2SAndroid Build Coastguard Worker "{0}: {1} of {2} passsed".format( 122*890232f2SAndroid Build Coastguard Worker module.__name__, module_passing, module_passing + module_failing 123*890232f2SAndroid Build Coastguard Worker ) 124*890232f2SAndroid Build Coastguard Worker ) 125*890232f2SAndroid Build Coastguard Worker passing = passing + module_passing 126*890232f2SAndroid Build Coastguard Worker failing = failing + module_failing 127*890232f2SAndroid Build Coastguard Worker return passing, failing 128