• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import sys
17import json
18import os
19import argparse
20import shutil
21sys.path.append(
22    os.path.dirname(os.path.dirname(os.path.dirname(
23        os.path.abspath(__file__)))))
24from scripts.util.file_utils import write_file  # noqa: E402
25from scripts.util import build_utils  # noqa: E402
26
27
28def copy_file(suite_path, template_path, target_path):
29    file_list = []
30    name_list = []
31    template_path = os.path.join(template_path, "src")
32
33    if not os.path.exists(suite_path):
34        raise Exception("suite path '{}' doesn't exist.".format(suite_path))
35
36    if os.path.exists(target_path):
37        shutil.rmtree(target_path)
38    os.makedirs(target_path, exist_ok=True)
39    shutil.copytree(template_path, os.path.join(target_path, "src"))
40
41    js_dest_path = os.path.join(target_path,
42        "src", "main", "js", "default", "test")
43    for root, dirs, files in os.walk(suite_path):
44        for item in files:
45            if item.endswith('.js'):
46                name_list.append(item)
47                file_list.append(os.path.join(root, item))
48
49    for file in file_list:
50        shutil.copy2(file, js_dest_path)
51    write_list_file(js_dest_path, name_list)
52
53
54def write_list_file(dest_path, name_list):
55    with open(os.path.join(dest_path, "List.test.js"), 'a') \
56        as list_data:
57        for name in name_list:
58            list_data.write("require('./%s')\n" % name)
59
60
61def get_hap_json(target_name, test_output_dir):
62    os.makedirs(test_output_dir, exist_ok=True)
63    json_file = os.path.join(test_output_dir, target_name + ".json")
64    json_info_data = {"driver": {
65       "type": "JSUnitTest"
66    }}
67    with open(json_file, 'w') as out_file:
68        json.dump(json_info_data, out_file)
69
70
71def main():
72    parser = argparse.ArgumentParser()
73    parser.add_argument('--suite_path', required=True)
74    parser.add_argument('--template_path', required=True)
75    parser.add_argument('--target_path', required=True)
76    parser.add_argument('--test_output_dir', required=True)
77    parser.add_argument('--target_name', required=True)
78    args = parser.parse_args()
79
80    copy_file(args.suite_path, args.template_path, args.target_path)
81    get_hap_json(args.target_name, args.test_output_dir)
82
83    return 0
84
85
86if __name__ == '__main__':
87    sys.exit(main())
88