Skip to content

Commit

Permalink
Introduce generic function to get variables properties
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Burghardt <[email protected]>
  • Loading branch information
marcusburghardt committed Jan 9, 2025
1 parent 5cc4ba4 commit 03b81cb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
17 changes: 17 additions & 0 deletions ssg/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ def _get_variables_content(content_dir: str) -> dict_type:
return variables_content


def get_variable_property(content_dir: str, variable_id: str, property_name: str) -> str:
"""
Retrieve a specific property of a variable from the content root directory.
Args:
content_dir (str): The root directory containing benchmark directories.
variable_id (str): The ID of the variable to retrieve the property for.
property_name (str): The name of the property to retrieve.
Returns:
str: The value of the specified property for the variable.
"""
variables_content = _get_variables_content(content_dir)
variable_content = variables_content.get(variable_id, {})
return variable_content.get(property_name, '')


def get_variable_options(content_dir: str, variable_id: str = None) -> dict_type:
"""
Retrieve the options for specific or all variables from the content root directory.
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/ssg-module/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
get_variable_files_in_folder,
get_variable_files,
get_variable_options,
get_variable_property,
get_variables_by_products,
get_variables_from_profiles,
get_variable_values,
Expand All @@ -25,7 +26,10 @@ def setup_test_files(base_dir, benchmark_dirs, create_txt_file=False):
path = base_dir / benchmark_dir
os.makedirs(path, exist_ok=True)
var_file = path / "test.var"
var_file.write_text("options:\n default: value\n option1: value1\n option2: value2\n")
var_file.write_text(
"options:\n default: value\n option1: value1\n option2: value2\n"
"title: Test Title\ndescription: Test Description\n"
)
if create_txt_file:
txt_file = path / "test.txt"
txt_file.write_text("options:\n option: value\n")
Expand Down Expand Up @@ -114,3 +118,19 @@ def __init__(self, product_id, profile_id, variables):

result = get_variables_from_profiles(profiles)
assert result == expected_result

def test_get_variable_property(tmp_path):
content_dir = tmp_path / "content"
benchmark_dirs = ["app", "app/rules"]
setup_test_files(content_dir, benchmark_dirs)

result = get_variable_property(str(content_dir), "test", "title")
assert result == "Test Title"

# Test for a non-existent property
result = get_variable_property(str(content_dir), "test", "non_existent_property")
assert result == ""

# Test for a non-existent variable
result = get_variable_property(str(content_dir), "non_existent_variable", "property_name")
assert result == ""

0 comments on commit 03b81cb

Please sign in to comment.