generate.py (6984B)
1 # Copyright 2025 The Chromium Authors 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 """Generator script for Web Bluetooth LayoutTests. 6 7 For each script-tests/X.js creates the following test files depending on the 8 contents of X.js 9 - getPrimaryService/X.https.window.js 10 - getPrimaryServices/X.https.window.js 11 - getPrimaryServices/X-with-uuid.https.window.js 12 13 script-tests/X.js files should contain "CALLS([variation1 | variation2 | ...])" 14 tokens that indicate what files to generate. Each variation in CALLS([...]) 15 should corresponds to a js function call and its arguments. Additionally a 16 variation can end in [UUID] to indicate that the generated file's name should 17 have the -with-uuid suffix. 18 19 The PREVIOUS_CALL token will be replaced with the function that replaced CALLS. 20 21 The FUNCTION_NAME token will be replaced with the name of the function that 22 replaced CALLS. 23 24 For example, for the following template file: 25 26 // script-tests/example.js 27 promise_test(() => { 28 return navigator.bluetooth.requestDevice(...) 29 .then(device => device.gatt.CALLS([ 30 getPrimaryService('heart_rate')| 31 getPrimaryServices('heart_rate')[UUID]])) 32 .then(device => device.gatt.PREVIOUS_CALL); 33 }, 'example test for FUNCTION_NAME'); 34 35 this script will generate: 36 37 // getPrimaryService/example.https.window.js 38 promise_test(() => { 39 return navigator.bluetooth.requestDevice(...) 40 .then(device => device.gatt.getPrimaryService('heart_rate')) 41 .then(device => device.gatt.getPrimaryService('heart_rate')); 42 }, 'example test for getPrimaryService'); 43 44 // getPrimaryServices/example-with-uuid.https.window.js 45 promise_test(() => { 46 return navigator.bluetooth.requestDevice(...) 47 .then(device => device.gatt.getPrimaryServices('heart_rate')) 48 .then(device => device.gatt.getPrimaryServices('heart_rate')); 49 }, 'example test for getPrimaryServices'); 50 51 Run 52 $ python //bluetooth/bidi/generate.py 53 and commit the generated files. 54 """ 55 56 import fnmatch 57 import os 58 import re 59 import sys 60 import logging 61 62 TEMPLATES_DIR = 'script-tests' 63 64 65 class GeneratedTest: 66 67 def __init__(self, data, path, template): 68 self.data = data 69 self.path = path 70 self.template = template 71 72 73 def GetGeneratedTests(): 74 """Yields a GeneratedTest for each call in templates in script-tests.""" 75 bluetooth_tests_dir = os.path.dirname(os.path.realpath(__file__)) 76 77 # Read Base Test Template. 78 base_template_file_handle = open( 79 os.path.join( 80 bluetooth_tests_dir, 81 TEMPLATES_DIR, 82 'base_test_js.template' 83 ), 'r') 84 base_template_file_data = base_template_file_handle.read() 85 base_template_file_handle.close() 86 87 # Get Templates. 88 89 template_path = os.path.join(bluetooth_tests_dir, TEMPLATES_DIR) 90 91 available_templates = [] 92 for root, _, files in os.walk(template_path): 93 for template in files: 94 if template.endswith('.js'): 95 available_templates.append(os.path.join(root, template)) 96 97 # Generate Test Files 98 for template in available_templates: 99 # Read template 100 template_file_handle = open(template, 'r') 101 template_file_data = template_file_handle.read() 102 template_file_handle.close() 103 104 template_name = os.path.splitext(os.path.basename(template))[0] 105 106 # Find function names in multiline pattern: CALLS( [ function_name,function_name2[UUID] ]) 107 result = re.search( 108 r'CALLS\(' + # CALLS( 109 r'[^\[]*' + # Any characters not [, allowing for new lines. 110 r'\[' + # [ 111 r'(.*?)' + # group matching: function_name(), function_name2[UUID] 112 r'\]\)', # adjacent closing characters: ]) 113 template_file_data, re.MULTILINE | re.DOTALL) 114 115 if result is None: 116 raise Exception('Template must contain \'CALLS\' tokens') 117 118 new_test_file_data = base_template_file_data.replace('TEST', 119 template_file_data) 120 # Replace CALLS([...]) with CALLS so that we don't have to replace the 121 # CALLS([...]) for every new test file. 122 new_test_file_data = new_test_file_data.replace(result.group(), 'CALLS') 123 124 # Replace 'PREVIOUS_CALL' with 'CALLS' so that we can replace it while 125 # replacing CALLS. 126 new_test_file_data = new_test_file_data.replace('PREVIOUS_CALL', 'CALLS') 127 128 for call in result.group(1).split('|'): 129 # Parse call 130 call = call.strip() 131 function_name, args, uuid_suffix = re.search(r'(.*?)\((.*)\)(\[UUID\])?', call).groups() 132 133 # Replace template tokens 134 call_test_file_data = new_test_file_data 135 call_test_file_data = call_test_file_data.replace('CALLS', '{}({})'.format(function_name, args)) 136 call_test_file_data = call_test_file_data.replace('FUNCTION_NAME', function_name) 137 138 # Get test file name 139 group_dir = os.path.basename(os.path.abspath(os.path.join(template, os.pardir))) 140 141 call_test_file_name = 'gen-{}{}.https.window.js'.format(template_name, '-with-uuid' if uuid_suffix else '') 142 call_test_file_path = os.path.join(bluetooth_tests_dir, group_dir, function_name, call_test_file_name) 143 144 yield GeneratedTest(call_test_file_data, call_test_file_path, template) 145 146 def main(): 147 logging.basicConfig(level=logging.INFO) 148 previous_generated_files = set() 149 current_path = os.path.dirname(os.path.realpath(__file__)) 150 for root, _, filenames in os.walk(current_path): 151 for filename in fnmatch.filter(filenames, 'gen-*.https.window.js'): 152 previous_generated_files.add(os.path.join(root, filename)) 153 154 generated_files = set() 155 for generated_test in GetGeneratedTests(): 156 prev_len = len(generated_files) 157 generated_files.add(generated_test.path) 158 if prev_len == len(generated_files): 159 logging.info('Generated the same test twice for template:\n%s', 160 generated_test.template) 161 162 # Create or open test file 163 directory = os.path.dirname(generated_test.path) 164 if not os.path.exists(directory): 165 os.makedirs(directory) 166 test_file_handle = open(generated_test.path, 'wb') 167 168 # Write contents 169 test_file_handle.write(generated_test.data.encode('utf-8')) 170 test_file_handle.close() 171 172 new_generated_files = generated_files - previous_generated_files 173 if len(new_generated_files) != 0: 174 logging.info('Newly generated tests:') 175 for generated_file in new_generated_files: 176 logging.info(generated_file) 177 178 obsolete_files = previous_generated_files - generated_files 179 if len(obsolete_files) != 0: 180 logging.warning('The following files might be obsolete:') 181 for generated_file in obsolete_files: 182 logging.warning(generated_file) 183 184 185 186 if __name__ == '__main__': 187 sys.exit(main())