• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding: utf-8
3
4"""
5Copyright (c) 2021 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
18"""
19
20import os
21import sys
22import subprocess
23import argparse
24import time
25
26CUR_FILE_DIR = os.path.dirname(__file__)
27TS2PANDA_DIR = os.path.abspath(os.path.join(CUR_FILE_DIR, ".."))
28CODE_ROOT = os.path.abspath(os.path.join(TS2PANDA_DIR, "../../.."))
29DEFAULT_TARGET_DIR = os.path.join(
30    CODE_ROOT, "out/hispark_taurus/clang_x64/obj/arkcompiler/ets_frontend/ts2panda")
31DEFAULT_NODE_MODULE = os.path.join(
32    CODE_ROOT, "prebuilts/build-tools/common/ts2abc/node_modules")
33
34
35def parse_args():
36    parser = argparse.ArgumentParser()
37
38    parser.add_argument('--src-dir',
39                        default=TS2PANDA_DIR,
40                        help='Source directory')
41    parser.add_argument('--dist-dir',
42                        default=DEFAULT_TARGET_DIR,
43                        help='Destination directory')
44    parser.add_argument("--node-modules",
45                        default=DEFAULT_NODE_MODULE,
46                        help='path to node-modules exetuable')
47    parser.add_argument('--platform',
48                        default="linux",
49                        help='platform, as: linux, mac, win')
50    parser.add_argument('--js-file',
51                        metavar='FILE',
52                        help='The name of the test use case file to execute')
53
54    return parser.parse_args()
55
56
57def run_command(cmd, execution_path=os.getcwd()):
58    print(" ".join(cmd))
59    proc = subprocess.Popen(cmd, cwd=execution_path)
60    ret = proc.wait()
61    return ret
62
63
64class Ts2abcTests():
65    def __init__(self, args):
66        self.args = args
67        self.src_dir = TS2PANDA_DIR
68        self.dist_dir = DEFAULT_TARGET_DIR
69        self.node_modules = DEFAULT_NODE_MODULE
70        self.platform = "linux"
71
72    def proce_parameters(self):
73        if self.args.src_dir:
74            self.src_dir = self.args.src_dir
75
76        if self.args.dist_dir:
77            self.dist_dir = self.args.dist_dir
78
79        if self.args.node_modules:
80            self.node_modules = self.args.node_modules
81
82        if self.args.platform:
83            self.platform = self.args.platform
84
85    def copy_node_modules(self):
86        src_dir = self.src_dir
87        dist_dir = self.dist_dir
88        run_command(['cp', '-f', os.path.join(src_dir, "package.json"),
89                     os.path.join(dist_dir, "package.json")])
90        run_command(['cp', '-f', os.path.join(src_dir, "package-lock.json"),
91                     os.path.join(dist_dir, "package-lock.json")])
92
93        if self.node_modules:
94            run_command(['cp', '-rf', self.node_modules, dist_dir])
95        else:
96            run_command(['npm', 'install'], dist_dir)
97
98    def copy_tests(self):
99        if os.path.exists(f'{self.dist_dir}/tests'):
100            run_command(['rm', '-rf', f'{self.dist_dir}/tests'])
101        run_command(['cp', '-rf', f'{self.src_dir}/tests', self.dist_dir])
102
103    def run_build(self):
104        plat_form = self.platform
105        tsc = "node_modules/typescript/bin/tsc"
106        if plat_form == "linux":
107            cmd = [tsc, '-b', 'src', 'tests']
108            ret = run_command(cmd, self.dist_dir)
109        elif plat_form == "win":
110            cmd = [tsc, '-b', 'src/tsconfig.win.json',
111                   'tests/tsconfig.win.json']
112            ret = run_command(cmd, self.dist_dir)
113        elif plat_form == 'mac':
114            cmd = [tsc, '-b', 'src/tsconfig.mac.json',
115                   'tests/tsconfig.mac.json']
116            ret = run_command(cmd, self.dist_dir)
117        if ret:
118            raise RuntimeError("Run [{}] failed !".format(" ".join(cmd)))
119
120    def run_tests(self):
121        os.chdir(self.dist_dir)
122        start_time = time.time()
123        plat_form = self.platform
124        mocha = "node_modules/mocha/bin/mocha"
125
126        if self.args.js_file:
127            tests_args = self.args.js_file
128        else:
129            tests_args = "tests/**/*.test.js"
130
131        if plat_form == "linux":
132            cmd = ['cp', f'{self.src_dir}/src/jshelpers.js', f'build/src/' ]
133            run_command(cmd, self.dist_dir)
134            cmd = [mocha, f'build/{tests_args}']
135            ret = run_command(cmd, self.dist_dir)
136        elif plat_form == "win":
137            cmd = ['cp', f'{self.src_dir}/src/jshelpers.js', f'build-win/src/' ]
138            run_command(cmd, self.dist_dir)
139            cmd = [mocha, f'build-win/{tests_args}']
140            ret = run_command(cmd, self.dist_dir)
141        elif plat_form == 'mac':
142            cmd = ['cp', f'{self.src_dir}/src/jshelpers.js', f'build-mac/src/' ]
143            run_command(cmd, self.dist_dir)
144            cmd = [mocha, f'build-mac/{tests_args}']
145            ret = run_command(cmd, self.dist_dir)
146        if ret:
147            raise RuntimeError("Run [{}] failed !".format(" ".join(cmd)))
148        else:
149            print("Run [{}] success!".format(" ".join(cmd)))
150        print("used: %.5f seconds" % (time.time() - start_time))
151
152
153def main():
154    args = parse_args()
155
156    test = Ts2abcTests(args)
157    test.copy_node_modules()
158    test.copy_tests()
159    test.run_build()
160    test.run_tests()
161
162
163if __name__ == "__main__":
164    sys.exit(main())
165