< Summary

Information
Class: DotNetApiDiff.Reporting.ReportGenerator
Assembly: DotNetApiDiff
File(s): /home/runner/work/dotnet-api-diff/dotnet-api-diff/src/DotNetApiDiff/Reporting/ReportGenerator.cs
Line coverage
85%
Covered lines: 41
Uncovered lines: 7
Coverable lines: 48
Total lines: 102
Line coverage: 85.4%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
GenerateReport(...)100%22100%
SaveReportAsync()50%5458.82%
GetSupportedFormats()100%11100%

File(s)

/home/runner/work/dotnet-api-diff/dotnet-api-diff/src/DotNetApiDiff/Reporting/ReportGenerator.cs

#LineLine coverage
 1// Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT
 2using DotNetApiDiff.Interfaces;
 3using DotNetApiDiff.Models;
 4using Microsoft.Extensions.Logging;
 5
 6namespace DotNetApiDiff.Reporting;
 7
 8/// <summary>
 9/// Implementation of the report generator that supports multiple output formats
 10/// </summary>
 11public class ReportGenerator : IReportGenerator
 12{
 13    private readonly ILogger<ReportGenerator> _logger;
 14    private readonly Dictionary<ReportFormat, IReportFormatter> _formatters;
 15
 16    /// <summary>
 17    /// Initializes a new instance of the <see cref="ReportGenerator"/> class.
 18    /// </summary>
 19    /// <param name="logger">The logger.</param>
 20    /// <param name="formatters">Optional dictionary of formatters to use. If null, default formatters will be created.<
 621    public ReportGenerator(ILogger<ReportGenerator> logger, Dictionary<ReportFormat, IReportFormatter>? formatters = nul
 622    {
 623        _logger = logger;
 24
 625        if (formatters != null)
 426        {
 427            _formatters = formatters;
 428        }
 29        else
 230        {
 231            _formatters = new Dictionary<ReportFormat, IReportFormatter>
 232            {
 233                { ReportFormat.Console, new ConsoleFormatter() },
 234                { ReportFormat.Json, new JsonFormatter() },
 235                { ReportFormat.Html, new HtmlFormatterScriban() },
 236                { ReportFormat.Markdown, new MarkdownFormatter() }
 237
 238                // Other formatters can be added in subsequent tasks
 239                // { ReportFormat.Xml, new XmlFormatter() }
 240            };
 241        }
 642    }
 43
 44    /// <summary>
 45    /// Generates a report from the comparison result
 46    /// </summary>
 47    /// <param name="result">The comparison result to generate a report from</param>
 48    /// <param name="format">The desired output format</param>
 49    /// <returns>Generated report as a string</returns>
 50    public string GenerateReport(ComparisonResult result, ReportFormat format)
 351    {
 352        _logger.LogInformation("Generating report in {Format} format", format);
 53
 354        if (_formatters.TryGetValue(format, out var formatter))
 255        {
 256            return formatter.Format(result);
 57        }
 58
 159        _logger.LogWarning("Requested format {Format} is not supported, falling back to Console format", format);
 160        return _formatters[ReportFormat.Console].Format(result);
 361    }
 62
 63    /// <summary>
 64    /// Saves a report to the specified file path
 65    /// </summary>
 66    /// <param name="result">The comparison result to generate a report from</param>
 67    /// <param name="format">The desired output format</param>
 68    /// <param name="filePath">Path where the report should be saved</param>
 69    public async Task SaveReportAsync(ComparisonResult result, ReportFormat format, string filePath)
 170    {
 171        _logger.LogInformation("Saving {Format} report to {FilePath}", format, filePath);
 72
 173        var report = GenerateReport(result, format);
 74
 75        try
 176        {
 77            // Ensure directory exists
 178            var directory = Path.GetDirectoryName(filePath);
 179            if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
 080            {
 081                Directory.CreateDirectory(directory);
 082            }
 83
 184            await File.WriteAllTextAsync(filePath, report);
 185            _logger.LogInformation("Report saved successfully to {FilePath}", filePath);
 186        }
 087        catch (Exception ex)
 088        {
 089            _logger.LogError(ex, "Failed to save report to {FilePath}", filePath);
 090            throw;
 91        }
 192    }
 93
 94    /// <summary>
 95    /// Gets the supported report formats
 96    /// </summary>
 97    /// <returns>Collection of supported report formats</returns>
 98    public IEnumerable<ReportFormat> GetSupportedFormats()
 199    {
 1100        return _formatters.Keys;
 1101    }
 102}