Skip to content

Commit

Permalink
apply review
Browse files Browse the repository at this point in the history
  • Loading branch information
TannazVhdBMWExt committed Sep 23, 2024
1 parent 724f26f commit 4a7dc65
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 101 deletions.
81 changes: 58 additions & 23 deletions lobster/tools/coda/coda.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
#
# lobster_coda - Extract C/C++ tracing tags from commands for LOBSTER
# Copyright (C) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
# Copyright (C) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -68,7 +68,8 @@ def parse_config_file(file_name: str) -> dict:
The configuration dictionary for coda must contain the OUTPUT key.
Each output configuration dictionary contains a file name as key and
a value dictionary containing the keys MARKERS and KIND.
The supported values for the MARKERS list are specified in SUPPORTED_REQUIREMENTS.
The supported values for the MARKERS list are specified in
SUPPORTED_REQUIREMENTS.
Parameters
----------
Expand Down Expand Up @@ -102,15 +103,18 @@ def parse_config_file(file_name: str) -> dict:
for output_file, output_file_config_dict in output_config_dict.items():
if MARKERS not in output_file_config_dict.keys():
raise Exception(f'Please follow the right config file structure! '
f'Missing attribute "{MARKERS}" for output file "{output_file}"')
f'Missing attribute "{MARKERS}" for output file '
f'"{output_file}"')
if KIND not in output_file_config_dict.keys():
raise Exception(f'Please follow the right config file structure! '
f'Missing attribute "{KIND}" for output file "{output_file}"')
f'Missing attribute "{KIND}" for output file '
f'"{output_file}"')

output_file_marker_list = output_file_config_dict.get(MARKERS)
for output_file_marker in output_file_marker_list:
if output_file_marker not in SUPPORTED_REQUIREMENTS:
raise Exception(f'"{output_file_marker}" is not a supported "{MARKERS}" value '
raise Exception(f'"{output_file_marker}" is not a supported '
f'"{MARKERS}" value '
f'for output file "{output_file}". '
f'Supported values are: "{supported_markers}"')

Expand All @@ -121,14 +125,17 @@ def get_test_file_list(file_dir_list: list, extension_list: list) -> list:
"""
Gets the list of test files.
Given file names are added to the test file list without validating against the extension list.
From given directory names only file names will be added to the test file list if their extension matches
against the extension list.
Given file names are added to the test file list without
validating against the extension list.
From given directory names only file names will be added
to the test file list if their extension matches against
the extension list.
Parameters
----------
file_dir_list : list
A list containing file names and/or directory names to parse for file names.
A list containing file names and/or directory names
to parse for file names.
extension_list : list
The list of file name extensions.
Expand Down Expand Up @@ -178,13 +185,18 @@ def collect_test_cases_from_test_files(test_file_list: list) -> list:
The list of test cases.
"""
parser = ParserForRequirements()
test_case_list = parser.collect_test_cases_for_test_files(test_files=test_file_list)
test_case_list = parser.collect_test_cases_for_test_files(
test_files=test_file_list
)
return test_case_list


def create_lobster_items_output_dict_from_test_cases(test_case_list: list, config_dict: dict) -> dict:
def create_lobster_items_output_dict_from_test_cases(
test_case_list: list,
config_dict: dict) -> dict:
"""
Creates the lobster items dictionary for the given test cases grouped by configured output.
Creates the lobster items dictionary for the given test cases grouped by
configured output.
Parameters
----------
Expand All @@ -196,7 +208,8 @@ def create_lobster_items_output_dict_from_test_cases(test_case_list: list, confi
Returns
-------
dict
The lobster items dictionary for the given test cases grouped by configured output.
The lobster items dictionary for the given test cases
grouped by configured output.
"""
prefix = os.getcwd()
lobster_items_output_dict = {}
Expand Down Expand Up @@ -235,17 +248,25 @@ def create_lobster_items_output_dict_from_test_cases(test_case_list: list, confi
)

contains_no_tracing_target = True
for output_file_name, output_config_dict in marker_output_config_dict.items():
for output_file_name, output_config_dict in (
marker_output_config_dict.items()):
tracing_target_list = []
tracing_target_kind = output_config_dict.get(KIND)
for marker in output_config_dict.get(MARKERS):
if marker not in map_test_type_to_key_name.keys():
continue

for test_case_marker_value in getattr(test_case, map_test_type_to_key_name.get(marker)):
for test_case_marker_value in getattr(
test_case,
map_test_type_to_key_name.get(marker)
):
if MISSING not in test_case_marker_value:
test_case_marker_value = test_case_marker_value.replace(CB_PREFIX, "")
tracing_target = Tracing_Tag(tracing_target_kind, test_case_marker_value)
test_case_marker_value = (
test_case_marker_value.replace(CB_PREFIX, ""))
tracing_target = Tracing_Tag(
tracing_target_kind,
test_case_marker_value
)
tracing_target_list.append(tracing_target)

if len(tracing_target_list) >= 1:
Expand All @@ -254,10 +275,12 @@ def create_lobster_items_output_dict_from_test_cases(test_case_list: list, confi
for tracing_target in tracing_target_list:
lobster_item.add_tracing_target(tracing_target)

lobster_items_output_dict.get(output_file_name)[key] = lobster_item
lobster_items_output_dict.get(output_file_name)[key] = (
lobster_item)

if contains_no_tracing_target:
lobster_items_output_dict.get(no_marker_output_file_name)[key] = activity
lobster_items_output_dict.get(no_marker_output_file_name)[key] = (
activity)

return lobster_items_output_dict

Expand All @@ -279,11 +302,22 @@ def write_lobster_items_output_dict(lobster_items_output_dict: dict):

if output_file_name:
with open(output_file_name, "w", encoding="UTF-8") as output_file:
lobster_write(output_file, Activity, LOBSTER_GENERATOR, lobster_items.values())
print(f'Written {item_count} lobster items to "{output_file_name}".')
lobster_write(
output_file,
Activity,
LOBSTER_GENERATOR,
lobster_items.values()
)
print(f'Written {item_count} lobster items to '
f'"{output_file_name}".')

else:
lobster_write(sys.stdout, Activity, LOBSTER_GENERATOR, lobster_items.values())
lobster_write(
sys.stdout,
Activity,
LOBSTER_GENERATOR,
lobster_items.values()
)
print(f'Written {item_count} lobster items to stdout.')


Expand Down Expand Up @@ -324,7 +358,8 @@ def lobster_coda(file_dir_list: list, config_dict: dict):

def main():
"""
Main function to parse arguments, read configuration and launch lobster_coda.
Main function to parse arguments, read configuration
and launch lobster_coda.
"""
ap = argparse.ArgumentParser()
ap.add_argument("files",
Expand Down
25 changes: 17 additions & 8 deletions lobster/tools/coda/parser/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,39 @@
"TM_CONDITION",
"TM_REQUIREMENT",
"TM_TABLE",
"TM_BOUNDARY",
]


# Regex
TEST_CASE_INTRO = re.compile(r"^\s*(" + "|".join(VALID_TEST_MACROS) + r")\s*\(")
TEST_CASE_INTRO = re.compile(r"^\s*(" +
"|".join(VALID_TEST_MACROS) +
r")\s*\(")
TEST_CASE_INFO = re.compile(
r"^\s*(" + "|".join(VALID_TEST_MACROS) + r")\s*\(\s*(?P<suite_name>\w+),\s*(?P<test_name>\w+)\)"
r"^\s*(" + "|".join(VALID_TEST_MACROS) +
r")\s*\(\s*(?P<suite_name>\w+),\s*(?P<test_name>\w+)\)"
)

CODEBEAMER_LINK = CODEBEMAER_URL + "/issue/"
REQUIREMENT = re.compile(r".*[@\\]requirement\s+([\s*/]*(((CB-#)|({}))\d+)\s*,?)+".format(CODEBEAMER_LINK))
REQUIREMENT = re.compile(r".*[@\\]requirement\s+"
r"([\s*/]*(((CB-#)|({}))\d+)\s*,?)+"
.format(CODEBEAMER_LINK))
REQUIREMENT_TAG = r"(CB-#\d+)"
REQUIREMENT_TAG_HTTP = r"([@\\]requirement(\s+(CB-#\d+\s+)*({}\d+\s*,?\s*/*\*?)+)+)".format(CODEBEAMER_LINK)
REQUIREMENT_TAG_HTTP = ((r"([@\\]requirement(\s+"
r"(CB-#\d+\s+)*({}\d+\s*,?\s*/*\*?)+)+)")
.format(CODEBEAMER_LINK))
REQUIREMENT_TAG_HTTP_NAMED = r"({}(?P<number>\d+))".format(CODEBEAMER_LINK)
REQUIRED_BY = re.compile(r".*[@\\]requiredby\s+([\s*/]*(\w*::\w+),?\s*)+")
REQUIRED_BY_TAG = r"(\w*::\w+)"
DEFECT = re.compile(
r"(@defect\s+)(((?:(CB-#\d+)|(OCT-#\d+)),?\s*)+)" + r"(?:///|/)\s+(((?:(CB-#\d+)|(OCT-#\d+)),?\s)+)?"
r"(@defect\s+)(((?:(CB-#\d+)|(OCT-#\d+)),?\s*)+)" +
r"(?:///|/)\s+(((?:(CB-#\d+)|(OCT-#\d+)),?\s)+)?"
)
BRIEF = re.compile(r"(@brief\s+)([^@]+)")
VERSION = re.compile(r"(@version\s+)(\d+([,]? \d+)*)+")
OCT_TAG = r"(OCT-#\d+)"
TESTMETHODS = re.compile(r"(@testmethods\s+)([^@]+)")
# unmatch whole testmethod if invalid method is used
# TESTMETHODS = re.compile(r"(@testmethods\s+)((" + "|".join(VALID_TESTMETHODS) + ")([,]? (" + "|".join(VALID_TESTMETHODS) + "))*)+")
TEST = re.compile(r"(@test\s+)([^@]+)")
# TESTMETHODS = re.compile(r"(@testmethods\s+
# )((" + "|".join(VALID_TESTMETHODS) +
# ")([,]? (" + "|".join(VALID_TESTMETHODS) + "))*)+")
TEST = re.compile(r"(@test\s+)([^@]+)")
4 changes: 2 additions & 2 deletions lobster/tools/coda/parser/requirements_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
This script verifies if the test files of a given target contains @requirement tag or not
This script verifies if the test files of a given
target contains @requirement tag or not
"""
import logging
from pathlib import Path
Expand Down Expand Up @@ -65,4 +66,3 @@ def collect_test_cases(file: Path) -> List[TestCase]:
if test_case:
test_cases.append(test_case)
return test_cases

Loading

0 comments on commit 4a7dc65

Please sign in to comment.