diff --git a/testTs/run_testTs.py b/testTs/run_testTs.py index 8980e37d20661c589a9352f6e8bb18dbae976108..2d176f1ed3a2c0d18faeb6d0afea8ad68912f317 100644 --- a/testTs/run_testTs.py +++ b/testTs/run_testTs.py @@ -46,9 +46,9 @@ def parse_args(): def skip(filepath, flag=False): - with open(SKIP_FILE_PATH, 'r') as f: - content = f.read() - skip_test = json.loads(content) + 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'] @@ -84,9 +84,9 @@ def run_test(file, tool, flag=False): 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 BaseException: - e = str(e) + command_os(['node', '--expose-gc', tool, '-m', 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: @@ -148,19 +148,19 @@ def run_test_machine(args): 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.append(''.join(out_content.split('\n'))) else: out_list = [] out_text_list = [] @@ -200,8 +200,8 @@ def compare(file, flag=False): for fi in files: fi = f'{root}/{fi}' if fi != out_path: - with open(fi, 'r') as f: - el_file_txt = f.read() + 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)): @@ -227,8 +227,8 @@ def summary(): 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 @@ -254,19 +254,20 @@ def prepare_ts_code(): 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') + 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 BaseException: print("pull test code fail") diff --git a/testTs/utils.py b/testTs/utils.py index cba62cc8988b3ac6321db36609fa76b82d9f515b..3c0ec14baab8bb2b4d35d0ae34e19b7bdef8d027 100644 --- a/testTs/utils.py +++ b/testTs/utils.py @@ -28,99 +28,72 @@ import json def command_os(order): - subprocess.run(order,shell=True) + subprocess.run(order) + def mk_dir(path): if not os.path.exists(path): os.makedirs(path) - -def git_checkout(git_brash): - command_os(f'git checkout {git_brash}') + def remove_dir(path): if os.path.exists(path): shutil.rmtree(path) + def remove_file(path): if os.path.exists(path): os.remove(path) + def clean_file(path): - with open(path,'w') as f: - f.write('') + with open(path, "w") as utils_clean: + utils_clean.write("") -def read_file(path): - content = [] - with open(path,'r') as f: - content = f.readlines() - - return content -def write_file(path,content): - with open(path,'w') as f: - f.write(content) +def read_file(path): + util_read_content = [] + with open(path, "r") as utils_read: + util_read_content = utils_read.readlines() -def write_append(path,content): - with open(path,'a+') as f: - f.write(content) + return util_read_content -def move_file(srcfile, dstfile): - subprocess.getstatusoutput("mv %s %s" % (srcfile, dstfile)) -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 write_file(path, write_content): + with open(path, "w") as utils_write: + utils_write.write(write_content) -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_append(path, 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(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 move_file(srcfile, dstfile): + subprocess.getstatusoutput("mv %s %s" % (srcfile, dstfile)) -def git_clean(cwd): - cmd = ['git', 'checkout', '--', '.'] - run_cmd_cwd(cmd, cwd) 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) diff --git a/ts2panda/scripts/run.py b/ts2panda/scripts/run.py index 661c1c02c037670ece8318acd835e15ab6be4f0d..c405cf71cefdfa34f01ef1caf01463efc79fa7dd 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 54281f9046976e2e41a98adf738c0d36d4a081a2..3d69fada8393f8059510282dea2b25bbe2da61f7 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 62cfa4b8e3724a5c266388b1bface31f760589f8..10cc51533eb68c9626c339d99ce0dc85272c098c 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -801,7 +801,7 @@ static void GenerateESModuleRecord(panda::pandasm::Program &prog) prog.record_table.emplace(ecmaModuleRecord.name, std::move(ecmaModuleRecord)); } -static void AddModuleRecord(panda::pandasm::Program &prog, std::string &moduleName, uint32_t moduleIdx) +static void AddModuleRecord(panda::pandasm::Program &prog, const std::string &moduleName, uint32_t moduleIdx) { auto iter = prog.record_table.find("_ESModuleRecord"); if (iter != prog.record_table.end()) { @@ -1144,7 +1144,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;