< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Class: ZhEaIsNsAaBn.DbSeeder.Tool.DesignVersionCheck
Assembly: ZhEaIsNsAaBn.DbSeeder.Tool
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.DbSeeder.Tool/DesignVersionCheck.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 69
Line coverage: 100%
Branch coverage
77%
Covered branches: 14
Total branches: 18
Branch coverage: 77.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Check(...)83.33%1212100%
DesignVersion(...)75%44100%
Plain(...)100%11100%
MajorMinor(...)50%22100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.DbSeeder.Tool/DesignVersionCheck.cs

#LineLine coverage
 1using System.Text.Json;
 2
 3namespace ZhEaIsNsAaBn.DbSeeder.Tool;
 4
 5/// <summary>
 6/// The startup project must reference <c>ZhEaIsNsAaBn.DbSeeder.Design</c> with the tool's <c>major.minor</c> (patch and
 7/// Read from its <c>deps.json</c> (<c>libraries["ZhEaIsNsAaBn.DbSeeder.Design/x.y.z"]</c>, package or project reference
 8/// runtime exactly, so this one check fixes both.
 9/// </summary>
 10internal static class DesignVersionCheck
 11{
 12    public const string DesignPackage = "ZhEaIsNsAaBn.DbSeeder.Design";
 13
 14
 15    /// <returns>The error text, or <see langword="null"/> when the versions match.</returns>
 16    /// <param name="toolVersion">The tool's version (e.g. <c>1.1.0</c>, <c>1.1.0-dev-0042</c>).</param>
 17    /// <param name="startupName">The startup project's name.</param>
 18    /// <param name="startupDepsJson">The startup project's <c>deps.json</c> text.</param>
 19    /// <param name="projectName">The <c>--project</c> name when it differs from the startup project.</param>
 20    /// <param name="projectDepsJson">Its <c>deps.json</c> text, when it has one.</param>
 21    public static string? Check( string toolVersion, string startupName, string startupDepsJson, string? projectName = n
 22    {
 3223        var design = DesignVersion( startupDepsJson );
 24
 3225        if ( design is null )
 26        {
 27            // "\n" joins, not a raw string: the text must not depend on the source file's line endings.
 828            var missing = string.Join( "\n", $"{startupName} doesn't reference {DesignPackage}. Add it to the startup pr
 829                                       $"  <PackageReference Include=\"{DesignPackage}\" Version=\"{toolVersion}\" Priva
 830                                       "PrivateAssets=\"all\" keeps it out of packages that reference this project." );
 31
 832            return projectName is not null && projectDepsJson is not null && DesignVersion( projectDepsJson ) is not nul
 833                       ? $"{missing}\nIt is referenced by {projectName}, but PrivateAssets=\"all\" stops it there: add i
 834                       : missing;
 35        }
 36
 4037        if ( MajorMinor( design ) == MajorMinor( toolVersion ) ) { return null; }
 38
 39
 840        var (major, minor) = MajorMinor( design );
 41
 842        return string.Join( "\n", $"The dbseeder tool is {toolVersion} but {startupName} uses {DesignPackage} {design}. 
 843                            $"  dotnet tool update ZhEaIsNsAaBn.DbSeeder.Tool --version {major}.{minor}.*",
 844                            $"  or set the {DesignPackage} PackageReference to {Plain( toolVersion )}." );
 45    }
 46
 47
 48    /// The Design version in a deps.json, <see langword="null"/> when it isn't there.
 49    public static string? DesignVersion( string depsJson )
 50    {
 3651        using var document = JsonDocument.Parse( depsJson );
 52
 3653        if ( !document.RootElement.TryGetProperty( "libraries", out var libraries ) ) { return null; }
 54
 3655        return libraries.EnumerateObject()
 3656                        .Select( l => l.Name )
 3657                        .FirstOrDefault( n => n.StartsWith( DesignPackage + "/", StringComparison.OrdinalIgnoreCase ) )?
 3658    }
 59
 6460    private static string Plain( string version ) => version.Split( '-', '+' )[ 0 ];
 61
 62    private static (int Major, int Minor) MajorMinor( string version )
 63    {
 5664        var parts = Plain( version ).Split( '.' );
 65
 5666        return ( int.Parse( parts[ 0 ], System.Globalization.CultureInfo.InvariantCulture ),
 5667                 parts.Length > 1 ? int.Parse( parts[ 1 ], System.Globalization.CultureInfo.InvariantCulture ) : 0 );
 68    }
 69}