JS Module Test Code Refactoring - JS Modification

Signed-off-by: wangqingkaihong <wangqing@kaihong.com>
This commit is contained in:
wangqingkaihong 2024-03-29 11:40:56 +08:00
parent 9d802dc9dc
commit 217b37aa21
23 changed files with 330 additions and 22 deletions

View File

@ -14,5 +14,6 @@
import("//arkcompiler/ets_runtime/test/test_helper.gni")
host_moduletest_assert_action("multiconstpoolarray") {
is_gen_js_by_script = true
deps = []
}

View File

@ -15,9 +15,11 @@
var arr = [
function foo() {
REPLACE_FUNC_FOO1
return "foo"
},
function foo2() {
REPLACE_FUNC_FOO2
return "foo2"
}
]

View File

@ -14,5 +14,6 @@
import("//arkcompiler/ets_runtime/test/test_helper.gni")
host_moduletest_action("multiconstpoolclass") {
is_gen_js_by_script = true
deps = []
}

View File

@ -18,9 +18,11 @@ class A {
print("A")
}
foo() {
REPLACE_FUNC_FOO1
return "foo"
}
foo2() {
REPLACE_FUNC_FOO2
return "foo2"
}
}

View File

@ -14,5 +14,6 @@
import("//arkcompiler/ets_runtime/test/test_helper.gni")
host_moduletest_action("multiconstpoolconstructor") {
is_gen_js_by_script = true
deps = []
}

View File

@ -15,9 +15,11 @@
class A {
constructor() {
REPLACE_FUNC_FOO1
print("A")
}
foo() {
REPLACE_FUNC_FOO2
return "foo"
}
}

View File

@ -14,5 +14,6 @@
import("//arkcompiler/ets_runtime/test/test_helper.gni")
host_moduletest_action("multiconstpoolfunc") {
is_gen_js_by_script = true
deps = []
}

View File

@ -14,10 +14,12 @@
*/
function foo() {
REPLACE_FUNC_FOO1
return "foo"
}
function foo2() {
REPLACE_FUNC_FOO2
return "foo2"
}

View File

@ -14,5 +14,6 @@
import("//arkcompiler/ets_runtime/test/test_helper.gni")
host_moduletest_action("multiconstpoolobj") {
is_gen_js_by_script = true
deps = []
}

View File

@ -15,9 +15,11 @@
var obj = {
"foo": function () {
REPLACE_FUNC_FOO1
return "foo"
},
"foo2": function() {
REPLACE_FUNC_FOO2
return "foo2"
}
}

View File

@ -69,6 +69,9 @@ host_quickfix_test_action("multi_patch") {
foreach(testcase, hot_reload_test_list) {
host_quickfix_test_action("${testcase}") {
entry_point = "--entry-point=base"
if (!is_debug) {
is_gen_js_by_script = true
}
}
}

View File

@ -0,0 +1,203 @@
#!/usr/bin/env python3
#coding: utf-8
"""
Copyright (c) 2024 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Description: run script
input: input file
output: output file
prefix: prefix
"""
import os
import sys
import argparse
replace_config1 = [
{"id": "REPLACE_FUNC_FOO1", "start": 0, "end": 31608},
{"id": "REPLACE_FUNC_FOO2", "start": 31609, "end": 65535},
]
replace_config2 = [
{"id": "REPLACE_FUNC_FOO1", "start": 0, "end": 65488},
{"id": "REPLACE_FUNC_FOO2", "start": 65489, "end": 65535},
]
replace_config3 = [
{"id": "REPLACE_FUNC_FOO1", "start": 0, "end": 65488},
{"id": "REPLACE_FUNC_FOO2", "start": 65489, "end": 65536},
]
file_list = [
{
"file_name": "ets_runtime/test/quickfix/multi_funcconstpool/base_modify.js",
"replace_config": replace_config1,
},
{
"file_name": "ets_runtime/test/quickfix/multi_funcconstpool/base.js",
"replace_config": replace_config1,
},
{
"file_name": "ets_runtime/test/quickfix/multi_funccallconstpool/base_modify.js",
"replace_config": replace_config1,
},
{
"file_name": "ets_runtime/test/quickfix/multi_funccallconstpool/base.js",
"replace_config": replace_config1,
},
{
"file_name": "ets_runtime/test/quickfix/multi_constructorconstpool/base_modify.js",
"replace_config": replace_config1,
},
{
"file_name": "ets_runtime/test/quickfix/multi_constructorconstpool/base.js",
"replace_config": replace_config1,
},
{
"file_name": "ets_runtime/test/quickfix/multi_closureconstpool/base_modify.js",
"replace_config": replace_config3,
},
{
"file_name": "ets_runtime/test/quickfix/multi_closureconstpool/base.js",
"replace_config": replace_config3,
},
{
"file_name": "ets_runtime/test/quickfix/multi_classconstpool/base_modify.js",
"replace_config": replace_config2,
},
{
"file_name": "ets_runtime/test/quickfix/multi_classconstpool/base.js",
"replace_config": replace_config2,
},
{
"file_name": "ets_runtime/test/moduletest/multiconstpoolobj/multiconstpoolobj.js",
"replace_config": replace_config2,
},
{
"file_name": "ets_runtime/test/moduletest/multiconstpoolfunc/multiconstpoolfunc.js",
"replace_config": replace_config2,
},
{
"file_name": "ets_runtime/test/moduletest/multiconstpoolconstructor/multiconstpoolconstructor.js",
"replace_config": replace_config2,
},
{
"file_name": "ets_runtime/test/moduletest/multiconstpoolclass/multiconstpoolclass.js",
"replace_config": replace_config2,
},
{
"file_name": "ets_runtime/test/moduletest/multiconstpoolarray/multiconstpoolarray.js",
"replace_config": replace_config2,
},
]
def generate_var(var_begin, var_end):
sVar = ""
for i in range(var_begin, var_end + 1):
sVar += 'var a%d = "%d";' % (i, i)
if (i + 1) % 6 == 0:
sVar += "\n"
return sVar
def read_file_content(input_file):
input_fd = os.open(input_file, os.O_RDONLY, 0o755)
with os.fdopen(input_fd, 'r') as fp:
return fp.read()
def write_file_content(output_file, data):
output_fd = os.open(output_file, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o755)
if os.path.exists(output_file):
with os.fdopen(output_fd, 'w') as fp:
fp.write(data)
def replace_in_data(data, replace_config):
for cfg in replace_config:
if cfg["id"] in data:
sVar = generate_var(cfg["start"], cfg["end"])
data = data.replace(cfg["id"], sVar)
return data
def replace_js(input_file, output_file):
for file in file_list:
if not input_file.endswith(file["file_name"]):
continue
data = read_file_content(input_file)
data = replace_in_data(data, file["replace_config"])
write_file_content(output_file, data)
return output_file
return None
def is_file_in_list(file_path):
input_dir_path = (
os.path.dirname(file_path)
if os.path.isfile(file_path)
else file_path
)
for item in file_list:
list_dir_path = os.path.dirname(item["file_name"])
if list_dir_path in input_dir_path:
return True
return False
def process_with_prefix(input_file, output_file, prefix):
input_fd = os.open(input_file, os.O_RDONLY, 0o755)
output_fd = os.open(output_file, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o755)
with os.fdopen(input_fd, 'r') as inputfp, os.fdopen(output_fd, 'w') as outputfp:
for line in inputfp:
outputfp.write(prefix + line)
def handle_files(input_file, output_file, prefix):
input_fd = os.open(input_file, os.O_RDONLY, 0o755)
output_fd = os.open(output_file, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o755)
output_file_dir = os.path.dirname(output_file)
with os.fdopen(input_fd, "r") as fp, os.fdopen(output_fd, "w") as outputfp:
lines = fp.readlines()
for line in lines:
ll = line.split(";")
original_js_filename = os.path.basename(ll[0])
js_fn = replace_js(prefix + ll[0], os.path.join(output_file_dir, original_js_filename))
if js_fn and os.path.exists(js_fn):
ll[0] = js_fn
else:
ll[0] = prefix + ll[0]
outputfp.write(";".join(ll))
def replace_merge_file(input_file, output_file, prefix):
if input_file.endswith(("base.txt", "patch.txt")) and is_file_in_list(prefix):
handle_files(input_file, output_file, prefix)
else:
process_with_prefix(input_file, output_file, prefix)
def replace_var(input_file, output_file, prefix):
if prefix:
replace_merge_file(input_file, output_file, prefix)
else:
replace_js(input_file, output_file)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--input", type=str, required=True)
parser.add_argument("--output", type=str, required=True)
parser.add_argument("--prefix", type=str)
args = parser.parse_args()
replace_var(os.path.abspath(args.input), args.output, args.prefix)
if __name__ == "__main__":
sys.exit(main())

View File

@ -17,9 +17,11 @@ class A {
constructor() {
}
foo() {
REPLACE_FUNC_FOO1
print("base foo");
}
foo2() {
REPLACE_FUNC_FOO2
print("base foo2");
}
}

View File

@ -17,9 +17,11 @@ class A {
constructor() {
}
foo() {
REPLACE_FUNC_FOO1
print("patch foo");
}
foo2() {
REPLACE_FUNC_FOO2
print("patch foo2");
}
}

View File

@ -15,7 +15,9 @@
var x = "-1";
function foo() {
REPLACE_FUNC_FOO1
function foo2() {
REPLACE_FUNC_FOO2
function foo3() {
print("base: " + a65536)
print("base: " + a65488)

View File

@ -15,7 +15,10 @@
var x = "-10";
function foo() {
REPLACE_FUNC_FOO1
function foo2() {
REPLACE_FUNC_FOO2
var y = "-100"
function foo3() {
print("patch: " + a65536)
print("patch: " + a65488)

View File

@ -15,9 +15,11 @@
class A {
constructor() {
REPLACE_FUNC_FOO1
this.str = "base"
}
foo() {
REPLACE_FUNC_FOO2
print("foo: " + this.str)
}
}

View File

@ -15,9 +15,11 @@
class A {
constructor() {
REPLACE_FUNC_FOO1
this.str = "patch"
}
foo() {
REPLACE_FUNC_FOO2
print("foo: " + this.str)
}
}

View File

@ -14,12 +14,13 @@
*/
function foo() {
REPLACE_FUNC_FOO1
print("base foo")
foo2()
}
function foo2() {
REPLACE_FUNC_FOO2
print("base foo2" )
}

View File

@ -14,12 +14,13 @@
*/
function foo() {
REPLACE_FUNC_FOO1
print("patch foo")
foo2()
}
function foo2() {
REPLACE_FUNC_FOO2
print("patch foo2" )
}

View File

@ -14,11 +14,12 @@
*/
function foo() {
REPLACE_FUNC_FOO1
print("base foo")
}
function foo2() {
REPLACE_FUNC_FOO2
print("base foo2" )
}

View File

@ -14,11 +14,12 @@
*/
function foo() {
REPLACE_FUNC_FOO1
print("patch foo")
}
function foo2() {
REPLACE_FUNC_FOO2
print("patch foo2" )
}

View File

@ -140,6 +140,10 @@ template("host_moduletest_action") {
if (defined(invoker.is_commonjs) && invoker.is_commonjs) {
_is_commonjs_ = true
}
_is_gen_js_ = false
if (defined(invoker.is_gen_js_by_script) && invoker.is_gen_js_by_script) {
_is_gen_js_ = true
}
_is_merge_ = false
if (defined(invoker.is_merge) && invoker.is_merge) {
_is_merge_ = true
@ -163,9 +167,14 @@ template("host_moduletest_action") {
_src_postfix_ = invoker.src_postfix
}
_test_js_path_ = "${_src_dir_}/${_target_name_}.${_src_postfix_}"
_test_abc_path_ = "$target_out_dir/${_target_name_}.abc"
_test_expect_path_ = "${_src_dir_}/expect_output.txt"
if (_is_gen_js_) {
_test_js_template_path_ = "${_src_dir_}/${_target_name_}.${_src_postfix_}"
_test_js_path_ = "$target_out_dir/${_target_name_}.${_src_postfix_}"
} else {
_test_js_path_ = "${_src_dir_}/${_target_name_}.${_src_postfix_}"
}
if (_is_merge_abc_) {
merge_file_raw = "./${_target_name_}.txt"
@ -188,13 +197,28 @@ template("host_moduletest_action") {
outputs = [ merge_file ]
}
}
if (_is_gen_js_) {
action("gen_${_target_name_}_js_file") {
script = "../../quickfix/generate_js_and_merge_file.py"
src_file = rebase_path(_test_js_template_path_)
dst_file = rebase_path(_test_js_path_)
args = [
"--input=$src_file",
"--output=$dst_file",
]
inputs = [ _test_js_template_path_ ]
outputs = [ _test_js_path_ ]
}
}
es2abc_gen_abc("gen_${_target_name_}_abc") {
extra_visibility = [ ":*" ] # Only targets in this file can depend on this.
extra_dependencies = _deps_
src_js = rebase_path(_test_js_path_)
dst_file = rebase_path(_test_abc_path_)
extra_args = []
if (_is_gen_js_) {
extra_dependencies = [ ":gen_${_target_name_}_js_file" ]
}
if (_is_module_) {
extra_args += [ "--module" ]
}
@ -574,6 +598,10 @@ template("host_moduletest_assert_action") {
if (defined(invoker.is_commonjs) && invoker.is_commonjs) {
_is_commonjs_ = true
}
_is_gen_js_ = false
if (defined(invoker.is_gen_js_by_script) && invoker.is_gen_js_by_script) {
_is_gen_js_ = true
}
_is_merge_ = false
if (defined(invoker.is_merge) && invoker.is_merge) {
_is_merge_ = true
@ -597,9 +625,15 @@ template("host_moduletest_assert_action") {
_src_postfix_ = invoker.src_postfix
}
_test_js_path_ = "${_src_dir_}/${_target_name_}.${_src_postfix_}"
_test_abc_path_ = "$target_out_dir/${_target_name_}.abc"
if (_is_gen_js_) {
_test_js_template_path_ = "${_src_dir_}/${_target_name_}.${_src_postfix_}"
_test_js_path_ = "$target_out_dir/${_target_name_}.${_src_postfix_}"
} else {
_test_js_path_ = "${_src_dir_}/${_target_name_}.${_src_postfix_}"
}
if (_is_merge_abc_) {
merge_file_raw = "./${_target_name_}.txt"
merge_file = "$target_out_dir/${_target_name_}.txt"
@ -621,6 +655,19 @@ template("host_moduletest_assert_action") {
outputs = [ merge_file ]
}
}
if (_is_gen_js_) {
action("gen_${_target_name_}_js_file") {
script = "../../quickfix/generate_js_and_merge_file.py"
src_file = rebase_path(_test_js_template_path_)
dst_file = rebase_path(_test_js_path_)
args = [
"--input=$src_file",
"--output=$dst_file",
]
inputs = [ _test_js_template_path_ ]
outputs = [ _test_js_path_ ]
}
}
es2abc_gen_abc("gen_${_target_name_}_abc") {
extra_visibility = [ ":*" ] # Only targets in this file can depend on this.
@ -628,6 +675,9 @@ template("host_moduletest_assert_action") {
src_js = rebase_path(_test_js_path_)
dst_file = rebase_path(_test_abc_path_)
extra_args = []
if (_is_gen_js_) {
extra_dependencies = [ ":gen_${_target_name_}_js_file" ]
}
if (_is_module_) {
extra_args += [ "--module" ]
}
@ -4487,24 +4537,39 @@ template("host_quickfix_test_action") {
} else {
merge_file_raw += "${_target_name_}/${filename}.txt"
}
merge_file = "$target_out_dir/${_target_name_}/${filename}.txt"
merge_file_prefix =
"//arkcompiler/ets_runtime/test/quickfix/${_target_name_}/"
action("gen_${_target_name_}_${filename}_merge_file") {
script = "//arkcompiler/ets_runtime/test/quickfix/generate_merge_file.py"
args = [
"--input",
rebase_path(merge_file_raw),
"--output",
rebase_path(merge_file),
"--prefix",
rebase_path(merge_file_prefix),
]
inputs = [ merge_file_raw ]
outputs = [ merge_file ]
if (defined(invoker.is_gen_js_by_script) && invoker.is_gen_js_by_script) {
action("gen_${_target_name_}_${filename}_js_file_and_merge") {
script = "//arkcompiler/ets_runtime/test/quickfix/generate_js_and_merge_file.py"
args = [
"--input",
rebase_path(merge_file_raw),
"--output",
rebase_path(merge_file),
"--prefix",
rebase_path(merge_file_prefix),
]
inputs = [ merge_file_raw ]
outputs = [ merge_file ]
}
} else {
action("gen_${_target_name_}_${filename}_merge_file") {
script =
"//arkcompiler/ets_runtime/test/quickfix/generate_merge_file.py"
args = [
"--input",
rebase_path(merge_file_raw),
"--output",
rebase_path(merge_file),
"--prefix",
rebase_path(merge_file_prefix),
]
inputs = [ merge_file_raw ]
outputs = [ merge_file ]
}
}
abc_path = "$target_out_dir/${_target_name_}/${filename}.abc"
@ -4512,7 +4577,12 @@ template("host_quickfix_test_action") {
es2abc_gen_abc("gen_${_target_name_}_${filename}_abc") {
extra_visibility =
[ ":*" ] # Only targets in this file can depend on this.
extra_dependencies = [ ":gen_${_target_name_}_${filename}_merge_file" ]
if (defined(invoker.is_gen_js_by_script) && invoker.is_gen_js_by_script) {
extra_dependencies =
[ ":gen_${_target_name_}_${filename}_js_file_and_merge" ]
} else {
extra_dependencies = [ ":gen_${_target_name_}_${filename}_merge_file" ]
}
if (defined(invoker.is_hotpatch) && filename == "patch") {
extra_dependencies += [ ":gen_${_target_name_}_base_abc" ]