-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
17d9380
commit a17d76d
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
## Instructions for Adding Another Coverage Type to the `CoverageProcessor` Class | ||
|
||
To add another coverage type to the `CoverageProcessor` class, follow these steps: | ||
|
||
### Step 1: Update the `coverage_type` Argument | ||
|
||
1. **Add the new coverage type to the `Literal` type hint** in the `CoverageProcessor` class initializer. | ||
|
||
```python | ||
class CoverageProcessor: | ||
def __init__( | ||
self, file_path: str, src_file_path: str, coverage_type: Literal["cobertura", "lcov", "jacoco", "new_coverage_type"] | ||
): | ||
# Initialization code remains the same | ||
``` | ||
|
||
### Step 2: Update the `parse_coverage_report` Method | ||
|
||
1. **Add a new `elif` block** to handle the new coverage type. | ||
|
||
```python | ||
def parse_coverage_report(self) -> Tuple[list, list, float]: | ||
if self.coverage_type == "cobertura": | ||
return self.parse_coverage_report_cobertura() | ||
elif self.coverage_type == "lcov": | ||
# Placeholder for LCOV report parsing | ||
raise NotImplementedError( | ||
f"Parsing for {self.coverage_type} coverage reports is not implemented yet." | ||
) | ||
elif self.coverage_type == "jacoco": | ||
return self.parse_coverage_report_jacoco() | ||
elif self.coverage_type == "new_coverage_type": | ||
return self.parse_coverage_report_new_coverage_type() | ||
else: | ||
raise ValueError(f"Unsupported coverage report type: {self.coverage_type}") | ||
``` | ||
|
||
### Step 3: Implement the New Coverage Report Parsing Method | ||
|
||
1. **Create a new method** to handle the parsing of the new coverage report type. | ||
|
||
```python | ||
def parse_coverage_report_new_coverage_type(self) -> Tuple[list, list, float]: | ||
""" | ||
Parses a new coverage report type to extract covered and missed line numbers for a specific file, | ||
and calculates the coverage percentage. | ||
Returns: | ||
Tuple[list, list, float]: A tuple containing lists of covered and missed line numbers, and the coverage percentage. | ||
""" | ||
# Example implementation - replace with actual parsing logic | ||
lines_covered, lines_missed = [], [] | ||
# Parsing logic for the new coverage report type | ||
# For example, if the report is in JSON format: | ||
import json | ||
with open(self.file_path, 'r') as f: | ||
report_data = json.load(f) | ||
# Example logic to process the JSON data | ||
filename = os.path.basename(self.src_file_path) | ||
for file_data in report_data.get('files', []): | ||
if file_data.get('filename') == filename: | ||
for line in file_data.get('lines', []): | ||
line_number = line.get('number') | ||
hits = line.get('hits') | ||
if hits > 0: | ||
lines_covered.append(line_number) | ||
else: | ||
lines_missed.append(line_number) | ||
break | ||
|
||
total_lines = len(lines_covered) + len(lines_missed) | ||
coverage_percentage = ( | ||
(len(lines_covered) / total_lines) if total_lines > 0 else 0 | ||
) | ||
|
||
return lines_covered, lines_missed, coverage_percentage | ||
``` | ||
|
||
### Step 4: Test the Implementation | ||
|
||
1. **Ensure the new coverage report type is properly parsed** by adding unit tests to `tests/test_CoverageProcessor.py`. | ||
2. **Verify the `CoverageProcessor` works with the new coverage type** by running the tests and checking the output. | ||
|
||
### Conclusion | ||
|
||
By following these steps, you can extend the `CoverageProcessor` class to support additional coverage report types, ensuring flexibility and adaptability for different coverage formats. |