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#
17
18import os
19import sys
20from datetime import datetime
21from typing import List
22
23import pytz
24from dotenv import load_dotenv
25
26from runner.logger import Log
27from runner.options.cli_options import get_args
28from runner.options.config import Config
29from runner.plugins_registry import PluginsRegistry
30from runner.runner_base import Runner
31
32
33def main() -> None:
34    dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
35    if os.path.exists(dotenv_path):
36        load_dotenv(dotenv_path)
37
38    args = get_args()
39    config = Config(args)
40    logger = Log.setup(config.general.verbose, config.general.work_dir)
41    config.log_warnings()
42    Log.summary(logger, f"Loaded configuration: {config}")
43    config.generate_config()
44
45    registry = PluginsRegistry()
46    config.custom.validate()
47    runners: List[Runner] = []
48
49    if config.general.processes == 1:
50        Log.default(logger, "Attention: tests are going to take only 1 process. The execution can be slow. "
51                            "You can set the option `--processes` to wished processes quantity "
52                            "or use special value `all` to use all available cores.")
53    start = datetime.now(pytz.UTC)
54    for test_suite in config.test_suites:
55        plugin = "ets" if test_suite.startswith("ets") or test_suite.startswith("sts") else test_suite
56        runner_class = registry.get_runner(plugin)
57        if runner_class is not None:
58            runners.append(runner_class(config))
59        else:
60            Log.exception_and_raise(logger, f"Plugin {plugin} not registered")
61
62    failed_tests = 0
63
64    if not config.general.generate_only:
65        for runner in runners:
66            Log.all(logger, f"Runner {runner.name} started")
67            runner.run()
68            Log.all(logger, f"Runner {runner.name} finished")
69            failed_tests += runner.summarize()
70            Log.all(logger, f"Runner {runner.name}: {failed_tests} failed tests")
71            if config.general.coverage.use_llvm_cov:
72                runner.create_coverage_html()
73
74    finish = datetime.now(pytz.UTC)
75    Log.default(logger, f"Runner has been working for {round((finish-start).total_seconds())} sec")
76
77    registry.cleanup()
78    sys.exit(0 if failed_tests == 0 else 1)
79
80
81if __name__ == "__main__":
82    main()
83