diff --git a/static_core/tests/tests-u-runner-2/main.py b/static_core/tests/tests-u-runner-2/main.py index 84c9a3411bbbee16667151fcf5b23f2425b2373b..c4fba0eed5d0e366e8e283b4fdaeaca34d90ba00 100644 --- a/static_core/tests/tests-u-runner-2/main.py +++ b/static_core/tests/tests-u-runner-2/main.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # - +import logging import os import sys from datetime import datetime, timedelta @@ -23,7 +23,7 @@ from typing import Any, cast import pytz -from runner.common_exceptions import InvalidConfiguration, RunnerException +from runner.common_exceptions import InvalidConfiguration, InvalidInitialization, RunnerException from runner.enum_types.verbose_format import VerboseKind from runner.environment import RunnerEnv from runner.init_runner import InitRunner @@ -40,10 +40,14 @@ def main() -> None: if init_runner.should_runner_initialize(sys.argv): init_runner.initialize(RunnerEnv.get_mandatory_props()) sys.exit(0) - RunnerEnv( - local_env=Path.cwd().with_name(".env"), - urunner_path=Path(__file__).parent, - global_env=init_runner.urunner_env_path).load_environment() + try: + RunnerEnv( + local_env=Path.cwd().with_name(".env"), + urunner_path=Path(__file__).parent, + global_env=init_runner.urunner_env_path).load_environment() + except InvalidInitialization as exc: + logging.critical(exc) + sys.exit(1) args = get_args() logger = load_config(args) diff --git a/static_core/tests/tests-u-runner-2/runner/common_exceptions.py b/static_core/tests/tests-u-runner-2/runner/common_exceptions.py index 6648ee00ccfbf5a05e23356339e612f0d3349e5a..e2407eb89aefa3d5d147cdcf1a4940e610c25c79 100644 --- a/static_core/tests/tests-u-runner-2/runner/common_exceptions.py +++ b/static_core/tests/tests-u-runner-2/runner/common_exceptions.py @@ -36,6 +36,10 @@ class RunnerException(Exception): super().__init__(self.message) +class InvalidInitialization(RunnerException): + pass + + class DownloadException(RunnerException): pass diff --git a/static_core/tests/tests-u-runner-2/runner/environment.py b/static_core/tests/tests-u-runner-2/runner/environment.py index 6bc2195cad3437b10deaafdb76fd48b2f4a5b2c4..544ec611999be3000a3f1ed89d6ad6573def05a2 100644 --- a/static_core/tests/tests-u-runner-2/runner/environment.py +++ b/static_core/tests/tests-u-runner-2/runner/environment.py @@ -21,10 +21,13 @@ from typing import ClassVar, NamedTuple from dotenv import load_dotenv +from runner.common_exceptions import InvalidInitialization from runner.init_runner import IsPath, MandatoryProp, MandatoryProps, PropName, RequireExist from runner.logger import Log _LOGGER = Log.get_logger(__file__) +RED = '\033[31m' +RESET = '\033[0m' class MandatoryPropDescription(NamedTuple): @@ -54,16 +57,16 @@ class RunnerEnv: def expand_mandatory_prop(prop_desc: MandatoryPropDescription) -> None: var_value = os.getenv(prop_desc.name) if var_value is None: - raise OSError(f"Mandatory environment variable '{prop_desc.name}' is not set. " - "Either specify it in the system environment or " - "run the runner with only init command: " - "./runner.sh init") + raise InvalidInitialization( + f"Mandatory environment variable '{prop_desc.name}' is not set. \n\n" + f"Run this command to initialize the runner: \n{RED}./runner.sh init{RESET} \n" + f"To see all available initialization options run:\n{RED}./runner.sh init --help{RESET}\n") if not prop_desc.is_path: return expanded = Path(var_value).expanduser().resolve() if prop_desc.require_exist and not expanded.exists(): - raise OSError(f"Mandatory environment variable '{prop_desc.name}' is set " - f"to value {var_value} which does not exist.") + raise InvalidInitialization(f"Mandatory environment variable '{prop_desc.name}' is set \n" + f"to value {var_value} which does not exist.") os.environ[prop_desc.name] = expanded.as_posix() @classmethod diff --git a/static_core/tests/tests-u-runner-2/runner/test/environment_test/environment_test.py b/static_core/tests/tests-u-runner-2/runner/test/environment_test/environment_test.py index 4d8d1d0ba1e07ce9927c7f7e91403e78b30819b2..42def7e8b27d6603438fff839d156b00dcf237a2 100644 --- a/static_core/tests/tests-u-runner-2/runner/test/environment_test/environment_test.py +++ b/static_core/tests/tests-u-runner-2/runner/test/environment_test/environment_test.py @@ -21,6 +21,7 @@ from typing import cast from unittest import TestCase from unittest.mock import patch +from runner.common_exceptions import InvalidInitialization from runner.environment import MandatoryPropDescription, RunnerEnv from runner.init_runner import InitRunner, MandatoryProp from runner.test.test_utils import assert_not_raise, random_suffix @@ -78,7 +79,7 @@ class EnvironmentTest(TestCase): def test_check_mandatory_prop_absent(self) -> None: # test self.assertRaises( - OSError, + InvalidInitialization, RunnerEnv.expand_mandatory_prop, MandatoryPropDescription('ARKCOMPILER_RUNTIME_CORE_PATH', is_path=True, require_exist=True)) @@ -88,7 +89,7 @@ class EnvironmentTest(TestCase): def test_check_mandatory_prop_not_exist_but_must(self) -> None: # test self.assertRaises( - OSError, + InvalidInitialization, RunnerEnv.expand_mandatory_prop, MandatoryPropDescription('ARKCOMPILER_RUNTIME_CORE_PATH', is_path=True, require_exist=True)) @@ -99,7 +100,7 @@ class EnvironmentTest(TestCase): # test assert_not_raise( test_case=self, - cls=OSError, + cls=InvalidInitialization, function=RunnerEnv.expand_mandatory_prop, params=[MandatoryPropDescription('WORK_DIR', is_path=True, require_exist=False)]) @@ -112,7 +113,7 @@ class EnvironmentTest(TestCase): # test assert_not_raise( test_case=self, - cls=OSError, + cls=InvalidInitialization, function=RunnerEnv.expand_mandatory_prop, params=[MandatoryPropDescription('ARKCOMPILER_RUNTIME_CORE_PATH', is_path=True, require_exist=True)]) # clear up @@ -127,7 +128,7 @@ class EnvironmentTest(TestCase): # test assert_not_raise( test_case=self, - cls=OSError, + cls=InvalidInitialization, function=RunnerEnv.expand_mandatory_prop, params=[MandatoryPropDescription('WORK_DIR', is_path=True, require_exist=False)]) # clear up