Skip to content

Commit

Permalink
YR/-BC-104-New-format-validation/CIAC-10001 (#4187)
Browse files Browse the repository at this point in the history
* testing

* ruff

* try

* exclude None

* try

* remove

* add name to the object

* changelog

* try

* add test

* Merge remote-tracking branch 'origin/master' into YR/test-graph-was-created-successfully/CIAC-9913

* init

* per command

* fix

* tomel

* revert

* try

* tomel

* better

* better

* remove function to tools

* fix

* push

* fix test

* fixes

* init

* test

* cr

* cr

* cr

* cr

* fix

* change name

* cr

* comment

* cr

* fix

* add comma
  • Loading branch information
RosenbergYehuda committed Apr 14, 2024
1 parent 42c819b commit 7691de3
Show file tree
Hide file tree
Showing 6 changed files with 415 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", "BC103","BC105", "BC108",
"BC100", "BC101", "BC102", "BC103", "BC104", "BC105", "BC108", "BC110",
"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
230 changes: 230 additions & 0 deletions demisto_sdk/commands/validate/tests/BC_validators_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
from typing import List

import pytest
Expand Down Expand Up @@ -27,6 +28,9 @@
from demisto_sdk.commands.validate.validators.BC_validators.BC103_args_name_change import (
ArgsNameChangeValidator,
)
from demisto_sdk.commands.validate.validators.BC_validators.BC104_have_commands_or_args_name_changed import (
HaveCommandsOrArgsNameChangedValidator,
)
from demisto_sdk.commands.validate.validators.BC_validators.BC105_id_changed import (
IdChangedValidator,
)
Expand All @@ -39,6 +43,9 @@
from demisto_sdk.commands.validate.validators.BC_validators.BC108_was_marketplace_modified import (
WasMarketplaceModifiedValidator,
)
from demisto_sdk.commands.validate.validators.BC_validators.BC110_new_required_argument_integration import (
NewRequiredArgumentIntegrationValidator,
)
from TestSuite.repo import ChangeCWD

ALL_MARKETPLACES = list(MarketplaceVersions)
Expand All @@ -49,6 +56,54 @@
XSOAR_MARKETPLACE_FOR_IN_PACK = [ALL_MARKETPLACES_FOR_IN_PACK[0]]


# Create a new content item with 3 commands with unique names. all commands have only 1 argument except the third command which has 2 arguments.

GENERIC_INTEGRATION_WITH_3_COMMANDS_AND_4_ARGS = create_integration_object(
paths=["script.commands"],
values=[
[
{
"name": "command_1",
"description": "test",
"arguments": [
{
"name": "arg_1_command_1",
"description": "nothing description.",
}
],
"outputs": [],
},
{
"name": "command_2",
"description": "test",
"arguments": [
{
"name": "arg_1_command_2",
"description": "nothing description.",
}
],
"outputs": [],
},
{
"name": "command_3",
"description": "test",
"arguments": [
{
"name": "arg_1_command_3",
"description": "nothing description.",
},
{
"name": "arg_2_command_3",
"description": "nothing description.",
},
],
"outputs": [],
},
]
],
)


@pytest.mark.parametrize(
"content_items, old_content_items, expected_number_of_failures, expected_msgs",
[
Expand Down Expand Up @@ -994,3 +1049,178 @@ def test_args_name_change_validator__passes():

create_old_file_pointers(modified_content_items, old_content_items)
assert not ArgsNameChangeValidator().is_valid(modified_content_items)


def test_HaveCommandsOrArgsNameChangedValidator__fails():
"""
Given
- A new content item with 3 commands. all commands have only 1 argument except the third command which has 2 arguments.
- An old content item with the same structure as the new content item, but with different command and argument names.
When
- Calling the HaveCommandsOrArgsNameChangedValidator.
Then
- Make sure the validation fails and the right error message is returned notifying only on changes in the second command name, and third command argument name.
- The first command has no changes.
- The second command name has changed from 'old_command_1' to 'command_1', and its arg name has changed from 'old_arg_1_command_1' to 'arg_1_command_1.
(the args change souled be ignored since we reported on its command name change.)
- The third command name has not changed, but one of its arguments name has changed from 'old_arg_1_command_2' to 'arg_1_command_2'.
"""
# Setup new content item with changes in command and argument names
new_content_item = GENERIC_INTEGRATION_WITH_3_COMMANDS_AND_4_ARGS

# Setup old content item with original command and argument names
old_content_item = deepcopy(new_content_item)
old_content_item.commands[1].name = "old_command_1"
old_content_item.commands[1].args[0].name = "old_arg_1_command_1"
old_content_item.commands[2].args[0].name = "old_arg_1_command_2"

# Create old file pointers and validate
create_old_file_pointers([new_content_item], [old_content_item])
results = HaveCommandsOrArgsNameChangedValidator().is_valid([new_content_item])

assert (
'changes to the names of the following existing commands:"old_command_1". In addition, you have made changes to the names of existing arguments: In command "command_3" the following arguments have been changed: "old_arg_1_command_2".'
in results[0].message
)


def test_HaveCommandsOrArgsNameChangedValidator__passes():
"""
Given
- An old content item with 3 commands. all commands have only 1 argument except the third command which has 2 arguments.
- A new content item with the same structure as the old content item, but with addition of a new argument to the third command, and a new command as well.
When
- Calling the HaveCommandsOrArgsNameChangedValidator.
Then
- Make sure the validation passes and no error messages are returned, since adding new commands or arguments is allowed.
"""
# Setup new content item with changes in command and argument names
old_content_item = GENERIC_INTEGRATION_WITH_3_COMMANDS_AND_4_ARGS
new_content_item = deepcopy(old_content_item)

new_content_item.commands.append(
create_integration_object(
paths=["script.commands"], values=[[{"name": "command_4"}]]
).commands[0]
)
# add a new argument to the third command
new_content_item.commands[2].args.append(
create_integration_object(
paths=[
"script.commands",
],
values=[
[
{
"name": "command_1",
"description": "test",
"arguments": [
{
"name": "arg_3_command_1",
}
],
"outputs": [],
}
]
],
)
.commands[0]
.args[0]
)

# Setup old content item with original command and argument names
old_content_item = GENERIC_INTEGRATION_WITH_3_COMMANDS_AND_4_ARGS

create_old_file_pointers([new_content_item], [old_content_item])
assert not HaveCommandsOrArgsNameChangedValidator().is_valid([new_content_item])


def test_NewRequiredArgumentValidator__fails():
"""
Given
- A old content item with 3 commands. all commands have only 1 argument except the third command which has 2 arguments.
- A new content item with the same structure as the old content item, but with changes in the second command argument to be required and a new required argument in the third command.
When
- Calling the NewRequiredArgumentValidator.
Then
- Make sure the validation fails and the right error message is returned notifying on the new required argument in the second command and the third command.
"""
old_content_item = GENERIC_INTEGRATION_WITH_3_COMMANDS_AND_4_ARGS
new_content_item = deepcopy(old_content_item)
# add new required arg
new_content_item.commands[2].args.append(
create_integration_object(
paths=[
"script.commands",
],
values=[
[
{
"name": "test_command",
"arguments": [
{
"name": "arg_3_command_3",
"required": True,
}
],
}
]
],
)
.commands[0]
.args[0]
)

# change existing arg to be required
new_content_item.commands[1].args[0].required = True

create_old_file_pointers([new_content_item], [old_content_item])
res = NewRequiredArgumentIntegrationValidator().is_valid([new_content_item])

assert (
"added the following new *required* arguments: in command 'command_2' you have added a new required argument:'arg_1_command_2'. in command 'command_3' you have added a new required argument:'arg_3_command_3'."
in res[0].message
)


def test_NewRequiredArgumentValidator__passes():
"""
Given
- A old content item with 3 commands. all commands have only 1 argument except the third command which has 2 arguments.
- A new content item with the same structure as the old content item, but with changes in the second command argument to be required and a new required argument in the new forth command with a required argument.
When
- Calling the NewRequiredArgumentValidator.
Then
- Make sure the validation passes and no error messages are returned, since adding new required arguments is allowed
if the new required argument is in a new command or if it has a default value.
"""
new_content_item = deepcopy(GENERIC_INTEGRATION_WITH_3_COMMANDS_AND_4_ARGS)
# change existing arg to be required but add a default value
new_content_item.commands[1].args[0].required = True
new_content_item.commands[1].args[0].defaultvalue = "test"
# add new command with required arg without a default value
new_content_item.commands.append(
create_integration_object(
paths=[
"script.commands",
],
values=[
[
{
"name": "command_4",
"arguments": [
{
"name": "arg_1_command_4",
"required": True,
}
],
}
]
],
).commands[0]
)
old_content_item = GENERIC_INTEGRATION_WITH_3_COMMANDS_AND_4_ARGS
create_old_file_pointers([new_content_item], [old_content_item])

assert not NewRequiredArgumentIntegrationValidator().is_valid([new_content_item])
21 changes: 21 additions & 0 deletions demisto_sdk/commands/validate/tests/tools_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from demisto_sdk.commands.validate.tools import (
collect_all_inputs_from_inputs_section,
collect_all_inputs_in_use,
compare_lists,
)


Expand Down Expand Up @@ -87,3 +88,23 @@ def test_collect_all_inputs_from_inputs_section(content_item, expected_result):
- A set with all inputs defined in the inputs section with no duplicates or empty spaces are returned.
"""
assert collect_all_inputs_from_inputs_section(content_item) == expected_result


def test_compare_lists():
"""
Given:
- A `main_list` containing the elements "a", "b", and "c".
- A `sub_list` containing the elements "a", "b", "b", and "d".
When:
- The function `compare_lists` is called with `sub_list` and `main_list` as arguments.
Then:
- Ensuring the value returned is a list containing the elements "b" and "d", which are present in `sub_list` but not in `main_list`.
"""
main_list = ["a", "b", "c"]
sub_list = ["a", "b", "b", "d"]
assert compare_lists(sub_list, main_list) == [
"b",
"d",
]
2 changes: 2 additions & 0 deletions demisto_sdk/commands/validate/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ def find_command(commands: List[Command], command_to_find: str) -> Optional[Comm
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.
for example:
compare_lists(['a', 'b', 'c', 'c', 'd'], ['a', 'b', 'c']) -> ['c', 'd']
Args:
sub_list: list of elements to compare if they are a subset of main_list
main_list: the list to compare against
Expand Down

0 comments on commit 7691de3

Please sign in to comment.