1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4""" 5Copyright (c) 2022 Huawei Device Co., Ltd. 6Licensed under the Apache License, Version 2.0 (the "License"); 7you may not use this file except in compliance with the License. 8You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12Unless required by applicable law or agreed to in writing, software 13distributed under the License is distributed on an "AS IS" BASIS, 14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15See the License for the specific language governing permissions and 16limitations under the License. 17 18Description: Use ark to execute test 262 test suite 19""" 20 21import os 22import subprocess 23import argparse 24import datetime 25import sys 26import shutil 27import json 28from glob import glob 29from utils import * 30from config import * 31 32 33class MyException(Exception): 34 def __init__(self, name): 35 self.name = name 36 37 38def parse_args(): 39 parser = argparse.ArgumentParser() 40 parser.add_argument('--testinstype', action='store_true', dest='testinstype', default=False, help="ins type test") 41 parser.add_argument('--dir', metavar='DIR', help="Directory to test") 42 parser.add_argument('--file', metavar='FILE', help="File to test") 43 parser.add_argument( 44 '--ark_frontend_tool', 45 help="ark frontend conversion tool") 46 arguments = parser.parse_args() 47 return arguments 48 49 50def skip(filepath, flag=False): 51 with open(SKIP_FILE_PATH, 'r') as read_skip: 52 sk_content = read_skip.read() 53 skip_test = json.loads(sk_content) 54 skip_test_list = skip_test['error.txt'] + skip_test['no2015'] + skip_test['tsc_error'] + \ 55 skip_test['import_skip'] + \ 56 skip_test['code_rule'] + skip_test['no_case'] + skip_test['not_supported_by_4.2.3'] 57 if os.path.isfile(filepath): 58 if filepath.endswith('.ts'): 59 if filepath not in skip_test_list: 60 return True 61 else: 62 if flag: 63 print( 64 f'This file is outside the scope of validation : {filepath}\n') 65 return False 66 67 68def abc_judge(filepath): 69 if not os.path.getsize(filepath): 70 print(f'Error : {filepath}.The file is empty') 71 72 73def run_test(file, tool, flag=False): 74 path_list = file.split(os.sep) 75 if path_list[0] != '.': 76 file = "." + os.sep + file 77 out_file_path = file.replace(TEST_PATH, OUT_PATH).replace(TS_EXT, TXT_EXT) 78 temp_out_file_path = file.replace(TS_EXT, TXT_EXT) 79 temp_abc_file_path = file.replace(TS_EXT, ABC_EXT) 80 ts_list = temp_out_file_path.split(os.sep) 81 ts_list.pop(-1) 82 ts_dir_path = os.sep.join(ts_list) 83 path_list = out_file_path.split(os.sep) 84 path_list.pop(-1) 85 out_dir_path = os.sep.join(path_list) 86 if not os.path.exists(out_dir_path): 87 os.makedirs(out_dir_path) 88 try: 89 if file in IMPORT_TEST['import'] + IMPORT_TEST['m_parameter']: 90 command_os(['node', '--expose-gc', tool, '-m', file, '--output-type']) 91 else: 92 command_os(['node', '--expose-gc', tool, file, '--output-type']) 93 except BaseException as e: 94 print(e) 95 if flag: 96 for root, dirs, files in os.walk(ts_dir_path): 97 for fi in files: 98 ts_file = f'{root}/{fi}' 99 if ABC_EXT in ts_file: 100 remove_file(ts_file) 101 elif TXT_EXT in ts_file: 102 sj_path = ts_file.replace(TEST_PATH, OUT_PATH) 103 move_file(ts_file, sj_path) 104 else: 105 if os.path.exists(temp_abc_file_path): 106 abc_judge(temp_abc_file_path) 107 remove_file(temp_abc_file_path) 108 if os.path.exists(temp_out_file_path): 109 move_file(temp_out_file_path, out_file_path) 110 111 112def run_test_machine(args): 113 ark_frontend_tool = DEFAULT_ARK_FRONTEND_TOOL 114 result_path = [] 115 if args.ark_frontend_tool: 116 ark_frontend_tool = args.ark_frontend_tool 117 118 if args.file: 119 if skip(args.file, True): 120 if args.file in IMPORT_TEST['import']: 121 run_test(args.file, ark_frontend_tool, True) 122 result = compare(args.file, True) 123 result_path.append(result) 124 else: 125 run_test(args.file, ark_frontend_tool) 126 result = compare(args.file) 127 result_path.append(result) 128 129 elif args.dir: 130 for root, dirs, files in os.walk(args.dir): 131 for file in files: 132 test_path = f'{root}/{file}' 133 if skip(test_path, True): 134 if test_path in IMPORT_TEST['import']: 135 run_test(test_path, ark_frontend_tool, True) 136 result = compare(test_path, True) 137 result_path.append(result) 138 else: 139 run_test(test_path, ark_frontend_tool) 140 result = compare(test_path) 141 result_path.append(result) 142 143 elif args.file is None and args.dir is None: 144 for root, dirs, files in os.walk(TS_CASES_DIR): 145 for file in files: 146 test_path = f'{root}/{file}' 147 if skip(test_path): 148 if test_path in IMPORT_TEST['import']: 149 run_test(test_path, ark_frontend_tool, True) 150 result = compare(test_path, True) 151 result_path.append(result) 152 else: 153 run_test(test_path, ark_frontend_tool) 154 result = compare(test_path) 155 result_path.append(result) 156 with open(OUT_RESULT_FILE, 'w') as read_out_result: 157 read_out_result.writelines(result_path) 158 159 160def read_out_file(file_path): 161 with open(file_path, 'r') as read_file_path: 162 out_content = read_file_path.read() 163 if out_content: 164 if '}\n{' in out_content: 165 out_list = out_content.split('}\n{') 166 else: 167 out_list = [] 168 out_list.append(''.join(out_content.split('\n'))) 169 else: 170 out_list = [] 171 out_text_list = [] 172 if len(out_list) > 1: 173 for i in range(len(out_list)): 174 if i == 0: 175 out_do = ''.join(out_list[i].split('\n')).strip(' ') + '}' 176 elif i == (len(out_list) - 1): 177 out_do = '{' + ''.join(out_list[i].split('\n')).strip(' ') 178 else: 179 out_do = ( 180 '{' + 181 ''.join( 182 out_list[i].split('\n')).strip(' ') + 183 '}') 184 out_txt = json.loads(out_do) 185 out_text_list.append(out_txt) 186 else: 187 for i in range(len(out_list)): 188 c = json.loads(out_list[i]) 189 out_text_list.append(c) 190 return out_text_list 191 192 193def compare(file, flag=False): 194 result = "" 195 path_list = file.split(os.sep) 196 if path_list[0] != '.': 197 file = "." + os.sep + file 198 out_path = file.replace(TEST_PATH, OUT_PATH).replace(TS_EXT, TXT_EXT) 199 expect_path = file.replace(TEST_PATH, EXPECT_PATH).replace(TS_EXT, TXT_EXT) 200 if flag: 201 path_list = out_path.split(os.sep) 202 del path_list[-1] 203 out_dir_path = os.sep.join(path_list) 204 for root, dirs, files in os.walk(out_dir_path): 205 for fi in files: 206 fi = f'{root}/{fi}' 207 if fi != out_path: 208 with open(fi, 'r') as read_out_txt: 209 el_file_txt = read_out_txt.read() 210 write_append(out_path, el_file_txt) 211 remove_file(fi) 212 if (not os.path.exists(out_path) or not os.path.exists(expect_path)): 213 print("There are no expected files or validation file generation: %s", file) 214 result = f'FAIL {file}\n' 215 else: 216 outcont = read_out_file(out_path) 217 expectcont = read_file(expect_path) 218 expectcontlist = [] 219 for i in expectcont: 220 i = json.loads(i.replace("'", '"').replace('\n', '')) 221 expectcontlist.append(i) 222 if outcont == expectcontlist: 223 result = f'PASS {file}\n' 224 else: 225 result = f'FAIL {file}\n' 226 print(result) 227 return result 228 229 230def summary(): 231 if not os.path.exists(OUT_RESULT_FILE): 232 return 1 233 count = -1 234 fail_count = 0 235 with open(OUT_RESULT_FILE, 'r') as read_outfile: 236 for count, line in enumerate(read_outfile): 237 if line.startswith("FAIL"): 238 fail_count += 1 239 pass 240 count += 1 241 242 print("\n Regression summary") 243 print("===============================") 244 print(" Total %5d " % (count)) 245 print("-------------------------------") 246 print(" Passed tests: %5d " % (count - fail_count)) 247 print(" Failed tests: %5d " % (fail_count)) 248 print("===============================") 249 250 return 0 if fail_count == 0 else 1 251 252 253def init_path(): 254 remove_dir(OUT_TEST_DIR) 255 mk_dir(OUT_TEST_DIR) 256 257 258def prepare_ts_code(): 259 if (os.path.exists(TS_CASES_DIR)): 260 return 261 try: 262 mk_dir(TS_CASES_DIR) 263 os.chdir('./testTs/test') 264 command_os(['git', 'init']) 265 command_os(['git', 'remote', 'add', 'origin', TS_GIT_PATH]) 266 command_os(['git', 'config', 'core.sparsecheckout', 'true']) 267 with os.fdopen(os.open('.git/info/sparse-checkout', os.O_APPEND|os.O_CREAT|os.O_WRONLY)) as outf: 268 subprocess.Popen(['echo', "tests/cases/conformance/"], stdout=outf) 269 command_os(['git', 'pull', '--depth', '1', 'origin', TS_TAG]) 270 if not os.path.exists("./tests/cases/conformance/"): 271 remove_dir(TS_CASES_DIR) 272 raise MyException( 273 "Pull TypeScript Code Fail, Please Check The Network Request") 274 command_os(['git', 'apply', '../test-case.patch']) 275 command_os(['cp', '-r', './tests/cases/conformance/.', './']) 276 command_os(['rm', '-rf', './tests']) 277 command_os(['rm', '-rf', '.git']) 278 os.chdir('../../') 279 except BaseException: 280 print("pull test code fail") 281 282 283def test_instype(args): 284 # output path for abc file generation 285 outpath = os.path.join(OUT_TEST_DIR, 'instype') 286 mk_dir(outpath) 287 288 # source ts files 289 files = glob(os.path.join(CUR_FILE_DIR, './instype/*.ts')); 290 ark_frontend_tool = DEFAULT_ARK_FRONTEND_TOOL 291 if args.ark_frontend_tool: 292 ark_frontend_tool = args.ark_frontend_tool 293 294 fail_list = [] 295 for file in files: 296 abc_file = os.path.abspath(os.path.join(outpath, '%s.abc' % os.path.splitext(os.path.basename(file))[0])) 297 cmd = ['node', '--expose-gc', ark_frontend_tool, os.path.abspath(file), 298 '--modules', '--display-typeinfo', '-o', abc_file]; 299 process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 300 try: 301 out, err = process.communicate(timeout=10) 302 except subprocess.TimeoutExpired: 303 process.kill() 304 fail_list.append(file) 305 print("TIMEOUT:", " ".join(cmd)) 306 continue 307 output = out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore") 308 309 expected_file = "%s-expected.txt" % (os.path.splitext(file)[0]) 310 expected = "" 311 try: 312 with open(expected_file, 'r') as expected_fp: 313 expected = expected_fp.read() 314 passed = expected == output 315 except Exception: 316 passed = False 317 if not passed: 318 fail_list.append(file) 319 print("FAILED:", " ".join(cmd)) 320 print("output:") 321 print(output) 322 print("expected:") 323 print(expected) 324 325 print("Summary:") 326 print("\033[37mTotal: %5d" % (len(files))) 327 print("\033[92mPassed: %5d" % (len(files) - len(fail_list))) 328 print("\033[91mFailed: %5d" % (len(fail_list))) 329 print("\033[0m") 330 331 return 0 if len(fail_list) == 0 else 1 332 333def main(args): 334 try: 335 init_path() 336 if (args.testinstype): 337 excuting_npm_install(args) 338 return test_instype(args) 339 else: 340 excuting_npm_install(args) 341 prepare_ts_code() 342 run_test_machine(args) 343 return summary() 344 except BaseException: 345 print("Run Python Script Fail") 346 return 1 347 348 349if __name__ == "__main__": 350 sys.exit(main(parse_args())) 351