Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uniqueness bug with dicts using keys other than name #132

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions seqerakit/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def parse_yaml_block(yaml_data, block_name):
name_values = set()

# Iterate over each item in the block.
# TODO: fix for resources that can be duplicate named in an org
for item in block:
cmd_args = parse_block(block_name, item)
name = find_name(cmd_args)
if name in name_values:
raise ValueError(
f" Duplicate 'name' specified in config file"
f" for {block_name}: {name}. Please specify a unique name."
f" Duplicate name key specified in config file"
f" for {block_name}: {name}. Please specify a unique value."
)
name_values.add(name)

Expand Down Expand Up @@ -344,12 +345,22 @@ def find_name(cmd_args):
"""
Find and return the value associated with --name in cmd_args, where cmd_args
can be a list, a tuple of lists, or nested lists/tuples.

The function searches for the following keys: --name, --user, --email.

Parameters:
- cmd_args: The command arguments (list, tuple, or nested structures).

Returns:
- The value associated with the first key found, or None if none are found.
"""
# Predefined list of keys to search for
keys = {"--name", "--user", "--email"}

def search(args):
it = iter(args)
for arg in it:
if arg == "--name":
if isinstance(arg, str) and arg in keys:
return next(it, None)
elif isinstance(arg, (list, tuple)):
result = search(arg)
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,35 @@ def test_error_type_yaml_file(mock_yaml_file):
"Please specify at least 'type' or 'file-path' for creating the resource."
in str(e.value)
)


def test_error_duplicate_name_yaml_file(mock_yaml_file):
test_data = {
"compute-envs": [
{
"name": "test_computeenv",
"workspace": "my_organization/my_workspace",
"credentials": "my_credentials",
"type": "aws-batch",
"config-mode": "forge",
"wait": "AVAILABLE",
},
{
"name": "test_computeenv",
"workspace": "my_organization/my_workspace",
"credentials": "my_credentials",
"type": "aws-batch",
"config-mode": "forge",
"wait": "AVAILABLE",
},
],
}
file_path = mock_yaml_file(test_data)

with pytest.raises(ValueError) as e:
helper.parse_all_yaml([file_path])
assert (
"Duplicate name key specified in config file for "
"compute-envs: test_computeenv. Please specify "
"a unique value." in str(e.value)
)
Loading