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 18Description: process options and configs for test suite 19""" 20 21import argparse 22import logging 23import os 24from enum import Enum 25 26import yaml 27 28import json5 29import utils 30 31 32arguments = {} 33configs = {} 34 35 36class TaskResult(Enum): 37 undefind = 0 38 passed = 1 39 failed = 2 40 41 42class CompilationInfo: 43 def __init__(self): 44 self.result = TaskResult.undefind 45 self.runtime_result = TaskResult.undefind 46 self.error_message = '' 47 self.time = 0 48 self.abc_size = 0 49 50 51class FullCompilationInfo: 52 def __init__(self): 53 self.debug_info = CompilationInfo() 54 self.release_info = CompilationInfo() 55 56 57class IncCompilationInfo: 58 def __init__(self): 59 self.debug_info = CompilationInfo() 60 self.release_info = CompilationInfo() 61 self.name = '' 62 63 64class BackupInfo: 65 def __init__(self): 66 self.cache_path = '' 67 self.cache_debug = '' 68 self.cache_release = '' 69 self.output_debug = [] 70 self.output_release = [] 71 72 73class TestTask: 74 def __init__(self): 75 self.name = '' 76 self.path = '' 77 self.bundle_name = '' 78 self.ability_name = '' 79 self.type = '' 80 self.build_path = [] 81 self.output_hap_path = '' 82 self.output_hap_path_signed = '' 83 self.output_app_path = '' 84 self.inc_modify_file = [] 85 86 self.full_compilation_info = FullCompilationInfo() 87 self.incre_compilation_info = {} 88 self.other_tests = {} 89 90 self.backup_info = BackupInfo() 91 92 93def parse_args(): 94 parser = argparse.ArgumentParser() 95 parser.add_argument('--sdkPath', type=str, dest='sdk_path', default='', 96 help='specify sdk path if need to update sdk. Default to use sdk specify in config.yaml') 97 parser.add_argument('--buildMode', type=str, dest='build_mode', default='all', 98 choices=['all', 'assemble', 'preview', 'hotreload', 'hotfix'], 99 help='specify build mode') 100 parser.add_argument('--hapMode', type=str, dest='hap_mode', default='all', 101 choices=['all', 'debug', 'release'], 102 help='specify hap mode') 103 parser.add_argument('--compileMode', type=str, dest='compile_mode', default='all', 104 choices=['all', 'full', 'incremental'], 105 help='specify compile mode') 106 parser.add_argument('--testCase', type=str, dest='test_case', default='all', 107 choices=['all', 'fa', 'stage', 'compatible8', 'js'], 108 help='specify test cases') 109 parser.add_argument('--testHap', type=str, dest='test_hap', default='all', 110 help="specify test haps, option can be 'all' or a list of haps seperated by ','") 111 parser.add_argument('--imagePath', type=str, dest='image_path', default='', 112 help='specify image path if need to update rk/phone images. Default not to update image') 113 parser.add_argument('--runHaps', dest='run_haps', action='store_true', default=False, 114 help='specify whether to verify by running the haps on board.') 115 parser.add_argument('--logLevel', type=str, dest='log_level', default='error', 116 choices=['debug', 'info', 'warn', 'error'], 117 help='specify log level of test suite') 118 parser.add_argument('--logFile', type=str, dest='log_file', default='', 119 help='specify the file log outputs to, empty string will output to console') 120 parser.add_argument('--compileTimeout', type=int, dest='compile_timeout', default=1800, 121 help='specify deveco compilation timeout') 122 global arguments 123 arguments = parser.parse_args() 124 125 126def parse_configs(): 127 config_yaml = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.yaml') 128 with open(config_yaml, 'r') as config_file: 129 global configs 130 configs = yaml.safe_load(config_file) 131 132 133def get_ark_disasm_path(task_path): 134 ark_disasm = 'ark_disasm.exe' if utils.is_windows() else 'ark_disasm' 135 profile_file = os.path.join(task_path, 'build-profile.json5') 136 with open(profile_file, 'r') as file: 137 profile_data = json5.load(file) 138 api_version = profile_data['app']['products'][0]['compileSdkVersion'] 139 if isinstance(api_version, int): 140 openharmony_sdk_path = configs.get('deveco_openharmony_sdk_path') 141 return os.path.join(openharmony_sdk_path, str(api_version), 'toolchains', ark_disasm) 142 else: 143 harmonyos_sdk_path = configs.get('deveco_harmonyos_sdk_path') 144 api_version_file_map = configs.get('api_version_file_name_map') 145 file_name = api_version_file_map.get(api_version) 146 return os.path.join(harmonyos_sdk_path, file_name, 'base', 'toolchains', ark_disasm) 147 148 149def create_test_tasks(): 150 task_list = [] 151 haps_list = configs.get('haps') 152 test_cases = 'all' if arguments.test_case == 'all' else [] 153 test_haps = 'all' if arguments.test_hap == 'all' else [] 154 if test_cases != 'all': 155 test_cases = arguments.test_case.split(',') 156 if test_haps != 'all': 157 test_haps = arguments.test_hap.split(',') 158 159 for hap in haps_list: 160 if test_cases == 'all' or test_haps == 'all' \ 161 or (test_cases and (hap['type'][0] in test_cases)) \ 162 or (test_haps and (hap['name'] in test_haps)): 163 if not os.path.exists(hap['path']): 164 logging.warning("Path of hap %s dosen't exist: %s", hap['name'], hap['path']) 165 continue 166 task = TestTask() 167 task.name = hap['name'] 168 task.path = hap['path'] 169 task.bundle_name = hap['bundle_name'] 170 task.ability_name = hap['ability_name'] 171 task.type = hap['type'] 172 task.hap_module = hap['hap_module'] 173 task.build_path = hap['build_path'] 174 task.cache_path = hap['cache_path'] 175 task.output_hap_path = hap['output_hap_path'] 176 task.output_hap_path_signed = hap['output_hap_path_signed'] 177 task.output_app_path = hap['output_app_path'] 178 task.inc_modify_file = hap['inc_modify_file'] 179 task.backup_info.cache_path = os.path.join(task.path, 'test_suite_cache') 180 task.ark_disasm_path = get_ark_disasm_path(task.path) 181 182 task_list.append(task) 183 184 return task_list 185 186 187def process_options(): 188 parse_args() 189 utils.init_logger(arguments.log_level, arguments.log_file) 190 parse_configs() 191 return create_test_tasks()