From 40aa26f4b7c4c49b7177c1b45e78c94c28c9e792 Mon Sep 17 00:00:00 2001 From: ejseqera Date: Thu, 21 Mar 2024 15:28:35 -0400 Subject: [PATCH 1/2] fix: uniqueness on name for members with user key --- seqerakit/helper.py | 17 ++++++++++++++--- tests/unit/test_helper.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/seqerakit/helper.py b/seqerakit/helper.py index 0eec9b3..3dec340 100644 --- a/seqerakit/helper.py +++ b/seqerakit/helper.py @@ -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) @@ -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) diff --git a/tests/unit/test_helper.py b/tests/unit/test_helper.py index 42a5fb8..0092f37 100644 --- a/tests/unit/test_helper.py +++ b/tests/unit/test_helper.py @@ -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) + ) From ccd77227c309994d8f62fac142d6cd405753a1f4 Mon Sep 17 00:00:00 2001 From: ejseqera Date: Thu, 21 Mar 2024 15:32:41 -0400 Subject: [PATCH 2/2] chore: linting failures --- tests/unit/test_helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_helper.py b/tests/unit/test_helper.py index 0092f37..fcdccfc 100644 --- a/tests/unit/test_helper.py +++ b/tests/unit/test_helper.py @@ -390,7 +390,7 @@ def test_error_duplicate_name_yaml_file(mock_yaml_file): with pytest.raises(ValueError) as e: helper.parse_all_yaml([file_path]) assert ( - "Duplicate name key specified in config file for " + "Duplicate name key specified in config file for " "compute-envs: test_computeenv. Please specify " "a unique value." in str(e.value) )