feat: Support ignoring members with a regex pattern

This commit is contained in:
Youssef Victor 2023-05-15 10:27:41 +03:00
Родитель 65d36bb6d8
Коммит 5b9abb060b
10 изменённых файлов: 110 добавлений и 34 удалений

Просмотреть файл

@ -8,7 +8,7 @@
<Author>nventive</Author>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<LangVersion>latest</LangVersion>
<!-- Version management is now done through NuGet, this avoids issues related version mismatch -->
<Version>255.255.255.255</Version>

Просмотреть файл

@ -69,5 +69,40 @@ namespace Uno.PackageDiff.Tests
Assert.IsNotNull(context.IgnoreSet);
Assert.IsFalse(ReportAnalyzer.GenerateReport(StreamWriter.Null, comparison, context.IgnoreSet));
}
[TestMethod]
public void When_Ignore_With_Regex()
{
var context = _builder.BuildAssemblies();
var res = AssemblyComparer.CompareTypes(context.BaseAssembly, context.TargetAssembly);
Assert.IsNotNull(context.IgnoreSet);
using var writer = new StringWriter();
Assert.IsTrue(ReportAnalyzer.GenerateReport(writer, res, context.IgnoreSet));
var result = writer.ToString();
Assert.AreEqual("""
### 0 missing types:
### 8 missing or changed method in existing types:
- `Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex`
* ``System.Int32 Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex.get_P1_Ignored()``
* ``System.Void Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex.set_P1_Ignored(System.Int32 value)``
* ``System.Int32 Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex.get_P2_Ignored()``
* ``System.Void Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex.set_P2_Ignored(System.Int32 value)``
* ``System.Int32 Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex.get_P3_Ignored()``
* ``System.Void Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex.set_P3_Ignored(System.Int32 value)``
* ``System.Int32 Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex.get_P_NotIgnored()``
* ``System.Void Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex.set_P_NotIgnored(System.Int32 value)``
### 0 missing or changed events in existing types:
### 0 missing or changed fields in existing types:
### 4 missing or changed properties in existing types:
- `Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex`
* ~~``System.Int32 Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex::P1_Ignored()``~~
* ~~``System.Int32 Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex::P2_Ignored()``~~
* ~~``System.Int32 Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex::P3_Ignored()``~~
* ``System.Int32 Uno.PackageDiff.Tests.Sources.Given_ReportAnalyzer_When_Ignore_With_Regex::P_NotIgnored()``
""", result);
}
}
}

Просмотреть файл

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Uno.PackageDiff.Tests.Sources
{
public class Given_ReportAnalyzer_When_Ignore_With_Regex
{
public int P1_Ignored { get; set; }
public int P2_Ignored { get; set; }
public int P3_Ignored { get; set; }
public int P_NotIgnored { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<DiffIgnore>
<IgnoreSets>
<IgnoreSet baseVersion="1.0.0">
<Properties>
<Member fullName=".+?_Ignored" isRegex="true" />
</Properties>
</IgnoreSet>
</IgnoreSets>
</DiffIgnore>

Просмотреть файл

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Uno.PackageDiff.Tests.Sources
{
public class Given_ReportAnalyzer_When_Ignore_With_Regex
{
}
}

Просмотреть файл

@ -21,6 +21,9 @@
<None Remove="Sources\Given_ReportAnalyzer_When_IgnoreProperty.base.cs" />
<None Remove="Sources\Given_ReportAnalyzer_When_IgnoreProperty.diff.xml" />
<None Remove="Sources\Given_ReportAnalyzer_When_IgnoreProperty.target.cs" />
<None Remove="Sources\Given_ReportAnalyzer_When_Ignore_With_Regex.base.cs" />
<None Remove="Sources\Given_ReportAnalyzer_When_Ignore_With_Regex.diff.xml" />
<None Remove="Sources\Given_ReportAnalyzer_When_Ignore_With_Regex.target.cs" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
namespace Uno.PackageDiff
@ -41,7 +42,7 @@ namespace Uno.PackageDiff
private static bool CompareVersion(string baseVersion, string ignoreSetVersion)
{
var ignoreSetVersionDotCount = ignoreSetVersion.Count(c => c == '.') ;
var ignoreSetVersionDotCount = ignoreSetVersion.Count(c => c == '.');
var baseVersion2 = new Version(baseVersion);
var ignoreSetVersion2 = new Version(ignoreSetVersionDotCount == 0 ? ignoreSetVersion + ".0" : ignoreSetVersion);
@ -83,5 +84,15 @@ namespace Uno.PackageDiff
{
[XmlAttribute("fullName")]
public string FullName { get; set; }
[XmlAttribute("isRegex")]
public bool IsRegex { get; set; }
public bool Matches(string member, StringComparison stringComparison = StringComparison.Ordinal)
{
return IsRegex
? Regex.IsMatch(member, FullName)
: FullName.Equals(member, stringComparison);
}
}
}

Просмотреть файл

@ -73,7 +73,7 @@ namespace Uno.PackageDiff
{
var targetFileName = Path.GetFileNameWithoutExtension(targetFile);
if(!ignoreSet.Assemblies.Any(m => m.FullName.Equals(targetFileName, StringComparison.OrdinalIgnoreCase)))
if(!ignoreSet.Assemblies.Any(m => m.Matches(targetFileName, StringComparison.OrdinalIgnoreCase)))
{
Console.WriteLine($"The assembly {targetFileName} could not be found in the target package");
differences = true;

Просмотреть файл

@ -9,28 +9,27 @@ namespace Uno.PackageDiff
{
public class ReportAnalyzer
{
public static bool GenerateReport(StreamWriter writer, ComparisonResult results, IgnoreSet ignoreSet)
public static bool GenerateReport(TextWriter writer, ComparisonResult results, IgnoreSet ignoreSet)
{
var ignoredTypeNames = ignoreSet?.Types.Select(t2 => t2.FullName).ToArray();
var isFailed = false;
isFailed |= ReportMissingTypes(writer, results, ignoreSet);
isFailed |= ReportMethods(writer, results, ignoreSet, ignoredTypeNames);
isFailed |= ReportEvents(writer, results, ignoreSet, ignoredTypeNames);
isFailed |= ReportFields(writer, results, ignoreSet, ignoredTypeNames);
isFailed |= ReportProperties(writer, results, ignoreSet, ignoredTypeNames);
isFailed |= ReportMethods(writer, results, ignoreSet);
isFailed |= ReportEvents(writer, results, ignoreSet);
isFailed |= ReportFields(writer, results, ignoreSet);
isFailed |= ReportProperties(writer, results, ignoreSet);
return isFailed;
}
private static bool ReportMissingTypes(StreamWriter writer, ComparisonResult results, IgnoreSet ignoreSet)
private static bool ReportMissingTypes(TextWriter writer, ComparisonResult results, IgnoreSet ignoreSet)
{
var shouldFail = false;
writer.WriteLine("### {0} missing types:", results.InvalidTypes.Length);
foreach(var invalidType in results.InvalidTypes)
{
var invalidTypeSignature = invalidType.ToSignature();
var isIgnored = ignoreSet.Types
.Select(t => t.FullName)
.Contains(invalidType.ToSignature());
.Any(t => t.Matches(invalidTypeSignature));
var strike = isIgnored
? "~~" : "";
@ -46,7 +45,7 @@ namespace Uno.PackageDiff
return shouldFail;
}
private static bool ReportProperties(StreamWriter writer, ComparisonResult results, IgnoreSet ignoreSet, string[] ignoredTypeNames)
private static bool ReportProperties(TextWriter writer, ComparisonResult results, IgnoreSet ignoreSet)
{
var shouldFail = false;
@ -61,10 +60,10 @@ namespace Uno.PackageDiff
writer.WriteLine("- `{0}`", updatedType.Key);
foreach(var property in updatedType)
{
var propertySignature = property.ToSignature();
var isIgnored = ignoreSet.Properties
.Select(t => t.FullName)
.Contains(property.ToSignature())
|| IsDeclaringTypeIgnored(property, ignoredTypeNames);
.Any(t => t.Matches(propertySignature))
|| IsDeclaringTypeIgnored(property, ignoreSet);
var strike = isIgnored
? "~~" : "";
@ -82,7 +81,7 @@ namespace Uno.PackageDiff
return shouldFail;
}
private static bool ReportFields(StreamWriter writer, ComparisonResult results, IgnoreSet ignoreSet, string[] ignoredTypeNames)
private static bool ReportFields(TextWriter writer, ComparisonResult results, IgnoreSet ignoreSet)
{
var shouldFail = false;
@ -97,10 +96,9 @@ namespace Uno.PackageDiff
writer.WriteLine("- `{0}`", updatedType.Key);
foreach(var field in updatedType)
{
var isIgnored = ignoreSet.Fields
.Select(t => t.FullName)
.Contains(field.ToSignature())
|| IsDeclaringTypeIgnored(field, ignoredTypeNames);
var fieldSignature = field.ToSignature();
var isIgnored = ignoreSet.Fields.Any(f => f.Matches(fieldSignature))
|| IsDeclaringTypeIgnored(field, ignoreSet);
var strike = isIgnored
? "~~" : "";
@ -118,7 +116,7 @@ namespace Uno.PackageDiff
return shouldFail;
}
private static bool ReportMethods(StreamWriter writer, ComparisonResult results, IgnoreSet ignoreSet, string[] ignoredTypeNames)
private static bool ReportMethods(TextWriter writer, ComparisonResult results, IgnoreSet ignoreSet)
{
var shouldFail = false;
@ -135,10 +133,8 @@ namespace Uno.PackageDiff
{
var methodSignature = method.ToSignature();
var isIgnored = ignoreSet.Methods
.Select(t => t.FullName)
.Contains(methodSignature)
|| IsDeclaringTypeIgnored(method, ignoredTypeNames);
var isIgnored = ignoreSet.Methods.Any(m => m.Matches(methodSignature))
|| IsDeclaringTypeIgnored(method, ignoreSet);
var strike = isIgnored
? "~~" : "";
@ -156,7 +152,7 @@ namespace Uno.PackageDiff
return shouldFail;
}
private static bool ReportEvents(StreamWriter writer, ComparisonResult results, IgnoreSet ignoreSet, string[] ignoredTypeNames)
private static bool ReportEvents(TextWriter writer, ComparisonResult results, IgnoreSet ignoreSet)
{
var shouldFail = false;
@ -171,10 +167,8 @@ namespace Uno.PackageDiff
writer.WriteLine("- `{0}`", updatedType.Key);
foreach(var evt in updatedType)
{
var isIgnored = ignoreSet.Events
.Select(t => t.FullName)
.Contains(evt.ToString())
|| IsDeclaringTypeIgnored(evt, ignoredTypeNames);
var isIgnored = ignoreSet.Events.Any(e => e.Matches(evt.ToString()))
|| IsDeclaringTypeIgnored(evt, ignoreSet);
var strike = isIgnored
? "~~" : "";
@ -192,7 +186,7 @@ namespace Uno.PackageDiff
return shouldFail;
}
private static bool IsDeclaringTypeIgnored(IMemberDefinition memberDefinition, string[] ignoredTypes)
=> ignoredTypes.Contains(memberDefinition.DeclaringType.ToSignature());
private static bool IsDeclaringTypeIgnored(IMemberDefinition memberDefinition, IgnoreSet ignoreSet)
=> ignoreSet.Types.Any(t => t.Matches(memberDefinition.DeclaringType.ToSignature()));
}
}

Просмотреть файл

@ -6,7 +6,6 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<LangVersion>latest</LangVersion>
<RollForward>major</RollForward>
<PackAsTool>true</PackAsTool>