Skip to content

Commit

Permalink
[Validate] Refactor IM108 (#4064)
Browse files Browse the repository at this point in the history
* new validate IM108

* Merge branch 'master' into IM108_validate_refactoring
  • Loading branch information
jbabazadeh committed Feb 20, 2024
1 parent 0f0ce54 commit 43be044
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
4 changes: 4 additions & 0 deletions demisto_sdk/commands/content_graph/objects/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,7 @@ def get_related_content(self) -> Dict[RelatedFileType, Dict]:
@property
def readme(self) -> str:
return self.get_related_text_file(RelatedFileType.README)

@property
def author_image_path(self) -> Path:
return Path(self.related_content[RelatedFileType.AUTHOR_IMAGE]["path"][0])
45 changes: 45 additions & 0 deletions demisto_sdk/commands/validate/tests/IM_validators_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest

from demisto_sdk.commands.validate.tests.test_tools import (
create_metadata_object,
)
from demisto_sdk.commands.validate.validators.IM_validators.IM108_author_image_is_empty import (
AuthorImageIsEmptyValidator,
)


@pytest.mark.parametrize(
"content_items, expected_number_of_failures, expected_msgs",
[
([create_metadata_object()], 0, []),
(
[create_metadata_object(image="")],
1,
["The author image should not be empty. Please provide a relevant image."],
),
],
)
def test_AuthorImageIsEmptyValidator_is_valid(
content_items, expected_number_of_failures, expected_msgs
):
"""
Given
content_items.
- Case 1: Author image not empty.
- Case 2: Author image is empty.
When
- Calling the AuthorImageIsEmptyValidator is_valid function.
Then
- Make sure the right amount of pack author image failed, and that the right error message is returned.
- Case 1: Shouldn't fail.
- Case 2: Should fail.
"""
results = AuthorImageIsEmptyValidator().is_valid(content_items)
assert len(results) == expected_number_of_failures
assert all(
[
result.message == expected_msg
for result, expected_msg in zip(results, expected_msgs)
]
)
3 changes: 3 additions & 0 deletions demisto_sdk/commands/validate/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def create_metadata_object(
values: Optional[List[Any]] = None,
fields_to_delete: Optional[List[str]] = None,
readme_text: str = "",
image: Optional[str] = None,
) -> PackMetadata:
"""Creating an pack_metadata object with altered fields from a default pack_metadata json structure.
Expand All @@ -204,6 +205,8 @@ def create_metadata_object(
PackParser.parse_ignored_errors = MagicMock(return_value={})
pack.pack_metadata.write_json(json_content)
pack.readme.write_text(readme_text)
if image is not None:
pack.author_image.write(image)
return BaseContent.from_path(Path(pack.path))


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

from typing import Iterable, List

from demisto_sdk.commands.common.constants import RelatedFileType
from demisto_sdk.commands.content_graph.objects.pack import Pack
from demisto_sdk.commands.validate.validators.base_validator import (
BaseValidator,
ValidationResult,
)

ContentTypes = Pack


class AuthorImageIsEmptyValidator(BaseValidator[ContentTypes]):
error_code = "IM108"
description = "Checks that the author image file is not empty"
error_message = (
"The author image should not be empty. Please provide a relevant image."
)
related_field = "image"
is_auto_fixable = False
related_file_type = [RelatedFileType.AUTHOR_IMAGE]

def is_valid(self, content_items: Iterable[ContentTypes]) -> List[ValidationResult]:
return [
ValidationResult(
validator=self,
message=self.error_message,
content_object=content_item,
)
for content_item in content_items
if (content_item.author_image_path.stat().st_size == 0)
]

0 comments on commit 43be044

Please sign in to comment.