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