diff --git a/gdk/commands/component/transformer/BuildRecipeTransformer.py b/gdk/commands/component/transformer/BuildRecipeTransformer.py index c09d67d6..c65de4a9 100644 --- a/gdk/commands/component/transformer/BuildRecipeTransformer.py +++ b/gdk/commands/component/transformer/BuildRecipeTransformer.py @@ -31,8 +31,7 @@ def transform(self, build_folders): # Validate the size of the recipe file before processing its content. if not utils.valid_recipe_file_size(self.project_config.recipe_file): - logging.error("The size of the recipe exceeds the maximum allowed size. Please ensure the recipe size " - "does not exceed 16 KB.") + logging.error(error_messages.RECIPE_SIZE_INVALID.format(self.project_config.recipe_file)) raise Exception(error_messages.RECIPE_SIZE_INVALID.format(self.project_config.recipe_file)) component_recipe = CaseInsensitiveRecipeFile().read(self.project_config.recipe_file) diff --git a/gdk/common/RecipeValidator.py b/gdk/common/RecipeValidator.py index fda3658d..fff4ede2 100644 --- a/gdk/common/RecipeValidator.py +++ b/gdk/common/RecipeValidator.py @@ -61,23 +61,21 @@ def validate_recipe_format_version(self): Raises ------ Exception - If the RecipeFormatVersion field is missing or unsupported. + If the RecipeFormatVersion field is missing. """ recipe_format_version = self.recipe_data.get("recipeformatversion") if not recipe_format_version: - logging.error("Recipe validation failed for 'RecipeFormatVersion'. This field is required but missing " - "from the recipe. Please correct it and try again.") - raise Exception("The recipe file is invalid. The 'RecipeFormatVersion' field is mandatory in the recipe.") + err_msg = "Recipe validation failed for 'RecipeFormatVersion'. This field is required but missing " \ + "from the recipe. Please correct it and try again." + logging.error(err_msg) + raise Exception(err_msg) supported_recipe_version = ["2020-01-25"] if recipe_format_version not in supported_recipe_version: - logging.error(f"Recipe validation failed for: 'RecipeFormatVersion: {recipe_format_version}'.") - logging.error(f"The provided RecipeFormatVersion '{recipe_format_version}' is not supported in this gdk " - f"version. Please ensure that it is a valid RecipeFormatVersion compatible with the gdk, " - f"and refer to the list of supported RecipeFormatVersion: {supported_recipe_version}.") - raise Exception("The provided RecipeFormatVersion in the recipe is invalid. Please ensure that it follows " - "the correct format and matches one of the supported versions.") + logging.warning(f"The provided RecipeFormatVersion '{recipe_format_version}' is not supported in this gdk " + f"version. Please ensure that it is a valid RecipeFormatVersion compatible with the gdk, " + f"and refer to the list of supported RecipeFormatVersion: {supported_recipe_version}.") def validate_semantics(self): """ diff --git a/gdk/common/consts.py b/gdk/common/consts.py index b3797361..395b2be9 100644 --- a/gdk/common/consts.py +++ b/gdk/common/consts.py @@ -13,6 +13,10 @@ "metavar", "dest", ] + +# FILE SIZE +MAX_RECIPE_FILE_SIZE_BYTES = 16000 + # FILES config_schema_file = "config_schema.json" user_input_recipe_schema_file = "user_input_recipe_schema.json" diff --git a/gdk/common/exceptions/error_messages.py b/gdk/common/exceptions/error_messages.py index eaabd1fb..4ddd3bbe 100644 --- a/gdk/common/exceptions/error_messages.py +++ b/gdk/common/exceptions/error_messages.py @@ -8,7 +8,7 @@ ) PROJECT_CONFIG_FILE_INVALID = "Project configuration file '{}' is invalid. Please correct its format and try again. Error: {} " RECIPE_FILE_INVALID = "The input recipe file '{}' is invalid. Please correct its format and try again. Error: {} " -RECIPE_SIZE_INVALID = "The input recipe file '{}' has an invalid size. Please make sure it does not exceed 16KB and try again." +RECIPE_SIZE_INVALID = "The input recipe file '{}' has an invalid size. Please make sure it does not exceed 16kB and try again." CLI_MODEL_FILE_NOT_EXISTS = "Model validation failed. CLI model file doesn't exist." USER_INPUT_RECIPE_NOT_EXISTS = ( "Recipe file not found. " diff --git a/gdk/common/utils.py b/gdk/common/utils.py index f0f90b2b..e9e660bb 100644 --- a/gdk/common/utils.py +++ b/gdk/common/utils.py @@ -8,6 +8,7 @@ import gdk import gdk._version as version +from gdk.common import consts from gdk.common.exceptions import syntax_error_message @@ -229,7 +230,7 @@ def parse_json_schema_errors(error): def valid_recipe_file_size(file_path): file_size = os.path.getsize(file_path) - return file_size < 16234 # max allowed byte + return file_size <= consts.MAX_RECIPE_FILE_SIZE_BYTES error_line = "\n=============================== ERROR ===============================\n" diff --git a/tests/gdk/commands/component/transformer/test_BuildRecipeTransformer.py b/tests/gdk/commands/component/transformer/test_BuildRecipeTransformer.py index f488ae99..b8a029d4 100644 --- a/tests/gdk/commands/component/transformer/test_BuildRecipeTransformer.py +++ b/tests/gdk/commands/component/transformer/test_BuildRecipeTransformer.py @@ -94,17 +94,21 @@ def test_transform_missing_recipe_format_version_expect_exception(self): try: transformer.transform(build_folders) except Exception as e: - assert str(e) == "The recipe file is invalid. The 'RecipeFormatVersion' field is mandatory in the recipe." + assert str(e) == "Recipe validation failed for 'RecipeFormatVersion'. This field is required " \ + "but missing from the recipe. Please correct it and try again." mock_size.assert_called_once_with(config.recipe_file) mock_read.assert_called_once_with(config.recipe_file) mock_update.assert_not_called() mock_create.assert_not_called() - def test_transform_invalid_recipe_format_version_expect_exception(self): + def test_transform_invalid_recipe_format_version_expect_early_warning(self): mock_size = self.mocker.patch("gdk.common.utils.valid_recipe_file_size", return_value=True) mock_read = self.mocker.patch.object(CaseInsensitiveRecipeFile, "read", - return_value=CaseInsensitiveDict({"RecipeFormatVersion": "77777"})) + return_value=CaseInsensitiveDict({ + "RecipeFormatVersion": "2023-01-25", + "ComponentName": "com.example.HelloWorld" + })) mock_update = self.mocker.patch.object(BuildRecipeTransformer, "update_component_recipe_file", return_value=None) mock_create = self.mocker.patch.object(BuildRecipeTransformer, "create_build_recipe_file", return_value=None) @@ -112,11 +116,16 @@ def test_transform_invalid_recipe_format_version_expect_exception(self): config = ComponentBuildConfiguration({}) transformer = BuildRecipeTransformer(config) - try: - transformer.transform(build_folders) - except Exception as e: - assert str(e) == "The provided RecipeFormatVersion in the recipe is invalid. Please ensure that it " \ - "follows the correct format and matches one of the supported versions." + with mock.patch('gdk.common.RecipeValidator.logging') as mock_logging: + with pytest.raises(Exception): + transformer.transform(build_folders) + + assert mock_logging.warning.call_count == 1 + warnings = mock_logging.warning.call_args[0] + expected_warnings = "The provided RecipeFormatVersion '2023-01-25' is not supported in this gdk version. " \ + "Please ensure that it is a valid RecipeFormatVersion compatible with the gdk, " \ + "and refer to the list of supported RecipeFormatVersion: ['2020-01-25']." + assert any(expected_warnings in arg for arg in warnings) mock_size.assert_called_once_with(config.recipe_file) mock_read.assert_called_once_with(config.recipe_file) diff --git a/tests/gdk/common/test_RecipeValidator.py b/tests/gdk/common/test_RecipeValidator.py index 81f0ebad..465741d5 100644 --- a/tests/gdk/common/test_RecipeValidator.py +++ b/tests/gdk/common/test_RecipeValidator.py @@ -23,23 +23,23 @@ def test_validate_missing_recipe_format_version_expect_exception(self): validator = RecipeValidator(invalid_recipe) with pytest.raises(Exception) as e: validator.validate_recipe_format_version() - self.assertEqual(str(e.value), "The recipe file is invalid. The 'RecipeFormatVersion' field is mandatory in " - "the recipe.") + self.assertEqual(str(e.value), "Recipe validation failed for 'RecipeFormatVersion'. This field is required " + "but missing from the recipe. Please correct it and try again.") mock_logging_error.assert_called_with("Recipe validation failed for 'RecipeFormatVersion'. This field is " "required but missing from the recipe. Please correct it and try again.") - def test_validate_invalid_recipe_format_version_expect_exception(self): - mock_logging_error = self.mocker.patch('logging.error') - invalid_recipe = CaseInsensitiveDict({"RecipeFormatVersion": "99999"}) - validator = RecipeValidator(invalid_recipe) - with pytest.raises(Exception) as e: + def test_validate_incompatible_recipe_format_version_expect_warning(self): + recipe_data = CaseInsensitiveDict({"RecipeFormatVersion": "2023-01-25"}) + validator = RecipeValidator(recipe_data) + with mock.patch('gdk.common.RecipeValidator.logging') as mock_logging: validator.validate_recipe_format_version() - self.assertEqual(str(e.value), "The provided RecipeFormatVersion in the recipe is invalid. Please ensure that " - "it follows the correct format and matches one of the supported versions.") - mock_logging_error.assert_called_with("The provided RecipeFormatVersion '99999' is not supported in this gdk " - "version. Please ensure that it is a valid RecipeFormatVersion " - "compatible with the gdk, and refer to the list of supported " - "RecipeFormatVersion: ['2020-01-25'].") + assert mock_logging.warning.call_count == 1 + warnings = mock_logging.warning.call_args[0] + expected_warnings = "The provided RecipeFormatVersion '2023-01-25' is not supported in this " \ + "gdk version. Please ensure that it is a valid RecipeFormatVersion " \ + "compatible with the gdk, and refer to the list of supported " \ + "RecipeFormatVersion: ['2020-01-25']." + assert any(expected_warnings in arg for arg in warnings) def test_validate_semantics_valid_recipe(self): valid_recipe = CaseInsensitiveDict({