Skip to content

Commit

Permalink
Merge dbc746a into 44b4997
Browse files Browse the repository at this point in the history
  • Loading branch information
RosenbergYehuda committed Apr 3, 2024
2 parents 44b4997 + dbc746a commit 911d175
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
2 changes: 1 addition & 1 deletion demisto_sdk/commands/validate/sdk_validation_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ select = [
"BA100", "BA101", "BA105", "BA106", "BA110", "BA111", "BA114", "BA116", "BA118", "BA119", "BA124", "BA126",
"PA100", "PA101", "PA102", "PA103", "PA104", "PA105", "PA107", "PA108", "PA109", "PA111", "PA113", "PA115", "PA117", "PA118", "PA119", "PA120",
"PA121", "PA123", "PA125", "PA127", "PA128", "PA130",
"BC100", "BC101", "BC102", "BC105", "BC108",
"BC100", "BC101", "BC102", "BC103","BC105", "BC108",
"IN100", "IN101", "IN102", "IN104", "IN106", "IN107", "IN108", "IN109", "IN110", "IN112", "IN113", "IN114", "IN115", "IN117", "IN118", "IN121", "IN122", "IN123", "IN124", "IN125", "IN126", "IN127", "IN130",
"IN131", "IN134", "IN135", "IN139", "IN141", "IN142", "IN144", "IN145", "IN146", "IN149", "IN150", "IN151", "IN152", "IN153", "IN154", "IN156", "IN158", "IN159", "IN160",
"IN161", "IN162",
Expand Down
56 changes: 56 additions & 0 deletions demisto_sdk/commands/validate/tests/BC_validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
from demisto_sdk.commands.validate.validators.BC_validators.BC102_is_context_path_changed import (
IsContextPathChangedValidator,
)
from demisto_sdk.commands.validate.validators.BC_validators.BC103_have_the_args_changed import (
HaveTheArgsChangedValidator,
)
from demisto_sdk.commands.validate.validators.BC_validators.BC105_id_changed import (
IdChangedValidator,
)
Expand Down Expand Up @@ -941,3 +944,56 @@ def test_IsValidToversionOnModifiedValidator_is_valid(content_items, old_content
and result[0].message
== "Changing the maximal supported version field `toversion` is not allowed. Please undo, or request a force merge."
)


def test_have_the_args_changed_validator__fails():
"""
Given:
- Script content item with a changed argument name.
- Old Script content item with the old argument name.
When:
- Calling the `HaveTheArgsChangedValidator` function.
Then:
- The results should be as expected.
- Should fail the validation since the user changed the argument name.
"""
modified_content_items = [
create_script_object(paths=["args[0].name"], values=["new_arg"])
]
old_content_items = [
create_script_object(paths=["args[0].name"], values=["old_arg"])
]

create_old_file_pointers(modified_content_items, old_content_items)

results = HaveTheArgsChangedValidator().is_valid(modified_content_items)
assert (
" contain changes to the names of the following existing arguments: old_arg. Please undo the changes."
in results[0].message
)


def test_have_the_args_changed_validator__passes():
"""
Given:
- Script content item with a new argument name, and an existing argument name.
- Old Script content item with the existing argument name.
When:
- Calling the `HaveTheArgsChangedValidator` function.
Then:
- The results should be as expected.
- Should pass the validation since the user didn't change existing argument names, only added new ones.
"""
modified_content_items = [
create_script_object(paths=["args[0].name"], values=["old_arg"])
]
new_arg = create_script_object(paths=["args[0].name"], values=["new_arg"]).args[0]
modified_content_items[0].args.append(new_arg)
old_content_items = [
create_script_object(paths=["args[0].name"], values=["old_arg"])
]

create_old_file_pointers(modified_content_items, old_content_items)
13 changes: 13 additions & 0 deletions demisto_sdk/commands/validate/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from collections import Counter
from pathlib import Path
from typing import Dict, List, Optional, Set

Expand Down Expand Up @@ -147,3 +148,15 @@ def find_command(commands: List[Command], command_to_find: str) -> Optional[Comm
if command.name == command_to_find:
return command
return None


def compare_lists(sub_list: List[str], main_list: List[str]) -> List[str]:
"""
Compares two lists and returns the elements that are in the sub list but not in the main list, including duplicates.
Args:
sub_list: list of elements to compare if they are a subset of main_list
main_list: the list to compare against
Returns:
List the elements that appear in the sublist but not in the main list, including duplicates if they are not all present in the main list.
"""
return list((Counter(sub_list) - Counter(main_list)).elements())
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

from typing import Iterable, List

from demisto_sdk.commands.common.constants import GitStatuses
from demisto_sdk.commands.content_graph.objects.script import Script
from demisto_sdk.commands.validate.tools import compare_lists
from demisto_sdk.commands.validate.validators.base_validator import (
BaseValidator,
ValidationResult,
)

ContentTypes = Script


class HaveTheArgsChangedValidator(BaseValidator[ContentTypes]):
error_code = "BC103"
description = "Check if the argument name has been changed."
rationale = "If an existing argument has been renamed, it will break backward compatibility."
error_message = (
"Possible backward compatibility break: Your updates to this file contain changes "
"to the names of the following existing arguments: {args}. Please undo the changes."
)
related_field = "args.name"
is_auto_fixable = False
expected_git_statuses = [GitStatuses.MODIFIED]

def is_valid(self, content_items: Iterable[ContentTypes]) -> List[ValidationResult]:
results: List[ValidationResult] = []
for content_item in content_items:

current_args = [arg.name for arg in content_item.args]
old_args = [arg.name for arg in content_item.old_base_content_object.args] # type: ignore
args_diff = compare_lists(sub_list=old_args, main_list=current_args)

if args_diff:
results.append(
ValidationResult(
validator=self,
message=self.error_message.format(args=", ".join(args_diff)),
content_object=content_item,
)
)

return results

0 comments on commit 911d175

Please sign in to comment.