From 85a90d89c773b0bf1872be258b43f42459fa7998 Mon Sep 17 00:00:00 2001 From: openharmony_ci <120357966@qq.com> Date: Mon, 16 May 2022 08:17:18 +0000 Subject: [PATCH] merge pr_196 Modify the subprocess_run function under testTs and add the const modifier Signed-off-by: zhzhangdh --- testTs/run_testTs.py | 180 ++++++++++++++++++++----------------- testTs/utils.py | 96 ++++++-------------- ts2panda/scripts/run.py | 36 ++++---- ts2panda/ts2abc/main.cpp | 2 +- ts2panda/ts2abc/ts2abc.cpp | 2 +- 5 files changed, 146 insertions(+), 170 deletions(-) diff --git a/testTs/run_testTs.py b/testTs/run_testTs.py index 74d42d7fdf..bbc1637553 100644 --- a/testTs/run_testTs.py +++ b/testTs/run_testTs.py @@ -28,68 +28,77 @@ import json from utils import * from config import * + class MyException(Exception): def __init__(self, name): self.name = name + def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('--dir', metavar='DIR', help="Directory to test") parser.add_argument('--file', metavar='FILE', help="File to test") - parser.add_argument('--ark_frontend_tool', help="ark frontend conversion tool") + parser.add_argument( + '--ark_frontend_tool', + help="ark frontend conversion tool") arguments = parser.parse_args() return arguments -def skip(filepath,flag = False): - with open(SKIP_FILE_PATH,'r') as f: - content = f.read() - test_list = [] - import_list =[] - skip_test = json.loads(content) - skip_test_list = skip_test['error.txt'] + skip_test['no2015'] + skip_test['tsc_error'] + skip_test['import_skip'] + skip_test['code_rule'] + skip_test['no_case'] +def skip(filepath, flag=False): + with open(SKIP_FILE_PATH, 'r') as read_skip: + sk_content = read_skip.read() + skip_test = json.loads(sk_content) + skip_test_list = skip_test['error.txt'] + skip_test['no2015'] + skip_test['tsc_error'] + \ + skip_test['import_skip'] + \ + skip_test['code_rule'] + skip_test['no_case'] if os.path.isfile(filepath): - if '.ts' in filepath: + if filepath.endswith('.ts'): if filepath not in skip_test_list: return True else: if flag: - print(f'This file is outside the scope of validation : {filepath}\n') + print( + f'This file is outside the scope of validation : {filepath}\n') return False + def abc_judge(filepath): if not os.path.getsize(filepath): - print(f'Error : {filepath} ,The file is empty') + print(f'Error : {filepath}.The file is empty') + -#Using machine translation (out_file: output content is written to the file path, tools:tool selection) -def run_test(file, tool,flag = False): +def run_test(file, tool, flag=False): path_list = file.split(os.sep) if path_list[0] != '.': file = "." + os.sep + file - out_file_path = file.replace(TEST_PATH,OUT_PATH).replace(TS_EXT,TXT_EXT) - temp_out_file_path = file.replace(TS_EXT,TXT_EXT) - temp_abc_file_path = file.replace(TS_EXT,ABC_EXT) + out_file_path = file.replace(TEST_PATH, OUT_PATH).replace(TS_EXT, TXT_EXT) + temp_out_file_path = file.replace(TS_EXT, TXT_EXT) + temp_abc_file_path = file.replace(TS_EXT, ABC_EXT) ts_list = temp_out_file_path.split(os.sep) ts_list.pop(-1) ts_dir_path = os.sep.join(ts_list) path_list = out_file_path.split(os.sep) path_list.pop(-1) out_dir_path = os.sep.join(path_list) - if not os.path.exists(out_dir_path): + if not os.path.exists(out_dir_path): os.makedirs(out_dir_path) try: - command_os(f'node --expose-gc {tool} -m {file} --output-type') - except: - e = str(e) + if file in IMPORT_TEST['import'] + IMPORT_TEST['m_parameter']: + command_os(['node', '--expose-gc', tool, '-m', file, '--output-type']) + else: + command_os(['node', '--expose-gc', tool, file, '--output-type']) + except BaseException as e: + print(e) if flag: - for root,dirs,files in os.walk(ts_dir_path): - for fi in files: - ts_file = f'{root}/{fi}' + for root, dirs, files in os.walk(ts_dir_path): + for fi in files: + ts_file = f'{root}/{fi}' if ABC_EXT in ts_file: remove_file(ts_file) - elif TXT_EXT in ts_file: - sj_path =ts_file.replace(TEST_PATH,OUT_PATH) - move_file(ts_file,sj_path) + elif TXT_EXT in ts_file: + sj_path = ts_file.replace(TEST_PATH, OUT_PATH) + move_file(ts_file, sj_path) else: if os.path.exists(temp_abc_file_path): abc_judge(temp_abc_file_path) @@ -103,58 +112,58 @@ def run_test_machine(args): result_path = [] if args.ark_frontend_tool: ark_frontend_tool = args.ark_frontend_tool - if args.file: - if skip(args.file,True): + if skip(args.file, True): if args.file in IMPORT_TEST['import']: - run_test(args.file, ark_frontend_tool,True) - result = compare(args.file,True) + run_test(args.file, ark_frontend_tool, True) + result = compare(args.file, True) result_path.append(result) else: run_test(args.file, ark_frontend_tool) result = compare(args.file) result_path.append(result) - + elif args.dir: - for root,dirs,files in os.walk(args.dir): + for root, dirs, files in os.walk(args.dir): for file in files: test_path = f'{root}/{file}' - if skip(test_path,True): + if skip(test_path, True): if test_path in IMPORT_TEST['import']: - run_test(test_path, ark_frontend_tool,True) - result = compare(test_path,True) + run_test(test_path, ark_frontend_tool, True) + result = compare(test_path, True) result_path.append(result) else: run_test(test_path, ark_frontend_tool) result = compare(test_path) result_path.append(result) - - elif args.file == None and args.dir == None: - for root,dirs,files in os.walk(TS_CASES_DIR): + + elif args.file is None and args.dir is None: + for root, dirs, files in os.walk(TS_CASES_DIR): for file in files: test_path = f'{root}/{file}' if skip(test_path): if test_path in IMPORT_TEST['import']: - run_test(test_path, ark_frontend_tool,True) - result = compare(test_path,True) + run_test(test_path, ark_frontend_tool, True) + result = compare(test_path, True) result_path.append(result) else: run_test(test_path, ark_frontend_tool) result = compare(test_path) result_path.append(result) - with open(OUT_RESULT_FILE,'w') as f: - f.writelines(result_path) - + with open(OUT_RESULT_FILE, 'w') as read_out_result: + read_out_result.writelines(result_path) + + def read_out_file(file_path): - with open(file_path, 'r') as f: - content = f.read() - if content: - if '}\n{' in content: - out_list = content.split('}\n{') + with open(file_path, 'r') as read_file_path: + out_content = read_file_path.read() + if out_content: + if '}\n{' in out_content: + out_list = out_content.split('}\n{') else: - out_list =[] - out_list.append(''.join(content.split('\n'))) + out_list = [] + out_list.append(''.join(out_content.split('\n'))) else: out_list = [] out_text_list = [] @@ -162,10 +171,14 @@ def read_out_file(file_path): for i in range(len(out_list)): if i == 0: out_do = ''.join(out_list[i].split('\n')).strip(' ') + '}' - elif i == (len(out_list)-1): + elif i == (len(out_list) - 1): out_do = '{' + ''.join(out_list[i].split('\n')).strip(' ') else: - out_do = ('{' + ''.join(out_list[i].split('\n')).strip(' ') + '}') + out_do = ( + '{' + + ''.join( + out_list[i].split('\n')).strip(' ') + + '}') out_txt = json.loads(out_do) out_text_list.append(out_txt) else: @@ -174,24 +187,25 @@ def read_out_file(file_path): out_text_list.append(c) return out_text_list -def compare(file,flag = False): + +def compare(file, flag=False): result = "" path_list = file.split(os.sep) if path_list[0] != '.': - file = "." + os.sep + file - out_path = file.replace(TEST_PATH, OUT_PATH).replace(TS_EXT,TXT_EXT) - expect_path = file.replace(TEST_PATH, EXPECT_PATH).replace(TS_EXT,TXT_EXT) + file = "." + os.sep + file + out_path = file.replace(TEST_PATH, OUT_PATH).replace(TS_EXT, TXT_EXT) + expect_path = file.replace(TEST_PATH, EXPECT_PATH).replace(TS_EXT, TXT_EXT) if flag: path_list = out_path.split(os.sep) del path_list[-1] out_dir_path = os.sep.join(path_list) - for root,dirs,files in os.walk(out_dir_path): + for root, dirs, files in os.walk(out_dir_path): for fi in files: fi = f'{root}/{fi}' if fi != out_path: - with open(fi,'r') as f: - el_file_txt = f.read() - write_append(out_path,el_file_txt) + with open(fi, 'r') as read_out_txt: + el_file_txt = read_out_txt.read() + write_append(out_path, el_file_txt) remove_file(fi) if (not os.path.exists(out_path) or not os.path.exists(expect_path)): print("There are no expected files or validation file generation: %s", file) @@ -201,7 +215,7 @@ def compare(file,flag = False): expectcont = read_file(expect_path) expectcontlist = [] for i in expectcont: - i = json.loads(i.replace("'",'"').replace('\n','')) + i = json.loads(i.replace("'", '"').replace('\n', '')) expectcontlist.append(i) if outcont == expectcontlist: result = f'PASS {file}\n' @@ -209,15 +223,15 @@ def compare(file,flag = False): result = f'FAIL {file}\n' print(result) return result - + def summary(): if not os.path.exists(OUT_RESULT_FILE): return count = -1 fail_count = 0 - with open(OUT_RESULT_FILE, 'r') as f: - for count, line in enumerate(f): + with open(OUT_RESULT_FILE, 'r') as read_outfile: + for count, line in enumerate(read_outfile): if line.startswith("FAIL"): fail_count += 1 pass @@ -225,10 +239,10 @@ def summary(): print("\n Regression summary") print("===============================") - print(" Total %5d " %(count)) + print(" Total %5d " % (count)) print("-------------------------------") - print(" Passed tests: %5d " %(count-fail_count)) - print(" Failed tests: %5d " %(fail_count)) + print(" Passed tests: %5d " % (count - fail_count)) + print(" Failed tests: %5d " % (fail_count)) print("===============================") @@ -236,29 +250,32 @@ def init_path(): remove_dir(OUT_TEST_DIR) mk_dir(OUT_TEST_DIR) -#Pull use cases and TS test files + def prepare_ts_code(): if (os.path.exists(TS_CASES_DIR)): return try: mk_dir(TS_CASES_DIR) os.chdir('./testTs/test') - command_os('git init') - command_os(f'git remote add origin {TS_GIT_PATH}') - command_os('git config core.sparsecheckout true') - command_os('echo "tests/cases/conformance/" >> .git/info/sparse-checkout') - command_os(f'git pull --depth 1 origin {TS_TAG}') + command_os(['git', 'init']) + command_os(['git', 'remote', 'add', 'origin', TS_GIT_PATH]) + command_os(['git', 'config', 'core.sparsecheckout', 'true']) + with os.fdopen(os.open('.git/info/sparse-checkout', os.O_APPEND|os.O_CREAT|os.O_WRONLY)) as outf: + subprocess.Popen(['echo', "tests/cases/conformance/"], stdout=outf) + command_os(['git', 'pull', '--depth', '1', 'origin', TS_TAG]) if not os.path.exists("./tests/cases/conformance/"): remove_dir(TS_CASES_DIR) - raise MyException("Pull TypeScript Code Fail, Please Check The Network Request") - command_os(f'git apply ../test-case.patch') - command_os(f'cp -r tests/cases/conformance/* ./') - command_os(f'rm -rf ./tests') - command_os('rm -rf .git') + raise MyException( + "Pull TypeScript Code Fail, Please Check The Network Request") + command_os(['git', 'apply', '../test-case.patch']) + command_os(['cp', '-r', './tests/cases/conformance/.', './']) + command_os(['rm', '-rf', './tests']) + command_os(['rm', '-rf', '.git']) os.chdir('../../') - except: + except BaseException: print("pull test code fail") + def main(args): try: init_path() @@ -266,8 +283,9 @@ def main(args): prepare_ts_code() run_test_machine(args) summary() - except: + except BaseException: print("Run Python Script Fail") + if __name__ == "__main__": - sys.exit(main(parse_args())) \ No newline at end of file + sys.exit(main(parse_args())) diff --git a/testTs/utils.py b/testTs/utils.py index a3e1b4799b..6888fd9781 100644 --- a/testTs/utils.py +++ b/testTs/utils.py @@ -27,115 +27,73 @@ import subprocess import json -#Executing terminal Commands def command_os(order): - subprocess.run(order,shell=True) + subprocess.run(order) + -#Creating a folder def mk_dir(path): if not os.path.exists(path): os.makedirs(path) - -#Switch branch -def git_checkout(git_brash): - command_os(f'git checkout {git_brash}') -#Delete folders + def remove_dir(path): if os.path.exists(path): shutil.rmtree(path) -#Delete the file + def remove_file(path): if os.path.exists(path): os.remove(path) -#Clear file contents (path: file path) + def clean_file(path): - with open(path,'w') as f: - f.write('') + with open(path, "w") as utils_clean: + utils_clean.write("") + -#Read file contents (all) def read_file(path): - content = [] - with open(path,'r') as f: - content = f.readlines() - - return content - -#Write to the file, overwrite the previous content -def write_file(path,content): - with open(path,'w') as f: - f.write(content) - -#Appending files (path: file path, content: write content) -def write_append(path,content): - with open(path,'a+') as f: - f.write(content) + util_read_content = [] + with open(path, "r") as utils_read: + util_read_content = utils_read.readlines() -def move_file(srcfile, dstfile): - subprocess.getstatusoutput("mv %s %s" % (srcfile, dstfile)) + return util_read_content -def git_clone(git_url, code_dir): - cmd = ['git', 'clone', git_url, code_dir] - ret = run_cmd_cwd(cmd) - assert not ret, f"\n error: Cloning '{git_url}' failed." -def git_checkout(git_bash, cwd): - cmd = ['git', 'checkout', git_bash] - ret = run_cmd_cwd(cmd, cwd) - assert not ret, f"\n error: git checkout '{git_bash}' failed." +def write_file(path, write_content): + with open(path, "w") as utils_write: + utils_write.write(write_content) -def git_apply(patch_file, cwd): - cmd = ['git', 'apply', patch_file] - ret = run_cmd_cwd(cmd, cwd) - assert not ret, f"\n error: Failed to apply '{patch_file}'" +def write_append(path, add_content): + fd = os.open(path, os.O_APPEND|os.O_CREAT|os.O_WRONLY) + with os.fdopen(fd, 'a+') as utils_append: + utils_append.write(add_content) -def git_clean(cwd): - cmd = ['git', 'checkout', '--', '.'] - run_cmd_cwd(cmd, cwd) +def move_file(srcfile, dstfile): + subprocess.getstatusoutput("mv %s %s" % (srcfile, dstfile)) + -#Output the current time (can be used to calculate the running time of the program) def current_time(): return datetime.datetime.now() -class Command(): - def __init__(self, cmd): - self.cmd = cmd - - def run(self): - LOGGING.debug("command: " + self.cmd) - out = os.popen(self.cmd).read() - LOGGING.info(out) - return out - -def run_cmd(command): - cmd = Command(command) - return cmd.run() def excuting_npm_install(args): ark_frontend_tool = os.path.join(DEFAULT_ARK_FRONTEND_TOOL) if args.ark_frontend_tool: ark_frontend_tool = os.path.join(args.ark_frontend_tool) - ts2abc_build_dir = os.path.join(os.path.dirname( - os.path.realpath(ark_frontend_tool)), "..") + ts2abc_build_dir = os.path.join(os.path.dirname(os.path.realpath(ark_frontend_tool)), "..") if os.path.exists(os.path.join(ts2abc_build_dir, "package.json")): npm_install(ts2abc_build_dir) elif os.path.exists(os.path.join(ts2abc_build_dir, "..", "package.json")): npm_install(os.path.join(ts2abc_build_dir, "..")) - + + def npm_install(cwd): try: os.chdir(cwd) - command_os('npm install') + command_os(["npm", "install"]) os.chdir(WORK_PATH) - except Exception as e: + except BaseException as e: print(e) - - - - - \ No newline at end of file diff --git a/ts2panda/scripts/run.py b/ts2panda/scripts/run.py index 661c1c02c0..c405cf71ce 100755 --- a/ts2panda/scripts/run.py +++ b/ts2panda/scripts/run.py @@ -76,31 +76,31 @@ def node_modules(options): run_command(['npm', 'install'], dist_dir) -def per_platForm_config(options, dir): +def per_platform_config(options, inp_dir): dist_dir = options.dist_dir - if os.path.exists(os.path.join(dist_dir, dir)): - shutil.rmtree(os.path.join(dist_dir, dir), ignore_errors=True) - cmd = ['mv', 'dist', dir] + if os.path.exists(os.path.join(dist_dir, inp_dir)): + shutil.rmtree(os.path.join(dist_dir, inp_dir), ignore_errors=True) + cmd = ['mv', 'dist', inp_dir] run_command(cmd, dist_dir) run_command(['cp', '-f', "package.json", - "./{}/package.json".format(dir)], dist_dir) + "./{}/package.json".format(inp_dir)], dist_dir) run_command(['cp', '-f', "package-lock.json", - "./{}/package-lock.json".format(dir)], dist_dir) + "./{}/package-lock.json".format(inp_dir)], dist_dir) (js2abc_dir, _) = os.path.split(options.js2abc) - build_dir = os.path.join(js2abc_dir, dir) + build_dir = os.path.join(js2abc_dir, inp_dir) if os.path.exists(build_dir): shutil.rmtree(build_dir) - run_command(['cp', '-r', os.path.join(dist_dir, dir), js2abc_dir]) + run_command(['cp', '-r', os.path.join(dist_dir, inp_dir), js2abc_dir]) bin_dir = os.path.join(build_dir, 'bin') if not os.path.exists(bin_dir): os.mkdir(bin_dir) run_command(['cp', '-f', options.js2abc, bin_dir]) - obj_bin_dir = os.path.join(dist_dir, dir, 'bin/') + obj_bin_dir = os.path.join(dist_dir, inp_dir, 'bin/') if not os.path.exists(obj_bin_dir): os.mkdir(obj_bin_dir) run_command(['cp', '-f', options.js2abc, obj_bin_dir]) run_command(['cp', '-r', os.path.join(dist_dir,"node_modules"), - os.path.join(dist_dir, dir)]) + os.path.join(dist_dir, inp_dir)]) def npm_run_build(options): @@ -112,19 +112,19 @@ def npm_run_build(options): '--env', 'buildMode={}'.format(options.buildMode)] run_command(cmd, options.dist_dir) if plat_form == "linux": - per_platForm_config(options, "build") + per_platform_config(options, "build") elif plat_form == "win": - per_platForm_config(options, "build-win") + per_platform_config(options, "build-win") elif plat_form == 'mac': - per_platForm_config(options, "build-mac") + per_platform_config(options, "build-mac") def main(): - ARGS = parse_args() - set_env(ARGS.node) - if not os.path.exists(os.path.join(ARGS.dist_dir, "node_modules")): - node_modules(ARGS) - npm_run_build(ARGS) + args = parse_args() + set_env(args.node) + if not os.path.exists(os.path.join(args.dist_dir, "node_modules")): + node_modules(args) + npm_run_build(args) if __name__ == "__main__": diff --git a/ts2panda/ts2abc/main.cpp b/ts2panda/ts2abc/main.cpp index 54281f9046..3d69fada83 100644 --- a/ts2panda/ts2abc/main.cpp +++ b/ts2panda/ts2abc/main.cpp @@ -21,7 +21,7 @@ #include "ts2abc.h" int Preprocess(const panda::ts2abc::Options &options, const panda::PandArgParser &argParser, std::string &output, - std::string &data, std::string &usage) + std::string &data, const std::string &usage) { std::string input; if (!options.GetCompileByPipeArg()) { diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index 631685c609..5d3fbfdb07 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -1011,7 +1011,7 @@ static bool ParseData(const std::string &data, panda::pandasm::Program &prog) return true; } -static bool IsStartOrEndPosition(int idx, char *buff, std::string &data) +static bool IsStartOrEndPosition(int idx, char *buff, const std::string &data) { if (buff[idx] != '$') { return false; -- Gitee