Coverage for lobster/tools/cpptest/requirements_parser.py: 91%
26 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-09-22 04:43 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-09-22 04:43 +0000
1# LOBSTER - Lightweight Open BMW Software Traceability Evidence Report
2# Copyright (C) 2024-2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as
6# published by the Free Software Foundation, either version 3 of the
7# License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12# Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public
15# License along with this program. If not, see
16# <https://www.gnu.org/licenses/>.
18"""
19This script verifies if the test files of a given
20target contains @requirement tag or not
21"""
22import logging
23from pathlib import Path
24from typing import List
26from lobster.tools.cpptest.testcase import TestCase
29class ParserForRequirements:
30 @staticmethod
31 def collect_test_cases_for_test_files(
32 test_files: List[Path],
33 codebeamer_url: str = "",
34 ) -> List:
35 """
36 Parse a list of source files for test cases
38 Parameters
39 ----------
40 test_files: List[Path]
41 Source files to parse
42 codebeamer_url: str
44 Returns
45 -------
46 List[TestCase]
47 List of parsed TestCase
48 """
49 test_cases = []
51 for file in set(test_files):
52 file_test_cases = (
53 ParserForRequirements.collect_test_cases(file, codebeamer_url))
54 test_cases.extend(file_test_cases)
56 return test_cases
58 @staticmethod
59 def collect_test_cases(
60 file: Path,
61 codebeamer_url: str = "",
62 ) -> List[TestCase]:
63 """
64 Parse a source file for test cases
66 Parameters
67 ----------
68 file: Path
69 Source file to parse
70 codebeamer_url: str
72 Returns
73 -------
74 List[TestCase]
75 List of parsed TestCase
76 """
78 try:
79 with open(file, "r", encoding="UTF-8", errors="ignore") as f:
80 lines = f.readlines()
82 except Exception as e: # pylint: disable=broad-exception-caught
83 logging.error("exception %s", e)
84 return []
86 test_cases = []
88 for i in range(0, len(lines)):
89 test_case = TestCase.try_parse(file, lines, i, codebeamer_url)
91 if test_case:
92 test_cases.append(test_case)
93 return test_cases