Skip to content

Commit

Permalink
Add constants in a class and make codebeamer_url configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
TannazVhdBMWExt committed Sep 27, 2024
1 parent b9f6424 commit 02a6620
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 117 deletions.
26 changes: 17 additions & 9 deletions lobster/tools/coda/coda.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
from lobster.items import Tracing_Tag, Activity
from lobster.location import File_Reference
from lobster.io import lobster_write
from lobster.tools.coda.parser.constants import LOBSTER_GENERATOR
from lobster.tools.coda.parser.constants import Constants
from lobster.tools.coda.parser.requirements_parser import ParserForRequirements

OUTPUT = "output"
CODEBEAMER_URL = "codebeamer_url"
MARKERS = "markers"
KIND = "kind"

Expand Down Expand Up @@ -65,7 +66,8 @@ def parse_config_file(file_name: str) -> dict:
"""
Parse the configuration dictionary from given config file.
The configuration dictionary for coda must contain the OUTPUT key.
The configuration dictionary for coda must contain the OUTPUT and
CODEBEAMER_URL keys.
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
Expand Down Expand Up @@ -93,9 +95,10 @@ def parse_config_file(file_name: str) -> dict:
with open(file_name, "r", encoding='utf-8') as file:
config_dict: dict = json.loads(file.read())

if OUTPUT not in config_dict.keys():
if (OUTPUT not in config_dict.keys() or
CODEBEAMER_URL not in config_dict.keys()):
raise ValueError(f'Please follow the right config file structure! '
f'Missing attribute "{OUTPUT}"')
f'Missing attribute "{OUTPUT}" and "{CODEBEAMER_URL}"')

output_config_dict = config_dict.get(OUTPUT)

Expand Down Expand Up @@ -171,14 +174,16 @@ def get_test_file_list(file_dir_list: list, extension_list: list) -> list:
return test_file_list


def collect_test_cases_from_test_files(test_file_list: list) -> list:
def collect_test_cases_from_test_files(test_file_list: list,
codebeamer_url: str) -> list:
"""
Collects the list of test cases from the given test files.
Parameters
----------
test_file_list : list
The list of test files.
codebeamer_url: str
Returns
-------
Expand All @@ -187,7 +192,8 @@ def collect_test_cases_from_test_files(test_file_list: list) -> list:
"""
parser = ParserForRequirements()
test_case_list = parser.collect_test_cases_for_test_files(
test_files=test_file_list
test_files=test_file_list,
codebeamer_url = codebeamer_url
)
return test_case_list

Expand Down Expand Up @@ -296,6 +302,7 @@ def write_lobster_items_output_dict(lobster_items_output_dict: dict):
lobster_items_output_dict : dict
The lobster items dictionary grouped by output.
"""
lobster_generator = Constants.LOBSTER_GENERATOR
for output_file_name, lobster_items in lobster_items_output_dict.items():
item_count = len(lobster_items)
if item_count <= 1:
Expand All @@ -306,7 +313,7 @@ def write_lobster_items_output_dict(lobster_items_output_dict: dict):
lobster_write(
output_file,
Activity,
LOBSTER_GENERATOR,
lobster_generator,
lobster_items.values()
)
print(f'Written {item_count} lobster items to '
Expand All @@ -316,7 +323,7 @@ def write_lobster_items_output_dict(lobster_items_output_dict: dict):
lobster_write(
sys.stdout,
Activity,
LOBSTER_GENERATOR,
lobster_generator,
lobster_items.values()
)
print(f'Written {item_count} lobster items to stdout.')
Expand All @@ -343,7 +350,8 @@ def lobster_coda(file_dir_list: list, config_dict: dict):

test_case_list = \
collect_test_cases_from_test_files(
test_file_list=test_file_list
test_file_list=test_file_list,
codebeamer_url=config_dict.get(CODEBEAMER_URL, '')
)

lobster_items_output_dict: dict = \
Expand Down
111 changes: 54 additions & 57 deletions lobster/tools/coda/parser/constants.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,64 @@
import re

# URLs
GIT_BASEDIR = "/vispiron-swe"
GITHUB_URL = "https://github.com/"
CODEBEMAER_URL = "https://codebeamer.bmwgroup.net/cb"

class Constants:
def __init__(self, codebeamer_url = ''):

NON_EXISTING_INFO = "---"
self.CODEBEAMER_LINK = codebeamer_url + "/issue/"
self.REQUIREMENT = re.compile(r".*[@\\]requirement\s+"
r"([\s*/]*(((CB-#)|({}))\d+)\s*,?)+"
.format(self.CODEBEAMER_LINK))
self.REQUIREMENT_TAG_HTTP = ((r"([@\\]requirement(\s+"
r"(CB-#\d+\s+)*({}\d+\s*,?\s*/*\*?)+)+)")
.format(self.CODEBEAMER_LINK))
self.REQUIREMENT_TAG_HTTP_NAMED = r"({}(?P<number>\d+))".format(self.CODEBEAMER_LINK)

LOBSTER_GENERATOR = "lobster_coda"
NON_EXISTING_INFO = "---"

VALID_TEST_MACROS = [
"TEST",
"TEST_P",
"TEST_F",
"TYPED_TEST",
"TYPED_TEST_P",
"TYPED_TEST_SUITE",
"TEST_P_INSTANCE",
"TEST_F_INSTANCE",
]
LOBSTER_GENERATOR = "lobster_coda"

VALID_TESTMETHODS = [
"TM_EQUIVALENCE",
"TM_PAIRWISE",
"TM_GUESSING",
"TM_BOUNDARY",
"TM_CONDITION",
"TM_REQUIREMENT",
"TM_TABLE",
]
VALID_TESTMETHODS = [
"TM_EQUIVALENCE",
"TM_PAIRWISE",
"TM_GUESSING",
"TM_BOUNDARY",
"TM_CONDITION",
"TM_REQUIREMENT",
"TM_TABLE",
]

VALID_TEST_MACROS = [
"TEST",
"TEST_P",
"TEST_F",
"TYPED_TEST",
"TYPED_TEST_P",
"TYPED_TEST_SUITE",
"TEST_P_INSTANCE",
"TEST_F_INSTANCE",
]

# Regex
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+)\)"
)
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+)\)"
)
REQUIREMENT_TAG = r"(CB-#\d+)"

CODEBEAMER_LINK = CODEBEMAER_URL + "/issue/"
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+"
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)+)?"
)
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+)([^@]+)")
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)+)?"
)
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+)([^@]+)")
11 changes: 7 additions & 4 deletions lobster/tools/coda/parser/requirements_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
class ParserForRequirements:

@staticmethod
def collect_test_cases_for_test_files(test_files: List[Path]) -> List:
def collect_test_cases_for_test_files(test_files: List[Path],
codebeamer_url: str = "") -> List:
"""
Parse a list of source files for test cases
Parameters
----------
test_files: List[Path]
Source files to parse
codebeamer_url: str
Returns
-------
Expand All @@ -29,20 +31,21 @@ def collect_test_cases_for_test_files(test_files: List[Path]) -> List:
test_cases = []

for file in set(test_files):
file_test_cases = ParserForRequirements.collect_test_cases(file)
file_test_cases = ParserForRequirements.collect_test_cases(file, codebeamer_url)
test_cases.extend(file_test_cases)

return test_cases

@staticmethod
def collect_test_cases(file: Path) -> List[TestCase]:
def collect_test_cases(file: Path, codebeamer_url: str = "") -> List[TestCase]:
"""
Parse a source file for test cases
Parameters
----------
file: Path
Source file to parse
codebeamer_url: str
Returns
-------
Expand All @@ -61,7 +64,7 @@ def collect_test_cases(file: Path) -> List[TestCase]:
test_cases = []

for i in range(0, len(lines)):
test_case = TestCase.try_parse(file, lines, i)
test_case = TestCase.try_parse(file, lines, i, codebeamer_url)

if test_case:
test_cases.append(test_case)
Expand Down
Loading

0 comments on commit 02a6620

Please sign in to comment.