< Summary

Information
Class: DotNetApiDiff.Models.Configuration.ComparisonConfiguration
Assembly: DotNetApiDiff
File(s): /home/runner/work/dotnet-api-diff/dotnet-api-diff/src/DotNetApiDiff/Models/Configuration/ComparisonConfiguration.cs
Line coverage
92%
Covered lines: 52
Uncovered lines: 4
Coverable lines: 56
Total lines: 139
Line coverage: 92.8%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Mappings()100%11100%
get_Exclusions()100%11100%
get_BreakingChangeRules()100%11100%
get_Filters()100%11100%
get_OutputFormat()100%11100%
get_OutputPath()100%11100%
get_FailOnBreakingChanges()100%11100%
CreateDefault()100%11100%
LoadFromJsonFile(...)66.66%6680.95%
IsValid()100%66100%
SaveToJsonFile(...)100%11100%

File(s)

/home/runner/work/dotnet-api-diff/dotnet-api-diff/src/DotNetApiDiff/Models/Configuration/ComparisonConfiguration.cs

#LineLine coverage
 1// Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT
 2using System.ComponentModel.DataAnnotations;
 3using System.Text.Json;
 4using System.Text.Json.Serialization;
 5
 6namespace DotNetApiDiff.Models.Configuration;
 7
 8/// <summary>
 9/// Main configuration for API comparison
 10/// </summary>
 11public class ComparisonConfiguration
 12{
 13    /// <summary>
 14    /// Configuration for namespace and type mappings
 15    /// </summary>
 16    [JsonPropertyName("mappings")]
 28017    public MappingConfiguration Mappings { get; set; } = new MappingConfiguration();
 18
 19    /// <summary>
 20    /// Configuration for excluding types and members
 21    /// </summary>
 22    [JsonPropertyName("exclusions")]
 29823    public ExclusionConfiguration Exclusions { get; set; } = new ExclusionConfiguration();
 24
 25    /// <summary>
 26    /// Configuration for breaking change rules
 27    /// </summary>
 28    [JsonPropertyName("breakingChangeRules")]
 33329    public BreakingChangeRules BreakingChangeRules { get; set; } = new BreakingChangeRules();
 30
 31    /// <summary>
 32    /// Configuration for filtering types and namespaces
 33    /// </summary>
 34    [JsonPropertyName("filters")]
 32335    public FilterConfiguration Filters { get; set; } = new FilterConfiguration();
 36
 37    /// <summary>
 38    /// Output format for the comparison results
 39    /// </summary>
 40    [JsonPropertyName("outputFormat")]
 24541    public ReportFormat OutputFormat { get; set; } = ReportFormat.Console;
 42
 43    /// <summary>
 44    /// Path to the output file (if not specified, output is written to console)
 45    /// </summary>
 46    [JsonPropertyName("outputPath")]
 9947    public string? OutputPath { get; set; }
 48
 49    /// <summary>
 50    /// Whether to fail on breaking changes
 51    /// </summary>
 52    [JsonPropertyName("failOnBreakingChanges")]
 19053    public bool FailOnBreakingChanges { get; set; } = true;
 54
 55    /// <summary>
 56    /// Creates a default configuration
 57    /// </summary>
 58    /// <returns>A default configuration</returns>
 59    public static ComparisonConfiguration CreateDefault()
 6460    {
 6461        return new ComparisonConfiguration
 6462        {
 6463            Mappings = MappingConfiguration.CreateDefault(),
 6464            Exclusions = ExclusionConfiguration.CreateDefault(),
 6465            BreakingChangeRules = BreakingChangeRules.CreateDefault(),
 6466            Filters = FilterConfiguration.CreateDefault(),
 6467            OutputFormat = ReportFormat.Console,
 6468            OutputPath = null,
 6469            FailOnBreakingChanges = true
 6470        };
 6471    }
 72
 73    /// <summary>
 74    /// Loads configuration from a JSON file
 75    /// </summary>
 76    /// <param name="path">Path to the JSON file</param>
 77    /// <returns>The loaded configuration</returns>
 78    /// <exception cref="FileNotFoundException">Thrown when the file is not found</exception>
 79    /// <exception cref="JsonException">Thrown when the JSON is invalid</exception>
 80    public static ComparisonConfiguration LoadFromJsonFile(string path)
 1681    {
 1682        if (!File.Exists(path))
 283        {
 284            throw new FileNotFoundException($"Configuration file not found: {path}");
 85        }
 86
 1487        var json = File.ReadAllText(path);
 1488        var options = new JsonSerializerOptions
 1489        {
 1490            PropertyNameCaseInsensitive = true,
 1491            ReadCommentHandling = JsonCommentHandling.Skip,
 1492            AllowTrailingCommas = true,
 1493            Converters = { new JsonStringEnumConverter(null, false) }
 1494        };
 95
 1496        var config = JsonSerializer.Deserialize<ComparisonConfiguration>(json, options);
 1297        if (config == null)
 098        {
 099            throw new JsonException("Failed to deserialize configuration");
 100        }
 101
 12102        if (!config.IsValid())
 0103        {
 0104            throw new ValidationException("Configuration validation failed");
 105        }
 106
 12107        return config;
 12108    }
 109
 110    /// <summary>
 111    /// Validates the configuration
 112    /// </summary>
 113    /// <returns>True if valid, false otherwise</returns>
 114    public bool IsValid()
 26115    {
 26116        return Mappings.IsValid() &&
 26117               Exclusions.IsValid() &&
 26118               Filters.IsValid() &&
 26119               Enum.IsDefined(typeof(ReportFormat), OutputFormat);
 26120    }
 121
 122    /// <summary>
 123    /// Saves configuration to a JSON file
 124    /// </summary>
 125    /// <param name="path">Path to the JSON file</param>
 126    /// <exception cref="IOException">Thrown when the file cannot be written</exception>
 127    public void SaveToJsonFile(string path)
 3128    {
 3129        var options = new JsonSerializerOptions
 3130        {
 3131            WriteIndented = true,
 3132            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 3133            Converters = { new JsonStringEnumConverter(null, false) }
 3134        };
 135
 3136        var json = JsonSerializer.Serialize(this, options);
 3137        File.WriteAllText(path, json);
 3138    }
 139}