1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2021-2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import logging 18from functools import cached_property 19from typing import Dict, Optional, List 20 21from runner.options.decorator_value import value, _to_str, _to_path 22 23from runner.logger import Log 24 25_LOGGER = logging.getLogger("runner.options.options_custom") 26 27 28class CustomSuiteOptions: 29 def __str__(self) -> str: 30 return _to_str(self, 1) 31 32 def to_dict(self) -> Dict[str, object]: 33 return { 34 "suite": self.suite_name, 35 "test-root": self.test_root, 36 "list-root": self.list_root, 37 "generator": self.generator, 38 "generator-options": self.generator_options, 39 } 40 41 @cached_property 42 @value(yaml_path="custom.suite-name", cli_name="custom_suite_name") 43 def suite_name(self) -> Optional[str]: 44 return None 45 46 @cached_property 47 @value(yaml_path="custom.test-root", cli_name="custom_test_root", cast_to_type=_to_path) 48 def test_root(self) -> Optional[str]: 49 return None 50 51 @cached_property 52 @value(yaml_path="custom.list-root", cli_name="custom_list_root", cast_to_type=_to_path) 53 def list_root(self) -> Optional[str]: 54 return None 55 56 @cached_property 57 @value(yaml_path="custom.generator", cli_name="custom_generator", cast_to_type=_to_path) 58 def generator(self) -> Optional[str]: 59 return None 60 61 @cached_property 62 @value(yaml_path="custom.generator-options", cli_name="custom_generator_option") 63 def generator_options(self) -> List[str]: 64 return [] 65 66 def validate(self) -> None: 67 if not self.__validate(): 68 Log.exception_and_raise( 69 _LOGGER, 70 "Invalid set of options for the custom suite.\n" 71 "Expected min configuration: --custom-suite XXX --custom-test-root TTT --custom-list-root LLL\n" 72 "With generator: --custom-suite XXX --custom-test-root TTT --custom-list-root LLL " 73 "--custom-generator GGG --custom-generator-option CGO1 --custom-generator-option CG02") 74 75 def get_command_line(self) -> str: 76 generator_options = [f'--custom-generator-option="{arg}"' for arg in self.generator_options] 77 options = [ 78 f'--custom-suite={self.suite_name}' if self.suite_name is not None else '', 79 f'--custom-test-root={self.test_root}' if self.test_root is not None else '', 80 f'--custom-list-root={self.list_root}' if self.list_root is not None else '', 81 f'--custom-generator={self.generator}' if self.generator is not None else '', 82 ' '.join(generator_options), 83 ] 84 return ' '.join(options) 85 86 def __validate(self) -> bool: 87 if self.suite_name is not None: 88 if self.test_root is None or self.list_root is None: 89 return False 90 if self.generator_options: 91 if self.generator is None: 92 return False 93 return True 94 return (self.test_root is None and 95 self.list_root is None and 96 self.generator is None and 97 len(self.generator_options) == 0) 98